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:
Reference
new
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): optional Alpha component (0–1), defaults to 1
Returns
Color
set
Set the color components
Parameters
r(number): Red (0–255)g(number): Green (0–255)b(number): Blue (0–255)a(number): optional Alpha (0–1)
get
Get the RGBA components of the color
Returns
number: Red componentnumber: Green componentnumber: Blue componentnumber: Alpha component
toData
Returns a table representing the color
Returns
table: r: number, g: number, b: number, a: number }
equals
Compare this color to another
Parameters
other(Color)
Returns
boolean
lerp
Linearly interpolate between two colors
Parameters
a(Color): Start colorb(Color): End colort(number): Interpolation factor (0–1)
Returns
Color
toGreyScale
Convert color to grayscale luminance
Returns
number: value
toHsv
Convert RGB to HSV
Returns
number: Hue (0–360), number s Saturation (0–1), number v Value (0–1)
setFromHsv
Set color from HSV values
Parameters
h(number): Hue (0–360)s(number): Saturation (0–1)v(number): Value (0–1)
shiftHue
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]
copy
Return a copy of this color
Returns
Color
toHex
Convert the color to a hexadecimal string (ignores alpha).
Returns
string: color string (e.g. "#FFA07A")
fromHex
Create a Color from a hexadecimal string
Parameters
hex(string): Hex color string in the form "#RRGGBB"
Returns
Color
premultiplyAlpha
Premultiply RGB values by alpha (in-place). Useful for certain rendering pipelines.
invert
Invert the color (in-place). Alpha is not changed.