-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcolor.lua
More file actions
663 lines (583 loc) · 17.9 KB
/
color.lua
File metadata and controls
663 lines (583 loc) · 17.9 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
--[=[
Color parsing, formatting and computation in HSL, HSV an RGB spaces.
Written by Cosmin Apreutesei. Public Domain.
HSL-RGB conversions from Sputnik by Yuri Takhteyev (MIT/X License).
color.parse(str[, space]) -> [space, ]x, y, z[, a] parse color string
color.convert(dspace, sspace, x, y, z[, a]) -> ... convert color from sspace to dspace
color.format([fmt], space, x, y, z[, a]) -> s format color (see above)
color.clamp(space, x, y, z[, a]) -> x, y, z[, a] clamp values to color space
* color spaces: 'hsl', 'hsv', 'rgb'.
* r, g, b, s, L, v are in 0..1 range, h is in 0..360 range.
* color formats: '#', '#rrggbbaa', '#rrggbb', '#rgba', '#rgb', '#g', '#gg',
'rgba32', 'argb32', 'rgba', 'rgb', 'rgba%', 'rgb%', 'hsla', 'hsl', 'hsla%', 'hsl%'.
* x, y, z means r, g, b in the 'rgb' color space, h, s, L in the 'hsl' color
space and h, s, v in the 'hsv' color space.
* when alpha is missing, the color has no alpha when formatting or clamping.
CONSTRUCTORS
color(str) -> col create a HSL color object from a string
color([space, ]x, y, z[, a]) -> col create a HSL color object from discrete values
color([space, ]{x, y, z[, a]}) -> col create a HSL color object from a table
color.hsl(h, s, L[, a]) -> col calls `color('hsl', h, s, L, a)`
color.hsv(h, s, v[, a]) -> col calls `color('hsv', h, s, v, a)`
color.rgb(r, g, b[, a]) -> col calls `color('rgb', r, g, b, a)`
FIELDS
col.h, col.s, col.L, col.a color fields (for reading and writing)
col() -> h, s, L[, a] color fields unpacked
CONVERSION
col:hsl() -> h, s, L color fields unpacked without alpha
col:hsla() -> h, s, L, a color fields unpacked with alpha
col:hsv() -> h, s, v convert to HSV
col:rgb() -> r, g, b convert to RGB
col:hsva() -> h, s, v, a convert to HSVA
col:rgba() -> r, g, b, a convert to RGBA
FORMATTING
col:format([fmt]) -> str convert to string
tostring(col) -> str calls `col:format'#'`
COMPUTATION IN HSL SPACE
col:hue_offset(hue_delta) -> color create a color with a different hue (in degrees)
col:complementary() -> color create a complementary color
col:neighbors(angle) -> color1, color2 create two neighboring colors (by hue), offset by "angle"
col:triadic() -> color1, color2 create two new colors to make a triadic color scheme
col:split_complementary(angle) -> color1, color2 create two new colors, offset by angle from a color's complementary
col:desaturate_to(saturation) -> color create a new color with saturation set to a new value
col:desaturate_by(r) -> color create a new color with saturation set to a old saturation times r
col:lighten_to(lightness) -> color create a new color with lightness set to a new value
col:lighten_by(r) -> color create a new color with lightness set to its lightness times r
col:bw([whiteL]) -> color create a new color with lightness either 0 or 1 based on whiteL threshold
col:variations(f, n) -> {color1, ...} create n variations of a color using supplied function and return them as a table
col:tints(n) -> {color1, ...} create n tints of a color and return them as a table
col:shades(n) -> {color1, ...} create n shades of a color and return them as a table
col:tint(r) -> color create a color tint
col:shade(r) -> color create a color shade
]=]
local bit = require'bit'
local band, shr = bit.band, bit.rshift
local function clamp01(x)
return math.min(math.max(x, 0), 1)
end
local function round(x)
return math.floor(x + 0.5)
end
--clamping -------------------------------------------------------------------
local clamps = {} --{space -> func(x, y, z, a)}
local function clamp_hsx(h, s, x, a)
return h % 360, clamp01(s), clamp01(x)
end
clamps.hsl = clamp_hsx
clamps.hsv = clamp_hsx
function clamps.rgb(r, g, b)
return clamp01(r), clamp01(g), clamp01(b)
end
local function clamp(space, x, y, z, a)
x, y, z = clamps[space](x, y, z)
if a then return x, y, z, clamp01(a) end
return x, y, z
end
--conversion -----------------------------------------------------------------
--HSL <-> RGB
--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
local function hsl_to_rgb(h, s, L)
h = h / 360
local m2 = L <= .5 and L*(s+1) or L+s-L*s
local m1 = L*2-m2
return
h2rgb(m1, m2, h+1/3),
h2rgb(m1, m2, h),
h2rgb(m1, m2, h-1/3)
end
--rgb is in (0..1, 0..1, 0..1); hsl is (0..360, 0..1, 0..1)
local function rgb_to_hsl(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
--HSV <-> RGB
local function rgb_to_hsv(r, g, b)
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
local function hsv_to_rgb(h, s, v)
if s == 0 then --gray
return v, v, v
end
local H = h / 60
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
function hsv_to_hsl(h, s, v) --TODO: direct conversion
return rgb_to_hsl(hsv_to_rgb(h, s, v))
end
function hsl_to_hsv(h, s, l) --TODO: direct conversion
return rgb_to_hsv(hsl_to_rgb(h, s, l))
end
local converters = {
rgb = {hsl = rgb_to_hsl, hsv = rgb_to_hsv},
hsl = {rgb = hsl_to_rgb, hsv = hsl_to_hsv},
hsv = {rgb = hsv_to_rgb, hsl = hsv_to_hsl},
}
local function convert(dest_space, space, x, y, z, ...)
if space ~= dest_space then
x, y, z = converters[space][dest_space](x, y, z)
end
return x, y, z, ...
end
--parsing --------------------------------------------------------------------
local hex = {
[2] = {'#g', 'rgb'},
[3] = {'#gg', 'rgb'},
[4] = {'#rgb', 'rgb'},
[5] = {'#rgba', 'rgb'},
[7] = {'#rrggbb', 'rgb'},
[9] = {'#rrggbbaa', 'rgb'},
}
local s3 = {
hsl = {'hsl', 'hsl'},
hsv = {'hsv', 'hsv'},
rgb = {'rgb', 'rgb'},
}
local s4 = {
hsla = {'hsla', 'hsl'},
hsva = {'hsva', 'hsv'},
rgba = {'rgba', 'rgb'},
}
local function string_format(s)
local t
if s:sub(1, 1) == '#' then
t = hex[#s]
else
t = s4[s:sub(1, 4)] or s3[s:sub(1, 3)]
end
if t then
return t[1], t[2] --format, colorspace
end
end
local parsers = {}
local function parse(s)
local g = tonumber(s:sub(2, 2), 16)
if not g then return end
g = (g * 16 + g) / 255
return g, g, g
end
parsers['#g'] = parse
local function parse(s)
local r = tonumber(s:sub(2, 2), 16)
local g = tonumber(s:sub(3, 3), 16)
local b = tonumber(s:sub(4, 4), 16)
if not (r and g and b) then return end
r = (r * 16 + r) / 255
g = (g * 16 + g) / 255
b = (b * 16 + b) / 255
if #s == 5 then
local a = tonumber(s:sub(5, 5), 16)
if not a then return end
return r, g, b, (a * 16 + a) / 255
else
return r, g, b
end
end
parsers['#rgb'] = parse
parsers['#rgba'] = parse
local function parse(s)
local g = tonumber(s:sub(2, 3), 16)
if not g then return end
g = g / 255
return g, g, g
end
parsers['#gg'] = parse
local function parse(s)
local r = tonumber(s:sub(2, 3), 16)
local g = tonumber(s:sub(4, 5), 16)
local b = tonumber(s:sub(6, 7), 16)
if not (r and g and b) then return end
r = r / 255
g = g / 255
b = b / 255
if #s == 9 then
local a = tonumber(s:sub(8, 9), 16)
if not a then return end
return r, g, b, a / 255
else
return r, g, b
end
end
parsers['#rrggbb'] = parse
parsers['#rrggbbaa'] = parse
local rgb_patt = '^rgb%s*%(([^,]+),([^,]+),([^,]+)%)$'
local rgba_patt = '^rgba%s*%(([^,]+),([^,]+),([^,]+),([^,]+)%)$'
local function np(s)
local p = s and tonumber((s:match'^([^%%]+)%%%s*$'))
return p and p * .01
end
local function n255(s)
local n = tonumber(s)
return n and n / 255
end
local function parse(s)
local r, g, b, a = s:match(rgba_patt)
r = np(r) or n255(r)
g = np(g) or n255(g)
b = np(b) or n255(b)
a = np(a) or tonumber(a)
if not (r and g and b and a) then return end
return r, g, b, a
end
parsers.rgba = parse
local function parse(s)
local r, g, b = s:match(rgb_patt)
r = np(r) or n255(r)
g = np(g) or n255(g)
b = np(b) or n255(b)
if not (r and g and b) then return end
return r, g, b
end
parsers.rgb = parse
local hsl_patt = '^hsl%s*%(([^,]+),([^,]+),([^,]+)%)$'
local hsla_patt = '^hsla%s*%(([^,]+),([^,]+),([^,]+),([^,]+)%)$'
local hsv_patt = hsl_patt:gsub('hsl', 'hsv')
local hsva_patt = hsla_patt:gsub('hsla', 'hsva')
local function parser(patt)
return function(s)
local h, s, x, a = s:match(patt)
h = tonumber(h)
s = np(s) or tonumber(s)
x = np(x) or tonumber(x)
a = np(a) or tonumber(a)
if not (h and s and x and a) then return end
return h, s, x, a
end
end
parsers.hsla = parser(hsla_patt)
parsers.hsva = parser(hsva_patt)
local function parser(patt)
return function(s)
local h, s, x = s:match(patt)
h = tonumber(h)
s = np(s) or tonumber(s)
x = np(x) or tonumber(x)
if not (h and s and x) then return end
return h, s, x
end
end
parsers.hsl = parser(hsl_patt)
parsers.hsv = parser(hsv_patt)
local function parse(s, dest_space)
local fmt, space = string_format(s)
if not fmt then return end
local parse = parsers[fmt]
if not parse then return end
if dest_space then
return convert(dest_space, space, parse(s))
else
return space, parse(s)
end
end
local function parse_argb32(x)
local b = band(shr(x, 0), 0xff) / 0xff
local g = band(shr(x, 8), 0xff) / 0xff
local r = band(shr(x, 16), 0xff) / 0xff
local a = band(shr(x, 24), 0xff) / 0xff
return r, g, b, a
end
local function parse_rgba32(x)
local a = band(shr(x, 0), 0xff) / 0xff
local b = band(shr(x, 8), 0xff) / 0xff
local g = band(shr(x, 16), 0xff) / 0xff
local r = band(shr(x, 24), 0xff) / 0xff
return r, g, b, a
end
--formatting -----------------------------------------------------------------
local format_spaces = {
['#'] = 'rgb',
['#rrggbbaa'] = 'rgb', ['#rrggbb'] = 'rgb',
['#rgba'] = 'rgb', ['#rgb'] = 'rgb', rgba = 'rgb', rgb = 'rgb',
hsla = 'hsl', hsl = 'hsl', ['hsla%'] = 'hsl', ['hsl%'] = 'hsl',
hsva = 'hsv', hsv = 'hsv', ['hsva%'] = 'hsv', ['hsv%'] = 'hsv',
rgba32 = 'rgb', argb32 = 'rgb',
}
local function loss(x) --...of precision when converting to #rgb
return math.abs(x * 15 - round(x * 15))
end
local threshold = math.abs(loss(0x89 / 255))
local function short(x)
return loss(x) < threshold
end
local function format(fmt, space, x, y, z, a)
fmt = fmt or space --the names match
local dest_space = format_spaces[fmt]
if not dest_space then
error('invalid format '..tostring(fmt))
end
x, y, z, a = convert(dest_space, space, x, y, z, a)
if fmt == '#' then --shortest hex
if short(x) and short(y) and short(z) and short(a or 1) then
fmt = a and '#rgba' or '#rgb'
else
fmt = a and '#rrggbbaa' or '#rrggbb'
end
end
a = a or 1
if fmt == '#rrggbbaa' or fmt == '#rrggbb' then
return string.format(
fmt == '#rrggbbaa' and '#%02x%02x%02x%02x' or '#%02x%02x%02x',
round(x * 255),
round(y * 255),
round(z * 255),
round(a * 255))
elseif fmt == '#rgba' or fmt == '#rgb' then
return string.format(
fmt == '#rgba' and '#%1x%1x%1x%1x' or '#%1x%1x%1x',
round(x * 15),
round(y * 15),
round(z * 15),
round(a * 15))
elseif fmt == 'rgba' or fmt == 'rgb' then
return string.format(
fmt == 'rgba' and 'rgba(%d,%d,%d,%.2g)' or 'rgb(%d,%d,%g)',
round(x * 255),
round(y * 255),
round(z * 255),
a)
elseif fmt:sub(-1) == '%' then --hsl|v(a)%
return string.format(
#fmt == 5 and '%s(%d,%d%%,%d%%,%.2g)' or '%s(%d,%d%%,%d%%)',
fmt:sub(1, -2),
round(x),
round(y * 100),
round(z * 100),
a)
elseif fmt == 'rgba32' then
return
round(x * 255) * 2^24
+ round(y * 255) * 2^16
+ round(z * 255) * 2^8
+ round(a * 255)
elseif fmt == 'argb32' then
return
round(a * 255) * 2^24
+ round(x * 255) * 2^16
+ round(y * 255) * 2^8
+ round(z * 255)
else --hsl|v(a)
return string.format(
#fmt == 4 and '%s(%d,%.2g,%.2g,%.2g)' or '%s(%d,%.2g,%.2g)',
fmt, round(x), y, z, a)
end
end
--color object ---------------------------------------------------------------
local color = {}
--new([space, ]x, y, z[, a])
--new([space, ]'str')
--new([space, ]{x, y, z[, a]})
local function new(space, x, y, z, a)
if not (type(space) == 'string' and x) then --shift args
space, x, y, z, a = 'hsl', space, x, y, z
end
local h, s, L
if type(x) == 'string' then
h, s, L, a = parse(x, 'hsl')
else
if type(x) == 'table' then
x, y, z, a = x[1], x[2], x[3], x[4]
end
h, s, L, a = convert('hsl', space, clamp(space, x, y, z, a))
end
local c = {
h = h, s = s, L = L, a = a,
__index = color,
__tostring = color.__tostring,
__call = color.__call,
}
return setmetatable(c, c)
end
local function new_with(space)
return function(...)
return new(space, ...)
end
end
function color:__call() return self.h, self.s, self.L, self.a end
function color:hsl() return self.h, self.s, self.L end
function color:hsla() return self.h, self.s, self.L, self.a or 1 end
function color:hsv() return convert('hsv', 'hsl', self:hsl()) end
function color:hsva() return convert('hsv', 'hsl', self:hsla()) end
function color:rgb() return convert('rgb', 'hsl', self:hsl()) end
function color:rgba() return convert('rgb', 'hsl', self:hsla()) end
function color:convert(space) return convert(space, 'hsl', self:hsla()) end
function color:format(fmt) return format(fmt, 'hsl', self()) end
function color:__tostring()
return self:format'#'
end
function color:hue_offset(delta)
return new(self.h + delta, self.s, self.L)
end
function color:complementary()
return self:hue_offset(180)
end
function color:neighbors(angle)
local angle = angle or 30
return self:hue_offset(angle), self:hue_offset(360-angle)
end
function color:triadic()
return self:neighbors(120)
end
function color:split_complementary(angle)
return self:neighbors(180-(angle or 30))
end
function color:desaturate_to(saturation)
return new(self.h, saturation, self.L)
end
function color:desaturate_by(r)
return new(self.h, self.s*r, self.L)
end
function color:lighten_to(lightness)
return new(self.h, self.s, lightness)
end
function color:lighten_by(r)
return new(self.h, self.s, self.L*r)
end
function color:bw(whiteL)
return new(self.h, self.s, self.L >= (whiteL or .5) and 0 or 1)
end
function color:variations(f, n)
n = n or 5
local results = {}
for i=1,n do
table.insert(results, f(self, i, n))
end
return results
end
function color:tints(n)
return self:variations(function(color, i, n)
return color:lighten_to(color.L + (1-color.L)/n*i)
end, n)
end
function color:shades(n)
return self:variations(function(color, i, n)
return color:lighten_to(color.L - (color.L)/n*i)
end, n)
end
function color:tint(r)
return self:lighten_to(self.L + (1-self.L)*r)
end
function color:shade(r)
return self:lighten_to(self.L - self.L*r)
end
--module ---------------------------------------------------------------------
local color_module = {
clamp = clamp,
convert = convert,
parse = parse,
parse_rgba32 = parse_rgba32,
parse_argb32 = parse_argb32,
format = format,
hsl = new_with'hsl',
hsv = new_with'hsv',
rgb = new_with'rgb',
}
function color_module:__call(...)
return new(...)
end
setmetatable(color_module, color_module)
--demo -----------------------------------------------------------------------
if not ... then
print(parse'#fff')
print(parse'#808080')
print(parse'#0000')
print(parse'#80808080')
print(parse'#80')
print(parse'#7f')
print(parse'#88')
print(parse'#8')
print()
print(parse'rgb (128, 128, 128 )')
print(parse'rgba(128, 128, 128, 0.42)')
print(parse'rgba(128, 128, 128, 0.42)')
print(parse'hsla(360, 42.3%, 42.3%, 0.423)')
print(parse'hsla(360, .432, .432, 0.42 )')
print(parse'hsl (360, 42.3%, 42.3% )')
print(parse'hsl (360, .432, .432 )')
print(parse'hsla(360, .432, .432, 0.42 )')
print(parse'hsla(180, 1, .5, .5)')
print(parse'rgba(128, 128, 128, .5)')
print(parse'rgba(100%, 0%, 50%, 25%)')
print()
print(format(nil, 'rgb', .533, .533, .533, .533))
print(format('#rrggbb', 'rgb', .533, .533, .533, .533))
print(format('#rgba', 'rgb', .533, .533, .533, .533))
print(format('#rgb', 'rgb', .5, .5, .5, .5))
print(format('rgba', 'rgb', .5, .5, .5, .5))
print(format('rgb', 'rgb', .5, .5, .5, .5))
print(format('rgb', 'rgb', .5, .5, .5))
print(format(nil, 'hsl', 360, .5, .5, .5))
print(format(nil, 'hsl', 360, .5, .5, .5))
print(format(nil, 'hsl', 360, .5, .5))
print(format('#', 'rgb', 0x88/255, 0xff/255, 0x22/255))
print(format('#', 'rgb', 0x88/255, 0xff/255, 0x22/255, 0x55/255))
print(format('#', 'rgb', 1, 1, 1, 0x89/255))
print(color_module(180, 1, 1))
print(color_module'#abcd')
print(color_module('rgb', 1, 1, 1, .8))
print(color_module('rgb', 1, 0, 0, .8))
print(color_module('rgb', 0, 0, 0, .8))
print(color_module('rgb', 1, 0, .3))
local r, g, b, a = parse_rgba32(0xffffffff)
local h, s, l = convert('hsl', 'rgb', r, g, b)
print(h, s, l)
l = 0
local r, g, b = convert('rgb', 'hsl', h, s, l)
local c = format('rgba32', 'rgb', r, g, b, a)
print(string.format('%x', c))
end
return color_module