-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathcolour.lua
301 lines (260 loc) · 7.41 KB
/
colour.lua
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
--[[
colour handling stuff
]]
local path = (...):gsub("colour", "")
local math = require(path.."mathx")
local colour = {}
-------------------------------------------------------------------------------
-- hex handling routines
-- pack and unpack into 24 or 32 bit hex numbers
local ok, bit = pcall(require, "bit")
if ok then
--we have bit operations module, use the fast path
local band, bor = bit.band, bit.bor
local lshift, rshift = bit.lshift, bit.rshift
--rgb only (no alpha)
function colour.pack_rgb(r, g, b)
local br = lshift(band(0xff, r * 255), 16)
local bg = lshift(band(0xff, g * 255), 8)
local bb = lshift(band(0xff, b * 255), 0)
return bor( br, bg, bb )
end
function colour.unpack_rgb(rgb)
local r = rshift(band(rgb, 0x00ff0000), 16) / 255
local g = rshift(band(rgb, 0x0000ff00), 8) / 255
local b = rshift(band(rgb, 0x000000ff), 0) / 255
return r, g, b
end
--argb format (common for shared hex)
function colour.pack_argb(r, g, b, a)
local ba = lshift(band(0xff, a * 255), 24)
local br = lshift(band(0xff, r * 255), 16)
local bg = lshift(band(0xff, g * 255), 8)
local bb = lshift(band(0xff, b * 255), 0)
return bor( br, bg, bb, ba )
end
function colour.unpack_argb(argb)
local r = rshift(band(argb, 0x00ff0000), 16) / 255
local g = rshift(band(argb, 0x0000ff00), 8) / 255
local b = rshift(band(argb, 0x000000ff), 0) / 255
local a = rshift(band(argb, 0xff000000), 24) / 255
return r, g, b, a
end
--rgba format
function colour.pack_rgba(r, g, b, a)
local br = lshift(band(0xff, r * 255), 24)
local bg = lshift(band(0xff, g * 255), 16)
local bb = lshift(band(0xff, b * 255), 8)
local ba = lshift(band(0xff, a * 255), 0)
return bor( br, bg, bb, ba )
end
function colour.unpack_rgba(rgba)
local r = rshift(band(rgba, 0xff000000), 24) / 255
local g = rshift(band(rgba, 0x00ff0000), 16) / 255
local b = rshift(band(rgba, 0x0000ff00), 8) / 255
local a = rshift(band(rgba, 0x000000ff), 0) / 255
return r, g, b, a
end
else
--we don't have bitops, use a slower pure-float path
local floor = math.floor
--rgb only (no alpha)
function colour.pack_rgb(r, g, b)
local br = floor(0xff * r) % 0x100 * 0x10000
local bg = floor(0xff * g) % 0x100 * 0x100
local bb = floor(0xff * b) % 0x100
return br + bg + bb
end
function colour.unpack_rgb(rgb)
local r = floor(rgb / 0x10000) % 0x100
local g = floor(rgb / 0x100) % 0x100
local b = floor(rgb) % 0x100
return r / 255, g / 255, b / 255
end
--argb format (common for shared hex)
function colour.pack_argb(r, g, b, a)
local ba = floor(0xff * a) % 0x100 * 0x1000000
return colour.pack_rgb(r, g, b) + ba
end
function colour.unpack_argb(argb)
local r, g, b = colour.unpack_rgb(argb)
local a = floor(argb / 0x1000000) % 0x100
return r, g, b, a / 255
end
--rgba format
function colour.pack_rgba(r, g, b, a)
local ba = floor(0xff * a) % 0x100
return colour.pack_rgb(r, g, b) * 0x100 + ba
end
function colour.unpack_rgba(rgba)
local r, g, b = colour.unpack_rgb(floor(rgba / 0x100))
local a = floor(rgba) % 0x100
return r, g, b, a
end
end
-------------------------------------------------------------------------------
-- colour space conversion
-- rgb is the common language for computers
-- but it's useful to have other spaces to work in for us as humans :)
--convert hsl to rgb
--all components are 0-1, hue is fraction of a turn rather than degrees or radians
function colour.hsl_to_rgb(h, s, l)
--wedge slice
local w = (math.wrap(h, 0, 1) * 6)
--chroma
local c = (1 - math.abs(2 * l - 1)) * s
--secondary
local x = c * (1 - math.abs(w % 2 - 1))
--lightness boost
local m = l - c / 2
--per-wedge logic
local r, g, b = m, m, m
if w < 1 then
r = r + c
g = g + x
elseif w < 2 then
r = r + x
g = g + c
elseif w < 3 then
g = g + c
b = b + x
elseif w < 4 then
g = g + x
b = b + c
elseif w < 5 then
b = b + c
r = r + x
else
b = b + x
r = r + c
end
return r, g, b
end
--convert rgb to hsl
function colour.rgb_to_hsl(r, g, b)
local max, min = math.max(r, g, b), math.min(r, g, b)
if max == min then return 0, 0, min end
local l, d = max + min, max - min
local s = d / (l > 1 and (2 - l) or l)
l = l / 2
local h --depends on below
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then
h = (b - r) / d + 2
else
h = (r - g) / d + 4
end
return h / 6, s, l
end
--convert hsv to rgb
--all components are 0-1, hue is fraction of a turn rather than degrees or radians
function colour.hsv_to_rgb(h, s, v)
--wedge slice
local w = (math.wrap(h, 0, 1) * 6)
--chroma
local c = v * s
--secondary
local x = c * (1 - math.abs(w % 2 - 1))
--match value
local m = v - c
--per-wedge logic
local r, g, b = m, m, m
if w < 1 then
r = r + c
g = g + x
elseif w < 2 then
r = r + x
g = g + c
elseif w < 3 then
g = g + c
b = b + x
elseif w < 4 then
g = g + x
b = b + c
elseif w < 5 then
b = b + c
r = r + x
else
b = b + x
r = r + c
end
return r, g, b
end
--convert rgb to hsv
function colour.rgb_to_hsv(r, g, b)
local max, min = math.max(r, g, b), math.min(r, g, b)
if max == min then return 0, 0, min end
local v, d = max, max - min
local s = (max == 0) and 0 or (d / max)
local h --depends on below
if max == r then
h = (g - b) / d
if g < b then h = h + 6 end
elseif max == g then
h = (b - r) / d + 2
else
h = (r - g) / d + 4
end
return h / 6, s, v
end
--conversion between hsl and hsv
function colour.hsl_to_hsv(h, s, l)
local v = l + s * math.min(l, 1 - l)
s = (v == 0) and 0 or (2 * (1 - l / v))
return h, s, v
end
function colour.hsv_to_hsl(h, s, v)
local l = v * (1 - s / 2)
s = (l == 0 or l == 1) and 0 or ((v - l) / math.min(l, 1 - l))
return h, s, l
end
--oklab https://bottosson.github.io/posts/oklab/
function colour.oklab_to_rgb(l, a, b)
local _l = l + 0.3963377774 * a + 0.2158037573 * b
local _m = l - 0.1055613458 * a - 0.0638541728 * b
local _s = l - 0.0894841775 * a - 1.2914855480 * b
_l = math.pow(_l, 3.0)
_m = math.pow(_m, 3.0)
_s = math.pow(_s, 3.0)
local red, green, blue = love.math.linearToGamma(
( 4.0767245293 * _l - 3.3072168827 * _m + 0.2307590544 * _s),
(-1.2681437731 * _l + 2.6093323231 * _m - 0.3411344290 * _s),
(-0.0041119885 * _l - 0.7034763098 * _m + 1.7068625689 * _s)
)
return red, green, blue
end
function colour.rgb_to_oklab(red, green, blue)
red, green, blue = love.math.gammaToLinear(red, green, blue)
local _l = 0.4121656120 * red + 0.5362752080 * green + 0.0514575653 * blue
local _m = 0.2118591070 * red + 0.6807189584 * green + 0.1074065790 * blue
local _s = 0.0883097947 * red + 0.2818474174 * green + 0.6302613616 * blue
_l = math.pow(_l, 1.0 / 3.0)
_m = math.pow(_m, 1.0 / 3.0)
_s = math.pow(_s, 1.0 / 3.0)
local l = 0.2104542553 * _l + 0.7936177850 * _m - 0.0040720468 * _s
local a = 1.9779984951 * _l - 2.4285922050 * _m + 0.4505937099 * _s
local b = 0.0259040371 * _l + 0.7827717662 * _m - 0.8086757660 * _s
return l, a, b
end
--colour distance functions
--distance of one colour to another (linear space)
--can be used for finding nearest colours for palette mapping, for example
function colour.distance_rgb(
ar, ag, ab,
br, bg, bb
)
local dr, dg, db = ar - br, ag - bg, ab - bb
return math.sqrt(dr * dr + dg * dg + db * db)
end
function colour.distance_packed_rgb(a, b)
local ar, ag, ab = colour.unpack_rgb(a)
local br, bg, bb = colour.unpack_rgb(b)
return colour.distance_rgb(
ar, ag, ab,
br, bg, bb
)
end
--todo: rgba and various other unpacks
return colour