Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Colorizing Images

in Questions Posts: 684

I've searched a bit, and it looks like nobody mentioned it till now.

Some options to colorize images readImage("smt") exist. But I am missing a good one.

tint(r, g, b) — tinting the image. However, dark values will not display the image at all. Brightness-Contrast is necessary.

fill(r, g, b, a) rect(x, y, w, h) — Putting a transparent rect in front of the image.

However, none of this is good enough for colorize with brightness-contrast options. Can someone give tips/hints of coding a function which can really colorize from black to white and from red, green and blue till cyan, magenta, and yellow.

Comments

  • Posts: 213

    @TokOut The best way to alter the colors of an image is through a shader. That way you can basically alter every pixel to your desire

  • Posts: 684

    I need another way, as I have no idea of shaders.

  • dave1707dave1707 Mod
    Posts: 5,996

    @TokOut Are you after something like this.

    displayMode(FULLSCREEN)
    supportedOrientations(PORTRAIT_ANY)
    
    function setup()
        tab1={}
        tab={"Planet Cute:Character Boy","Planet Cute:Character Cat Girl",
        "Planet Cute:Character Horn Girl","Planet Cute:Character Pink Girl",
        "Planet Cute:Character Princess Girl","Small World:Court"}
        for z=1,#tab do
            colorize(z) 
        end
    end
    
    function colorize(n)
        tab1[n]=readImage(tab[n])
        w=tab1[n].width
        h=tab1[n].height
        for x=1,w do
            for y=1,h do
                r,g,b,a=tab1[n]:get(x,y)
                if a>0 then
                    if r>=g and r>=b then
                        r=255
                    end
                    if g>=r and g>=b then
                        g=255
                    end
                    if b>=r and b>=g then
                        b=255
                    end
                    tab1[n]:set(x,y,r,g,b)
                end
            end
        end
    end
    
    function draw()
        background(50)
        for z=1,#tab do
            sprite(tab[z],200,HEIGHT-160*z+90)
            sprite(tab1[z],400,HEIGHT-160*z+90)
        end
    end
    
  • Posts: 684

    I found what I need.

    displayMode(FULLSCREEN)
    supportedOrientations(LANDSCAPE_ANY)
    
    function setup()
        --[[gray = "Project:gray-basic"
        m = {
            colorize(readImage(gray), color(205, 0, 0, 255)),
            colorize(readImage(gray), color(0, 155, 0, 255)),
            colorize(readImage(gray), color(0, 0, 255, 255)),
            colorize(readImage(gray), color(0, 0, 0, 255)),
            colorize(readImage(gray), color(205, 205, 205, 255))
        }]]
        picture = readImage("Project:gray-basic")
        picture1 = readImage("Project:gray-brick")
        parameter.color("C", color(255), function(a)
            picture = colorize(readImage("Project:gray-basic"), a)
            picture1 = colorize(readImage("Project:gray-brick"), a)
        end)
    end
    
    function colorize(img, col)
        for x = 1, img.width do
            for y = 1, img.height do
                local r, g, b = img:get(x, y)
                local r1, g1, b1 = col.r, col.g, col.b
                img:set(x, y,
                color(
                    (r + r1)/2,
                    (g + g1)/2,
                    (b + b1)/2)
                )
            end
        end
        return img
    end
    
    function draw()
        background(30)
        --[[local n = 0
        for a, b in ipairs(m) do
            sprite(b, 50 + 17 * n, 50, 16, 16)
            n=n+1
        end]]
        spriteMode(CORNER)
        -- Basic
        sprite(picture, 10, 50, 16, 16)
        sprite(picture, 10, 85, 32, 32)
        sprite(picture, 10, 150, 64, 64)
        sprite(picture, 10, 255, 128, 128)
    
        -- Brick
        sprite(picture1, 140, 50, 16, 16)
        sprite(picture1, 140, 85, 32, 32)
        sprite(picture1, 140, 150, 64, 64)
        sprite(picture1, 140, 255, 128, 128)
    end
    

    However, I don't like it, how it changes, I need something more.

Sign In or Register to comment.