Change cel color when another cel color is beneath it

Hi. I’m working on tring to make it so that when one pixel is blue, and the pixel under it is blank, it will turn into a dark blue. Here is what I have so far. (I’m completely new to Lua, but understand how most coding languages work.)
ocal dlg = Dialog { title = “Color Shading” }

–Colors 1-9
local color0 = Color(0, 0, 0, 0)

dlg:color {
id = “color1”,
label = "Color 1: ",
color = Color(30, 175, 255, 255)
}

dlg:color {
id = “color2”,
label = "Color 2: ",
color = Color(18, 166, 248, 255)
}

dlg:color {
id = “color3”,
label = "Color 3: ",
color = Color(17, 159, 237, 255)
}

dlg:color {
id = “color4”,
label = "Color 4: ",
color = Color(20, 149, 220, 255)
}

dlg:color {
id = “color5”,
label = "Color 5: ",
color = Color(15, 138, 206, 255)
}

dlg:color {
id = “color6”,
label = "Color 6: ",
color = Color(12, 127, 191, 255)
}

dlg:color {
id = “color7”,
label = "Color 7: ",
color = Color(13, 117, 174, 255)
}

dlg:color {
id = “color8”,
label = "Color 8: ",
color = Color(10, 104, 156, 255)
}

dlg:color {
id = “color9”,
label = "Color 9: ",
color = Color(11, 96, 143, 255)
}

dlg:button {
id = “enter”,
text = “Enter”,
onclick = function()
shade ()
dlg:close()
end
}

dlg:show {wait = false}

–Functions
shade = function ()
if Color == dlg:color4 + cel.position (0, -1) Color == color0 then
color = color9
end

io.read()

My issue is tying to figure out how to gain the position of one color, then the cell underneath that color. If the color under the first color is blank, then set the first pixel color to a different color. This is so It will shade the bottom of a single color blob, and with more coding, shading the blob.

You can use image.getpixel (Document) to get color and image.drawPixel to set color. We have to consider what the color mode of the sprite is in that those functions treat low-level representation.

For example, the process can be written like this:

local cel = app.activeCel
assert(cel ~= nil and cel.image ~= nil)

local fromColor = Color({ r = 0xFF, g = 0, b = 0 })
local toColor = Color({ r = 0x7F, g = 0, b = 0 })

-- image:drawPixel only accepts integer pixel values (see: app.pixelColor)
local toColorInt = 0
if cel.sprite.colorMode == ColorMode.RGB then
    toColorInt = toColor.rgbaPixel
elseif cel.sprite.colorMode == ColorMode.GRAY then
    toColorInt = toColor.grayPixel
else
    toColorInt = toColor.index
end

local image = cel.image
for y = 0, cel.bounds.height - 1 do
    for x = 0, cel.bounds.width - 1 do
        local color = Color(image:getPixel(x, y))
        local underIsBlank = false

        if y == cel.bounds.height - 1 then
            -- bottom of image
            underIsBlank = true
        elseif image.colorMode == ColorMode.INDEXED then
            -- compare palette index
            underIsBlank = image:getPixel(x, y + 1) == cel.sprite.transparentColor
        else
            -- image.colorMode is ColorMode.RGB or ColorMode.GRAY
            underIsBlank = Color(image:getPixel(x, y + 1)).alpha == 0
        end

        if color == fromColor and underIsBlank then
            image:drawPixel(x, y, toColorInt)
        end
    end
end

Thank you so much! I will try this out.

It worked! Thanks so much!

1 Like