Lua · Examples
Color
Colours are packed integers. Build them once, then fade, mix and cycle them.
Each snippet runs as-is: copy it, press From Clipboard on the Lua page, turn it on. Quick start
#Make colours once
Building a colour every frame is wasted work. Keep them in locals at the top of the script.
color_1.lua
local white = color(240, 242, 246)
local red = color(255, 70, 70)
local glass = color(12, 14, 18, 200)
function on_frame()
draw.rect_filled(20, 20, 160, 40, glass, 8)
draw.text(32, 31, "Ready", white, 15)
end#Fade in and out
color.alpha swaps only the alpha. Drive it with a 0 to 1 value.
color_2.lua
local white = color(255, 255, 255)
local shown_at = globals.time()
function on_frame()
local t = globals.time() - shown_at
local a = 255 * math.clamp(1 - (t - 2) / 0.5, 0, 1)
if a <= 0 then return end
draw.text(40, 40, "Fades out after 2 seconds", color.alpha(white, a), 16)
end#Health colour
color.lerp mixes two colours. Red at 0 hp, green at 100.
color_3.lua
local low, high = color(255, 60, 60), color(80, 230, 120)
function on_frame()
local me = game.local_player()
if not me then return end
local col = color.lerp(low, high, me.health / 100)
draw.text_outline(40, 80, me.health .. " HP", col, 22)
end#Rainbow
color.hsv with a hue that moves with time.
color_4.lua
function on_frame()
local w = draw.screen()
local col = color.hsv(globals.time() * 90, 0.65, 1)
draw.rect_filled(0, 0, w, 3, col)
end#Read the channels
color.unpack gives the four channels back.
color_5.lua
ui.color("col", "Pick a colour", color(120, 170, 255))
function on_menu()
local r, g, b, a = color.unpack(ui.get("col"))
menu.text(string.format("r %d g %d b %d a %d", r, g, b, a))
end