Rgba

From codeTank

Jump to: navigation, search

Contents

Description

rgba creates tables that holds color information. It is primarily used to specify color for image functions.

Arguments

rgba() -- equivalent to rgba(0, 0, 0, 255)
rgba(red) -- equivalent to rgba(red, 0, 0, 255)
rgba(red, green) -- equivalent to rgba(red, green, 0, 255)
rgba(red, green, blue) -- equivalent to rgba(red, green, blue, 255)
rgba(red, green, blue, alpha)

There are four optional arguments for rgba. Each argument specifies an amount of intensity per color channel. The four color channels are red, green, blue, and alpha. Each channel can range from 0 (void of color), to 255 (maximum color). alpha also ranges from 0 (completely transparent), to 255 (completely solid).

Examples:

rgba(255, 0, 0) -- pure red, solid
rgba(0, 255, 0, 127) -- pure green, half clear
rgba(0, 0, 255, 63) -- pure blue, mostly clear
rgba(84, 84, 84, 240) -- dark gray, mostly solid
rgba(255, 140, 0, 0) -- completely clear
rgba(178, 34, 34, 200) -- fire brick, mostly solid

Note: As added convenience, rgba can also accept a table instead of a list of arguments.

Returns

rgba will return a table, with the appropriate values set. rgba is equivalent to:

function rgba(r, g, b, a)
    r = tonumber(r) or 0
    g = tonumber(g) or 0
    b = tonumber(b) or 0
    a = tonumber(a) or 255
    return {r, g, b, a,
        red = r, green = g, blue = b, alpha = a,
        r = r, g = g, b = b, a = a}
end

Example

bg_color = rgb(100, 200, 255) -- must be solid
fg_color = rgba(255, 0, 0, 127) -- half clear red

bg = image.create(300, 300)
fg = image.create(40, 30)
fg:clear(fg_color)

y = 0
wnd = window.create{
    width = bg.width,
    height = bg.height,
    resize = false,

    onpaint = function(wnd, pnt)
            bg:blit(pnt, 0, 0)
            bg:clear(bg_color)
            for i = 0, 300 do
                bg:setpixel(i, i, rgb(255, 255, 255))
            end
            fg:blit(bg, 100, 80 + y)
            y = (y + 1) % 80
        end,
    }

while window.getcount() > 0 do
    window.pumpmessages()
    wnd:invalidate(false)
end

Special Notes

Please be aware that the paint functions are not aware of the alpha channel. The paint library assumes every pixel is solid. Only the image library is aware of the alpha channel, and will use it correctly. Please also note that when an image is blitted to the paint device, that it's alpha will be quantized to either solid, or not-solid. For more information on this, see image.blit.

See Also

Personal tools