Forum Archive

Drawing a image with a "tint_color" in an ImageContext

Phuket2

Like every thing else, this is not my strong suit. I would just like to render/draw the built in images as @omz does with a tint_color

In the code Snippet below, you can see I have been trying some things. I am sure it's so easy if you know how. I really do try before asking here. Then the brain just gets full.

if self.image_name:
                with ui.GState():
                    ui.set_color('blue')
                    #ui.set_shadow()
                    #ui.set_blend_mode(30)
                    #print(ui.BLEND_DESTINATION_IN)
                    img = ui.Image.named(self.image_name)
                    img_rect = ui.Rect(*bounds).inset(*self.image_margin)
                    if img:
                        img.draw(*img_rect)

Edit. Black and white pictures from Pythonista selection render fine, but only in black or white

JonB

have you tried the ui.Image method with_rendering_mode(ui.RENDERING_MODE_TEMPLATE)? I believe this is what ui.Button's use to do the tinting (the image must have an alpha channel)

JonB

simple example

 img=ui.Image.named('iow:alert_32')
 with ui.ImageContext(100,100) as ctx:
    ui.set_color((1,0,0))
    img.with_rendering_mode(ui.RENDERING_MODE_TEMPLATE).draw()
    out=ctx.get_image()
    out.show()
Phuket2

@JonB , thanks.
img.with_rendering_mode(ui.RENDERING_MODE_TEMPLATE).draw()
Works perfectly. In this case drawing to the screen from the draw method

Phuket2

Ok, to be more correct if someone following this thread.

I am sort of using like this. not saying it's good. Just wanted to show for completeness.

import ui

class MyClass(ui.View):
    def __init__(self, image_name, tint_color = 'black',
                        shape_bg_color = 'orange', *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.image_name = image_name
        self.image_margin = (20, 20)
        self.tint_color = tint_color
        self.shape_bg_color = shape_bg_color

    def draw(self):
        with ui.GState():
            ui.set_color(self.shape_bg_color)
            shape = ui.Path.oval(*self.bounds)
            shape.fill()

            ui.set_color(self.tint_color)
            img = ui.Image.named(self.image_name)
            img_rect = ui.Rect(*self.bounds).inset(*self.image_margin)
            if img:
                img.with_rendering_mode(ui.RENDERING_MODE_TEMPLATE).draw(*img_rect)

if __name__ == '__main__':
    wh = 300
    mc = MyClass('iow:ios7_stopwatch_256', frame = (0, 0, wh, wh),
                bg_color = 'purple', shape_bg_color = 'white',
                tint_color = 'black')
    mc.present('sheet')