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 ifcolor.a < 1
Returns:
-
string
#rrggbb
|#rrggbbaa
See also:
- Color:__eq (other)
-
Check if colors are equal.
Parameters:
- other Color
Returns:
-
boolean
all values are equal
- Color:__lt (other)
-
Checks whether color is darker.
Parameters:
- other Color
Returns:
-
boolean
self is darker than other
- Color:__le (other)
-
Checks whether color is as dark or darker.
Parameters:
- other Color
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:__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)
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
[0;1]
.
They will be applied in the order:rgba -> hsl -> hwb -> hsv -> cmyk
Iflightness
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:
- number[0;1] red
- number[0;1] green
- number[0;1] blue
- Color:rgba ()
-
Get rgba values.
Returns:
- number[0;1] red
- number[0;1] green
- number[0;1] blue
- number[0;1] alpha
- Color:hsv ()
-
Get hsv values.
Returns:
- number[0;1] hue
- number[0;1] saturation
- number[0;1] value
- Color:hsva ()
-
Get hsv values.
Returns:
- number[0;1] hue
- number[0;1] saturation
- number[0;1] value
- number[0;1] alpha
- Color:hsl ()
-
Get hsl values.
Returns:
- number[0;1] hue
- number[0;1] saturation
- number[0;1] lightness
- Color:hsla ()
-
Get hsl values.
Returns:
- number[0;1] hue
- number[0;1] saturation
- number[0;1] lightness
- number[0;1] alpha
- Color:hwb ()
-
Get hwb values.
Returns:
- number[0;1] hue
- number[0;1] whiteness
- number[0;1] blackness
- Color:hwba ()
-
Get hwb values.
Returns:
- number[0;1] hue
- number[0;1] whiteness
- number[0;1] blackness
- number[0;1] alpha
- Color:cmyk ()
-
Get cmyk values.
Returns:
- number[0;1] cyan
- number[0;1] magenta
- number[0;1] yellow
- 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:analogous ()
-
Generate analogous color scheme.
Returns:
- Color:triad ()
-
Generate triadic color scheme.
Returns:
- Color:tetrad ()
-
Generate tetradic color scheme.
Returns:
- Color:compound ()
-
Generate compound color scheme.
Returns:
- 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
isnil
,color:tostring()
is the same astostring(color)
.Parameters:
- format
optional string
One of:
#fff
,#ffff
,#ffffff
,#ffffffff
, rgb, rgba, hsv, hsva, hsl, hsla, hwb, hwba, ncol, cmyk
Returns:
See also:
- format
optional string
One of:
- 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