Skip to main content

Constructors

lerp

Linearly interpolates between two colors using the parameter t, where t = 0 returns ‘from’ and t = 1 returns ‘to’.
local black = Color.rgb(0, 0, 0)
local white = Color.rgb(255, 255, 255)

local t = 0.5

-- Linearly interpolate halfway between black and white
local gray = Color.lerp(black, white, t)

print(gray)
-- Returns a gray color between black and white

rgb

Returns a color constructed from red, green, and blue channels. Alpha defaults to 255 (fully opaque). Channel values are clamped to [0, 255].
-- Red
self.color = Color.rgb(255, 0, 0)

rgba

Returns a color constructed from red, green, blue, and alpha channels. Channel values are clamped to [0, 255].
-- Red at 50% opacity
self.color = Color.rgba(255, 0, 0, 128)

Static Functions

red

Returns the red channel of the color. If a value is provided, returns a new color with that channel updated.
local myColor = Color.rgba(128, 55, 12, 128)
local redChannel = Color.red(myColor)
print(redChannel)
-- 128

local newColor = Color.red(myColor, 200)
print(Color.red(newColor))
-- 200

green

Returns the green channel of the color. If a value is provided, returns a new color with that channel updated.
local myColor = Color.rgba(128, 55, 12, 128)
local greenChannel = Color.green(myColor)
print(greenChannel)
-- 55

local newColor = Color.green(myColor, 200)
print(Color.green(newColor))
-- 200

blue

Returns the blue channel of the color. If a value is provided, returns a new color with that channel updated.
local myColor = Color.rgba(128, 55, 12, 128)
local blueChannel = Color.blue(myColor)
print(blueChannel)
-- 12

local newColor = Color.blue(myColor, 200)
print(Color.blue(newColor))
-- 200

alpha

Returns the alpha channel of the color, or returns a new color with the alpha channel set to the specified value. Values are clamped to [0, 255].
local myColor = Color.rgba(128, 55, 12, 128)
local alphaChannel = Color.alpha(myColor)
print(alphaChannel)
-- 128

local newColor = Color.alpha(myColor, 200)
print(Color.alpha(newColor))
-- 200

opacity

Returns the opacity of the color as a normalized value in the range [0.0, 1.0], or returns a new color with its alpha set from the specified opacity.
local myColor = Color.rgba(255, 0, 0, 128)

-- Get the opacity of a color
local myOpacity = Color.opacity(myColor)
print(myOpacity)
-- 0.5

-- Get a new color with the specified opacity
local newColor = Color.opacity(myColor, 1.0)
print(Color.opacity(newColor))
-- 1.0