The Color class represents colors and provides conversion between different color spaces.
Constructor
Color([r, g, b [, a]])
Alpha component (0-255, default: 255).
local color = Color(255, 128, 0)
local transparent = Color(255, 0, 0, 128)
Color (table constructor - RGB)
local color = Color{ r=255, g=128, b=0 }
local color2 = Color{ red=255, green=128, blue=0, alpha=255 }
Color (table constructor - HSV)
local color = Color{ h=180, s=0.5, v=1.0 }
local color2 = Color{ hue=180, saturation=0.5, value=1.0 }
Color (table constructor - HSL)
local color = Color{ h=180, s=0.5, l=0.5 }
local color2 = Color{ hue=180, saturation=0.5, lightness=0.5 }
Color (table constructor - Grayscale)
local gray = Color{ gray=128, alpha=255 }
Color (table constructor - Indexed)
local indexed = Color{ index=5 }
Properties
Hue (context-dependent: HSV or HSL).
Saturation (context-dependent: HSV or HSL).
Raw grayscale pixel value.
Examples
Creating Colors in Different Spaces
-- RGB
local red = Color(255, 0, 0)
local blue = Color{ r=0, g=0, b=255 }
-- HSV
local orange = Color{ h=30, s=1.0, v=1.0 }
-- HSL
local pink = Color{ h=350, s=0.8, l=0.7 }
-- Grayscale
local gray = Color{ gray=128 }
-- With alpha
local transparent = Color{ r=255, g=0, b=0, a=128 }
Color Manipulation
local color = Color(255, 128, 0)
-- Modify components
color.red = 200
color.alpha = 200
-- Work with HSV
color.hsvHue = color.hsvHue + 30
color.hsvSaturation = 0.8
-- Work with HSL
color.hslLightness = 0.5
print(string.format(
"RGB(%d, %d, %d, %d)",
color.red, color.green, color.blue, color.alpha
))
Using Colors with Drawing
local sprite = Sprite(32, 32)
local image = sprite.cels[1].image
-- Draw with different colors
for y=0,31 do
for x=0,31 do
local color = Color{
h = (x + y) * 360 / 63,
s = 1.0,
v = 1.0
}
image:drawPixel(x, y, color)
end
end
Setting Foreground/Background Colors
app.fgColor = Color{ r=255, g=0, b=0 }
app.bgColor = Color{ r=0, g=0, b=255 }
print("Foreground:", app.fgColor.red, app.fgColor.green, app.fgColor.blue)
print("Background:", app.bgColor.red, app.bgColor.green, app.bgColor.blue)
Color Comparisons
local color1 = Color(255, 0, 0)
local color2 = Color{ r=255, g=0, b=0 }
local color3 = Color(255, 0, 0, 200)
if color1 == color2 then
print("Colors are equal")
end
if color1 ~= color3 then
print("Colors differ in alpha")
end