[color] Add color operations and utilities.

This commit is contained in:
Lorenzo Cogotti 2023-06-01 23:12:43 +02:00
parent 4ad64d0343
commit 2abcf253ca
1 changed files with 58 additions and 0 deletions

58
color.lua Normal file
View File

@ -0,0 +1,58 @@
--- Utility functions to manipulate colors.
--
-- Colors are represented as RGB or RGBA, each channel
-- with values in range [0, 1].
--
-- @module gear.color
-- @copyright 2023 The DoubleFourteen Code Forge
-- @author Lorenzo Cogotti
local Color = {}
--- Darken RGB color by the specified amount.
--
-- @number r red channel
-- @number g green channel
-- @number b blue channel
-- @number amount darken factor, must be in [0,1] range.
function Color.darken(r, g, b, amount)
r = r * (1 - amount)
g = g * (1 - amount)
b = b * (1 - amount)
return r, g, b
end
--- Lighten RGB color by the specified amount.
--
-- @number r red channel
-- @number g green channel
-- @number b blue channel
-- @number amount lightening factor, must be in [0,1] range.
function Color.lighten(r, g, b, amount)
r = r + (1 - r) * amount
g = g + (1 - g) * amount
b = b + (1 - b) * amount
return r, g, b
end
--- Given two RGB colors, calculate fade by the specified amount.
--
-- @number cr first color, red channel
-- @number cg first color, green channel
-- @number cb first color, blue channel
-- @number ar second color, red channel
-- @number ag second color, green channel
-- @number ab second color, blue channel
-- @number amount fading factor, must be in [0,1] range.
function Color.fade(cr, cg, cb, ar, ag, ab, amount)
cr = cr + (ar - cr) * amount
cg = cg + (ag - cg) * amount
cb = cb + (ab - cb) * amount
return cr, cg, cb
end
return Color