LifeLua 21-08-2025
Documentation for LifeLua, a Lua interpreter for the PS Vita
Loading...
Searching...
No Matches
color

Creating and changing colors. More...

Detailed Description

A color is a value holding red, green, blue and alpha (opacity), each from 0 to 255. Create one with color.new() and pass it to any function that draws something.

Colors are never changed in place: functions like color.r() or color.add() return a new color.

You can also call these functions as methods, and use +, - and == on colors:

local red = color.new(255, 0, 0)
local blue = color.new(0, 0, 255)
local purple = red + blue -- same as color.add(red, blue)
local halfred = red:a(128) -- same as color.a(red, 128)
print(red:r(), purple == red) -- 255 false

Functions

color color.new (number r, number g, number b, number a)
 
number color.r (color color, number r)
 
number color.g (color color, number g)
 
number color.b (color color, number b)
 
number color.a (color color, number a)
 
color color.add (color color1, color color2)
 
color color.sub (color color1, color color2)
 
color color.blend (color color1, color color2)
 
color color.mix (color color1, color color2, number p1, number p2)
 
color tocolor (number color)
 

Function Documentation

◆ color.a()

number color.a ( color color,
number a )

Gets the alpha (opacity) of a color, or makes a copy of the color with a different alpha

Also available as a method: c:a() / c:a(value)

Parameters
colorThe color
aNew alpha value, 0-255 (optional)
Returns
The alpha value (0-255) if a is omitted, otherwise a new color with the changed alpha
Example:
local white = color.new(255, 255, 255)
local faded = white:a(100) -- white stays the same, faded is a new color

◆ color.add()

color color.add ( color color1,
color color2 )

Adds two colors together, channel by channel (results are capped at 255)

Also available as color1 + color2 and color1:add(color2)

Parameters
color1First color
color2Second color
Returns
The new color

◆ color.b()

number color.b ( color color,
number b )

Gets the blue value of a color, or makes a copy of the color with a different blue value

Also available as a method: c:b() / c:b(value)

Parameters
colorThe color
bNew blue value, 0-255 (optional)
Returns
The blue value (0-255) if b is omitted, otherwise a new color with the changed blue value

◆ color.blend()

color color.blend ( color color1,
color color2 )

Blends two colors evenly (50% of each)

Also available as a method: color1:blend(color2)

Parameters
color1First color
color2Second color
Returns
The new color

◆ color.g()

number color.g ( color color,
number g )

Gets the green value of a color, or makes a copy of the color with a different green value

Also available as a method: c:g() / c:g(value)

Parameters
colorThe color
gNew green value, 0-255 (optional)
Returns
The green value (0-255) if g is omitted, otherwise a new color with the changed green value

◆ color.mix()

color color.mix ( color color1,
color color2,
number p1,
number p2 )

Mixes two colors using a weight for each. The weights don't need to add up to 100, they are relative to each other (3, 1 gives 75% of color1 and 25% of color2).

Also available as a method: color1:mix(color2, p1, p2)

Parameters
color1First color
color2Second color
p1Weight of the first color
p2Weight of the second color
Returns
The new color
Example:
local orange = color.mix(color.new(255, 0, 0), color.new(255, 255, 0), 50, 50)

◆ color.new()

color color.new ( number r,
number g,
number b,
number a )

Creates a new color. Values outside of 0-255 are clamped.

Parameters
rRed, 0-255
gGreen, 0-255
bBlue, 0-255
aAlpha (opacity), 0 is invisible and 255 is fully visible (optional, default: 255)
Returns
The new color
Example:
local white = color.new(255, 255, 255)
local transparent_black = color.new(0, 0, 0, 128)

◆ color.r()

number color.r ( color color,
number r )

Gets the red value of a color, or makes a copy of the color with a different red value

Also available as a method: c:r() / c:r(value)

Parameters
colorThe color
rNew red value, 0-255 (optional)
Returns
The red value (0-255) if r is omitted, otherwise a new color with the changed red value

◆ color.sub()

color color.sub ( color color1,
color color2 )

Subtracts the second color from the first, channel by channel (results are capped at 0)

Also available as color1 - color2 and color1:sub(color2)

Parameters
color1Color to subtract from
color2Color to subtract
Returns
The new color

◆ tocolor()

color tocolor ( number color)

Converts a number to a color. The number is read in 0xAABBGGRR order: alpha, blue, green, red (the reverse of the usual HTML order).

Any function that takes a color also accepts such a number directly.

Parameters
colorThe color as a number
Returns
The new color
Example:
local red = tocolor(0xFF0000FF) -- alpha FF, blue 00, green 00, red FF
local blue = tocolor(0xFFFF0000) -- alpha FF, blue FF, green 00, red 00
draw.rect(0, 0, 100, 100, 0xFF00FF00) -- green, without converting first
color tocolor(number color)
Definition lj_draw.h:379