Class Color

Parse, convert and manipulate color values.

Fields

Color.r Red component.
Color.g Green component.
Color.b Blue component.
Color.a Alpha component.
Color.colorNames Table of color names.

Metamethods

Color:__call (value) Color constructor.
Color:__tostring () Get color in rgb hex notation.
Color:__eq (other) Check if colors are equal.
Color:__lt (other) Checks whether color is darker.
Color:__le (other) Checks whether color is as dark or darker.
Color:__pairs () Iterate through color.
Color:__unm () Get inverted clone of color.
Color:__add (a, b) Mix two colors evenly.
Color:__sub (a, b) Complement of even mix.
Color:__band (a, b) Apply rgb mask to color.

Methods

Color:clone () Clone color
Color:set (value) Set color to value.
Color:rgb () Get rgb values.
Color:rgba () Get rgba values.
Color:hsv () Get hsv values.
Color:hsva () Get hsv values.
Color:hsl () Get hsl values.
Color:hsla () Get hsl values.
Color:hwb () Get hwb values.
Color:hwba () Get hwb values.
Color:cmyk () Get cmyk values.
Color:rotate (value) Rotate hue of color.
Color:invert () Invert the color.
Color:grey () Reduce saturation to 0.
Color:blackOrWhite (lightness) Set to black or white depending on lightness.
Color:mix (other, strength) Mix two colors together.
Color:complement () Generate complementary color.
Color:analogous () Generate analogous color scheme.
Color:triad () Generate triadic color scheme.
Color:tetrad () Generate tetradic color scheme.
Color:compound () Generate compound color scheme.
Color:evenlySpaced (n, r) Generate evenly spaced color scheme.
Color:tostring (format) Get string representation of color.
Color:band (a, b) Apply rgb mask to color, providing backwards compatibility for Lua 5.1 and LuaJIT 2.1.0-beta3 (e.g.
Color:isColor (color) Check whether color is a Color.


Fields

Color.r
Red component.
  • r
Color.g
Green component.
  • g
Color.b
Blue component.
  • b
Color.a
Alpha component.
  • a
Color.colorNames
Table of color names.
Can be set to a table containing named colors to be used by Color:set
Values must be compatible with Color:set
Default: nil

Usage:

    Color.colorNames = { red = "#ff0000", green = "#00ff00", blue = "#0000ff" }
    local color = Color "green"

Metamethods

Color:__call (value)
Color constructor.

Parameters:

See also:

Color:__tostring ()
Get color in rgb hex notation.
only adds alpha value if color.a < 1

Returns:

    string #rrggbb | #rrggbbaa

See also:

Color:__eq (other)
Check if colors are equal.

Parameters:

Returns:

    boolean all values are equal
Color:__lt (other)
Checks whether color is darker.

Parameters:

Returns:

    boolean self is darker than other
Color:__le (other)
Checks whether color is as dark or darker.

Parameters:

Returns:

    boolean self is as dark or darker than other
Color:__pairs ()
Iterate through color.

Iterates through r, g, b, and a.

Color:__unm ()
Get inverted clone of color.

Returns:

    Color
Color:__add (a, b)
Mix two colors evenly.

Parameters:

Returns:

    Color new color

See also:

Color:__sub (a, b)
Complement of even mix.

Parameters:

Returns:

    Color new color

See also:

Color:__band (a, b)
Apply rgb mask to color.

Parameters:

  • a Color or number color or mask
  • b Color or number color or mask (if a and b are colors b is used as mask)

Returns:

    Color new color

Usage:

    local new_col = color & 0xff00ff -- get new color without the green channel

Methods

Color:clone ()
Clone color

Returns:

    Color copy
Color:set (value)
Set color to value.
Called by constructor

Possible value types:
  • Color
  • color name as specified in Color.colorNames
  • css style functions as string:
    • rgb(r, g, b)
    • rgba(r, g, b, a)
    • hsl(h, s, l)
    • hsla(h, s, l, a)
    • hsv(h, s, v)
    • hsva(h, s, v, a)
    • hwb(h, w, b)
    • hwba(h, w, b, a)
    • cmyk(c, m, y, k)
    Values are in the same ranges as in css ([0;255] for rgb, [0;1] for alpha, ...)
    functions can be specified in a simplified syntax: rgb(r, g, b) == rgb r g b
  • NCol string: R10, 50%, 50%
  • hex string: #rgb | #rgba | #rrggbb | #rrggbbaa (# can be omitted)
  • rgb values in [0;1]: {r, g, b[, a]} | {r=r, g=g, b=b[, a=a]}
  • hsv values in [0;1]: {h=h, s=s, v=v[, a=a]}
  • hsl values in [0;1]: {h=h, s=s, l=l[, a=a]}
  • hwb values in [0;1]: {h=h, w=w, b=b[, a=a]}
  • cmyk values in [0;1]: {c=c, m=m, y=y, k=k}
  • single set mode, table with any combination of the following:
    • red
    • green
    • blue
    • alpha
    • hue
    • saturation
    • value
    • lightness
    • whiteness
    • blackness
    • cyan
    • magenta
    • yellow
    • key
    All values are in [0;1].
    They will be applied in the order: rgba -> hsl -> hwb -> hsv -> cmyk
    If lightness is given, saturation is treated as hsl saturation, otherwise it will be treated as hsv saturation.

Parameters:

Returns:

    Color self

See also:

Usage:

  • color:set "#f1f1f1"
  • color:set "rgba(241, 241, 241, 0.5)"
  • color:set "hsl 180 100% 20%"
  • color:set { r = 0.255, g = 0.729, b = 0.412 }
  • color:set { 0.255, 0.729, 0.412 } -- same as above
  • color:set { h = 0.389, s = 0.65, v = 0.73 }
Color:rgb ()
Get rgb values.

Returns:

  1. number[0;1] red
  2. number[0;1] green
  3. number[0;1] blue
Color:rgba ()
Get rgba values.

Returns:

  1. number[0;1] red
  2. number[0;1] green
  3. number[0;1] blue
  4. number[0;1] alpha
Color:hsv ()
Get hsv values.

Returns:

  1. number[0;1] hue
  2. number[0;1] saturation
  3. number[0;1] value
Color:hsva ()
Get hsv values.

Returns:

  1. number[0;1] hue
  2. number[0;1] saturation
  3. number[0;1] value
  4. number[0;1] alpha
Color:hsl ()
Get hsl values.

Returns:

  1. number[0;1] hue
  2. number[0;1] saturation
  3. number[0;1] lightness
Color:hsla ()
Get hsl values.

Returns:

  1. number[0;1] hue
  2. number[0;1] saturation
  3. number[0;1] lightness
  4. number[0;1] alpha
Color:hwb ()
Get hwb values.

Returns:

  1. number[0;1] hue
  2. number[0;1] whiteness
  3. number[0;1] blackness
Color:hwba ()
Get hwb values.

Returns:

  1. number[0;1] hue
  2. number[0;1] whiteness
  3. number[0;1] blackness
  4. number[0;1] alpha
Color:cmyk ()
Get cmyk values.

Returns:

  1. number[0;1] cyan
  2. number[0;1] magenta
  3. number[0;1] yellow
  4. number[0;1] key
Color:rotate (value)
Rotate hue of color.

Parameters:

  • value number[0;1] or table Part of full turn or table containing degree or radians

Returns:

    Color self

Usage:

  • color:rotate(0.5)
  • color:rotate {deg=180}
  • color:rotate {rad=math.pi}
Color:invert ()
Invert the color.

Returns:

    Color self
Color:grey ()
Reduce saturation to 0.

Returns:

    Color self
Color:blackOrWhite (lightness)
Set to black or white depending on lightness.

Parameters:

  • lightness optional number[0;1] Cutoff point (Default: 0.5)

Returns:

    Color self
Color:mix (other, strength)
Mix two colors together.

Parameters:

  • other Color
  • strength optional number 0 results in self, 1 results in other (Default: 0.5)

Returns:

    Color self
Color:complement ()
Generate complementary color.

Returns:

    Color
Color:analogous ()
Generate analogous color scheme.

Returns:

  1. Color
  2. Color self
  3. Color
Color:triad ()
Generate triadic color scheme.

Returns:

  1. Color self
  2. Color
  3. Color
Color:tetrad ()
Generate tetradic color scheme.

Returns:

  1. Color self
  2. Color
  3. Color
  4. Color
Color:compound ()
Generate compound color scheme.

Returns:

  1. Color
  2. Color self
  3. Color
Color:evenlySpaced (n, r)
Generate evenly spaced color scheme.
Generalization of triad and tetrad.

Parameters:

  • n int Return n colors
  • r optional number Space colors over r rotations (Default: 1)

Returns:

    {Color,...} Table with n colors including self at index 1
Color:tostring (format)
Get string representation of color.

If format is nil, color:tostring() is the same as tostring(color).

Parameters:

  • format optional string One of: #fff, #ffff, #ffffff, #ffffffff, rgb, rgba, hsv, hsva, hsl, hsla, hwb, hwba, ncol, cmyk

Returns:

    string

See also:

Color:band (a, b)
Apply rgb mask to color, providing backwards compatibility for Lua 5.1 and LuaJIT 2.1.0-beta3 (e.g. inside Neovim), which don't provide native support for bitwise operators.

Parameters:

  • a Color or number color or mask
  • b Color or number color or mask (if a and b are colors b is used as mask)

Returns:

    Color new color

Usage:

    local new_col = Color.band(color, 0xff00ff) -- get new color without the green channel
Color:isColor (color)
Check whether color is a Color.

Parameters:

  • color

Returns:

    boolean is a color

Usage:

    if Color.isColor(color) then print "It's a color!" end
generated by LDoc 1.4.6 Last updated 2022-12-30 15:01:58