Defold Colors provides customizable palettes and color utility features to a Defold game engine project.
Please click the ☆ button on GitHub if this repository is useful or interesting. Thank you!
Add the latest version to your project's dependencies:
https://github.com/whiteboxdev/library-defold-colors/archive/master.zip
Import the dcolors Lua module into your relevant scripts:
local dcolors = require "dcolors.dcolors"
The dcolors.palette
property allows you to access the colors stored inside the loaded palette. A palette is structured as follows:
example =
{
white = vmath.vector4(1, 1, 1, 1),
black = vmath.vector4(0, 0, 0, 1),
yellow = vmath.vector4(1, 0, 1, 1),
...
}
In this case, example
is the name of the palette. Each color inside has a key of name
and a value of vector4
.
To access a color, use dcolors.palette.COLOR_NAME
. To add or remove colors from a palette, use the color-related functions.
To add or remove palettes, use the palette-related functions. To change the palette which dcolors.palette
refers to, call dcolors.choose_palette()
.
You may also use dcolors for its utility features, separate from its palette service. For example, dcolors.set_alpha()
modifies the vector4
passed in, then returns the new color. This is useful for consolidating code. For example, let us assume you want to get the color of a gui node, modify its alpha value to 100
, then set the color of that gui node to the modified color. Normally, you could perform this task like so:
local color = gui.get_color(gui.get_node("node"))
color.w = 100
gui.set_color(gui.get_node("node"), color)
However, with the inline convenience of dcolors, you could perform this task like so:
gui.set_color(gui.get_node("node"), dcolors.set_alpha(gui.get_color(gui.get_node("node")), 100))
RGB, HSL, and Hex conversions are also supported.
Table containing all registered palettes and colors. This may be traversed if you are saving or loading color data. You may also use dcolors.vault.PALETTE_NAME.COLOR_NAME
to avoid switching palettes with dcolors.choose_palette()
. The vault is structured as follows:
dcolors.vault =
{
<palette_name> =
{
<color_name> = vmath.vector4( ... ),
...
},
...
}
Currently loaded palette. To access a color, use dcolors.palette.COLOR_NAME
. A palette is structured as follows:
<palette_name> =
{
<color_name> = vmath.vector4( ... ),
...
}
Sets the red component of a color.
color
:vector4
to modify.red
: Number denoting a new red value.
Returns a vector4
.
Sets the green component of a color.
color
:vector4
to modify.green
: Number denoting a new green value.
Returns a vector4
.
Sets the blue component of a color.
color
:vector4
to modify.blue
: Number denoting a new blue value.
Returns a vector4
.
Sets the alpha component of a color.
color
:vector4
to modify.alpha
: Number denoting a new alpha value.
Returns a vector4
.
Checks if a palette exists.
palette_name
: Name of palette.
Returns true
or false
.
Adds an empty palette to the vault. If no palette is currently loaded, then dcolors.palette
becomes palette_name
. Does nothing if the palette already exists.
palette_name
: Name of palette.
Removes a palette from the vault. If the currently loaded palette is removed, then dcolors.palette
becomes nil
. Does nothing if the palette does not exist.
palette_name
: Name of palette.
Clears all colors from a palette. Does nothing if the palette does not exist.
palette_name
: Name of palette.
Points the dcolors.palette
property to a palette. Does nothing if the palette does not exist.
palette_name
: Name of palette.
Checks if a color exists within a palette.
palette_name
: Name of palette.color
:vector4
or name of color.
Returns true
or false
.
Adds a color to a palette. Does nothing if the palette does not exist. If color_name
already exists, its associated color value will be overwritten with color
.
palette_name
: Name of palette.color_name
: Name of color.color
:vector4
to add.
Removes a color from a palette. Does nothing if the palette does not exist. Does nothing if the color does not exist.
palette
: Name of palette.color
:vector4
or name of color.
Applies color.w
to its other components.
color
:vector4
to modify.
Returns a vector4
.
Checks if a color is formatted in hexadecimal. Does not check for a preceding #
symbol.
color
: String of color value.lengths
: Map of acceptable string lengths, structured as follows:
local lengths =
{
[3] = true, -- #ABC
[6] = true, -- #AABBCC
[8] = true -- #AABBCCFF (alpha)
}
Returns true
or false
.
Converts an RGBA color to HSLA.
color
:vector4
to convert.
Returns a vector4
.
Converts an HSLA color to RGBA.
color
:vector4
to convert.
Returns a vector4
.
Converts an RGBA color to Hex.
color
:vector4
to convert.
Returns a string
.
Converts a Hex color to RGBA.
color
:string
to convert. Do not include a prefix such as0x
or#
.
Returns a vector4
.