Debian Codegraph @

texmf-dist/tex/luatex/farbe/farbe.lua

raw
1 -- farbe.lua
2 -- Copyright 2025 Josef Friedrich
3 --
4 -- This work may be distributed and/or modified under the
5 -- conditions of the LaTeX Project Public License, either version 1.3c
6 -- of this license or (at your option) any later version.
7 -- The latest version of this license is in
8 -- http://www.latex-project.org/lppl.txt
9 -- and version 1.3c or later is part of all distributions of LaTeX
10 -- version 2008/05/04 or later.
11 --
12 -- This work has the LPPL maintenance status `maintained'.
13 --
14 -- The Current Maintainer of this work is Josef Friedrich.
15 --
16 -- This work consists of the files farbe.lua, farbe.tex,
17 -- and farbe.sty.
18 -- https://github.com/latex3/xcolor/blob/main/xcolor.dtx
19 ---@alias r number red (0.0 - 1.0)
20 ---@alias g number green (0.0 - 1.0)
21 ---@alias b number blue (0.0 - 1.0)
22 ---@alias a number alpha (0.0 - 1.0)
23 ---@alias c number cyan (0.0 - 1.0)
24 ---@alias m number magenta (0.0 - 1.0)
25 ---@alias y number yellow (0.0 - 1.0)
26 ---@alias k number key(black) (0.0 - 1.0)
27 local schemes = {
28
29 base = {
30 'black',
31 'blue',
32 'brown',
33 'cyan',
34 'darkgray',
35 'gray',
36 'green',
37 'lightgray',
38 'lime',
39 'magenta',
40 'olive',
41 'orange',
42 'pink',
43 'purple',
44 'red',
45 'teal',
46 'violet',
47 'white',
48 'yellow',
49 },
50
51 svg = {
52 'AliceBlue',
53 'AntiqueWhite',
54 'Aqua',
55 'Aquamarine',
56 'Azure',
57 'Beige',
58 'Bisque',
59 'Black',
60 'BlanchedAlmond',
61 'Blue',
62 'BlueViolet',
63 'Brown',
64 'BurlyWood',
65 'CadetBlue',
66 'Chartreuse',
67 'Chocolate',
68 'Coral',
69 'CornflowerBlue',
70 'Cornsilk',
71 'Crimson',
72 'Cyan',
73 'DarkBlue',
74 'DarkCyan',
75 'DarkGoldenrod',
76 'DarkGray',
77 'DarkGreen',
78 'DarkGrey',
79 'DarkKhaki',
80 'DarkMagenta',
81 'DarkOliveGreen',
82 'DarkOrange',
83 'DarkOrchid',
84 'DarkRed',
85 'DarkSalmon',
86 'DarkSeaGreen',
87 'DarkSlateBlue',
88 'DarkSlateGray',
89 'DarkSlateGrey',
90 'DarkTurquoise',
91 'DarkViolet',
92 'DeepPink',
93 'DeepSkyBlue',
94 'DimGray',
95 'DimGrey',
96 'DodgerBlue',
97 'FireBrick',
98 'FloralWhite',
99 'ForestGreen',
100 'Fuchsia',
101 'Gainsboro',
102 'GhostWhite',
103 'Gold',
104 'Goldenrod',
105 'Gray',
106 'Green',
107 'GreenYellow',
108 'Grey',
109 'Honeydew',
110 'HotPink',
111 'IndianRed',
112 'Indigo',
113 'Ivory',
114 'Khaki',
115 'Lavender',
116 'LavenderBlush',
117 'LawnGreen',
118 'LemonChiffon',
119 'LightBlue',
120 'LightCoral',
121 'LightCyan',
122 'LightGoldenrod',
123 'LightGoldenrodYellow',
124 'LightGray',
125 'LightGreen',
126 'LightGrey',
127 'LightPink',
128 'LightSalmon',
129 'LightSeaGreen',
130 'LightSkyBlue',
131 'LightSlateBlue',
132 'LightSlateGray',
133 'LightSlateGrey',
134 'LightSteelBlue',
135 'LightYellow',
136 'Lime',
137 'LimeGreen',
138 'Linen',
139 'Magenta',
140 'Maroon',
141 'MediumAquamarine',
142 'MediumBlue',
143 'MediumOrchid',
144 'MediumPurple',
145 'MediumSeaGreen',
146 'MediumSlateBlue',
147 'MediumSpringGreen',
148 'MediumTurquoise',
149 'MediumVioletRed',
150 'MidnightBlue',
151 'MintCream',
152 'MistyRose',
153 'Moccasin',
154 'NavajoWhite',
155 'Navy',
156 'NavyBlue',
157 'OldLace',
158 'Olive',
159 'OliveDrab',
160 'Orange',
161 'OrangeRed',
162 'Orchid',
163 'PaleGoldenrod',
164 'PaleGreen',
165 'PaleTurquoise',
166 'PaleVioletRed',
167 'PapayaWhip',
168 'PeachPuff',
169 'Peru',
170 'Pink',
171 'Plum',
172 'PowderBlue',
173 'Purple',
174 'Red',
175 'RosyBrown',
176 'RoyalBlue',
177 'SaddleBrown',
178 'Salmon',
179 'SandyBrown',
180 'SeaGreen',
181 'Seashell',
182 'Sienna',
183 'Silver',
184 'SkyBlue',
185 'SlateBlue',
186 'SlateGray',
187 'SlateGrey',
188 'Snow',
189 'SpringGreen',
190 'SteelBlue',
191 'Tan',
192 'Teal',
193 'Thistle',
194 'Tomato',
195 'Turquoise',
196 'Violet',
197 'VioletRed',
198 'Wheat',
199 'White',
200 'WhiteSmoke',
201 'Yellow',
202 'YellowGreen',
203 },
204
205 x11 = {
206 'AntiqueWhite1',
207 'AntiqueWhite2',
208 'AntiqueWhite3',
209 'AntiqueWhite4',
210 'Aquamarine1',
211 'Aquamarine2',
212 'Aquamarine3',
213 'Aquamarine4',
214 'Azure1',
215 'Azure2',
216 'Azure3',
217 'Azure4',
218 'Bisque1',
219 'Bisque2',
220 'Bisque3',
221 'Bisque4',
222 'Blue1',
223 'Blue2',
224 'Blue3',
225 'Blue4',
226 'Brown1',
227 'Brown2',
228 'Brown3',
229 'Brown4',
230 'Burlywood1',
231 'Burlywood2',
232 'Burlywood3',
233 'Burlywood4',
234 'CadetBlue1',
235 'CadetBlue2',
236 'CadetBlue3',
237 'CadetBlue4',
238 'Chartreuse1',
239 'Chartreuse2',
240 'Chartreuse3',
241 'Chartreuse4',
242 'Chocolate1',
243 'Chocolate2',
244 'Chocolate3',
245 'Chocolate4',
246 'Coral1',
247 'Coral2',
248 'Coral3',
249 'Coral4',
250 'Cornsilk1',
251 'Cornsilk2',
252 'Cornsilk3',
253 'Cornsilk4',
254 'Cyan1',
255 'Cyan2',
256 'Cyan3',
257 'Cyan4',
258 'DarkGoldenrod1',
259 'DarkGoldenrod2',
260 'DarkGoldenrod3',
261 'DarkGoldenrod4',
262 'DarkOliveGreen1',
263 'DarkOliveGreen2',
264 'DarkOliveGreen3',
265 'DarkOliveGreen4',
266 'DarkOrange1',
267 'DarkOrange2',
268 'DarkOrange3',
269 'DarkOrange4',
270 'DarkOrchid1',
271 'DarkOrchid2',
272 'DarkOrchid3',
273 'DarkOrchid4',
274 'DarkSeaGreen1',
275 'DarkSeaGreen2',
276 'DarkSeaGreen3',
277 'DarkSeaGreen4',
278 'DarkSlateGray1',
279 'DarkSlateGray2',
280 'DarkSlateGray3',
281 'DarkSlateGray4',
282 'DeepPink1',
283 'DeepPink2',
284 'DeepPink3',
285 'DeepPink4',
286 'DeepSkyBlue1',
287 'DeepSkyBlue2',
288 'DeepSkyBlue3',
289 'DeepSkyBlue4',
290 'DodgerBlue1',
291 'DodgerBlue2',
292 'DodgerBlue3',
293 'DodgerBlue4',
294 'Firebrick1',
295 'Firebrick2',
296 'Firebrick3',
297 'Firebrick4',
298 'Gold1',
299 'Gold2',
300 'Gold3',
301 'Gold4',
302 'Goldenrod1',
303 'Goldenrod2',
304 'Goldenrod3',
305 'Goldenrod4',
306 'Green1',
307 'Green2',
308 'Green3',
309 'Green4',
310 'Honeydew1',
311 'Honeydew2',
312 'Honeydew3',
313 'Honeydew4',
314 'HotPink1',
315 'HotPink2',
316 'HotPink3',
317 'HotPink4',
318 'IndianRed1',
319 'IndianRed2',
320 'IndianRed3',
321 'IndianRed4',
322 'Ivory1',
323 'Ivory2',
324 'Ivory3',
325 'Ivory4',
326 'Khaki1',
327 'Khaki2',
328 'Khaki3',
329 'Khaki4',
330 'LavenderBlush1',
331 'LavenderBlush2',
332 'LavenderBlush3',
333 'LavenderBlush4',
334 'LemonChiffon1',
335 'LemonChiffon2',
336 'LemonChiffon3',
337 'LemonChiffon4',
338 'LightBlue1',
339 'LightBlue2',
340 'LightBlue3',
341 'LightBlue4',
342 'LightCyan1',
343 'LightCyan2',
344 'LightCyan3',
345 'LightCyan4',
346 'LightGoldenrod1',
347 'LightGoldenrod2',
348 'LightGoldenrod3',
349 'LightGoldenrod4',
350 'LightPink1',
351 'LightPink2',
352 'LightPink3',
353 'LightPink4',
354 'LightSalmon1',
355 'LightSalmon2',
356 'LightSalmon3',
357 'LightSalmon4',
358 'LightSkyBlue1',
359 'LightSkyBlue2',
360 'LightSkyBlue3',
361 'LightSkyBlue4',
362 'LightSteelBlue1',
363 'LightSteelBlue2',
364 'LightSteelBlue3',
365 'LightSteelBlue4',
366 'LightYellow1',
367 'LightYellow2',
368 'LightYellow3',
369 'LightYellow4',
370 'Magenta1',
371 'Magenta2',
372 'Magenta3',
373 'Magenta4',
374 'Maroon1',
375 'Maroon2',
376 'Maroon3',
377 'Maroon4',
378 'MediumOrchid1',
379 'MediumOrchid2',
380 'MediumOrchid3',
381 'MediumOrchid4',
382 'MediumPurple1',
383 'MediumPurple2',
384 'MediumPurple3',
385 'MediumPurple4',
386 'MistyRose1',
387 'MistyRose2',
388 'MistyRose3',
389 'MistyRose4',
390 'NavajoWhite1',
391 'NavajoWhite2',
392 'NavajoWhite3',
393 'NavajoWhite4',
394 'OliveDrab1',
395 'OliveDrab2',
396 'OliveDrab3',
397 'OliveDrab4',
398 'Orange1',
399 'Orange2',
400 'Orange3',
401 'Orange4',
402 'OrangeRed1',
403 'OrangeRed2',
404 'OrangeRed3',
405 'OrangeRed4',
406 'Orchid1',
407 'Orchid2',
408 'Orchid3',
409 'Orchid4',
410 'PaleGreen1',
411 'PaleGreen2',
412 'PaleGreen3',
413 'PaleGreen4',
414 'PaleTurquoise1',
415 'PaleTurquoise2',
416 'PaleTurquoise3',
417 'PaleTurquoise4',
418 'PaleVioletRed1',
419 'PaleVioletRed2',
420 'PaleVioletRed3',
421 'PaleVioletRed4',
422 'PeachPuff1',
423 'PeachPuff2',
424 'PeachPuff3',
425 'PeachPuff4',
426 'Pink1',
427 'Pink2',
428 'Pink3',
429 'Pink4',
430 'Plum1',
431 'Plum2',
432 'Plum3',
433 'Plum4',
434 'Purple1',
435 'Purple2',
436 'Purple3',
437 'Purple4',
438 'Red1',
439 'Red2',
440 'Red3',
441 'Red4',
442 'RosyBrown1',
443 'RosyBrown2',
444 'RosyBrown3',
445 'RosyBrown4',
446 'RoyalBlue1',
447 'RoyalBlue2',
448 'RoyalBlue3',
449 'RoyalBlue4',
450 'Salmon1',
451 'Salmon2',
452 'Salmon3',
453 'Salmon4',
454 'SeaGreen1',
455 'SeaGreen2',
456 'SeaGreen3',
457 'SeaGreen4',
458 'Seashell1',
459 'Seashell2',
460 'Seashell3',
461 'Seashell4',
462 'Sienna1',
463 'Sienna2',
464 'Sienna3',
465 'Sienna4',
466 'SkyBlue1',
467 'SkyBlue2',
468 'SkyBlue3',
469 'SkyBlue4',
470 'SlateBlue1',
471 'SlateBlue2',
472 'SlateBlue3',
473 'SlateBlue4',
474 'SlateGray1',
475 'SlateGray2',
476 'SlateGray3',
477 'SlateGray4',
478 'Snow1',
479 'Snow2',
480 'Snow3',
481 'Snow4',
482 'SpringGreen1',
483 'SpringGreen2',
484 'SpringGreen3',
485 'SpringGreen4',
486 'SteelBlue1',
487 'SteelBlue2',
488 'SteelBlue3',
489 'SteelBlue4',
490 'Tan1',
491 'Tan2',
492 'Tan3',
493 'Tan4',
494 'Thistle1',
495 'Thistle2',
496 'Thistle3',
497 'Thistle4',
498 'Tomato1',
499 'Tomato2',
500 'Tomato3',
501 'Tomato4',
502 'Turquoise1',
503 'Turquoise2',
504 'Turquoise3',
505 'Turquoise4',
506 'VioletRed1',
507 'VioletRed2',
508 'VioletRed3',
509 'VioletRed4',
510 'Wheat1',
511 'Wheat2',
512 'Wheat3',
513 'Wheat4',
514 'Yellow1',
515 'Yellow2',
516 'Yellow3',
517 'Yellow4',
518 'Gray0',
519 'Green0',
520 'Grey0',
521 'Maroon0',
522 'Purple0',
523 },
524 }
525
526 local log = (function()
527 local opts = { verbosity = 0 }
528
529 local function print_message(message, ...)
530 print(string.format(message, ...))
531 end
532
533 local function info(message, ...)
534 if opts.verbosity > 0 then
535 print_message(message, ...)
536 end
537 end
538
539 local function debug(message, ...)
540 if opts.verbosity > 1 then
541 print_message(message, ...)
542 end
543 end
544
545 local function verbose(message, ...)
546 if opts.verbosity > 2 then
547 print_message(message, ...)
548 end
549 end
550
551 return { opts = opts, info = info, debug = debug, verbose = verbose }
552 end)()
553
554 log.opts.verbosity = 3
555
556 local colors = {
557 -- base
558 black = { 0, 0, 0 },
559 blue = { 0, 0, 1 },
560 brown = { 0.75, 0.5, 0.25 },
561 cyan = { 0, 1, 1 },
562 darkgray = { 0.25, 0.25, 0.25 },
563 gray = { 0.5, 0.5, 0.5 },
564 green = { 0, 1, 0 },
565 lightgray = { 0.75, 0.75, 0.75 },
566 lime = { 0.75, 1, 0 },
567 magenta = { 1, 0, 1 },
568 olive = { 0.5, 0.5, 0 },
569 orange = { 1, 0.5, 0 },
570 pink = { 1, 0.75, 0.75 },
571 purple = { 0.75, 0, 0.25 },
572 red = { 1, 0, 0 },
573 teal = { 0, 0.5, 0.5 },
574 violet = { 0.5, 0, 0.5 },
575 white = { 1, 1, 1 },
576 yellow = { 1, 1, 0 },
577
578 ---svg
579 ---
580 ---This svg color names are taken from the [xcolor](https://github.com/latex3/xcolor/blob/c5035d41c6070f4e8936196a994ad04336704872/xcolor.dtx#L7134-L7286) package.
581 ---https://www.w3.org/TR/2003/REC-SVG11-20030114/types.html#ColorKeywords
582 ---https://www.w3.org/TR/css-color-3/#svg-color
583 ---
584 AliceBlue = { .94, .972, 1 },
585 AntiqueWhite = { .98, .92, .844 },
586 Aqua = { 0, 1, 1 },
587 Aquamarine = { .498, 1, .83 },
588 Azure = { .94, 1, 1 },
589 Beige = { .96, .96, .864 },
590 Bisque = { 1, .894, .77 },
591 Black = { 0, 0, 0 },
592 BlanchedAlmond = { 1, .92, .804 },
593 Blue = { 0, 0, 1 },
594 BlueViolet = { .54, .17, .888 },
595 Brown = { .648, .165, .165 },
596 BurlyWood = { .87, .72, .53 },
597 CadetBlue = { .372, .62, .628 },
598 Chartreuse = { .498, 1, 0 },
599 Chocolate = { .824, .41, .116 },
600 Coral = { 1, .498, .312 },
601 CornflowerBlue = { .392, .585, .93 },
602 Cornsilk = { 1, .972, .864 },
603 Crimson = { .864, .08, .235 },
604 Cyan = { 0, 1, 1 },
605 DarkBlue = { 0, 0, .545 },
606 DarkCyan = { 0, .545, .545 },
607 DarkGoldenrod = { .72, .525, .044 },
608 DarkGray = { .664, .664, .664 },
609 DarkGreen = { 0, .392, 0 },
610 DarkGrey = { .664, .664, .664 },
611 DarkKhaki = { .74, .716, .42 },
612 DarkMagenta = { .545, 0, .545 },
613 DarkOliveGreen = { .332, .42, .185 },
614 DarkOrange = { 1, .55, 0 },
615 DarkOrchid = { .6, .196, .8 },
616 DarkRed = { .545, 0, 0 },
617 DarkSalmon = { .912, .59, .48 },
618 DarkSeaGreen = { .56, .736, .56 },
619 DarkSlateBlue = { .284, .24, .545 },
620 DarkSlateGray = { .185, .31, .31 },
621 DarkSlateGrey = { .185, .31, .31 },
622 DarkTurquoise = { 0, .808, .82 },
623 DarkViolet = { .58, 0, .828 },
624 DeepPink = { 1, .08, .576 },
625 DeepSkyBlue = { 0, .75, 1 },
626 DimGray = { .41, .41, .41 },
627 DimGrey = { .41, .41, .41 },
628 DodgerBlue = { .116, .565, 1 },
629 FireBrick = { .698, .132, .132 },
630 FloralWhite = { 1, .98, .94 },
631 ForestGreen = { .132, .545, .132 },
632 Fuchsia = { 1, 0, 1 },
633 Gainsboro = { .864, .864, .864 },
634 GhostWhite = { .972, .972, 1 },
635 Gold = { 1, .844, 0 },
636 Goldenrod = { .855, .648, .125 },
637 Gray = { .5, .5, .5 },
638 Green = { 0, .5, 0 },
639 GreenYellow = { .68, 1, .185 },
640 Grey = { .5, .5, .5 },
641 Honeydew = { .94, 1, .94 },
642 HotPink = { 1, .41, .705 },
643 IndianRed = { .804, .36, .36 },
644 Indigo = { .294, 0, .51 },
645 Ivory = { 1, 1, .94 },
646 Khaki = { .94, .9, .55 },
647 Lavender = { .9, .9, .98 },
648 LavenderBlush = { 1, .94, .96 },
649 LawnGreen = { .488, .99, 0 },
650 LemonChiffon = { 1, .98, .804 },
651 LightBlue = { .68, .848, .9 },
652 LightCoral = { .94, .5, .5 },
653 LightCyan = { .88, 1, 1 },
654 LightGoldenrod = { .933, .867, .51 }, -- Colors taken from Unix/X11
655 LightGoldenrodYellow = { .98, .98, .824 },
656 LightGray = { .828, .828, .828 },
657 LightGreen = { .565, .932, .565 },
658 LightGrey = { .828, .828, .828 },
659 LightPink = { 1, .712, .756 },
660 LightSalmon = { 1, .628, .48 },
661 LightSeaGreen = { .125, .698, .668 },
662 LightSkyBlue = { .53, .808, .98 },
663 LightSlateBlue = { .518, .44, 1 }, -- Colors taken from Unix/X11
664 LightSlateGray = { .468, .532, .6 },
665 LightSlateGrey = { .468, .532, .6 },
666 LightSteelBlue = { .69, .77, .87 },
667 LightYellow = { 1, 1, .88 },
668 Lime = { 0, 1, 0 },
669 LimeGreen = { .196, .804, .196 },
670 Linen = { .98, .94, .9 },
671 Magenta = { 1, 0, 1 },
672 Maroon = { .5, 0, 0 },
673 MediumAquamarine = { .4, .804, .668 },
674 MediumBlue = { 0, 0, .804 },
675 MediumOrchid = { .73, .332, .828 },
676 MediumPurple = { .576, .44, .86 },
677 MediumSeaGreen = { .235, .7, .444 },
678 MediumSlateBlue = { .484, .408, .932 },
679 MediumSpringGreen = { 0, .98, .604 },
680 MediumTurquoise = { .284, .82, .8 },
681 MediumVioletRed = { .78, .084, .52 },
682 MidnightBlue = { .098, .098, .44 },
683 MintCream = { .96, 1, .98 },
684 MistyRose = { 1, .894, .884 },
685 Moccasin = { 1, .894, .71 },
686 NavajoWhite = { 1, .87, .68 },
687 Navy = { 0, 0, .5 },
688 NavyBlue = { 0, 0, .5 }, -- Colors taken from Unix/X11
689 OldLace = { .992, .96, .9 },
690 Olive = { .5, .5, 0 },
691 OliveDrab = { .42, .556, .136 },
692 Orange = { 1, .648, 0 },
693 OrangeRed = { 1, .27, 0 },
694 Orchid = { .855, .44, .84 },
695 PaleGoldenrod = { .932, .91, .668 },
696 PaleGreen = { .596, .985, .596 },
697 PaleTurquoise = { .688, .932, .932 },
698 PaleVioletRed = { .86, .44, .576 },
699 PapayaWhip = { 1, .936, .835 },
700 PeachPuff = { 1, .855, .725 },
701 Peru = { .804, .52, .248 },
702 Pink = { 1, .752, .796 },
703 Plum = { .868, .628, .868 },
704 PowderBlue = { .69, .88, .9 },
705 Purple = { .5, 0, .5 },
706 Red = { 1, 0, 0 },
707 RosyBrown = { .736, .56, .56 },
708 RoyalBlue = { .255, .41, .884 },
709 SaddleBrown = { .545, .27, .075 },
710 Salmon = { .98, .5, .448 },
711 SandyBrown = { .956, .644, .376 },
712 SeaGreen = { .18, .545, .34 },
713 Seashell = { 1, .96, .932 },
714 Sienna = { .628, .32, .176 },
715 Silver = { .752, .752, .752 },
716 SkyBlue = { .53, .808, .92 },
717 SlateBlue = { .415, .352, .804 },
718 SlateGray = { .44, .5, .565 },
719 SlateGrey = { .44, .5, .565 },
720 Snow = { 1, .98, .98 },
721 SpringGreen = { 0, 1, .498 },
722 SteelBlue = { .275, .51, .705 },
723 Tan = { .824, .705, .55 },
724 Teal = { 0, .5, .5 },
725 Thistle = { .848, .75, .848 },
726 Tomato = { 1, .39, .28 },
727 Turquoise = { .25, .88, .815 },
728 Violet = { .932, .51, .932 },
729 VioletRed = { .816, .125, .565 }, -- Colors taken from Unix/X11
730 Wheat = { .96, .87, .7 },
731 White = { 1, 1, 1 },
732 WhiteSmoke = { .96, .96, .96 },
733 Yellow = { 1, 1, 0 },
734 YellowGreen = { .604, .804, .196 },
735
736 ---x11
737 ---
738 ---This x11 color names are taken from the [xcolor](https://github.com/latex3/xcolor/blob/c5035d41c6070f4e8936196a994ad04336704872/xcolor.dtx#L7289-L7607) package.
739 ---https://en.wikipedia.org/wiki/X11_color_names
740 ---https://gitlab.freedesktop.org/xorg/app/rgb/raw/master/rgb.txt
741 AntiqueWhite1 = { 1, .936, .86 },
742 AntiqueWhite2 = { .932, .875, .8 },
743 AntiqueWhite3 = { .804, .752, .69 },
744 AntiqueWhite4 = { .545, .512, .47 },
745 Aquamarine1 = { .498, 1, .83 },
746 Aquamarine2 = { .464, .932, .776 },
747 Aquamarine3 = { .4, .804, .668 },
748 Aquamarine4 = { .27, .545, .455 },
749 Azure1 = { .94, 1, 1 },
750 Azure2 = { .88, .932, .932 },
751 Azure3 = { .756, .804, .804 },
752 Azure4 = { .512, .545, .545 },
753 Bisque1 = { 1, .894, .77 },
754 Bisque2 = { .932, .835, .716 },
755 Bisque3 = { .804, .716, .62 },
756 Bisque4 = { .545, .49, .42 },
757 Blue1 = { 0, 0, 1 },
758 Blue2 = { 0, 0, .932 },
759 Blue3 = { 0, 0, .804 },
760 Blue4 = { 0, 0, .545 },
761 Brown1 = { 1, .25, .25 },
762 Brown2 = { .932, .23, .23 },
763 Brown3 = { .804, .2, .2 },
764 Brown4 = { .545, .136, .136 },
765 Burlywood1 = { 1, .828, .608 },
766 Burlywood2 = { .932, .772, .57 },
767 Burlywood3 = { .804, .668, .49 },
768 Burlywood4 = { .545, .45, .332 },
769 CadetBlue1 = { .596, .96, 1 },
770 CadetBlue2 = { .556, .898, .932 },
771 CadetBlue3 = { .48, .772, .804 },
772 CadetBlue4 = { .325, .525, .545 },
773 Chartreuse1 = { .498, 1, 0 },
774 Chartreuse2 = { .464, .932, 0 },
775 Chartreuse3 = { .4, .804, 0 },
776 Chartreuse4 = { .27, .545, 0 },
777 Chocolate1 = { 1, .498, .14 },
778 Chocolate2 = { .932, .464, .13 },
779 Chocolate3 = { .804, .4, .112 },
780 Chocolate4 = { .545, .27, .075 },
781 Coral1 = { 1, .448, .336 },
782 Coral2 = { .932, .415, .312 },
783 Coral3 = { .804, .356, .27 },
784 Coral4 = { .545, .244, .185 },
785 Cornsilk1 = { 1, .972, .864 },
786 Cornsilk2 = { .932, .91, .804 },
787 Cornsilk3 = { .804, .785, .694 },
788 Cornsilk4 = { .545, .532, .47 },
789 Cyan1 = { 0, 1, 1 },
790 Cyan2 = { 0, .932, .932 },
791 Cyan3 = { 0, .804, .804 },
792 Cyan4 = { 0, .545, .545 },
793 DarkGoldenrod1 = { 1, .725, .06 },
794 DarkGoldenrod2 = { .932, .68, .055 },
795 DarkGoldenrod3 = { .804, .585, .048 },
796 DarkGoldenrod4 = { .545, .396, .03 },
797 DarkOliveGreen1 = { .792, 1, .44 },
798 DarkOliveGreen2 = { .736, .932, .408 },
799 DarkOliveGreen3 = { .635, .804, .352 },
800 DarkOliveGreen4 = { .43, .545, .24 },
801 DarkOrange1 = { 1, .498, 0 },
802 DarkOrange2 = { .932, .464, 0 },
803 DarkOrange3 = { .804, .4, 0 },
804 DarkOrange4 = { .545, .27, 0 },
805 DarkOrchid1 = { .75, .244, 1 },
806 DarkOrchid2 = { .698, .228, .932 },
807 DarkOrchid3 = { .604, .196, .804 },
808 DarkOrchid4 = { .408, .132, .545 },
809 DarkSeaGreen1 = { .756, 1, .756 },
810 DarkSeaGreen2 = { .705, .932, .705 },
811 DarkSeaGreen3 = { .608, .804, .608 },
812 DarkSeaGreen4 = { .41, .545, .41 },
813 DarkSlateGray1 = { .592, 1, 1 },
814 DarkSlateGray2 = { .552, .932, .932 },
815 DarkSlateGray3 = { .475, .804, .804 },
816 DarkSlateGray4 = { .32, .545, .545 },
817 DeepPink1 = { 1, .08, .576 },
818 DeepPink2 = { .932, .07, .536 },
819 DeepPink3 = { .804, .064, .464 },
820 DeepPink4 = { .545, .04, .312 },
821 DeepSkyBlue1 = { 0, .75, 1 },
822 DeepSkyBlue2 = { 0, .698, .932 },
823 DeepSkyBlue3 = { 0, .604, .804 },
824 DeepSkyBlue4 = { 0, .408, .545 },
825 DodgerBlue1 = { .116, .565, 1 },
826 DodgerBlue2 = { .11, .525, .932 },
827 DodgerBlue3 = { .094, .455, .804 },
828 DodgerBlue4 = { .064, .305, .545 },
829 Firebrick1 = { 1, .19, .19 },
830 Firebrick2 = { .932, .172, .172 },
831 Firebrick3 = { .804, .15, .15 },
832 Firebrick4 = { .545, .1, .1 },
833 Gold1 = { 1, .844, 0 },
834 Gold2 = { .932, .79, 0 },
835 Gold3 = { .804, .68, 0 },
836 Gold4 = { .545, .46, 0 },
837 Goldenrod1 = { 1, .756, .145 },
838 Goldenrod2 = { .932, .705, .132 },
839 Goldenrod3 = { .804, .608, .112 },
840 Goldenrod4 = { .545, .41, .08 },
841 Green1 = { 0, 1, 0 },
842 Green2 = { 0, .932, 0 },
843 Green3 = { 0, .804, 0 },
844 Green4 = { 0, .545, 0 },
845 Honeydew1 = { .94, 1, .94 },
846 Honeydew2 = { .88, .932, .88 },
847 Honeydew3 = { .756, .804, .756 },
848 Honeydew4 = { .512, .545, .512 },
849 HotPink1 = { 1, .43, .705 },
850 HotPink2 = { .932, .415, .655 },
851 HotPink3 = { .804, .376, .565 },
852 HotPink4 = { .545, .228, .385 },
853 IndianRed1 = { 1, .415, .415 },
854 IndianRed2 = { .932, .39, .39 },
855 IndianRed3 = { .804, .332, .332 },
856 IndianRed4 = { .545, .228, .228 },
857 Ivory1 = { 1, 1, .94 },
858 Ivory2 = { .932, .932, .88 },
859 Ivory3 = { .804, .804, .756 },
860 Ivory4 = { .545, .545, .512 },
861 Khaki1 = { 1, .965, .56 },
862 Khaki2 = { .932, .9, .52 },
863 Khaki3 = { .804, .776, .45 },
864 Khaki4 = { .545, .525, .305 },
865 LavenderBlush1 = { 1, .94, .96 },
866 LavenderBlush2 = { .932, .88, .898 },
867 LavenderBlush3 = { .804, .756, .772 },
868 LavenderBlush4 = { .545, .512, .525 },
869 LemonChiffon1 = { 1, .98, .804 },
870 LemonChiffon2 = { .932, .912, .75 },
871 LemonChiffon3 = { .804, .79, .648 },
872 LemonChiffon4 = { .545, .536, .44 },
873 LightBlue1 = { .75, .936, 1 },
874 LightBlue2 = { .698, .875, .932 },
875 LightBlue3 = { .604, .752, .804 },
876 LightBlue4 = { .408, .512, .545 },
877 LightCyan1 = { .88, 1, 1 },
878 LightCyan2 = { .82, .932, .932 },
879 LightCyan3 = { .705, .804, .804 },
880 LightCyan4 = { .48, .545, .545 },
881 LightGoldenrod1 = { 1, .925, .545 },
882 LightGoldenrod2 = { .932, .864, .51 },
883 LightGoldenrod3 = { .804, .745, .44 },
884 LightGoldenrod4 = { .545, .505, .298 },
885 LightPink1 = { 1, .684, .725 },
886 LightPink2 = { .932, .635, .68 },
887 LightPink3 = { .804, .55, .585 },
888 LightPink4 = { .545, .372, .396 },
889 LightSalmon1 = { 1, .628, .48 },
890 LightSalmon2 = { .932, .585, .448 },
891 LightSalmon3 = { .804, .505, .385 },
892 LightSalmon4 = { .545, .34, .26 },
893 LightSkyBlue1 = { .69, .888, 1 },
894 LightSkyBlue2 = { .644, .828, .932 },
895 LightSkyBlue3 = { .552, .712, .804 },
896 LightSkyBlue4 = { .376, .484, .545 },
897 LightSteelBlue1 = { .792, .884, 1 },
898 LightSteelBlue2 = { .736, .824, .932 },
899 LightSteelBlue3 = { .635, .71, .804 },
900 LightSteelBlue4 = { .43, .484, .545 },
901 LightYellow1 = { 1, 1, .88 },
902 LightYellow2 = { .932, .932, .82 },
903 LightYellow3 = { .804, .804, .705 },
904 LightYellow4 = { .545, .545, .48 },
905 Magenta1 = { 1, 0, 1 },
906 Magenta2 = { .932, 0, .932 },
907 Magenta3 = { .804, 0, .804 },
908 Magenta4 = { .545, 0, .545 },
909 Maroon1 = { 1, .204, .7 },
910 Maroon2 = { .932, .19, .655 },
911 Maroon3 = { .804, .16, .565 },
912 Maroon4 = { .545, .11, .385 },
913 MediumOrchid1 = { .88, .4, 1 },
914 MediumOrchid2 = { .82, .372, .932 },
915 MediumOrchid3 = { .705, .32, .804 },
916 MediumOrchid4 = { .48, .215, .545 },
917 MediumPurple1 = { .67, .51, 1 },
918 MediumPurple2 = { .624, .475, .932 },
919 MediumPurple3 = { .536, .408, .804 },
920 MediumPurple4 = { .365, .28, .545 },
921 MistyRose1 = { 1, .894, .884 },
922 MistyRose2 = { .932, .835, .824 },
923 MistyRose3 = { .804, .716, .71 },
924 MistyRose4 = { .545, .49, .484 },
925 NavajoWhite1 = { 1, .87, .68 },
926 NavajoWhite2 = { .932, .81, .63 },
927 NavajoWhite3 = { .804, .7, .545 },
928 NavajoWhite4 = { .545, .475, .37 },
929 OliveDrab1 = { .752, 1, .244 },
930 OliveDrab2 = { .7, .932, .228 },
931 OliveDrab3 = { .604, .804, .196 },
932 OliveDrab4 = { .41, .545, .132 },
933 Orange1 = { 1, .648, 0 },
934 Orange2 = { .932, .604, 0 },
935 Orange3 = { .804, .52, 0 },
936 Orange4 = { .545, .352, 0 },
937 OrangeRed1 = { 1, .27, 0 },
938 OrangeRed2 = { .932, .25, 0 },
939 OrangeRed3 = { .804, .215, 0 },
940 OrangeRed4 = { .545, .145, 0 },
941 Orchid1 = { 1, .512, .98 },
942 Orchid2 = { .932, .48, .912 },
943 Orchid3 = { .804, .41, .79 },
944 Orchid4 = { .545, .28, .536 },
945 PaleGreen1 = { .604, 1, .604 },
946 PaleGreen2 = { .565, .932, .565 },
947 PaleGreen3 = { .488, .804, .488 },
948 PaleGreen4 = { .33, .545, .33 },
949 PaleTurquoise1 = { .732, 1, 1 },
950 PaleTurquoise2 = { .684, .932, .932 },
951 PaleTurquoise3 = { .59, .804, .804 },
952 PaleTurquoise4 = { .4, .545, .545 },
953 PaleVioletRed1 = { 1, .51, .67 },
954 PaleVioletRed2 = { .932, .475, .624 },
955 PaleVioletRed3 = { .804, .408, .536 },
956 PaleVioletRed4 = { .545, .28, .365 },
957 PeachPuff1 = { 1, .855, .725 },
958 PeachPuff2 = { .932, .796, .68 },
959 PeachPuff3 = { .804, .688, .585 },
960 PeachPuff4 = { .545, .468, .396 },
961 Pink1 = { 1, .71, .772 },
962 Pink2 = { .932, .664, .72 },
963 Pink3 = { .804, .57, .62 },
964 Pink4 = { .545, .39, .424 },
965 Plum1 = { 1, .732, 1 },
966 Plum2 = { .932, .684, .932 },
967 Plum3 = { .804, .59, .804 },
968 Plum4 = { .545, .4, .545 },
969 Purple1 = { .608, .19, 1 },
970 Purple2 = { .57, .172, .932 },
971 Purple3 = { .49, .15, .804 },
972 Purple4 = { .332, .1, .545 },
973 Red1 = { 1, 0, 0 },
974 Red2 = { .932, 0, 0 },
975 Red3 = { .804, 0, 0 },
976 Red4 = { .545, 0, 0 },
977 RosyBrown1 = { 1, .756, .756 },
978 RosyBrown2 = { .932, .705, .705 },
979 RosyBrown3 = { .804, .608, .608 },
980 RosyBrown4 = { .545, .41, .41 },
981 RoyalBlue1 = { .284, .464, 1 },
982 RoyalBlue2 = { .264, .43, .932 },
983 RoyalBlue3 = { .228, .372, .804 },
984 RoyalBlue4 = { .152, .25, .545 },
985 Salmon1 = { 1, .55, .41 },
986 Salmon2 = { .932, .51, .385 },
987 Salmon3 = { .804, .44, .33 },
988 Salmon4 = { .545, .298, .224 },
989 SeaGreen1 = { .33, 1, .624 },
990 SeaGreen2 = { .305, .932, .58 },
991 SeaGreen3 = { .264, .804, .5 },
992 SeaGreen4 = { .18, .545, .34 },
993 Seashell1 = { 1, .96, .932 },
994 Seashell2 = { .932, .898, .87 },
995 Seashell3 = { .804, .772, .75 },
996 Seashell4 = { .545, .525, .51 },
997 Sienna1 = { 1, .51, .28 },
998 Sienna2 = { .932, .475, .26 },
999 Sienna3 = { .804, .408, .224 },
1000 Sienna4 = { .545, .28, .15 },
1001 SkyBlue1 = { .53, .808, 1 },
1002 SkyBlue2 = { .494, .752, .932 },
1003 SkyBlue3 = { .424, .65, .804 },
1004 SkyBlue4 = { .29, .44, .545 },
1005 SlateBlue1 = { .512, .435, 1 },
1006 SlateBlue2 = { .48, .404, .932 },
1007 SlateBlue3 = { .41, .35, .804 },
1008 SlateBlue4 = { .28, .235, .545 },
1009 SlateGray1 = { .776, .888, 1 },
1010 SlateGray2 = { .725, .828, .932 },
1011 SlateGray3 = { .624, .712, .804 },
1012 SlateGray4 = { .424, .484, .545 },
1013 Snow1 = { 1, .98, .98 },
1014 Snow2 = { .932, .912, .912 },
1015 Snow3 = { .804, .79, .79 },
1016 Snow4 = { .545, .536, .536 },
1017 SpringGreen1 = { 0, 1, .498 },
1018 SpringGreen2 = { 0, .932, .464 },
1019 SpringGreen3 = { 0, .804, .4 },
1020 SpringGreen4 = { 0, .545, .27 },
1021 SteelBlue1 = { .39, .72, 1 },
1022 SteelBlue2 = { .36, .675, .932 },
1023 SteelBlue3 = { .31, .58, .804 },
1024 SteelBlue4 = { .21, .392, .545 },
1025 Tan1 = { 1, .648, .31 },
1026 Tan2 = { .932, .604, .288 },
1027 Tan3 = { .804, .52, .248 },
1028 Tan4 = { .545, .352, .17 },
1029 Thistle1 = { 1, .884, 1 },
1030 Thistle2 = { .932, .824, .932 },
1031 Thistle3 = { .804, .71, .804 },
1032 Thistle4 = { .545, .484, .545 },
1033 Tomato1 = { 1, .39, .28 },
1034 Tomato2 = { .932, .36, .26 },
1035 Tomato3 = { .804, .31, .224 },
1036 Tomato4 = { .545, .21, .15 },
1037 Turquoise1 = { 0, .96, 1 },
1038 Turquoise2 = { 0, .898, .932 },
1039 Turquoise3 = { 0, .772, .804 },
1040 Turquoise4 = { 0, .525, .545 },
1041 VioletRed1 = { 1, .244, .59 },
1042 VioletRed2 = { .932, .228, .55 },
1043 VioletRed3 = { .804, .196, .47 },
1044 VioletRed4 = { .545, .132, .32 },
1045 Wheat1 = { 1, .905, .73 },
1046 Wheat2 = { .932, .848, .684 },
1047 Wheat3 = { .804, .73, .59 },
1048 Wheat4 = { .545, .494, .4 },
1049 Yellow1 = { 1, 1, 0 },
1050 Yellow2 = { .932, .932, 0 },
1051 Yellow3 = { .804, .804, 0 },
1052 Yellow4 = { .545, .545, 0 },
1053 Gray0 = { .745, .745, .745 },
1054 Green0 = { 0, 1, 0 },
1055 Grey0 = { .745, .745, .745 },
1056 Maroon0 = { .69, .19, .376 },
1057 Purple0 = { .628, .125, .94 },
1058 }
1059
1060 --- https://luarocks.org/modules/Firanel/lua-color
1061 --- Copyright (c) 2021 Firanel
1062
1063 ---https://github.com/Firanel/lua-color/blob/master/util/bitwise.lua
1064 local bitwise = (function()
1065 -- Implementations of bitwise operators so that lua-color can be used
1066 -- with Lua 5.1 and LuaJIT 2.1.0-beta3 (e.g. inside Neovim).
1067
1068 -- Code taken directly from:
1069 -- https://stackoverflow.com/questions/5977654/how-do-i-use-the-bitwise-operator-xor-in-lua
1070
1071 local function bit_xor(a, b)
1072 local p, c = 1, 0
1073 while a > 0 and b > 0 do
1074 local ra, rb = a % 2, b % 2
1075 if ra ~= rb then
1076 c = c + p
1077 end
1078 a, b, p = (a - ra) / 2, (b - rb) / 2, p * 2
1079 end
1080 if a < b then
1081 a = b
1082 end
1083 while a > 0 do
1084 local ra = a % 2
1085 if ra > 0 then
1086 c = c + p
1087 end
1088 a, p = (a - ra) / 2, p * 2
1089 end
1090 return c
1091 end
1092
1093 local function bit_or(a, b)
1094 local p, c = 1, 0
1095 while a + b > 0 do
1096 local ra, rb = a % 2, b % 2
1097 if ra + rb > 0 then
1098 c = c + p
1099 end
1100 a, b, p = (a - ra) / 2, (b - rb) / 2, p * 2
1101 end
1102 return c
1103 end
1104
1105 local function bit_not(n)
1106 local p, c = 1, 0
1107 while n > 0 do
1108 local r = n % 2
1109 if r < 1 then
1110 c = c + p
1111 end
1112 n, p = (n - r) / 2, p * 2
1113 end
1114 return c
1115 end
1116
1117 local function bit_and(a, b)
1118 local p, c = 1, 0
1119 while a > 0 and b > 0 do
1120 local ra, rb = a % 2, b % 2
1121 if ra + rb > 1 then
1122 c = c + p
1123 end
1124 a, b, p = (a - ra) / 2, (b - rb) / 2, p * 2
1125 end
1126 return c
1127 end
1128
1129 local function bit_lshift(x, by)
1130 return x * 2 ^ by
1131 end
1132
1133 local function bit_rshift(x, by)
1134 return math.floor(x / 2 ^ by)
1135 end
1136
1137 return {
1138 bit_xor = bit_xor,
1139 bit_or = bit_or,
1140 bit_not = bit_not,
1141 bit_and = bit_and,
1142 bit_lshift = bit_lshift,
1143 bit_rshift = bit_rshift,
1144 }
1145 end)()
1146
1147 --- https://github.com/Firanel/lua-color/blob/master/util/class.lua
1148 local class = (function()
1149
1150 -- Code based on:
1151 -- http://lua-users.org/wiki/SimpleLuaClasses
1152
1153 ---Helper function to create classes
1154 ---
1155 ---@usage local Color = class(function () --[[ constructor ]] end)
1156 ---@usage local Color2 = class(
1157 -- Color,
1158 -- function () --[[ constructor ]] end,
1159 -- { prop_a = "some value" }
1160 -- )
1161 local function class(base, init, defaults)
1162 local c = defaults or {} -- a new class instance
1163 if not init and type(base) == 'function' then
1164 init = base
1165 base = nil
1166 elseif type(base) == 'table' then
1167 -- our new class is a shallow copy of the base class!
1168 for i, v in pairs(base) do
1169 c[i] = v
1170 end
1171 c._base = base
1172 end
1173 -- the class will be the metatable for all its objects,
1174 -- and they will look up their methods in it.
1175 c.__index = c
1176
1177 -- expose a constructor which can be called by <classname>(<args>)
1178 local mt = {}
1179 mt.__call = function(class_tbl, ...)
1180 local obj = {}
1181 setmetatable(obj, c)
1182 if init then
1183 init(obj, ...)
1184 else
1185 -- make sure that any stuff from the base class is initialized!
1186 if base and base.init then
1187 base.init(obj, ...)
1188 end
1189 end
1190 return obj
1191 end
1192 c.init = init
1193 c.is_a = function(self, klass)
1194 local m = getmetatable(self)
1195 while m do
1196 if m == klass then
1197 return true
1198 end
1199 m = m._base
1200 end
1201 return false
1202 end
1203 setmetatable(c, mt)
1204 return c
1205 end
1206
1207 return class
1208 end)()
1209
1210 ---https://github.com/Firanel/lua-color/blob/master/utils/init.lua
1211 local utils = (function()
1212 local function min_ind(first, ...)
1213 local min, ind = first, 1
1214 for i, v in ipairs { ... } do
1215 if v < min then
1216 min, ind = v, i + 1
1217 end
1218 end
1219 return min, ind
1220 end
1221
1222 local function max_ind(first, ...)
1223 local max, ind = first, 1
1224 for i, v in ipairs { ... } do
1225 if v > max then
1226 max, ind = v, i + 1
1227 end
1228 end
1229 return max, ind
1230 end
1231
1232 local function round(x)
1233 return x + 0.5 - (x + 0.5) % 1
1234 end
1235
1236 local function clamp(x, min, max)
1237 return x < min and min or x > max and max or x
1238 end
1239
1240 local function map(t, cb)
1241 local n = {}
1242 for i, v in ipairs(t) do
1243 n[i] = cb(v)
1244 end
1245 return n
1246 end
1247
1248 return {
1249 min = min_ind,
1250 max = max_ind,
1251 round = round,
1252 clamp = clamp,
1253 map = map,
1254 }
1255 end)()
1256
1257 ---Source: [texmf-dist/tex/context/base/mkiv/attr-col.lua](https://git.texlive.info/texlive/tree/Master/texmf-dist/tex/context/base/mkiv/attr-col.lua)
1258 local convert = (function()
1259
1260 ---
1261 ---https://www.rapidtables.com/convert/color/rgb-to-cmyk.html
1262 ---https://www.101computing.net/cmyk-to-rgb-conversion-algorithm/
1263 ---
1264 ---@param r r # red (0.0 - 1.0)
1265 ---@param g g # green (0.0 - 1.0)
1266 ---@param b b # blue (0.0 - 1.0)
1267 ---
1268 ---@return c c # cyan (0.0 - 1.0)
1269 ---@return m m # magenta (0.0 - 1.0)
1270 ---@return y y # yellow (0.0 - 1.0)
1271 ---@return k k # key(black) (0.0 - 1.0)
1272 local function rgb_to_cmyk(r, g, b)
1273 local K = math.max(r, g, b)
1274 if K == 0 then
1275 return 0.0, 0.0, 0.0, 1.0
1276 end
1277 local k = 1 - K
1278 local c = (K - r) / K
1279 local m = (K - g) / K
1280 local y = (K - b) / K
1281 return c, m, y, k
1282 end
1283
1284 ---https://www.rapidtables.com/convert/color/cmyk-to-rgb.html
1285 local function cmyk_to_rgb(c, m, y, k)
1286 if not k then
1287 k = 0
1288 end
1289 ---texmf-dist/tex/context/base/mkiv/attr-col.lua
1290 -- local d = 1.0 - k
1291 -- local r = 1.0 - math.min(1.0, c * d + k)
1292 -- local g = 1.0 - math.min(1.0, m * d + k)
1293 -- local b = 1.0 - math.min(1.0, y * d + k)
1294
1295 ---texmf-dist/tex/context/base/mkiv/attr-col.lua
1296 -- local r = 1.0 - math.min(1.0, c + k)
1297 -- local g = 1.0 - math.min(1.0, m + k)
1298 -- local b = 1.0 - math.min(1.0, y + k)
1299
1300 ---https://github.com/Firanel/lua-color/blob/eba73e53e9abd2e8da4d56b016fd77b45c2f3b79/init.lua#L335-L340
1301 local K = 1 - k
1302 local r = (1 - c) * K
1303 local g = (1 - m) * K
1304 local b = (1 - y) * K
1305
1306 return r, g, b
1307
1308 end
1309
1310 local function rgb_to_gray(r, g, b)
1311 if not r then
1312 return 0
1313 end
1314 local w = colors.weightgray
1315 if w == true then
1316 return .30 * r + .59 * g + .11 * b
1317 elseif not w then
1318 return r / 3 + g / 3 + b / 3
1319 else
1320 return w[1] * r + w[2] * g + w[3] * b
1321 end
1322 end
1323
1324 local function cmyk_to_gray(c, m, y, k)
1325 return rgb_to_gray(cmyk_to_rgb(c, m, y, k))
1326 end
1327
1328 -- http://en.wikipedia.org/wiki/HSI_color_space
1329 -- http://nl.wikipedia.org/wiki/HSV_(kleurruimte)
1330
1331 -- h /= 60; // sector 0 to 5
1332 -- i = floor( h );
1333 -- f = h - i; // factorial part of h
1334
1335 local function hsv_to_rgb(h, s, v)
1336 if s > 1 then
1337 s = 1
1338 elseif s < 0 then
1339 s = 0
1340 elseif s == 0 then
1341 return v, v, v
1342 end
1343 if v > 1 then
1344 s = 1
1345 elseif v < 0 then
1346 v = 0
1347 end
1348 if h < 0 then
1349 h = 0
1350 elseif h >= 360 then
1351 h = mod(h, 360)
1352 end
1353 local hd = h / 60
1354 local hi = floor(hd)
1355 local f = hd - hi
1356 local p = v * (1 - s)
1357 local q = v * (1 - f * s)
1358 local t = v * (1 - (1 - f) * s)
1359 if hi == 0 then
1360 return v, t, p
1361 elseif hi == 1 then
1362 return q, v, p
1363 elseif hi == 2 then
1364 return p, v, t
1365 elseif hi == 3 then
1366 return p, q, v
1367 elseif hi == 4 then
1368 return t, p, v
1369 elseif hi == 5 then
1370 return v, p, q
1371 else
1372 print('error in hsv -> rgb', h, s, v)
1373 return 0, 0, 0
1374 end
1375 end
1376
1377 local function rgb_to_hsv(r, g, b)
1378 local offset, maximum, other_1, other_2
1379 if r >= g and r >= b then
1380 offset, maximum, other_1, other_2 = 0, r, g, b
1381 elseif g >= r and g >= b then
1382 offset, maximum, other_1, other_2 = 2, g, b, r
1383 else
1384 offset, maximum, other_1, other_2 = 4, b, r, g
1385 end
1386 if maximum == 0 then
1387 return 0, 0, 0
1388 end
1389 local minimum = other_1 < other_2 and other_1 or other_2
1390 if maximum == minimum then
1391 return 0, 0, maximum
1392 end
1393 local delta = maximum - minimum
1394 return (offset + (other_1 - other_2) / delta) * 60, delta / maximum,
1395 maximum
1396 end
1397
1398 local function gray_to_rgb(s) -- unweighted
1399 return 1 - s, 1 - s, 1 - s
1400 end
1401
1402 local function hsv_to_gray(h, s, v)
1403 return rgb_to_gray(hsv_to_rgb(h, s, v))
1404 end
1405
1406 local function gray_to_hsv(s)
1407 return 0, 0, s
1408 end
1409
1410 ---
1411 ---@param h any # hue
1412 ---@param black any # black
1413 ---@param white any # white
1414 ---
1415 ---@return number r
1416 ---@return number g
1417 ---@return number b
1418 local function hwb_to_rgb(h, black, white)
1419 local r, g, b = hsv_to_rgb(h, 1, .5)
1420 local f = 1 - white - black
1421 return f * r + white, f * g + white, f * b + white
1422 end
1423
1424 return {
1425 rgb_to_cmyk = rgb_to_cmyk,
1426 cmyk_to_rgb = cmyk_to_rgb,
1427 rgb_to_gray = rgb_to_gray,
1428 cmyk_to_gray = cmyk_to_gray,
1429 hsv_to_rgb = hsv_to_rgb,
1430 rgb_to_hsv = rgb_to_hsv,
1431 gray_to_rgb = gray_to_rgb,
1432 hsv_to_gray = hsv_to_gray,
1433 gray_to_hsv = gray_to_hsv,
1434 hwb_to_rgb = hwb_to_rgb,
1435 }
1436
1437 end)()
1438
1439 --- https://github.com/Firanel/lua-color/blob/master/init.lua
1440
1441 ---The Class Color is the main class of the submodule. It represents a
1442 ---RGB color.
1443 ---
1444 ---@class Color
1445 ---@field r number # Red component.
1446 ---@field g number # Green component.
1447 ---@field b number # Blue component.
1448 ---@field a number # Alpha component.
1449 ---
1450 ---@function Color:__call
1451 ---
1452 ---@param value Color string|table|Color value (default: `nil`)
1453 ---
1454 ---@see Color:set
1455 local Color = (function()
1456
1457 ---Parse, convert and manipulate color values.
1458 ---
1459 -- @classmod Color
1460
1461 -- Lua 5.1 compat
1462 local bit_and = bitwise.bit_and
1463 local bit_lshift = bitwise.bit_lshift
1464 local bit_rshift = bitwise.bit_rshift
1465
1466 -- Utils
1467
1468 local function hcm_to_rgb(h, c, m)
1469 local r, g, b = 0, 0, 0
1470
1471 h = h * 6
1472 local x = c * (1 - math.abs(h % 2 - 1))
1473
1474 if h <= 1 then
1475 r, g, b = c, x, 0
1476 elseif h <= 2 then
1477 r, g, b = x, c, 0
1478 elseif h <= 3 then
1479 r, g, b = 0, c, x
1480 elseif h <= 4 then
1481 r, g, b = 0, x, c
1482 elseif h <= 5 then
1483 r, g, b = x, 0, c
1484 elseif h <= 6 then
1485 r, g, b = c, 0, x
1486 end
1487
1488 return r + m, g + m, b + m
1489 end
1490
1491 ---
1492 ---@param str string A number encoded as a string with an optional percent sign.
1493 ---
1494 ---@return number result A number from 0 - 1
1495 local function tonumPercent(str)
1496 if str:sub(-1) == '%' then
1497 return tonumber(str:sub(1, #str - 1)) / 100
1498 end
1499 return tonumber(str)
1500 end
1501
1502 -- Color
1503
1504 ---Color constructor.
1505 ---
1506 -- @function Color:__call
1507 ---
1508 ---@param ?string|table|Color value Color value (default: `nil`)
1509 ---
1510 ---@see Color:set
1511
1512 ---Red component.
1513 -- @field r
1514
1515 ---Green component.
1516 -- @field g
1517
1518 ---Blue component.
1519 -- @field b
1520
1521 ---Alpha component.
1522 -- @field a
1523
1524 ---Color class
1525 local Color = class(nil, function(this, value)
1526 if value then
1527 if type(value) == 'string' then
1528 -- # gets expanded to ##
1529 value = string.gsub(value, '^##', '#')
1530 end
1531 this:set(value)
1532 end
1533 end, { __is_color = true, r = 0, g = 0, b = 0, a = 1 })
1534
1535 ---Clone color
1536 ---
1537 ---@return Color copy
1538 function Color:clone()
1539 return Color(self)
1540 end
1541
1542 ---Set color to value.
1543 -- <br>
1544 -- Called by constructor
1545 -- <br><br>
1546 -- Possible value types:
1547 -- <ul>
1548 -- <li>`Color`</li>
1549 -- <li>color name as specified in `Color.colorNames`</li>
1550 -- <li>css style functions as string:<ul>
1551 -- <li>`rgb(r, g, b)`</li>
1552 -- <li>`rgba(r, g, b, a)`</li>
1553 -- <li>`hsl(h, s, l)`</li>
1554 -- <li>`hsla(h, s, l, a)`</li>
1555 -- <li>`hsv(h, s, v)`</li>
1556 -- <li>`hsva(h, s, v, a)`</li>
1557 -- <li>`hwb(h, w, b)`</li>
1558 -- <li>`hwba(h, w, b, a)`</li>
1559 -- <li>`cmyk(c, m, y, k)`</li>
1560 -- </ul>
1561 -- Values are in the same ranges as in css ([0;255] for rgb, [0;1] for alpha, ...)<br>
1562 -- functions can be specified in a simplified syntax: `rgb(r, g, b) == rgb r g b`
1563 -- </li>
1564 -- <li>NCol string: `R10, 50%, 50%`</li>
1565 -- <li>hex string: `#rgb` | `#rgba` | `#rrggbb` | `#rrggbbaa` (`#` can be omitted)</li>
1566 -- <li>rgb values in [0;1]: `{r, g, b[, a]}` | `{r=r, g=g, b=b[, a=a]}`</li>
1567 -- <li>hsv values in [0;1]: `{h=h, s=s, v=v[, a=a]}`</li>
1568 -- <li>hsl values in [0;1]: `{h=h, s=s, l=l[, a=a]}`</li>
1569 -- <li>hwb values in [0;1]: `{h=h, w=w, b=b[, a=a]}`</li>
1570 -- <li>cmyk values in [0;1]: `{c=c, m=m, y=y, k=k}`</li>
1571 -- <li>single set mode, table with any combination of the following: <ul>
1572 -- <li>`red`</li>
1573 -- <li>`green`</li>
1574 -- <li>`blue`</li>
1575 -- <li>`alpha`</li>
1576 -- <li>`hue`</li>
1577 -- <li>`saturation`</li>
1578 -- <li>`value`</li>
1579 -- <li>`lightness`</li>
1580 -- <li>`whiteness`</li>
1581 -- <li>`blackness`</li>
1582 -- <li>`cyan`</li>
1583 -- <li>`magenta`</li>
1584 -- <li>`yellow`</li>
1585 -- <li>`key`</li>
1586 -- </ul>
1587 -- All values are in `[0;1]`.<br>
1588 -- They will be applied in the order: `rgba -> hsl -> hwb -> hsv -> cmyk`<br>
1589 -- If `lightness` is given, saturation is treated as hsl saturation,
1590 -- otherwise it will be treated as hsv saturation.
1591 -- </li>
1592 -- </ul>
1593 ---
1594 ---@see Color:__call
1595 ---
1596 ---@param value string|table|Color
1597 ---
1598 ---@return Color self
1599 ---
1600 ---@usage color:set "#f1f1f1"
1601 ---@usage color:set "rgba(241, 241, 241, 0.5)"
1602 ---@usage color:set "hsl 180 100% 20%"
1603 ---@usage color:set { r = 0.255, g = 0.729, b = 0.412 }
1604 ---@usage color:set { 0.255, 0.729, 0.412 } -- same as above
1605 ---@usage color:set { h = 0.389, s = 0.65, v = 0.73 }
1606 function Color:set(value)
1607 assert(value)
1608
1609 -- from Color
1610 if value.__is_color then
1611 self.r = value.r
1612 self.g = value.g
1613 self.b = value.b
1614 self.a = value.a
1615
1616 elseif type(value) == 'string' then
1617 self.a = 1
1618
1619 if value:sub(1, 1) ~= '#' then
1620 local c = colors[value]
1621 if c then
1622 self.r = c[1]
1623 self.g = c[2]
1624 self.b = c[3]
1625 return
1626 end
1627
1628 local func, values = value:match '(%w+)[ %(]+([x ,.%x%%]+)'
1629 if func ~= nil then
1630 if func == 'rgb' then
1631 local r, g, b =
1632 values:match '([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)'
1633 assert(r and g and b)
1634 self.r = tonumber(r) / 0xff
1635 self.g = tonumber(g) / 0xff
1636 self.b = tonumber(b) / 0xff
1637 return self
1638 elseif func == 'rgba' then
1639 local r, g, b, a =
1640 values:match '([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+)[ ,]+([x.%x]+%%?)'
1641 assert(r and g and b and a)
1642 self.r = tonumber(r) / 0xff
1643 self.g = tonumber(g) / 0xff
1644 self.b = tonumber(b) / 0xff
1645 self.a = tonumPercent(a)
1646 return self
1647 elseif func == 'hsv' then
1648 local h, s, v =
1649 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1650 assert(h and s and v)
1651 return self:set{
1652 h = tonumber(h) / 360,
1653 s = tonumPercent(s),
1654 v = tonumPercent(v),
1655 }
1656 elseif func == 'hsva' then
1657 local h, s, v, a =
1658 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1659 assert(h and s and v and a)
1660 return self:set{
1661 h = tonumber(h) / 360,
1662 s = tonumPercent(s),
1663 v = tonumPercent(v),
1664 a = tonumPercent(a),
1665 }
1666 elseif func == 'hsl' then
1667 local h, s, l =
1668 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1669 assert(h and s and l)
1670 return self:set{
1671 h = tonumber(h) / 360,
1672 s = tonumPercent(s),
1673 l = tonumPercent(l),
1674 }
1675 elseif func == 'hsla' then
1676 local h, s, l, a =
1677 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1678 assert(h and s and l and a)
1679 return self:set{
1680 h = tonumber(h) / 360,
1681 s = tonumPercent(s),
1682 l = tonumPercent(l),
1683 a = tonumPercent(a),
1684 }
1685 elseif func == 'hwb' then
1686 local h, w, b =
1687 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1688 assert(h and w and b)
1689 return self:set{
1690 h = tonumber(h) / 360,
1691 w = tonumPercent(w),
1692 b = tonumPercent(b),
1693 }
1694 elseif func == 'hwba' then
1695 local h, w, b, a =
1696 values:match '([x.%x]+)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1697 assert(h and w and b and a)
1698 return self:set{
1699 h = tonumber(h) / 360,
1700 w = tonumPercent(w),
1701 b = tonumPercent(b),
1702 a = tonumPercent(a),
1703 }
1704 elseif func == 'cmyk' then
1705 local c, m, y, k =
1706 values:match '([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1707 assert(c and m and y and k)
1708 return self:set{
1709 c = tonumPercent(c),
1710 m = tonumPercent(m),
1711 y = tonumPercent(y),
1712 k = tonumPercent(k),
1713 }
1714 end
1715 else
1716 local col, dist, w, b, a =
1717 value:match '([RGBCMYrgbcmy])(%d*)[, ]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1718 if col == nil then
1719 col, dist, w, b, a =
1720 value:match '([RGBCMYrgbcmy])(%d*)[, ]+([x.%x]+%%?)[ ,]+([x.%x]+%%?)'
1721 end
1722 if col then
1723 col = col:lower()
1724
1725 local h
1726 if col == 'r' then
1727 h = 0
1728 elseif col == 'y' then
1729 h = 1 / 6
1730 elseif col == 'g' then
1731 h = 2 / 6
1732 elseif col == 'c' then
1733 h = 3 / 6
1734 elseif col == 'b' then
1735 h = 4 / 6
1736 elseif col == 'm' then
1737 h = 5 / 6
1738 end
1739
1740 if #dist > 0 then
1741 h = h + tonumber(dist) / 600
1742 end
1743
1744 return self:set{
1745 h = h,
1746 w = tonumPercent(w),
1747 b = tonumPercent(b),
1748 a = a and tonumPercent(a) or 1,
1749 }
1750 end
1751 end
1752 else
1753 value = value:sub(2)
1754 end
1755
1756 local pattern
1757 local div = 0xff
1758 if #value == 3 then
1759 pattern = '(%x)(%x)(%x)'
1760 div = 0xf
1761 elseif #value == 4 then
1762 pattern = '(%x)(%x)(%x)(%x)'
1763 div = 0xf
1764 elseif #value == 6 then
1765 pattern = '(%x%x)(%x%x)(%x%x)'
1766 elseif #value == 8 then
1767 pattern = '(%x%x)(%x%x)(%x%x)(%x%x)'
1768 else
1769 error('Not a valid color: ' .. tostring(value))
1770 end
1771 local r, g, b, a = value:match(pattern)
1772 assert(r ~= nil, 'Not a valid color: ' .. tostring(value))
1773 self.r = tonumber(r, 16) / div
1774 self.g = tonumber(g, 16) / div
1775 self.b = tonumber(b, 16) / div
1776 self.a = a ~= nil and tonumber(a, 16) / div or 1
1777
1778 -- table with rgb
1779 elseif value[1] ~= nil then
1780 self.r = value[1]
1781 self.g = value[2]
1782 self.b = value[3]
1783 self.a = value[4] or self.a or 1
1784 elseif value.r ~= nil then
1785 self.r = value.r
1786 self.g = value.g or self.g
1787 self.b = value.b or self.b
1788 self.a = value.a or self.a
1789
1790 elseif value.c ~= nil then
1791 self.r, self.g, self.b = convert.cmyk_to_rgb(value.c, value.m,
1792 value.y, value.k)
1793 self.a = 1
1794
1795 -- table with hs[vl]
1796 elseif value.h ~= nil then
1797 if value.w ~= nil then -- hwb
1798 value.v = 1 - value.b
1799 value.s = 1 - value.w / value.v
1800 end
1801
1802 local hue, saturation = value.h, value.s
1803 assert(hue ~= nil, saturation ~= nil)
1804
1805 local r, g, b = 0, 0, 0
1806
1807 if value.v ~= nil then
1808 local v = value.v
1809 local chroma = saturation * v
1810 r, g, b = hcm_to_rgb(hue, chroma, v - chroma)
1811
1812 elseif value.l ~= nil then
1813 local lightness = value.l
1814 local chroma = (1 - math.abs(2 * lightness - 1)) * saturation
1815 r, g, b = hcm_to_rgb(hue, chroma, lightness - chroma / 2)
1816 end
1817
1818 self.r = r
1819 self.g = g
1820 self.b = b
1821 self.a = value.a or self.a or 1
1822
1823 else -- Single set mode
1824 if value.red then
1825 self.r = value.red
1826 end
1827 if value.green then
1828 self.g = value.green
1829 end
1830 if value.blue then
1831 self.b = value.blue
1832 end
1833 if value.alpha then
1834 self.a = value.alpha
1835 end
1836
1837 if value.lightness then
1838 local h, s, l = self:hsl()
1839 self:set{
1840 h = value.hue or h,
1841 s = value.saturation or s,
1842 l = value.lightness or l,
1843 }
1844 value.hue = nil
1845 value.saturation = nil
1846 end
1847
1848 if value.whiteness or value.blackness then
1849 local h, w, b = self:hwb()
1850 self:set{
1851 h = value.hue or h,
1852 w = value.whiteness or w,
1853 b = value.backness or b,
1854 }
1855 value.hue = nil
1856 end
1857
1858 if value.hue or value.saturation or value.value then
1859 local h, s, v = self:hsv()
1860 self:set{
1861 h = value.hue or h,
1862 s = value.saturation or s,
1863 v = value.value or v,
1864 }
1865 end
1866
1867 if value.cyan or value.magenta or value.yellow or value.key then
1868 local c, m, y, k = self:cmyk()
1869 self:set{
1870 c = value.cyan or c,
1871 m = value.magenta or m,
1872 y = value.yellow or y,
1873 k = value.key or k,
1874 }
1875 end
1876 end
1877
1878 local r, g, b, a = utils.clamp(self.r, 0, 1),
1879 utils.clamp(self.g, 0, 1), utils.clamp(self.b, 0, 1),
1880 utils.clamp(self.a, 0, 1)
1881 assert(r and g and b and a, 'Color invalid')
1882 return self
1883 end
1884
1885 ---Get rgb values.
1886 ---
1887 ---@return number[0;1] red
1888 ---@return number[0;1] green
1889 ---@return number[0;1] blue
1890 function Color:rgb()
1891 return self.r, self.g, self.b
1892 end
1893
1894 ---Get rgba values.
1895 ---
1896 ---@return number[0;1] red
1897 ---@return number[0;1] green
1898 ---@return number[0;1] blue
1899 ---@return number[0;1] alpha
1900 function Color:rgba()
1901 return self.r, self.g, self.b, self.a
1902 end
1903
1904 function Color:_hsvm()
1905 local r, g, b = self.r, self.g, self.b
1906
1907 local max, max_i = utils.max(r, g, b)
1908 local min = math.min(r, g, b)
1909 local chroma = max - min
1910
1911 local hue
1912 if chroma == 0 then
1913 hue = 0
1914 elseif max_i == 1 then
1915 hue = ((g - b) / chroma) / 6
1916 elseif max_i == 2 then
1917 hue = (2 + (b - r) / chroma) / 6
1918 elseif max_i == 3 then
1919 hue = (4 + (r - g) / chroma) / 6
1920 end
1921
1922 local saturation = max == 0 and 0 or chroma / max
1923
1924 return hue, saturation, max, min
1925 end
1926
1927 ---Get hsv values.
1928 ---
1929 ---@return number[0;1] hue
1930 ---@return number[0;1] saturation
1931 ---@return number[0;1] value
1932 function Color:hsv()
1933 local h, s, v = self:_hsvm()
1934 return h, s, v
1935 end
1936
1937 ---Get hsv values.
1938 ---
1939 ---@return number[0;1] hue
1940 ---@return number[0;1] saturation
1941 ---@return number[0;1] value
1942 ---@return number[0;1] alpha
1943 function Color:hsva()
1944 local h, s, v = self:_hsvm()
1945 return h, s, v, self.a
1946 end
1947
1948 ---Get hsl values.
1949 ---
1950 ---@return number[0;1] hue
1951 ---@return number[0;1] saturation
1952 ---@return number[0;1] lightness
1953 function Color:hsl()
1954 local hue, _, max, min = self:_hsvm()
1955 local lightness = (max + min) / 2
1956
1957 local saturation = lightness == 0 and 0 or (max - lightness) /
1958 math.min(lightness, 1 - lightness)
1959
1960 if saturation ~= saturation then
1961 saturation = 0
1962 end
1963
1964 return hue, saturation, lightness
1965 end
1966
1967 ---Get hsl values.
1968 ---
1969 ---@return number[0;1] hue
1970 ---@return number[0;1] saturation
1971 ---@return number[0;1] lightness
1972 ---@return number[0;1] alpha
1973 function Color:hsla()
1974 local h, s, l = self:hsl()
1975 return h, s, l, self.a
1976 end
1977
1978 ---Get hwb values.
1979 ---
1980 ---@return number[0;1] hue
1981 ---@return number[0;1] whiteness
1982 ---@return number[0;1] blackness
1983 function Color:hwb()
1984 local h, s, v = self:hsv()
1985 local w = (1 - s) * v
1986 local b = 1 - v
1987 return h, w, b
1988 end
1989
1990 ---Get hwb values.
1991 ---
1992 ---@return number[0;1] hue
1993 ---@return number[0;1] whiteness
1994 ---@return number[0;1] blackness
1995 ---@return number[0;1] alpha
1996 function Color:hwba()
1997 local h, w, b = self:hwb()
1998 return h, w, b, self.a
1999 end
2000
2001 ---Get cmyk values.
2002 ---
2003 ---@return number[0;1] cyan
2004 ---@return number[0;1] magenta
2005 ---@return number[0;1] yellow
2006 ---@return number[0;1] key
2007 function Color:cmyk()
2008 return convert.rgb_to_cmyk(self.r, self.g, self.b)
2009 end
2010
2011 ---Rotate hue of color.
2012 ---
2013 ---@param number[0;1]|table value Part of full turn or table containing degree or radians
2014 ---
2015 ---@return Color self
2016 ---
2017 ---@usage color:rotate(0.5)
2018 ---@usage color:rotate {deg=180}
2019 ---@usage color:rotate {rad=math.pi}
2020 function Color:rotate(value)
2021 local r
2022 if type(value) == 'number' then
2023 r = value
2024 elseif value.rad ~= nil then
2025 r = value.rad / (math.pi * 2)
2026 elseif value.deg ~= nil then
2027 r = value.deg / 360
2028 else
2029 error('No valid argument')
2030 end
2031
2032 local h, s, v = self:hsv()
2033 h = (h + r) % 1
2034 self:set{ h = h, s = s, v = v, a = self.a }
2035
2036 return self
2037 end
2038
2039 ---Invert the color.
2040 ---
2041 ---@return Color self
2042 function Color:invert()
2043 self.r = 1 - self.r
2044 self.g = 1 - self.g
2045 self.b = 1 - self.b
2046 return self
2047 end
2048
2049 ---Reduce saturation to 0.
2050 ---
2051 ---@return Color self
2052 function Color:grey()
2053 local h, _, v = self:hsv()
2054 self:set{ h = h, s = 0, v = v, a = self.a }
2055 return self
2056 end
2057
2058 ---Set to black or white depending on lightness.
2059 ---
2060 ---@param ?number[0;1] lightness Cutoff point (Default: 0.5)
2061 ---
2062 ---@return Color self
2063 function Color:blackOrWhite(lightness)
2064 local _, _, l = self:hsl()
2065 local v = l > lightness and 1 or 0
2066 self.r = v
2067 self.g = v
2068 self.b = v
2069 return self
2070 end
2071
2072 ---Mix two colors together.
2073 ---
2074 ---@param Color other
2075 ---@param ?number strength 0 results in self, 1 results in other (Default: 0.5)
2076 ---
2077 ---@return Color self
2078 function Color:mix(other, strength)
2079 if strength == nil then
2080 strength = 0.5
2081 end
2082 self.r = self.r * (1 - strength) + other.r * strength
2083 self.g = self.g * (1 - strength) + other.g * strength
2084 self.b = self.b * (1 - strength) + other.b * strength
2085 self.a = self.a * (1 - strength) + other.a * strength
2086 return self
2087 end
2088
2089 ---Generate complementary color.
2090 ---
2091 ---@return Color
2092 function Color:complement()
2093 return Color(self):rotate(0.5)
2094 end
2095
2096 ---Generate analogous color scheme.
2097 ---
2098 ---@return Color
2099 ---@return Color self
2100 ---@return Color
2101 function Color:analogous()
2102 local h, s, v = self:hsv()
2103 return Color { h = (h - 1 / 12) % 1, s = s, v = v, a = self.a },
2104 self, Color { h = (h + 1 / 12) % 1, s = s, v = v, a = self.a }
2105 end
2106
2107 ---Generate triadic color scheme.
2108 ---
2109 ---@return Color self
2110 ---@return Color
2111 ---@return Color
2112 function Color:triad()
2113 local h, s, v = self:hsv()
2114 return self,
2115 Color { h = (h + 1 / 3) % 1, s = s, v = v, a = self.a },
2116 Color { h = (h + 2 / 3) % 1, s = s, v = v, a = self.a }
2117 end
2118
2119 ---Generate tetradic color scheme.
2120 ---
2121 ---@return Color self
2122 ---@return Color
2123 ---@return Color
2124 ---@return Color
2125 function Color:tetrad()
2126 local h, s, v = self:hsv()
2127 return self,
2128 Color { h = (h + 1 / 4) % 1, s = s, v = v, a = self.a },
2129 Color { h = (h + 2 / 4) % 1, s = s, v = v, a = self.a },
2130 Color { h = (h + 3 / 4) % 1, s = s, v = v, a = self.a }
2131 end
2132
2133 ---Generate compound color scheme.
2134 ---
2135 ---@return Color
2136 ---@return Color self
2137 ---@return Color
2138 function Color:compound()
2139 local ca, _, cb = self:complement():analogous()
2140 return ca, self, cb
2141 end
2142
2143 ---Generate evenly spaced color scheme.
2144 -- <br>
2145 -- Generalization of `triad` and `tetrad`.
2146 ---
2147 ---@param int n Return n colors
2148 ---@param ?number r Space colors over r rotations (Default: 1)
2149 ---
2150 ---@return {Color,...} Table with n colors including self at index 1
2151 function Color:evenlySpaced(n, r)
2152 assert(n > 0, 'n needs to be greater than 0')
2153 r = r or 1
2154
2155 local res = { self }
2156
2157 local rot = r / n
2158 local h, s, v = self:hsv()
2159 local a = self.a
2160
2161 for i = 1, n - 1 do
2162 h = (h + rot) % 1
2163 table.insert(res, Color { h = h, s = s, v = v, a = a })
2164 end
2165
2166 return res
2167 end
2168
2169 ---Get string representation of color.
2170 ---
2171 -- If `format` is `nil`, `color:tostring()` is the same as `tostring(color)`.
2172 ---
2173 ---@param ?string format One of: `#fff`, `#ffff`, `#ffffff`, `#ffffffff`,
2174 -- rgb, rgba, hsv, hsva, hsl, hsla, hwb, hwba, ncol, cmyk
2175 ---
2176 ---@return string
2177 ---
2178 ---@see Color:__tostring
2179 function Color:tostring(format)
2180 if format == nil then
2181 return tostring(self)
2182 end
2183
2184 format = format:lower()
2185
2186 if format:sub(1, 1) == '#' then
2187 if #format == 4 then
2188 return string.format('#%x%x%x', utils.round(self.r * 0xf),
2189 utils.round(self.g * 0xf), utils.round(self.b * 0xf))
2190 elseif #format == 5 then
2191 return string.format('#%x%x%x%x', utils.round(self.r * 0xf),
2192 utils.round(self.g * 0xf), utils.round(self.b * 0xf),
2193 utils.round(self.a * 0xf))
2194 elseif #format == 7 then
2195 return string.format('#%02x%02x%02x',
2196 utils.round(self.r * 0xff), utils.round(self.g * 0xff),
2197 utils.round(self.b * 0xff))
2198 elseif #format == 9 then
2199 return string.format('#%02x%02x%02x%02x',
2200 utils.round(self.r * 0xff), utils.round(self.g * 0xff),
2201 utils.round(self.b * 0xff), utils.round(self.a * 0xff))
2202 end
2203 elseif format == 'rgb' then
2204 return string.format('rgb(%d, %d, %d)',
2205 utils.round(self.r * 0xff), utils.round(self.g * 0xff),
2206 utils.round(self.b * 0xff))
2207 elseif format == 'rgba' then
2208 return string.format('rgba(%d, %d, %d, %s)',
2209 utils.round(self.r * 0xff), utils.round(self.g * 0xff),
2210 utils.round(self.b * 0xff), self.a)
2211 elseif format == 'hsv' then
2212 local h, s, v = self:hsv()
2213 return string.format('hsv(%d, %d%%, %d%%)', utils.round(h * 360),
2214 utils.round(s * 100), utils.round(v * 100))
2215 elseif format == 'hsva' then
2216 local h, s, v, a = self:hsva()
2217 return string.format('hsva(%d, %d%%, %d%%, %s)',
2218 utils.round(h * 360), utils.round(s * 100),
2219 utils.round(v * 100), a)
2220 elseif format == 'hsl' then
2221 local h, s, l = self:hsl()
2222 return string.format('hsl(%d, %d%%, %d%%)', utils.round(h * 360),
2223 utils.round(s * 100), utils.round(l * 100))
2224 elseif format == 'hsla' then
2225 local h, s, l, a = self:hsla()
2226 return string.format('hsla(%d, %d%%, %d%%, %s)',
2227 utils.round(h * 360), utils.round(s * 100),
2228 utils.round(l * 100), a)
2229 elseif format == 'hwb' then
2230 local h, w, b = self:hwb()
2231 return string.format('hwb(%d, %d%%, %d%%)', utils.round(h * 360),
2232 utils.round(w * 100), utils.round(b * 100))
2233 elseif format == 'hwba' then
2234 local h, w, b, a = self:hwba()
2235 return string.format('hwba(%d, %d%%, %d%%, %s)',
2236 utils.round(h * 360), utils.round(w * 100),
2237 utils.round(b * 100), a)
2238 elseif format == 'ncol' then
2239 local h, w, b = self:hwb()
2240 local h_maj, h_min = math.modf(h * 6)
2241 h_maj = h_maj % 6
2242
2243 local col
2244 if h_maj == 0 then
2245 col = 'R'
2246 elseif h_maj == 1 then
2247 col = 'Y'
2248 elseif h_maj == 2 then
2249 col = 'G'
2250 elseif h_maj == 3 then
2251 col = 'C'
2252 elseif h_maj == 4 then
2253 col = 'B'
2254 else
2255 col = 'M'
2256 end
2257
2258 return string.format('%s%d, %d%%, %d%%', col,
2259 utils.round(h_min * 100), utils.round(w * 100),
2260 utils.round(b * 100))
2261 elseif format == 'cmyk' then
2262 local c, m, y, k = self:cmyk()
2263 return string.format('cymk(%d%%, %d%%, %d%%, %d%%)',
2264 utils.round(c * 100), utils.round(m * 100),
2265 utils.round(y * 100), utils.round(k * 100))
2266 end
2267
2268 return tostring(self)
2269 end
2270
2271 ---Get color in rgb hex notation.
2272 -- <br>
2273 -- only adds alpha value if `color.a < 1`
2274 ---
2275 ---@return string `#rrggbb` | `#rrggbbaa`
2276 ---
2277 ---@see Color:tostring
2278 function Color:__tostring()
2279 if self.a < 1 then
2280 return string.format('#%02x%02x%02x%02x',
2281 utils.round(self.r * 0xff), utils.round(self.g * 0xff),
2282 utils.round(self.b * 0xff), utils.round(self.a * 0xff))
2283 else
2284 return string.format('#%02x%02x%02x', utils.round(self.r * 0xff),
2285 utils.round(self.g * 0xff), utils.round(self.b * 0xff))
2286 end
2287 end
2288
2289 ---Check if colors are equal.
2290 ---
2291 ---@param Color other
2292 ---
2293 ---@return boolean all values are equal
2294 function Color:__eq(other)
2295 return
2296 self.r == other.r and self.g == other.g and self.b == other.b and
2297 self.a == other.a
2298 end
2299
2300 ---Checks whether color is darker.
2301 ---
2302 ---@param Color other
2303 ---
2304 ---@return boolean self is darker than other
2305 function Color:__lt(other)
2306 local _, _, la = self:hsl()
2307 local _, _, lb = other:hsl()
2308 return la < lb
2309 end
2310
2311 ---Checks whether color is as dark or darker.
2312 ---
2313 ---@param Color other
2314 ---
2315 ---@return boolean self is as dark or darker than other
2316 function Color:__le(other)
2317 local _, _, la = self:hsl()
2318 local _, _, lb = other:hsl()
2319 return la <= lb
2320 end
2321
2322 ---Iterate through color.
2323 ---
2324 -- Iterates through r, g, b, and a.
2325 function Color:__pairs()
2326 local function iter(tbl, k)
2327 if k == nil then
2328 return 'r', self.r
2329 elseif k == 'r' then
2330 return 'g', self.g
2331 elseif k == 'g' then
2332 return 'b', self.b
2333 elseif k == 'b' then
2334 return 'a', self.a
2335 end
2336 end
2337
2338 return iter, self, nil
2339 end
2340
2341 ---Get inverted clone of color.
2342 ---
2343 ---@return Color
2344 function Color:__unm()
2345 return Color(self):invert()
2346 end
2347
2348 ---Mix two colors evenly.
2349 ---
2350 ---@param Color a first color
2351 ---@param Color b second color
2352 ---
2353 ---@return Color new color
2354 ---
2355 ---@see Color:mix
2356 function Color.__add(a, b)
2357 assert(Color.isColor(a) and Color.isColor(b),
2358 'Can only add two colors.')
2359 return Color(a):mix(b)
2360 end
2361
2362 ---Complement of even mix.
2363 ---
2364 ---@param Color a first color
2365 ---@param Color b second color
2366 ---
2367 ---@return Color new color
2368 ---
2369 ---@see Color:mix
2370 ---@see Color.__add
2371 function Color.__sub(a, b)
2372 assert(Color.isColor(a) and Color.isColor(b),
2373 'Can only add two colors.')
2374 return Color(a):mix(b):rotate(0.5)
2375 end
2376
2377 ---Apply rgb mask to color.
2378 ---
2379 ---@param Color|number a color or mask
2380 ---@param Color|number b color or mask (if a and b are colors b is used as mask)
2381 ---
2382 ---@return Color new color
2383 ---
2384 ---@usage local new_col = color & 0xff00ff -- get new color without the green channel
2385 function Color.__band(a, b)
2386 local color, mask
2387 if Color.isColor(a) and type(b) == 'number' then
2388 color = a
2389 mask = b
2390 elseif Color.isColor(b) and type(a) == 'number' then
2391 color = b
2392 mask = a
2393 elseif Color.isColor(a) and Color.isColor(b) then
2394 color = a
2395 mask = bit_lshift(utils.round(b.r * 0xff), 16) +
2396 bit_lshift(utils.round(b.g * 0xff), 8) +
2397 utils.round(b.b * 0xff)
2398 else
2399 error(
2400 'Required arguments: Color|number,Color|number Received: ' ..
2401 type(a) .. ',' .. type(b))
2402 end
2403
2404 return Color {
2405 bit_and(utils.round(color.r * 0xff), bit_rshift(mask, 16)) / 0xff,
2406 bit_and(utils.round(color.g * 0xff), bit_rshift(mask, 8)) / 0xff,
2407 bit_and(utils.round(color.b * 0xff), mask) / 0xff,
2408 color.a,
2409 }
2410 end
2411
2412 ---Apply rgb mask to color, providing backwards compatibility for Lua 5.1 and LuaJIT 2.1.0-beta3 (e.g. inside Neovim), which don't provide native support for bitwise operators.
2413 ---
2414 ---@param a Color|number # color or mask
2415 ---@param b Color|number # color or mask (if a and b are colors b is used as mask)
2416 ---
2417 ---@return Color new color
2418 ---
2419 ---@usage local new_col = Color.band(color, 0xff00ff) -- get new color without the green channel
2420 function Color.band(a, b)
2421 return Color.__band(a, b)
2422 end
2423
2424 ---Check whether `color` is a Color.
2425 ---
2426 ---@param color Color
2427 ---
2428 ---@return boolean # is a color
2429 ---
2430 ---@usage if Color.isColor(color) then print "It's a color!" end
2431 function Color.isColor(color)
2432 return color ~= nil and color.__is_color == true
2433 end
2434
2435 ---Format a PDF colorstack string. This string can be assigned to the
2436 ---`node.data` field of a PDF colorstack node.
2437 ---
2438 ---@return string # A string like this example `1 0 0 rg 1 0 0 RG`
2439 function Color:format_pdf_colorstack_string()
2440 return table.concat({
2441 self.r,
2442 self.g,
2443 self.b,
2444 'rg',
2445 self.r,
2446 self.g,
2447 self.b,
2448 'RG',
2449 }, ' ')
2450 end
2451
2452 ---Create a PDF colorstack node.
2453 ---
2454 ---@param command "set"|"push"|"pop"|"current"
2455 ---
2456 ---@return PdfColorstackWhatsitNode
2457 function Color:create_pdf_colorstack_node(command)
2458 local whatsit = node.new('whatsit', 'pdf_colorstack') --[[@as PdfColorstackWhatsitNode]]
2459 if command == 'set' then
2460 whatsit.command = 0
2461 elseif command == 'push' then
2462 whatsit.command = 1
2463 elseif command == 'pop' then
2464 whatsit.command = 2
2465 elseif command == 'current' then
2466 whatsit.command = 3
2467 end
2468 if command ~= 'pop' then
2469 whatsit.data = self:format_pdf_colorstack_string()
2470 end
2471 return whatsit
2472 end
2473
2474 ---Write a PDF colorstock node using `node.write()`.
2475 ---
2476 ---@param command "set"|"push"|"pop"|"current"
2477 ---
2478 function Color:write_pdf_colorstack_node(command)
2479 node.write(self:create_pdf_colorstack_node(command))
2480 end
2481
2482 function Color:write_box()
2483 self:write_pdf_colorstack_node('push')
2484 local rule = node.new('rule') --[[@as RuleNode]]
2485 rule.width = tex.sp('0.5cm')
2486 rule.height = tex.sp('0.5cm')
2487 node.write(rule)
2488 self:write_pdf_colorstack_node('pop')
2489 end
2490
2491 return Color
2492
2493 end)()
2494
2495 ---
2496 ---
2497 ---
2498 ---@param scheme 'base'|'svg'|'x11'
2499 local function print_color_table(scheme)
2500 for _, name in pairs(schemes[scheme]) do
2501 -- local color = Color({ r = rgb[1], g = rgb[2], b = rgb[3] })
2502 local color = colors[name]
2503 local r = utils.round(color[1] * 255)
2504 local g = utils.round(color[2] * 255)
2505 local b = utils.round(color[3] * 255)
2506 tex.print('\\par\\noindent')
2507 tex.print(string.format('\\FarbeBox{rgb(%d, %d, %d} ', r, g, b))
2508 tex.print('\\texttt{\\tiny\\enspace ' .. name .. '}')
2509 end
2510 end
2511
2512 ---
2513 ---@param operator string # The PDF color operator, e. g. `0.2 0.5 1 rg 0.2 0.5 1 RG`
2514 ---
2515 ---@return Color|nil
2516 local function convert_pdf_color_operator(operator)
2517 operator = operator:lower()
2518
2519 ---
2520 ---@param count_floats integer
2521 ---@param suffix string
2522 ---
2523 ---@return string
2524 local function build_pattern(count_floats, suffix)
2525 local pattern_float = '(%d*%.?%d+)'
2526
2527 local patterns = {}
2528 for i = 1, count_floats, 1 do
2529 patterns[i] = pattern_float
2530 end
2531 patterns[count_floats + 1] = suffix
2532
2533 return table.concat(patterns, ' +')
2534 end
2535
2536 local n = tonumber
2537
2538 local r, g, b = operator:match(build_pattern(3, 'rg'))
2539 if r ~= nil then
2540 log.debug('operator to RGB: %s %s %s', r, g, b)
2541 return Color({ r = n(r), g = n(g), b = n(b) })
2542 end
2543
2544 local c, m, y, k = operator:match(build_pattern(3, 'k'))
2545 if c ~= nil then
2546 log.debug('operator to CMYK: %s %s %s %s', c, m, y, k)
2547 return Color({ c = n(c), m = n(m), y = n(y), k = n(k) })
2548 end
2549
2550 local gray = operator:match(build_pattern(1, 'g'))
2551 if gray ~= nil then
2552 log.debug('operator to GRAY: %s', gray)
2553 return Color({ r = n(gray), g = n(gray), b = n(gray) })
2554 end
2555 end
2556
2557 ---
2558 ---@param name string # The name of the color.
2559 ---@param operator string # The PDF color operator, e. g. `0.2 0.5 1 rg 0.2 0.5 1 RG`
2560 local function import_color(name, operator)
2561 if colors[name] ~= nil then
2562 return
2563 end
2564 local color = convert_pdf_color_operator(operator)
2565 log.info('Import new color: name %s, operator: %s, converted: %s',
2566 name, operator, color)
2567 if color ~= nil then
2568 colors[name] = { color.r, color.g, color.b }
2569 end
2570 end
2571
2572 return {
2573 convert = convert,
2574 Color = Color --[[@as Color]] ,
2575 print_color_table = print_color_table,
2576 import_color = import_color,
2577 }