Skip to main content

Color

Defines a 32-bit color with four components for R,G,B,A. The value for each component must be between 0 and 255. The fourth component "A", is optional. If not specified, the default value of "A" is 255.

Each component represents a channel of color:

  • R controls the quantity of red in the color.
  • G controls the quantity of green in the color.
  • B controls the quantity of blue in the color.
  • A controls the transparency of the color.

Constructor

Color.new

Creates a new Color with the given r, g, b and a values.

Example

-- Assigns a Color, where R=255, G=0, B=255, A=255, and assigns to the color to the variable "a"
local a = Color.new(255, 0, 0, 255)
local b = Color.new(255, 0, 0)
local c = Color.new()

print(a) -- Prints '(255, 0, 0, 255)'
print(b) -- Prints '(255, 0, 0, 255)'
print(c) -- Prints '(0, 0, 0, 255)'

Properties

r

The red (R) component of the Color.

Example

local myColor = Color.new(255, 175, 100, 255)

print(myColor.r) -- Prints '255'

g

The green (G) component of the Color.

Example

local myColor = Color.new(255, 175, 100, 255)

print(myColor.g) -- Prints '175'

b

The blue (B) component of the Color.

Example

local myColor = Color.new(255, 175, 100, 255)

print(myColor.b) -- Prints '100'

a

The alpha (A) component of the Color.

Example

local myColor = Color.new(255, 175, 100, 255)

print(myColor.a) -- Prints '255'

Functions

lerp(from, to, t)

Returns a Color where the value of each component is linearly interpolated from the input colors, by the value specified in t (which should be between 0 and 1).

Example

local a = Color.new(240, 0, 0)
local b = Color.new(0, 240, 0)

print(Color.lerp(a, b, 0.5)) -- Prints '(120, 120, 0)'

parse(colorHex)

Returns a Color from the color hex string provided in the input.

Example

print(Color.parse('#ffff00'))   -- Prints '(255, 255, 0, 255)'

Math Operations

Color + Color

Adds each of the components of the colors.

Example

local a = Color.new(255, 0, 0)
local b = Color.new(0, 255, 0)

print(a + b) -- Prints '(255, 255, 0, 255)'

Color - Color

Subtracts each of the components of the colors.

Example

local a = Color.new(255, 0, 0, 255)
local b = Color.new(125, 0, 0, 0)

print(a - b) -- Prints '(130, 0, 0, 255)'

Color * number

Multiplies each of the components of the Color by the number.

Example

local a = Color.new(70, 0, 0, 255)

print(a * 3) -- Prints '(210, 0, 0, 765)'

Color * Color

Multiplies each of the components of the first Color by the respective component on the second Color.

Example

local a = Color.new(100, 0, 100, 255)
local b = Color.new(0, 255, 2, 1)

print(a * b) -- Prints '(0, 0, 200, 255)'

Color / number

Divides each of the components of the Color by the number.

Example

local a = Color.new(240, 0, 0, 120)

print(a / 3) -- Prints '(80, 0, 0, 40)'

Color / Color

Divides each of the components of the first Color by the respective component on the second Color.

Example

local a = Color.new(100, 255, 100, 255)
local b = Color.new(1, 255, 2, 1)

print(a / b) -- Prints '(100, 1, 50, 255)'

Constants

Color.black

Returns the color "black" i.e. (0, 0, 0, 255)

Color.blue

Returns the color "blue" i.e. (0, 0, 255, 255)

Color.clear

Returns the color "clear" (a.k.a "transparent") i.e. (0, 0, 0, 0)

Color.cyan

Returns the color "cyan" i.e. (0, 255, 255, 255)

Color.gray

Returns the color "gray" i.e. (125, 125, 125, 255)

Color.green

Returns the color "green" i.e. (0, 255, 0, 255)

Color.magenta

Returns the color "magenta" i.e. (255, 0, 255, 255)

Color.red

Returns the color "red" i.e. (255, 0, 0, 255)

Color.white

Returns the color "white" i.e. (255, 255, 255, 255)

Color.yellow

Returns the color "yellow" i.e. (255, 234, 4, 255)