[Breaking] color: Make expected range match docs

lighten/darken: documented range is 0-255, so we don't need to multiply.
Makes more sense to work with colors in the same range that we're
incrementing by, but this changes behaviour.

saturation/value: we use a percent, so it's 0-1. It's clamped so that's
obviously expected.

Add tests to validate this new behaviour.
This commit is contained in:
David Briscoe 2022-03-26 23:38:41 -07:00
parent 509594aec2
commit df92f0c5cd
2 changed files with 70 additions and 16 deletions

View File

@ -167,9 +167,9 @@ end
-- @treturn color out
function color.lighten(c, v)
return new(
utils.clamp(c[1] + v * 255, 0, 255),
utils.clamp(c[2] + v * 255, 0, 255),
utils.clamp(c[3] + v * 255, 0, 255),
utils.clamp(c[1] + v, 0, 255),
utils.clamp(c[2] + v, 0, 255),
utils.clamp(c[3] + v, 0, 255),
c[4]
)
end
@ -184,9 +184,9 @@ end
-- @treturn color out
function color.darken(c, v)
return new(
utils.clamp(c[1] - v * 255, 0, 255),
utils.clamp(c[2] - v * 255, 0, 255),
utils.clamp(c[3] - v * 255, 0, 255),
utils.clamp(c[1] - v, 0, 255),
utils.clamp(c[2] - v, 0, 255),
utils.clamp(c[3] - v, 0, 255),
c[4]
)
end
@ -215,7 +215,7 @@ function color.alpha(c, v)
t[i] = c[i]
end
t[4] = v * 255
t[4] = v
return t
end
@ -245,7 +245,7 @@ end
--- Set a color's saturation (hue, value, alpha unchanged)
-- @tparam color to alter
-- @tparam hue to set 0-359
-- @tparam saturation to set 0-1
-- @treturn color out
function color.saturation(col, percent)
local c = color_to_hsv(col)
@ -255,7 +255,7 @@ end
--- Set a color's value (saturation, hue, alpha unchanged)
-- @tparam color to alter
-- @tparam hue to set 0-359
-- @tparam value to set 0-1
-- @treturn color out
function color.value(col, percent)
local c = color_to_hsv(col)

View File

@ -7,10 +7,16 @@ local function assert_is_float_equal(a, b)
end
end
local function assert_is_approx_equal(a, b)
if math.abs(a - b) > 0.001 then
assert.is.equal(a, b)
end
end
describe("color:", function()
it("operators: add, subract, multiply", function()
local c = color(1,1,1,1)
local c = color(1, 1, 1, 1)
assert.is_true(c:is_color())
local r = c + c
assert.is_true(r:is_color())
@ -29,18 +35,66 @@ describe("color:", function()
assert_is_float_equal(r[3], 5)
end)
it("rgb -> hsv -> rgb", function()
local c = color(1,1,1,1)
local hsv = c:color_to_hsv_table()
local c1 = color.hsv_to_color_table(hsv)
local c2 = color.from_hsva(hsv[1], hsv[2], hsv[3], hsv[4])
local c3 = color.from_hsv(hsv[1], hsv[2], hsv[3])
c3[4] = c[4]
for i=1,4 do
assert_is_float_equal(c[i], c1[i])
assert_is_float_equal(c[i], c2[i])
assert_is_float_equal(c[i], c3[i])
end
assert.is_true(c:is_color())
assert.is_true(c1:is_color())
assert.is_true(c2:is_color())
assert.is_true(c3:is_color())
end)
it("hsv -> rgb -> hsv", function()
local hsv1 = { 0, 0.3, 0.8, 0.9 }
for h=0,359, 10 do
hsv1[1] = h
local cc = color.hsv_to_color_table(hsv1)
local hsv2 = cc:color_to_hsv_table()
for i=1,4 do
assert_is_approx_equal(hsv1[i], hsv2[i])
end
end
end)
it("lighten a color", function()
local c = color(0, 0, 0, 0)
local r = c:lighten(10)
assert.is.equal(r[1], 10)
r = c:lighten(1000)
assert.is.equal(r[1], 255)
end)
it("darken a color", function()
local c = color(255, 255, 255, 255)
local r = c:darken(10)
assert.is.equal(r[1], 245)
r = c:darken(1000)
assert.is.equal(r[1], 0)
end)
it("modify alpha", function()
local c = color(255, 255, 255, 255)
local r = c:alpha(10)
assert.is.equal(r[4], 10)
r = c:opacity(0.5)
assert.is.equal(r[4], 255/2)
end)
end)
--[[
from_hsv(h, s, v)
from_hsva(h, s, v, a)
invert(c)
lighten(c, v)
lerp(a, b, s)
darken(c, v)
multiply(c, v)
alpha(c, v)
opacity(c, v)
hue(color, hue)
saturation(color, percent)
value(color, percent)