@@ -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
88local M = {}
99
1010local 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 )
93105end
94106
182194--- @param red number
183195--- @param green number
184196--- @param blue number
185- --- @return string
197+ --- @return string hex_string Example : " FF0000 " , without " # " prefix
186198function 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 ))
0 commit comments