Skip to main content

Colors

Colors are implemented in the global namespace Color and fully support HSV and RGB spaces.

Construction and fields

Color object can be constructed either using the hex code or the respective RGB/HSV values.

Regardless of the initial color space used, both HSV/RGB channels as well as the luminance can be read and written to.

-- Hex RGB code and A between [0 to 1]
Color Color.from(int hex, float? a)

-- Color mask starting with optional "#" and containing one of the following formats as hex:
-- - "RRGGBBAA"
-- - "RRGGBB"
-- - "RGBA"
-- - "RGB"
Color Color.from(string hex, float? a)

-- R,G,B [0 to 255], S,V,A [0 to 1], H in radians.
Color Color.RGB(float r, float g, float b, float? a)
Color Color.HSV(float h, float s, float v, float? a)

-- Copies the color as is, note that Lua passes colors by reference.
Color Color.clone

-- Converts the vector representing (r,g,b)/(r,g,b,a) from 0 to 1 to a Color.
Color vec3.rgb
Color vec4.rgba

-- Inverse of the color.
Color Color.inverse

-- Perceived luminance of the color.
float Color.lum

-- Converts the Color to a vector representing (r,g,b)/(r,g,b,a) from 0 to 1.
vec3 Color.xyz
vec4 Color.xyzw

-- Color code (0xRRGGBBAA).
uint Color.code

-- Hue, saturation, value.
float Color.h
float Color.s
float Color.v

-- Red, green, blue, alpha.
float Color.r
float Color.g
float Color.b
float Color.a
Example/index.ts
const color = Color.RGB(255, 255, 255);
print(color.xyzw); // Prints (1.000000, 1.000000, 1.000000, 1.000000)
print(color, "=", color.h, color.s, color.v); // Prints #ffffffff = 0.00 0.00 1.00
color.s = 0.3;
print(color, "=", color.h, color.s, color.v); // Prints #ffb2b2ff = 0.00 0.30 1.00
info

Color object supports comparison and string conversion as metamethods.

Color mixing

Colors can be blended using the member function Blend, linearly interpolated between each other using Lerp and multipled using operator *.

-- Alpha blends two colors together.
Color Color:blend(Color other)

-- Linearly interpolates between one color to other with the given coefficient.
Color Color:lerp(Color other, float t)

Syntax sugars

Color object also defines the following syntax sugar that replace a field in-place:

-- Replaces Hue/Saturation/Value and returns a new color.
Color Color:hue(float h)
Color Color:saturation(float s)
Color Color:value(float v)

-- Replaces Red/Green/Blue/Alpha channel and returns a new color.
Color Color:red(float r)
Color Color:green(float g)
Color Color:blue(float b)
Color Color:alpha(float a)

-- Darkens the color by the given percentage and returns a new color.
Color Color:darken(float perc)

-- Lightens the color by the given percentage and returns a new color.
Color Color:lighten(float perc)

-- Replaces the colors luminance value and returns a new color.
Color Color:luminance(float perc)

This is done to avoid code like this:

Example/index.ts
const c = Color.MAGENTA.clone;
c.a = 0.2;
fn(c);
/* vs */
fn(Color.MAGENTA.alpha(0.2));

Constants

Color namespace exports every standard CSS color with CONST_CASE naming with two additions of our own.

  • SR_RED:
  • SR_BLACK:
  • ALICEBLUE:
  • ANTIQUEWHITE:
  • AQUA:
  • AQUAMARINE:
  • AZURE:
  • BEIGE:
  • BISQUE:
  • BLACK:
  • BLANCHEDALMOND:
  • BLUE:
  • BLUEVIOLET:
  • BROWN:
  • BURLYWOOD:
  • CADETBLUE:
  • CHARTREUSE:
  • CHOCOLATE:
  • CORAL:
  • CORNFLOWERBLUE:
  • CORNSILK:
  • CRIMSON:
  • CYAN:
  • DARKBLUE:
  • DARKCYAN:
  • DARKGOLDENROD:
  • DARKGRAY:
  • DARKGREEN:
  • DARKGREY:
  • DARKKHAKI:
  • DARKMAGENTA:
  • DARKOLIVEGREEN:
  • DARKORANGE:
  • DARKORCHID:
  • DARKRED:
  • DARKSALMON:
  • DARKSEAGREEN:
  • DARKSLATEBLUE:
  • DARKSLATEGRAY:
  • DARKSLATEGREY:
  • DARKTURQUOISE:
  • DARKVIOLET:
  • DEEPPINK:
  • DEEPSKYBLUE:
  • DIMGRAY:
  • DIMGREY:
  • DODGERBLUE:
  • FIREBRICK:
  • FLORALWHITE:
  • FORESTGREEN:
  • FUCHSIA:
  • GAINSBORO:
  • GHOSTWHITE:
  • GOLDENROD:
  • GOLD:
  • GRAY:
  • GREEN:
  • GREENYELLOW:
  • GREY:
  • HONEYDEW:
  • HOTPINK:
  • INDIANRED:
  • INDIGO:
  • IVORY:
  • KHAKI:
  • LAVENDERBLUSH:
  • LAVENDER:
  • LAWNGREEN:
  • LEMONCHIFFON:
  • LIGHTBLUE:
  • LIGHTCORAL:
  • LIGHTCYAN:
  • LIGHTGOLDENRODYELLOW:
  • LIGHTGRAY:
  • LIGHTGREEN:
  • LIGHTGREY:
  • LIGHTPINK:
  • LIGHTSALMON:
  • LIGHTSEAGREEN:
  • LIGHTSKYBLUE:
  • LIGHTSLATEGRAY:
  • LIGHTSLATEGREY:
  • LIGHTSTEELBLUE:
  • LIGHTYELLOW:
  • LIME:
  • LIMEGREEN:
  • LINEN:
  • MAGENTA:
  • MAROON:
  • MEDIUMAQUAMARINE:
  • MEDIUMBLUE:
  • MEDIUMORCHID:
  • MEDIUMPURPLE:
  • MEDIUMSEAGREEN:
  • MEDIUMSLATEBLUE:
  • MEDIUMSPRINGGREEN:
  • MEDIUMTURQUOISE:
  • MEDIUMVIOLETRED:
  • MIDNIGHTBLUE:
  • MINTCREAM:
  • MISTYROSE:
  • MOCCASIN:
  • NAVAJOWHITE:
  • NAVY:
  • OLDLACE:
  • OLIVE:
  • OLIVEDRAB:
  • ORANGE:
  • ORANGERED:
  • ORCHID:
  • PALEGOLDENROD:
  • PALEGREEN:
  • PALETURQUOISE:
  • PALEVIOLETRED:
  • PAPAYAWHIP:
  • PEACHPUFF:
  • PERU:
  • PINK:
  • PLUM:
  • POWDERBLUE:
  • PURPLE:
  • REBECCAPURPLE:
  • RED:
  • ROSYBROWN:
  • ROYALBLUE:
  • SADDLEBROWN:
  • SALMON:
  • SANDYBROWN:
  • SEAGREEN:
  • SEASHELL:
  • SIENNA:
  • SILVER:
  • SKYBLUE:
  • SLATEBLUE:
  • SLATEGRAY:
  • SLATEGREY:
  • SNOW:
  • SPRINGGREEN:
  • STEELBLUE:
  • TAN:
  • TEAL:
  • THISTLE:
  • TOMATO:
  • TURQUOISE:
  • VIOLET:
  • WHEAT:
  • WHITE:
  • WHITESMOKE:
  • YELLOW:
  • YELLOWGREEN: