A simple and useful color library for Lua.
Has a special extension that is automatically added if using Love2d.
To begin using this library you must first require it.
local colors = require "LoveColors" -- Replace LoveColors with your location
The color range. rgba color values should be in this range. Another common range would be 255.
There are several built in colors. You can see them all near the bottom of the file.
The entry point for this library.
returns a new color object with the given r, g, b, a values.
local OffWhite = colors.new(0.9804, 0.9765, 0.9647)
-- notice the alpha color was not passed.
-- alpha will default to colors.range if not passed.
local OffWhiteAlpha = colors.new(0.9804, 0.9765, 0.9647, 0.5)
Desaturates a color by intensity amount. Intensity should be 0-1 to represent the percent to desaturate the color.
colors.cyan:desaturate(0.5)
Simply lightens a color. amount should be passed 0-1
Simply darkens a color. amount should be passed 0-1
Interpolates a color linearly between two colors.
local percentFromBlue = 0.25 -- mostly blue but shift 25% towards red
local blueToRed = colors.blue:lerp(colors.red, percentFromBlue)
Changes the alpha value of a color to the percent value of amount. This value must be in the range 0-1. Translation to colors.range happens internally.
Unpacks values from the object into it's 4 parts.
local r, g, b, a = colors.red:unpack()
Returns a color with the linear average of each r, g, b, and a values individually.
This is faster than using averageHue but can lead to less intuitive outcomes.
-- using Love2d to visualize
local c = colors.new(0, 1, 0, 0)
for i = 1, 10 do
local n = {}
table.insert(n, c) -- Start the example color as black
for j = 1, i do
table.insert(n, colors.purple) -- Make it more purple as i gets higher
end
local nc = colors.average(unpack(n))
nc:set()
lg.rectangle("fill", 40, i * 20, 30, 20)
-- Note: The following will not produce exactly the same colors as the above
-- Because one averages them all at once which slightly changes the math.
c = c:average(colors.purple) -- You can still call it with metamethods!
c:set()
lg.rectangle("fill", 0, i * 20, 30, 20)
end
Takes color objects and averages their hsl color rotation.
This is more intensive than colors.average
Just spits out a random color. That's all.
Takes a color object, and returns a brand new identical one.
Transforms a Colors object into an HSL table alias: colors:hsl()
Transforms h,s,l to a Colors object
Transforms a Colors object into an HSV table alias: colors:hsv()
Transforms h,s,v to a Colors object
The following work just like the love.graphics versions, but with direct support for LoveColors
colors:set()
colors:setBackgroundColor()
colors:getColor()
colors:getBackgroundColor()
Adds the following simple metamethod support.
Adds each r,g,b,a value from a to b and returns a new Colors object
Subtracts each r,g,b,a value from a to b and returns a new Colors object
multiplies b to a. b can be a number or another color.
local a = colors.white * 0.5
print(a:unpack()) -- 0.5, 0.5, 0.5, 0.5
local b = colors.new(1, 1, 0.3, 1)
local c = colors.new(0.3, 1, 0.3)
local out = b * c
print(out:unpack()) -- 0.3, 1, 0.1, 1
Divides each color channel by b and returns a new Colors object
Returns boolean if the two colors are the same color. This returns true whether or not a
and b
have the same memory address.
I intend to make better use of HSL and HSV functions and make them have their own metatables