Color module

For playing with colors.

Usage

local Color = require('stdlib/color/color')

Functions

from_hex (hex[, alpha=1]) Get a color table with a hexadecimal string.
from_rgb ([r=0][, g=0][, b=0][, a=255]) Converts a color in the rgb format to a color table
set ([color=white][, alpha=1]) Set a value for the alpha channel in the given color table.
to_table (c_arr) Converts a color in the array format to a color in the table format.

Functions

# from_hex (hex[, alpha=1])

Get a color table with a hexadecimal string.

Optionally provide the value for the alpha channel.

Parameters:
  • hex : (string) hexadecimal color string (#ffffff, not #fff)
  • alpha : (float) the alpha value to set; such that [ 0 ⋜ value ⋜ 1 ] (default: 1)
Returns:
  • (Color) a color table with RGB converted from Hex and with alpha
# from_rgb ([r=0][, g=0][, b=0][, a=255])

Converts a color in the rgb format to a color table

Parameters:
  • r : (int) 0-255 red (default: 0)
  • g : (int) 0-255 green (default: 0)
  • b : (int) 0-255 blue (default: 0)
  • a : (int) 0-255 alpha (default: 255)
Returns:
# set ([color=white][, alpha=1])

Set a value for the alpha channel in the given color table.

color.a represents the alpha channel in the given color table.

  • If alpha is given, set color.a to it.
  • If alpha is not given, and if the given color table does not have a value for color.a, set color.a to 1.
  • If alpha is not given, and if the given color table already has a value for color.a, then leave color.a alone.

Parameters:
  • color : (defines.color or Color) the color to configure (default: white)
  • alpha : (float) the alpha value ([0 - 1]) to set for the given color (default: 1)
Returns:
  • (Color) a color table that has the specified value for the alpha channel
# to_table (c_arr)

Converts a color in the array format to a color in the table format.

Parameters: Returns:
  • (Color) a converted color — { r = c_arr[1], g = c_arr[2], b = c_arr[3], a = c_arr[4] }