Lua · Variables · Core

color

Make and change colours. Every colour in the API is one packed integer.

A colour is a whole number that holds red, green, blue and alpha (0 to 255 each). Make one with color(r, g, b, a) and pass it to any draw, ui or menu function. Store it in a variable once instead of building it every frame.

color is both a function and a table: color(255, 0, 0) and color.rgba(255, 0, 0) do the same thing.

FunctionWhat it does
color(r, g, b[, a])Packs four channels into one colour. Values outside 0 to 255 are clamped.
color.rgba(r, g, b[, a])Same as color(r, g, b, a).
color.unpack(c)Splits a colour back into its channels.
color.alpha(c, a)Returns the same colour with a new alpha. The best way to fade something in or out.
color.lerp(a, b, t)Mixes two colours. t = 0 gives a, t = 1 gives b. Keep t between 0 and 1.
color.hsv(h, s, v[, a])Makes a colour from hue, saturation and value. Handy for rainbow effects.

#color

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

Packs four channels into one colour. Values outside 0 to 255 are clamped.

Arguments

rinteger
Red, 0 to 255.
ginteger
Green, 0 to 255.
binteger
Blue, 0 to 255.
aintegeroptional
Alpha, 0 to 255. Default 255 (solid).

Returns

integer
The packed colour.
Example
local red = color(255, 70, 70)
local glass = color(12, 14, 18, 200)

#color.rgba

color.rgba(r, g, b[, a])

Same as color(r, g, b, a).

Returns

integer
The packed colour.

#color.unpack

color.unpack(c)

Splits a colour back into its channels.

Arguments

cinteger
A packed colour.

Returns

integer1
r
integer2
g
integer3
b
integer4
a
Example
local r, g, b, a = color.unpack(ui.get("col"))

#color.alpha

color.alpha(c, a)

Returns the same colour with a new alpha. The best way to fade something in or out.

Arguments

cinteger
A packed colour.
anumber
New alpha, 0 to 255. Decimals are rounded down.

Returns

integer
The new colour.
Example
draw.text(20, 20, "Hi", color.alpha(white, 255 * fade))

#color.lerp

color.lerp(a, b, t)

Mixes two colours. t = 0 gives a, t = 1 gives b. Keep t between 0 and 1.

Arguments

ainteger
Start colour.
binteger
End colour.
tnumber
Mix amount, 0 to 1.

Returns

integer
The mixed colour.
Example
local hp_col = color.lerp(color(255, 60, 60), color(80, 230, 120), p.health / 100)

#color.hsv

color.hsv(h, s, v[, a])

Makes a colour from hue, saturation and value. Handy for rainbow effects.

Arguments

hnumber
Hue in degrees, any value (wraps at 360).
snumber
Saturation, 0 to 1.
vnumber
Brightness, 0 to 1.
aintegeroptional
Alpha, 0 to 255. Default 255.

Returns

integer
The packed colour.
Example
local rainbow = color.hsv(globals.time() * 90, 0.7, 1)