Skip to main content
The Color class represents colors and provides conversion between different color spaces.

Constructor

Color([r, g, b [, a]])

r
number
Red component (0-255).
g
number
Green component (0-255).
b
number
Blue component (0-255).
a
number
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)

h
number
Hue (0-360).
s
number
Saturation (0-1).
v
number
Value (0-1).
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)

h
number
Hue (0-360).
s
number
Saturation (0-1).
l
number
Lightness (0-1).
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)

gray
number
Gray value (0-255).
local gray = Color{ gray=128, alpha=255 }

Color (table constructor - Indexed)

index
number
Palette index.
local indexed = Color{ index=5 }

Properties

red
number
Red component (0-255).
green
number
Green component (0-255).
blue
number
Blue component (0-255).
alpha
number
Alpha component (0-255).
hsvHue
number
HSV hue (0-360).
hsvSaturation
number
HSV saturation (0-1).
hsvValue
number
HSV value (0-1).
hslHue
number
HSL hue (0-360).
hslSaturation
number
HSL saturation (0-1).
hslLightness
number
HSL lightness (0-1).
hue
number
Hue (context-dependent: HSV or HSL).
saturation
number
Saturation (context-dependent: HSV or HSL).
value
number
HSV value (0-1).
lightness
number
HSL lightness (0-1).
gray
number
Grayscale value (0-255).
index
number
Palette index.
rgbaPixel
number
Raw RGBA pixel value.
grayPixel
number
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