Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Color

Color class that allows for specification and conversion of colors in various color spaces. Please not that most of the color conversion math is based on https://www.easyrgb.com/en/math.php. The internal color representation is a 4-tuple of GLclampf components in RGB color space and additional alpha. All color conversion, e.g., getters is computed on the fly, not cached, and is not optimized for, e.g., massive pixel processing.

Hierarchy

  • Color

Index

Constructors

constructor

  • Creates an instance of color (a 4-tuple in RGBA space).

    Parameters

    • Optional rgba: GLclampf3 | GLclampf4

      Either RGB tuple or RGBA tuple. If none is provided, default will be kept.

    • Optional alpha: GLclampf

      If RGB tuple is provided an additional alpha value can be specified.

    Returns Color

Properties

Protected _altered

_altered: boolean = false
see

altered

Protected _rgba

_rgba: GLclampf4 = [0.0, 0.0, 0.0, Color.DEFAULT_ALPHA]

Static Protected DEFAULT_ALPHA

DEFAULT_ALPHA: GLclampf = 1

Static Protected HEX_FORMAT_REGEX

HEX_FORMAT_REGEX: RegExp = new RegExp(/^(#|0x)?(([0-9a-f]{3}){1,2}|([0-9a-f]{4}){1,2})$/i)

Accessors

a

  • get a(): GLclampf
  • Read access to the Alpha component as float value in range [0.0, 1.0].

    Returns GLclampf

altered

  • get altered(): boolean
  • set altered(status: boolean): void
  • Whether or not color value has changed.

    Returns boolean

  • Intended for resetting alteration status.

    Parameters

    • status: boolean

    Returns void

b

  • get b(): GLclampf
  • Read access to the Blue component as float value in range [0.0, 1.0].

    Returns GLclampf

cmyk

  • Read access to the CMYK components as floating point 4-tuple, each value in range [0.0, 1.0].

    Returns GLclampf4

cmyka

  • Read access to the CMYKA components as floating point 5-tuple, each value in range [0.0, 1.0].

    Returns GLclampf5

g

  • get g(): GLclampf
  • Read access to the Green component as float value in range [0.0, 1.0].

    Returns GLclampf

hexRGB

  • get hexRGB(): string
  • Read access to the RGB components as hexadecimal string.

    Returns string

hexRGBA

  • get hexRGBA(): string
  • Read access to the RGBA components as hexadecimal string.

    Returns string

hsl

  • Read access to the HSL components as floating point 3-tuple, each value in range [0.0, 1.0].

    Returns GLclampf3

hsla

  • Read access to the HSLA components as floating point 4-tuple, each value in range [0.0, 1.0].

    Returns GLclampf4

lab

  • Read access to the LAB components as floating point 3-tuple, each value in range [0.0, 1.0].

    Returns GLclampf3

laba

  • Read access to the LABA components as floating point 4-tuple, each value in range [0.0, 1.0].

    Returns GLclampf4

r

  • get r(): GLclampf
  • Read access to the Red component as float value in range [0.0, 1.0].

    Returns GLclampf

rgb

  • Read access to the RGB components as floating point 3-tuple, each value in range [0.0, 1.0].

    Returns GLclampf3

rgbF32

  • get rgbF32(): Float32Array
  • Read access to the RGB components as array of three 32bit floats, each in range [0.0, 1.0].

    Returns Float32Array

rgbUI8

  • get rgbUI8(): Uint8Array
  • Read access to the RGB components as array of three bytes (8bit unsigned int), each in range [0, 255].

    Returns Uint8Array

rgba

  • Read access to the RGBA components as floating point 4-tuple, each value in range [0.0, 1.0].

    Returns GLclampf4

rgbaF32

  • get rgbaF32(): Float32Array
  • Read access to the RGBA components as array of four 32bit floats, each in range [0.0, 1.0].

    Returns Float32Array

rgbaUI8

  • get rgbaUI8(): Uint8Array
  • Read access to the RGBA components as array of four bytes (8bit unsigned int), each in range [0, 255].

    Returns Uint8Array

Methods

equals

  • equals(other: Color): boolean
  • Checks whether or not this color matches a second color (based on internal rgba floating representation).

    Parameters

    • other: Color

      Color to compare color values to.

    Returns boolean

    • True iff both colors have the exact same rgba floating point values.

fromCMYK

  • fromCMYK(cyan: GLclampf, magenta: GLclampf, yellow: GLclampf, key: GLclampf, alpha?: GLclampf): Color
  • Specifies the internal rgba store using a color in CMYK color space.

    Parameters

    • cyan: GLclampf

      Cyan color component in [0.0, 1.0]

    • magenta: GLclampf

      Magenta color component in [0.0, 1.0]

    • yellow: GLclampf

      Yellow color component in [0.0, 1.0]

    • key: GLclampf

      Key/Black color component in [0.0, 1.0]

    • Default value alpha: GLclampf = Color.DEFAULT_ALPHA

      Alpha color component in [0.0, 1.0]

    Returns Color

    • The color instance (this).

fromF32

  • fromF32(red: GLfloat, green: GLfloat, blue: GLfloat, alpha?: GLfloat): Color
  • Specifies the internal rgba store using a color in float (32bit) RGBA colors.

    Parameters

    • red: GLfloat

      Red color component in [0.0, 1.0]

    • green: GLfloat

      Green color component in [0.0, 1.0]

    • blue: GLfloat

      Blue color component in [0.0, 1.0]

    • Default value alpha: GLfloat = Color.DEFAULT_ALPHA

      Alpha color component in [0.0, 1.0]

    Returns Color

    • The color instance (this).

fromHSL

  • fromHSL(hue: GLclampf, saturation: GLclampf, lightness: GLclampf, alpha?: GLclampf): Color
  • Specifies the internal rgba store using a color in HSL color space.

    Parameters

    • hue: GLclampf

      Hue color component in [0.0, 1.0]

    • saturation: GLclampf

      Saturation color component in [0.0, 1.0]

    • lightness: GLclampf

      Lightness color component in [0.0, 1.0]

    • Default value alpha: GLclampf = Color.DEFAULT_ALPHA

      Alpha color component in [0.0, 1.0]

    Returns Color

    • The color instance (this).

fromHex

  • fromHex(hex: string): Color
  • Specifies the internal rgba store using a hexadecimal color string.

    Parameters

    • hex: string

      Hexadecimal color string: red, green, blue, and alpha (optional) each in ['00', 'ff'].

    Returns Color

    • The color instance (this).

fromLAB

  • fromLAB(lightness: GLclampf, greenRed: GLclampf, blueYellow: GLclampf, alpha?: GLclampf): Color
  • Specifies the internal rgba store using a color in CIE-Lab color space.

    Parameters

    • lightness: GLclampf

      Lightness color component in [0.0, 1.0]

    • greenRed: GLclampf

      Green-Red/a color component in [0.0, 1.0]

    • blueYellow: GLclampf

      Blue-Yellow/b color component in [0.0, 1.0]

    • Default value alpha: GLclampf = Color.DEFAULT_ALPHA

      Alpha color component in [0.0, 1.0]

    Returns Color

    • The color instance (this).

fromRGB

  • fromRGB(red: GLclampf, green: GLclampf, blue: GLclampf, alpha?: GLclampf): Color
  • Specifies the internal rgba store using a color in RGB color space.

    Parameters

    • red: GLclampf

      Red color component in [0.0, 1.0]

    • green: GLclampf

      Green color component in [0.0, 1.0]

    • blue: GLclampf

      Blue color component in [0.0, 1.0]

    • Default value alpha: GLclampf = Color.DEFAULT_ALPHA

      Alpha color component in [0.0, 1.0]

    Returns Color

    • The color instance (this).

fromUI8

  • Specifies the internal rgba store using a color in unsigned int (8bit) RGBA colors.

    Parameters

    • red: GLubyte

      Red color component in [0, 255]

    • green: GLubyte

      Green color component in [0, 255]

    • blue: GLubyte

      Blue color component in [0, 255]

    • Default value alpha: GLubyte = Math.floor(Color.DEFAULT_ALPHA * 255)

      Alpha color component in [0, 255]

    Returns Color

    • The color instance (this).

gray

  • Converts the color to a gray value using the specified algorithm.

    Parameters

    • Default value algorithm: GrayscaleAlgorithm = Color.GrayscaleAlgorithm.LinearLuminance

      The algorithm used for color to gray conversion.

    Returns GLclampf

tuple

  • Enables generic color access within a specified color space.

    Parameters

    • space: Space

      Expected color space of the requested color values.

    • Default value alpha: boolean = true

      Whether or not alpha channel should be provided as well.

    Returns GLclampf3 | GLclampf4 | GLclampf5

Static cmyk2rgb

  • Converts a color from CMYK space to RGB space.

    Parameters

    • cmyk: GLclampf4

      CMYK color tuple: cyan, magenta, yellow, and key, each in [0.0, 1.0].

    Returns GLclampf3

    • RGB color tuple: red, green, and blue, each in [0.0, 1.0]

Static hex2rgba

  • Converts a color from HEX string to RGBA space. The hex string can start with '#' or '0x' or neither of these.

    Parameters

    • hex: string

      Hexadecimal color string: red, green, and blue, each in ['00', 'ff'].

    Returns GLclampf4

    • RGBA color tuple: red, green, blue, and alpha, each in [0.0, 1.0]. On error [0, 0, 0, 0] is returned.

Static hsl2rgb

  • Converts a color from HSL space to RGB space.

    Parameters

    • hsl: GLclampf3

      HSL color tuple: hue, saturation, and lightness, each in [0.0, 1.0].

    Returns GLclampf3

    • RGB color tuple: red, green, and blue, each in [0.0, 1.0].

Static Protected hue2rgb

  • hue2rgb(p: GLfloat, q: GLfloat, t: GLfloat): GLfloat
  • Converts a hue value into an rgb value.

    Parameters

    • p: GLfloat
    • q: GLfloat
    • t: GLfloat

    Returns GLfloat

Static lab2rgb

  • Converts a color from LAB space to RGB space.

    Parameters

    • lab: GLclampf3

      LAB color tuple: lightness, greenRed, and blueYellow, each in [0.0, 1.0].

    Returns GLclampf3

    • RGB color tuple: red, green, and blue, each in [0.0, 1.0]

Static lab2xyz

  • Converts a color from LAB space to XYZ space.

    Parameters

    • lab: GLclampf3

      LAB color tuple: lightness, greenRed, and blueYellow, each in [0.0, 1.0].

    Returns GLclampf3

    • XYZ color tuple: x, y, and z, each in [0.0, 1.0].

Static mix

  • Performs a linear interpolation between x and y using a to weight between them within the specified color space.

    Parameters

    • x: Color

      First color stop for mix/linear interpolation.

    • y: Color

      Second color stop for mix/linear interpolation.

    • a: number

      Specify the value to use to interpolate between x and y.

    • Default value space: Space = Color.Space.LAB

      The color space that is to be used for linear interpolation of two colors.

    Returns Color

Static rgb2cmyk

  • Converts a color from RGB space to CMYK space.

    Parameters

    • rgb: GLclampf3

      RGB color tuple: red, green, and blue, each in [0.0, 1.0]

    Returns GLclampf4

    • CMYK color tuple: cyan, magenta, yellow, and key, each in [0.0, 1.0].

Static rgb2hex

  • Converts a color from RGB space to HEX string.

    Parameters

    • rgb: GLclampf3

      RGB color tuple: red, green, and blue, each in [0.0, 1.0]

    Returns string

    • Hexadecimal color string: red, green, and blue, each in ['00', 'ff'], with '#' prefix

Static rgb2hsl

  • Converts a color from HSL space to RGB space.

    Parameters

    • rgb: GLclampf3

      RGB color tuple: red, green, and blue, each in [0.0, 1.0].

    Returns GLclampf3

    • HSL color tuple: hue, saturation, and lightness, each in [0.0, 1.0].

Static rgb2lab

  • Converts a color from RGB space to LAB space.

    Parameters

    Returns GLclampf3

    • RGB color tuple: red, green, and blue, each in [0.0, 1.0]

Static rgb2xyz

  • Converts a color from Adobe-RGB space to XYZ space.

    Parameters

    • rgb: GLclampf3

      RGB color tuple: red, green, and blue, each in [0.0, 1.0]

    Returns GLclampf3

    • XYZ color tuple: x, y, and z, each in [0.0, 1.0].

Static rgba2hex

  • Converts a color from RGBA space to HEX string.

    Parameters

    • rgba: GLclampf4

      RGBA color tuple: red, green, blue, and alpha, each in [0.0, 1.0]

    Returns string

    • Hexadecimal color string: red, green, blue, and alpha, each in ['00', 'ff'], with '#' prefix

Static Protected to2CharHexCode

  • to2CharHexCode(value: number): string
  • Converts a float value to two-character HEX code.

    Parameters

    • value: number

      A float value in [0.0, 1.0].

    Returns string

    • Two-character hexadecimal representation in [00, FF].

Static xyz2lab

  • Converts a color from XYZ space to LAB space.

    Parameters

    • xyz: GLclampf3

      XYZ color tuple: x, y, and z, each in [0.0, 1.0].

    Returns GLclampf3

    • LAB color tuple: lightness, greenRed, and blueYellow, each in [0.0, 1.0].

Static xyz2rgb

  • Converts a color from XYZ space to Adobe-RGB space.

    Parameters

    • xyz: GLclampf3

      XYZ color tuple: x, y, and z, and refer to the D65/2° illuminant.

    Returns GLclampf3

    • RGB color tuple: red, green, and blue, each in [0.0, 1.0]