-
Notifications
You must be signed in to change notification settings - Fork 222
/
color.lua
423 lines (371 loc) · 10.4 KB
/
color.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
local util = require("neogit.lib.util")
local M = {}
--#region TYPES
---@class RGBA
---@field red number Float [0,1]
---@field green number Float [0,1]
---@field blue number Float [0,1]
---@field alpha number Float [0,1]
---@class HSV
---@field hue number Float [0,360)
---@field saturation number Float [0,1]
---@field value number Float [0,1]
---@class HSL
---@field hue number Float [0,360)
---@field saturation number Float [0,1]
---@field lightness number Float [0,1]
--#endregion
---@class Color
---@field red number
---@field green number
---@field blue number
---@field alpha number
---@field hue number
---@field saturation number
---@field value number
---@field lightness number
local Color = setmetatable({}, {})
function Color:init(r, g, b, a)
self:set_red(r)
self:set_green(g)
self:set_blue(b)
self:set_alpha(a)
end
---@param c Color
---@return Color
function Color.from_color(c)
return Color(c.red, c.green, c.blue, c.alpha)
end
---@param h number Hue. Float [0,360)
---@param s number Saturation. Float [0,1]
---@param v number Value. Float [0,1]
---@param a number (Optional) Alpha. Float [0,1]
---@return Color
function Color.from_hsv(h, s, v, a)
h = h % 360
s = util.clamp(s, 0, 1)
v = util.clamp(v, 0, 1)
a = util.clamp(a or 1, 0, 1)
local function f(n)
local k = (n + h / 60) % 6
return v - v * s * math.max(math.min(k, 4 - k, 1), 0)
end
return Color(f(5), f(3), f(1), a)
end
---@param h number Hue. Float [0,360)
---@param s number Saturation. Float [0,1]
---@param l number Lightness. Float [0,1]
---@param a number (Optional) Alpha. Float [0,1]
---@return Color
function Color.from_hsl(h, s, l, a)
h = h % 360
s = util.clamp(s, 0, 1)
l = util.clamp(l, 0, 1)
a = util.clamp(a or 1, 0, 1)
local _a = s * math.min(l, 1 - l)
local function f(n)
local k = (n + h / 30) % 12
return l - _a * math.max(math.min(k - 3, 9 - k, 1), -1)
end
return Color(f(0), f(8), f(4), a)
end
---Create a color from a hex number.
---@param c number|string Either a literal number or a css-style hex string (`#RRGGBB[AA]`).
---@return Color
function Color.from_hex(c)
local n = c
if type(c) == "string" then
local s = c:lower():match("#?([a-f0-9]+)")
n = tonumber(s, 16)
if #s <= 6 then
n = bit.lshift(n, 8) + 0xff
end
end
return Color(
bit.rshift(n, 24) / 0xff,
bit.band(bit.rshift(n, 16), 0xff) / 0xff,
bit.band(bit.rshift(n, 8), 0xff) / 0xff,
bit.band(n, 0xff) / 0xff
)
end
---@return Color
function Color:clone()
return Color(self.red, self.green, self.blue, self.alpha)
end
---Returns a new shaded color.
---@param f number Amount. Float [-1,1].
---@return Color
function Color:shade(f)
local t = f < 0 and 0 or 1.0
local p = f < 0 and f * -1.0 or f
return Color(
(t - self.red) * p + self.red,
(t - self.green) * p + self.green,
(t - self.blue) * p + self.blue,
self.alpha
)
end
---Returns a new color that's a linear blend between two colors.
---@param other Color
---@param f number Amount. Float [0,1].
function Color:blend(other, f)
return Color(
(other.red - self.red) * f + self.red,
(other.green - self.green) * f + self.green,
(other.blue - self.blue) * f + self.blue,
self.alpha
)
end
function Color.test_shade()
print("-- SHADE TEST -- ")
local c = Color.from_hex("#98c379")
for i = 0, 10 do
local f = (1 / 5) * i - 1
print(string.format("%-8.1f%s", f, c:shade(f):to_css()))
end
end
function Color.test_blend()
print("-- BLEND TEST -- ")
local c0 = Color.from_hex("#98c379")
local c1 = Color.from_hex("#61afef")
for i = 0, 10 do
local f = (1 / 10) * i
print(string.format("%-8.1f%s", f, c0:blend(c1, f):to_css()))
end
end
---Return the RGBA values in a new table.
---@return RGBA
function Color:to_rgba()
return { red = self.red, green = self.green, blue = self.blue, alpha = self.alpha }
end
---Convert the color to HSV.
---@return HSV
function Color:to_hsv()
local r = self.red
local g = self.green
local b = self.blue
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local delta = max - min
local h = 0
local s = 0
local v = max
if max == min then
h = 0
elseif max == r then
h = 60 * ((g - b) / delta)
elseif max == g then
h = 60 * ((b - r) / delta + 2)
elseif max == b then
h = 60 * ((r - g) / delta + 4)
end
if h < 0 then
h = h + 360
end
if max ~= 0 then
s = (max - min) / max
end
return { hue = h, saturation = s, value = v }
end
---Convert the color to HSL.
---@return HSL
function Color:to_hsl()
local r = self.red
local g = self.green
local b = self.blue
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local delta = max - min
local h = 0
local s = 0
local l = (max + min) / 2
if max == min then
h = 0
elseif max == r then
h = 60 * ((g - b) / delta)
elseif max == g then
h = 60 * ((b - r) / delta + 2)
elseif max == b then
h = 60 * ((r - g) / delta + 4)
end
if h < 0 then
h = h + 360
end
if max ~= 0 and min ~= 1 then
s = (max - l) / math.min(l, 1 - l)
end
return { hue = h, saturation = s, lightness = l }
end
---Convert the color to a hex number representation (`0xRRGGBB[AA]`).
---@param with_alpha boolean Include the alpha component.
---@return integer
function Color:to_hex(with_alpha)
local n = bit.bor(
bit.bor((self.blue * 0xff), bit.lshift((self.green * 0xff), 8)),
bit.lshift((self.red * 0xff), 16)
)
return with_alpha and bit.lshift(n, 8) + (self.alpha * 0xff) or n
end
---Convert the color to a css hex color (`#RRGGBB[AA]`).
---@param with_alpha boolean Include the alpha component.
---@return string
function Color:to_css(with_alpha)
local n = self:to_hex(with_alpha)
local l = with_alpha and 8 or 6
return string.format("#%0" .. l .. "x", n)
end
---@param v number Float [0,1].
function Color:set_red(v)
self._red = util.clamp(v or 1.0, 0, 1)
end
---@param v number Float [0,1].
function Color:set_green(v)
self._green = util.clamp(v or 1.0, 0, 1)
end
---@param v number Float [0,1].
function Color:set_blue(v)
self._blue = util.clamp(v or 1.0, 0, 1)
end
---@param v number Float [0,1].
function Color:set_alpha(v)
self._alpha = util.clamp(v or 1.0, 0, 1)
end
---@param v number Hue. Float [0,360).
function Color:set_hue(v)
local hsv = self:to_hsv()
hsv.hue = v % 360
local c = Color.from_hsv(hsv.hue, hsv.saturation, hsv.value)
self._red = c.red
self._green = c.green
self._blue = c.blue
end
---@param v number Float [0,1].
function Color:set_saturation(v)
local hsv = self:to_hsv()
hsv.saturation = util.clamp(v, 0, 1)
local c = Color.from_hsv(hsv.hue, hsv.saturation, hsv.value)
self._red = c.red
self._green = c.green
self._blue = c.blue
end
---@param v number Float [0,1].
function Color:set_value(v)
local hsv = self:to_hsv()
hsv.value = util.clamp(v, 0, 1)
local c = Color.from_hsv(hsv.hue, hsv.saturation, hsv.value)
self._red = c.red
self._green = c.green
self._blue = c.blue
end
---@param v number Float [0,1].
function Color:set_lightness(v)
local hsl = self:to_hsl()
hsl.lightness = util.clamp(v, 0, 1)
local c = Color.from_hsl(hsl.hue, hsl.saturation, hsl.lightness)
self._red = c.red
self._green = c.green
self._blue = c.blue
end
---Copy the values from another color.
---@param c Color
function Color:set_from_color(c)
self._red = c.red
self._green = c.green
self._blue = c.blue
self._alpha = c.alpha
end
---@param x RGBA|number[]|number Either an RGBA struct, or a vector, or the value for red.
---@param g number (Optional) Green. Float [0,1].
---@param b number (Optional) Blue. Float [0,1].
---@param a number (Optional) Alpha. Float [0,1].
function Color:set_from_rgba(x, g, b, a)
if type(x) == "number" then
self.red = x
self.green = g
self.blue = b
self.alpha = a or self.alpha
elseif type(x.red) == "number" then
self.red = x.red
self.green = x.green
self.blue = x.blue
self.alpha = x.alpha
else
self.red = x[1]
self.green = x[2]
self.blue = x[3]
self.alpha = x[4] or self.alpha
end
end
---@param x HSV|number[]|number Either an HSV struct, or a vector, or the value for hue.
---@param s number (Optional) Saturation. Float [0,1].
---@param v number (Optional) Value Float [0,1].
function Color:set_from_hsv(x, s, v)
local c
if type(x) == "number" then
c = Color.from_hsv(x, s, v, self.alpha)
elseif type(x.hue) == "number" then
c = Color.from_hsv(x.hue, x.saturation, x.value, self.alpha)
else
c = Color.from_hsv(x[1], x[2], x[3], self.alpha)
end
self:set_from_color(c)
end
---@param x HSL|number[]|number Either an HSL struct, or a vector, or the value for hue.
---@param s number (Optional) Saturation. Float [0,1].
---@param l number (Optional) Lightness. Float [0,1].
function Color:set_from_hsl(x, s, l)
local c
if type(x) == "number" then
c = Color.from_hsl(x, s, l, self.alpha)
elseif type(x.hue) == "number" then
c = Color.from_hsl(x.hue, x.saturation, x.lightness, self.alpha)
else
c = Color.from_hsl(x[1], x[2], x[3], self.alpha)
end
self:set_from_color(c)
end
do
-- stylua: ignore start
local getters = {
red = function(self) return self._red end,
green = function(self) return self._green end,
blue = function(self) return self._blue end,
alpha = function(self) return self._alpha end,
hue = function (self) return self:to_hsv().hue end,
saturation = function (self) return self:to_hsv().saturation end,
value = function (self) return self:to_hsv().value end,
lightness = function (self) return self:to_hsl().lightness end,
}
local setters = {
red = function(self, v) self:set_red(v) end,
green = function(self, v) self:set_green(v) end,
blue = function(self, v) self:set_blue(v) end,
alpha = function(self, v) self:set_alpha(v) end,
hue = function(self, v) self:set_hue(v) end,
saturation = function(self, v) self:set_saturation(v) end,
value = function(self, v) self:set_value(v) end,
lightness = function(self, v) self:set_lightness(v) end,
}
-- stylua: ignore end
function Color.__index(self, k)
if getters[k] then
return getters[k](self)
end
return Color[k]
end
function Color.__newindex(self, k, v)
if setters[k] then
setters[k](self, v)
else
rawset(self, k, v)
end
end
local mt = getmetatable(Color)
function mt.__call(_, ...)
local this = setmetatable({}, Color)
this:init(...)
return this
end
end
M.Color = Color
return M