Color¶
The Color class provides essential color management tools for graphics applications. It accurately represents colors using RGBA and offers optional HSV support. Quickly create, convert, interpolate, and manipulate colors with features like grayscale conversion, hue shifting, and component access. Perfect for game development, image processing, and any application requiring precise color control.
Usage¶
To use this class, add the following require at the top of your script:
local Color = require 'engine/data/color'
Reference¶
- class Color¶
- module:
Represents an RGBA color with optional HSV support and common color utilities.
Values r, g, b range from 0 to 255, a from 0 to 1. Includes math operators and static color constants.
- static new(r, g, b, a?)¶
Create a new color
- Parameters:
r (
number
) – Red component (0–255)g (
number
) – Green component (0–255)b (
number
) – Blue component (0–255)a? (
number
) – Alpha component (0–1), defaults to 1
- Return type:
- set(r, g, b, a?)¶
Set the color components
- Parameters:
r (
number
) – Red (0–255)g (
number
) – Green (0–255)b (
number
) – Blue (0–255)a? (
number
) – Alpha (0–1)
- get()¶
Get the RGBA components
- Returns:
r, number g, number b, number a
- Return type:
number
- toData()¶
Returns a table representing the color
- Returns:
{ r: number, g: number, b: number, a: number }
- Return type:
table
- lerp(a, b, t)¶
Linearly interpolate between two colors
- toGreyScale()¶
Convert color to grayscale luminance
- Returns:
Luminance value
- Return type:
number
- toHsv()¶
Convert RGB to HSV
- Returns:
h Hue (0–360), number s Saturation (0–1), number v Value (0–1)
- Return type:
number
- setFromHsv(h, s, v)¶
Set color from HSV values
- Parameters:
h (
number
) – Hue (0–360)s (
number
) – Saturation (0–1)v (
number
) – Value (0–1)
- shiftHue(hueShift)¶
Shift hue of color
- Parameters:
hueShift (
number
) – Degrees to shift hue
- clamp()¶
Clamp color values to valid ranges.
r, g, b are clamped to [0, 255]; a is clamped to [0, 1]
- toHex()¶
Convert the color to a hexadecimal string (ignores alpha).
- Returns:
Hex color string (e.g. “#FFA07A”)
- Return type:
str
- static fromHex(hex)¶
Create a Color from a hexadecimal string
- Parameters:
hex (
str
) – Hex color string in the form “#RRGGBB”- Return type:
- premultiplyAlpha()¶
Premultiply RGB values by alpha (in-place).
Useful for certain rendering pipelines.
- invert()¶
Invert the color (in-place).
Alpha is not changed.