Skip to content

Commit f39c23f

Browse files
committed
Add tests for color
1 parent 730559a commit f39c23f

File tree

3 files changed

+516
-5
lines changed

3 files changed

+516
-5
lines changed

druid/color.lua

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local logger = require("druid.system.druid_logger")
44

55
---Color palette and utility functions for working with colors.
66
---Supports palette management, hex conversion, RGB/HSB conversion, and color interpolation.
7-
---@class druid.palette
7+
---@class druid.color
88
local M = {}
99

1010
local PALETTE_DATA = {}
@@ -83,12 +83,24 @@ function M.lerp(t, color1, color2)
8383
local h1, s1, v1 = M.rgb2hsb(color1.x, color1.y, color1.z)
8484
local h2, s2, v2 = M.rgb2hsb(color2.x, color2.y, color2.z)
8585

86-
local h = h1 + (h2 - h1) * t
86+
local dh = h2 - h1
87+
if math.abs(dh) > 0.5 then
88+
if dh > 0 then
89+
dh = dh - 1
90+
else
91+
dh = dh + 1
92+
end
93+
end
94+
local h = (h1 + dh * t) % 1
8795
local s = s1 + (s2 - s1) * t
8896
local v = v1 + (v2 - v1) * t
8997

90-
local r, g, b, a = M.hsb2rgb(h, s, v)
91-
a = a or 1
98+
local a1 = color1.w or 1
99+
local a2 = color2.w or 1
100+
local a = a1 + (a2 - a1) * t
101+
102+
local r, g, b = M.hsb2rgb(h, s, v)
103+
92104
return vmath.vector4(r, g, b, a)
93105
end
94106

@@ -182,7 +194,7 @@ end
182194
---@param red number
183195
---@param green number
184196
---@param blue number
185-
---@return string
197+
---@return string hex_string Example: "FF0000", without "#" prefix
186198
function M.rgb2hex(red, green, blue)
187199
local r = string.format("%x", math.floor(red * 255))
188200
local g = string.format("%x", math.floor(green * 255))

test/test.gui_script

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function init(self)
1515
deftest.add(require("test.tests.test_rich_text"))
1616
deftest.add(require("test.tests.test_scroll"))
1717
deftest.add(require("test.tests.test_text"))
18+
deftest.add(require("test.tests.test_color"))
1819

1920
local is_report = (sys.get_config_int("test.report", 0) == 1)
2021
deftest.run({ coverage = { enabled = is_report } })

0 commit comments

Comments
 (0)