color

module:color

Description:
  • RGB color convertions

Source:

Methods

(static) color.contrast(rgb1, rgb2) → {number}

Description:
  • Compute contrast ratio between two colors using WCAG relative luminance

Source:
Parameters:
Name Type Description
rgb1 object | Array.<number>

First RGB color (0..255).

rgb2 object | Array.<number>

Second RGB color (0..255).

Returns:
Type Description
number

Contrast ratio (>= 1).

(static) color.darker(rgb, ratioopt) → {Object}

Description:
  • Make a color darker by reducing its HSL lightness by a ratio.

    New lightness is l - (l * ratio).

Source:
Parameters:
Name Type Attributes Default Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

ratio number <optional>
0.5

Fraction of lightness to remove.

Returns:
Type Description
Object

Adjusted RGB color.

(static) color.hex(rgb) → {string}

Description:
  • Convert RGB to a hex color string (#RRGGBB).

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
string

Hex color string in uppercase, e.g. "#FF00AA".

(static) color.hex2rgb(hex) → {object}

Description:
  • Convert a hex color string into an RGB object.

    Supports:

    • #RGB (shorthand)
    • #RRGGBB
    • #RRGGBBAA (alpha is returned as 0..1 in alpha)
Source:
Parameters:
Name Type Description
hex string

Hex color string, typically starting with "#".

Returns:
Type Description
object

RGB components in 0..255 and optional alpha in 0..1.

(static) color.hsl(rgb) → {Array.<number>}

Description:
  • Convert RGB to HSL.

    Output H is degrees (0..360), S and L are percentages (0..100).

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
Array.<number>

HSL values.

(static) color.hsl2rgb(hsl) → {Array.<number>}

Description:
  • Convert HSL to RGB.

    Input H is degrees (0..360), S and L are percentages (0..100).

Source:
Parameters:
Name Type Description
hsl object | Array.<number>

HSL object or [h,s,l] array.

Returns:
Type Description
Array.<number>

RGB components in 0..255.

(static) color.lighter(rgb, ratioopt) → {Object}

Description:
  • Make a color lighter by scaling its HSL lightness.

    New lightness is l + (l * ratio).

Source:
Parameters:
Name Type Attributes Default Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

ratio number <optional>
0.5

Multiplier applied to HSL lightness.

Returns:
Type Description
Object

Adjusted RGB color.

(static) color.luminance(rgb) → {number}

Description:
  • Compute a simple (non-gamma-corrected) luminance value.

    Uses coefficients similar to Rec.709 on sRGB values without linearization.

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
number

Luminance in roughly 0..1.

(static) color.negate(rgb) → {Object}

Description:
  • Invert/negate an RGB color.

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
Object

Negated RGB color.

(static) color.rluminance(rgb) → {number}

Description:
  • Compute relative luminance per WCAG (gamma-corrected / linearized sRGB).

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
number

Relative luminance (0..1).

(static) color.rotate(rgb, degreesopt) → {Object}

Description:
  • Rotate an RGB color around the hue wheel by a number of degrees, i.e. complement color

    Uses RGB -> HSL -> RGB conversion; keeps S and L the same.

Source:
Parameters:
Name Type Attributes Default Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

degrees number <optional>
180

Hue rotation amount in degrees (can be negative).

Returns:
Type Description
Object

Rotated RGB color.

(static) color.yiq(rgb) → {Object}

Description:
  • Convert RGB to YIQ color space.

    Commonly used for legacy TV signal encoding and quick brightness-ish computations.

Source:
Parameters:
Name Type Description
rgb object | Array.<number>

RGB object or [r,g,b] array (0..255).

Returns:
Type Description
Object

YIQ components.

(static) mix(rgb1, rgb2, weightopt) → {Object}

Description:
  • Mix (blend) two RGB/RGBA colors together using a weight.

    The weight controls how much of rgb1 is in the result:

    • weight = 1 -> returns rgb1
    • weight = 0 -> returns rgb2
    • weight = 0.5 -> equal mix

    Note: channels are returned as numbers (not clamped/rounded).

Source:
Parameters:
Name Type Attributes Default Description
rgb1 Array.<number> | object

First color (array or object).

rgb2 Array.<number> | object

Second color (array or object).

weight number <optional>
0.5

Blend weight from 0..1 (higher means more of rgb1).

Returns:
Type Description
Object

Mixed color as an object.

Examples
color.mix({ r: 255, g: 0, b: 0, alpha: 1 }, { r: 0, g: 0, b: 255, alpha: 1 })
{ r: 127.5, g: 0, b: 127.5, alpha: 1 }

closer to black (because weight favors rgb2)

color.mix([255, 255, 255, 1], [0, 0, 0, 1], 0.25)
{ r: 63.75, g: 63.75, b: 63.75, alpha: 1 }