Skip to content

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

new

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): optional Alpha component (0–1), defaults to 1

Returns

  • Color

set

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): optional Alpha (0–1)

get

get()

Get the RGBA components of the color

Returns

  • number: Red component
  • number: Green component
  • number: Blue component
  • number: Alpha component

toData

toData()

Returns a table representing the color

Returns

  • table: r: number, g: number, b: number, a: number }

equals

equals(other)

Compare this color to another

Parameters

  • other (Color)

Returns

  • boolean

lerp

lerp(a, b, t)

Linearly interpolate between two colors

Parameters

  • a (Color): Start color
  • b (Color): End color
  • t (number): Interpolation factor (0–1)

Returns

  • Color

toGreyScale

toGreyScale()

Convert color to grayscale luminance

Returns

  • number: value

toHsv

toHsv()

Convert RGB to HSV

Returns

  • number: Hue (0–360), number s Saturation (0–1), number v Value (0–1)

setFromHsv

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

shiftHue(hueShift)

Shift hue of color

Parameters

  • hueShift (number): Degrees to shift hue

clamp

clamp()

Clamp color values to valid ranges. r, g, b are clamped to [0, 255]; a is clamped to [0, 1]

copy

copy()

Return a copy of this color

Returns

  • Color

toHex

toHex()

Convert the color to a hexadecimal string (ignores alpha).

Returns

  • string: color string (e.g. "#FFA07A")

fromHex

static fromHex(hex)

Create a Color from a hexadecimal string

Parameters

  • hex (string): Hex color string in the form "#RRGGBB"

Returns

  • Color

premultiplyAlpha

premultiplyAlpha()

Premultiply RGB values by alpha (in-place). Useful for certain rendering pipelines.

invert

invert()

Invert the color (in-place). Alpha is not changed.