This repository was archived by the owner on Jul 11, 2026. It is now read-only.
forked from darrellanderson/CrLua
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColor.ttslua
More file actions
105 lines (94 loc) · 2.64 KB
/
Copy pathColor.ttslua
File metadata and controls
105 lines (94 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
--- Color manipulation.
-- @author Darrell
local TAG = 'CrLua.Color'
CrLua = CrLua or {} -- global, <include> wraps in a do .. end block
CrLua.Color = assert(not CrLua.Color) and {
_require = {}
}
--- Returns the RGB equivalent of the given HSV-defined color
-- @author https://github.com/luapower/color/blob/master/color.lua
function CrLua.Color.HSVtoRGB(h, s, v)
if s == 0 then --gray
return v, v, v
end
local H = h / 60.0
local i = math.floor(H) --which 1/6 part of hue circle
local f = H - i
local p = v * (1 - s)
local q = v * (1 - s * f)
local t = v * (1 - s * (1 - f))
if i == 0 then
return v, t, p
elseif i == 1 then
return q, v, p
elseif i == 2 then
return p, v, t
elseif i == 3 then
return p, q, v
elseif i == 4 then
return t, p, v
else
return v, p, q
end
end
--- Returns the HSV equivalent of the given RGB-defined color
-- @author https://github.com/luapower/color/blob/master/color.lua
function CrLua.Color.RGBtoHSV(red, green, blue)
local K = 0
if g < b then
g, b = b, g
K = -1
end
if r < g then
r, g = g, r
K = -2 / 6 - K
end
local chroma = r - math.min(g, b)
local h = math.abs(K + (g - b) / (6 * chroma + 1e-20))
local s = chroma / (r + 1e-20)
local v = r
return h * 360, s, v
end
-- @author https://github.com/luapower/color/blob/master/color.lua
function CrLua.Color.HSLtoRGB(h, s, L)
--hsl is in (0..360, 0..1, 0..1); rgb is (0..1, 0..1, 0..1)
local function h2rgb(m1, m2, h)
if h < 0 then h = h + 1 end
if h > 1 then h = h - 1 end
if h * 6 < 1 then
return m1 + (m2 - m1) * h * 6
elseif h * 2 < 1 then
return m2
elseif h * 3 < 2 then
return m1 + (m2 - m1) * (2 / 3 - h) * 6
else
return m1
end
end
h = h / 360.0
local m2 = L <= 0.5 and L * (s + 1) or L + s - L * s
local m1 = L * 2 - m2
local r = h2rgb(m1, m2, h+1/3)
local g = h2rgb(m1, m2, h)
local b = h2rgb(m1, m2, h-1/3)
return r, g, b
end
--rgb is in (0..1, 0..1, 0..1); hsl is (0..360, 0..1, 0..1)
-- @author https://github.com/luapower/color/blob/master/color.lua
function CrLua.Color.RGBtoHSL(r, g, b)
local min = math.min(r, g, b)
local max = math.max(r, g, b)
local delta = max - min
local h, s, l = 0, 0, (min + max) / 2
if l > 0 and l < 0.5 then s = delta / (max + min) end
if l >= 0.5 and l < 1 then s = delta / (2 - max - min) end
if delta > 0 then
if max == r and max ~= g then h = h + (g - b) / delta end
if max == g and max ~= b then h = h + 2 + (b - r) / delta end
if max == b and max ~= r then h = h + 4 + (r - g) / delta end
h = h / 6
end
if h < 0 then h = h + 1 end
if h > 1 then h = h - 1 end
return h * 360, s, l
end