Debian Codegraph @

color.go

raw
1 //
2 // Copyright (c) 2016-2022 The Aurora Authors. All rights reserved.
3 // This program is free software. It comes without any warranty,
4 // to the extent permitted by applicable law. You can redistribute
5 // it and/or modify it under the terms of the Unlicense. See LICENSE
6 // file for more details or see below.
7 //
8
9 //
10 // This is free and unencumbered software released into the public domain.
11 //
12 // Anyone is free to copy, modify, publish, use, compile, sell, or
13 // distribute this software, either in source code form or as a compiled
14 // binary, for any purpose, commercial or non-commercial, and by any
15 // means.
16 //
17 // In jurisdictions that recognize copyright laws, the author or authors
18 // of this software dedicate any and all copyright interest in the
19 // software to the public domain. We make this dedication for the benefit
20 // of the public at large and to the detriment of our heirs and
21 // successors. We intend this dedication to be an overt act of
22 // relinquishment in perpetuity of all present and future rights to this
23 // software under copyright law.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
31 // OTHER DEALINGS IN THE SOFTWARE.
32 //
33 // For more information, please refer to <http://unlicense.org/>
34 //
35
36 package aurora
37
38 // A Color type is a color. It can contain
39 // one background color, one foreground color
40 // and a format, including ideogram related
41 // formats.
42 type Color uint
43
44 /*
45
46 Developer note.
47
48 The uint type is architecture depended and can be
49 represented as int32 or int64.
50
51 Thus, we can use 32-bits only to be fast and
52 cross-platform.
53
54 All supported formats requires 14 bits. It is
55 first 14 bits.
56
57 A foreground color requires 8 bit + 1 bit (presence flag).
58 And the same for background color.
59
60 The Color representations
61
62 [ bg 8 bit ] [fg 8 bit ] [ fg/bg 2 bits ] [ fm 14 bits ]
63
64 https://play.golang.org/p/fq2zcNstFoF
65
66 */
67
68 // Special formats
69 const (
70 BoldFm Color = 1 << iota // 1
71 FaintFm // 2
72 ItalicFm // 3
73 UnderlineFm // 4
74 SlowBlinkFm // 5
75 RapidBlinkFm // 6
76 ReverseFm // 7
77 ConcealFm // 8
78 CrossedOutFm // 9
79
80 FrakturFm // 20
81 DoublyUnderlineFm // 21 or bold off for some systems
82
83 FramedFm // 51
84 EncircledFm // 52
85 OverlinedFm // 53
86
87 InverseFm = ReverseFm // alias to ReverseFm
88 BlinkFm = SlowBlinkFm // alias to SlowBlinkFm
89 HiddenFm = ConcealFm // alias to ConcealFm
90 StrikeThroughFm = CrossedOutFm // alias to CrossedOutFm
91
92 maskFm = BoldFm | FaintFm |
93 ItalicFm | UnderlineFm |
94 SlowBlinkFm | RapidBlinkFm |
95 ReverseFm |
96 ConcealFm | CrossedOutFm |
97
98 FrakturFm | DoublyUnderlineFm |
99
100 FramedFm | EncircledFm | OverlinedFm
101
102 flagFg Color = 1 << 14 // presence flag (14th bit)
103 flagBg Color = 1 << 15 // presence flag (15th bit)
104
105 shiftFg = 16 // shift for foreground (starting from 16th bit)
106 shiftBg = 24 // shift for background (starting from 24th bit)
107 )
108
109 // Foreground colors and related formats
110 const (
111
112 // 8 bits
113
114 // [ 0; 7] - 30-37
115 // [ 8; 15] - 90-97 bright
116 // [ 16; 231] - RGB
117 // [232; 255] - grayscale
118
119 BlackFg Color = (iota << shiftFg) | flagFg // 30, 90
120 RedFg // 31, 91
121 GreenFg // 32, 92
122 YellowFg // 33, 93
123 BlueFg // 34, 94
124 MagentaFg // 35, 95
125 CyanFg // 36, 96
126 WhiteFg // 37, 97
127
128 BrightFg Color = ((1 << 3) << shiftFg) | flagFg // -> 90
129
130 // the BrightFg itself doesn't represent
131 // a color, thus it has not flagFg
132
133 // 5 bits
134
135 //
136 maskFg = (0xff << shiftFg) | flagFg
137 )
138
139 // Background colors and related formats
140 const (
141
142 // 8 bits
143
144 // [ 0; 7] - 40-47
145 // [ 8; 15] - 100-107 bright
146 // [ 16; 231] - RGB
147 // [232; 255] - grayscale
148
149 BlackBg Color = (iota << shiftBg) | flagBg // 40, 100
150 RedBg // 41, 101
151 GreenBg // 42, 102
152 YellowBg // 43, 103
153 BlueBg // 44, 104
154 MagentaBg // 45, 105
155 CyanBg // 46, 106
156 WhiteBg // 47, 107
157
158 BrightBg Color = ((1 << 3) << shiftBg) | flagBg // -> 100
159
160 // the BrightBg itself doesn't represent
161 // a color, thus it has not flagBg
162
163 // 5 bits
164
165 //
166 maskBg = (0xff << shiftBg) | flagBg
167 )
168
169 const (
170 availFlags = "-+# 0"
171 esc = "\033["
172 clear = esc + "0m"
173 )
174
175 // Nos returns string like 1;7;31;45. It
176 // may be an empty string for empty color.
177 // If the zero is true, then the string
178 // is prepended with 0;
179 func (c Color) Nos(zero bool) string {
180 return string(c.appendNos(make([]byte, 0, 59), zero))
181 }
182
183 func appendCond(bs []byte, cond, semi bool, vals ...byte) []byte {
184 if !cond {
185 return bs
186 }
187 return appendSemi(bs, semi, vals...)
188 }
189
190 // if the semi is true, then prepend with semicolon
191 func appendSemi(bs []byte, semi bool, vals ...byte) []byte {
192 if semi {
193 bs = append(bs, ';')
194 }
195 return append(bs, vals...)
196 }
197
198 func itoa(t byte) string {
199 var (
200 a [3]byte
201 j = 2
202 )
203 for i := 0; i < 3; i, j = i+1, j-1 {
204 a[j] = '0' + t%10
205 if t = t / 10; t == 0 {
206 break
207 }
208 }
209 return string(a[j:])
210 }
211
212 func (c Color) appendFg(bs []byte, zero bool) []byte {
213
214 if zero || c&maskFm != 0 {
215 bs = append(bs, ';')
216 }
217
218 // 0- 7 : 30-37
219 // 8-15 : 90-97
220 // > 15 : 38;5;val
221
222 switch fg := (c & maskFg) >> shiftFg; {
223 case fg <= 7:
224 // '3' and the value itself
225 bs = append(bs, '3', '0'+byte(fg))
226 case fg <= 15:
227 // '9' and the value itself
228 bs = append(bs, '9', '0'+byte(fg&^0x08)) // clear bright flag
229 default:
230 bs = append(bs, '3', '8', ';', '5', ';')
231 bs = append(bs, itoa(byte(fg))...)
232 }
233 return bs
234 }
235
236 func (c Color) appendBg(bs []byte, zero bool) []byte {
237
238 if zero || c&(maskFm|maskFg) != 0 {
239 bs = append(bs, ';')
240 }
241
242 // 0- 7 : 40- 47
243 // 8-15 : 100-107
244 // > 15 : 48;5;val
245
246 switch fg := (c & maskBg) >> shiftBg; {
247 case fg <= 7:
248 // '3' and the value itself
249 bs = append(bs, '4', '0'+byte(fg))
250 case fg <= 15:
251 // '1', '0' and the value itself
252 bs = append(bs, '1', '0', '0'+byte(fg&^0x08)) // clear bright flag
253 default:
254 bs = append(bs, '4', '8', ';', '5', ';')
255 bs = append(bs, itoa(byte(fg))...)
256 }
257 return bs
258 }
259
260 func (c Color) appendFm9(bs []byte, zero bool) []byte {
261
262 bs = appendCond(bs, c&ItalicFm != 0,
263 zero || c&(BoldFm|FaintFm) != 0,
264 '3')
265 bs = appendCond(bs, c&UnderlineFm != 0,
266 zero || c&(BoldFm|FaintFm|ItalicFm) != 0,
267 '4')
268 // don't combine slow and rapid blink using only
269 // on of them, preferring slow blink
270 if c&SlowBlinkFm != 0 {
271 bs = appendSemi(bs,
272 zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
273 '5')
274 } else if c&RapidBlinkFm != 0 {
275 bs = appendSemi(bs,
276 zero || c&(BoldFm|FaintFm|ItalicFm|UnderlineFm) != 0,
277 '6')
278 }
279
280 // including 1-2
281 const mask6i = BoldFm | FaintFm |
282 ItalicFm | UnderlineFm |
283 SlowBlinkFm | RapidBlinkFm
284
285 bs = appendCond(bs, c&ReverseFm != 0,
286 zero || c&(mask6i) != 0,
287 '7')
288 bs = appendCond(bs, c&ConcealFm != 0,
289 zero || c&(mask6i|ReverseFm) != 0,
290 '8')
291 bs = appendCond(bs, c&CrossedOutFm != 0,
292 zero || c&(mask6i|ReverseFm|ConcealFm) != 0,
293 '9')
294
295 return bs
296 }
297
298 // append 1;3;38;5;216 like string that represents ANSI
299 // color of the Color; the zero argument requires
300 // appending of '0' before to reset previous format
301 // and colors
302 func (c Color) appendNos(bs []byte, zero bool) []byte {
303
304 if zero {
305 bs = append(bs, '0') // reset previous
306 }
307
308 // formats
309 //
310
311 if c&maskFm != 0 {
312
313 // 1-2
314
315 // don't combine bold and faint using only on of them, preferring bold
316
317 if c&BoldFm != 0 {
318 bs = appendSemi(bs, zero, '1')
319 } else if c&FaintFm != 0 {
320 bs = appendSemi(bs, zero, '2')
321 }
322
323 // 3-9
324
325 const mask9 = ItalicFm | UnderlineFm |
326 SlowBlinkFm | RapidBlinkFm |
327 ReverseFm | ConcealFm | CrossedOutFm
328
329 if c&mask9 != 0 {
330 bs = c.appendFm9(bs, zero)
331 }
332
333 // 20-21
334
335 const (
336 mask21 = FrakturFm | DoublyUnderlineFm
337 mask9i = BoldFm | FaintFm | mask9
338 )
339
340 if c&mask21 != 0 {
341 bs = appendCond(bs, c&FrakturFm != 0,
342 zero || c&mask9i != 0,
343 '2', '0')
344 bs = appendCond(bs, c&DoublyUnderlineFm != 0,
345 zero || c&(mask9i|FrakturFm) != 0,
346 '2', '1')
347 }
348
349 // 50-53
350
351 const (
352 mask53 = FramedFm | EncircledFm | OverlinedFm
353 mask21i = mask9i | mask21
354 )
355
356 if c&mask53 != 0 {
357 bs = appendCond(bs, c&FramedFm != 0,
358 zero || c&mask21i != 0,
359 '5', '1')
360 bs = appendCond(bs, c&EncircledFm != 0,
361 zero || c&(mask21i|FramedFm) != 0,
362 '5', '2')
363 bs = appendCond(bs, c&OverlinedFm != 0,
364 zero || c&(mask21i|FramedFm|EncircledFm) != 0,
365 '5', '3')
366 }
367
368 }
369
370 // foreground
371 if c&maskFg != 0 {
372 bs = c.appendFg(bs, zero)
373 }
374
375 // background
376 if c&maskBg != 0 {
377 bs = c.appendBg(bs, zero)
378 }
379
380 return bs
381 }
382
383 // ColorIndex is index of pre-defined 8-bit foreground or
384 // background colors from 0 to 255 (38;5;n).
385 //
386 // 0- 7: standard colors (as in ESC [ 30–37 m)
387 // 8- 15: high intensity colors (as in ESC [ 90–97 m)
388 // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
389 // 232-255: grayscale from black to white in 24 steps
390 type ColorIndex uint8
391
392 // GrayIndex from 0 to 23.
393 type GrayIndex uint8
394
395 // The Colored interface represents a value with a Color.
396 type Colored interface {
397 Color() Color // color of the value
398 }
399
400 // Reset returns Color without a color and formats.
401 func (c Color) Reset() Color {
402 return Color(0)
403 }
404
405 //
406 // Formats
407 //
408
409 // Bold or increased intensity (1).
410 func (c Color) Bold() Color {
411 return (c &^ FaintFm) | BoldFm
412 }
413
414 // Faint, decreased intensity (2).
415 func (c Color) Faint() Color {
416 return (c &^ BoldFm) | FaintFm
417 }
418
419 // DoublyUnderline or Bold off, double-underline
420 // per ECMA-48 (21).
421 func (c Color) DoublyUnderline() Color {
422 return (c &^ UnderlineFm) | DoublyUnderlineFm
423 }
424
425 // Fraktur, rarely supported (20).
426 func (c Color) Fraktur() Color {
427 return c | FrakturFm
428 }
429
430 // Italic, not widely supported, sometimes
431 // treated as inverse (3).
432 func (c Color) Italic() Color {
433 return c | ItalicFm
434 }
435
436 // Underline (4).
437 func (c Color) Underline() Color {
438 return (c &^ DoublyUnderlineFm) | UnderlineFm
439 }
440
441 // SlowBlink, blinking less than 150
442 // per minute (5).
443 func (c Color) SlowBlink() Color {
444 return (c &^ RapidBlinkFm) | SlowBlinkFm
445 }
446
447 // RapidBlink, blinking 150+ per minute,
448 // not widely supported (6).
449 func (c Color) RapidBlink() Color {
450 return (c &^ SlowBlinkFm) | RapidBlinkFm
451 }
452
453 // Blink is alias for the SlowBlink.
454 func (c Color) Blink() Color {
455 return c.SlowBlink()
456 }
457
458 // Reverse video, swap foreground and
459 // background colors (7).
460 func (c Color) Reverse() Color {
461 return c | ReverseFm
462 }
463
464 // Inverse is alias for the Reverse
465 func (c Color) Inverse() Color {
466 return c.Reverse()
467 }
468
469 // Conceal, hidden, not widely supported (8).
470 func (c Color) Conceal() Color {
471 return c | ConcealFm
472 }
473
474 // Hidden is alias for the Conceal
475 func (c Color) Hidden() Color {
476 return c.Conceal()
477 }
478
479 // CrossedOut, characters legible, but
480 // marked for deletion (9).
481 func (c Color) CrossedOut() Color {
482 return c | CrossedOutFm
483 }
484
485 // StrikeThrough is alias for the CrossedOut.
486 func (c Color) StrikeThrough() Color {
487 return c.CrossedOut()
488 }
489
490 // Framed (51).
491 func (c Color) Framed() Color {
492 return c | FramedFm
493 }
494
495 // Encircled (52).
496 func (c Color) Encircled() Color {
497 return c | EncircledFm
498 }
499
500 // Overlined (53).
501 func (c Color) Overlined() Color {
502 return c | OverlinedFm
503 }
504
505 // Foreground colors
506 //
507 // Black foreground color (30)
508 func (c Color) Black() Color {
509 return (c &^ maskFg) | BlackFg
510 }
511
512 // Red foreground color (31)
513 func (c Color) Red() Color {
514 return (c &^ maskFg) | RedFg
515 }
516
517 // Green foreground color (32)
518 func (c Color) Green() Color {
519 return (c &^ maskFg) | GreenFg
520 }
521
522 // Yellow foreground color (33)
523 func (c Color) Yellow() Color {
524 return (c &^ maskFg) | YellowFg
525 }
526
527 // Blue foreground color (34)
528 func (c Color) Blue() Color {
529 return (c &^ maskFg) | BlueFg
530 }
531
532 // Magenta foreground color (35)
533 func (c Color) Magenta() Color {
534 return (c &^ maskFg) | MagentaFg
535 }
536
537 // Cyan foreground color (36)
538 func (c Color) Cyan() Color {
539 return (c &^ maskFg) | CyanFg
540 }
541
542 // White foreground color (37)
543 func (c Color) White() Color {
544 return (c &^ maskFg) | WhiteFg
545 }
546
547 // Bright foreground colors
548 //
549 // BrightBlack foreground color (90)
550 func (c Color) BrightBlack() Color {
551 return (c &^ maskFg) | BrightFg | BlackFg
552 }
553
554 // BrightRed foreground color (91)
555 func (c Color) BrightRed() Color {
556 return (c &^ maskFg) | BrightFg | RedFg
557 }
558
559 // BrightGreen foreground color (92)
560 func (c Color) BrightGreen() Color {
561 return (c &^ maskFg) | BrightFg | GreenFg
562 }
563
564 // BrightYellow foreground color (93)
565 func (c Color) BrightYellow() Color {
566 return (c &^ maskFg) | BrightFg | YellowFg
567 }
568
569 // BrightBlue foreground color (94)
570 func (c Color) BrightBlue() Color {
571 return (c &^ maskFg) | BrightFg | BlueFg
572 }
573
574 // BrightMagenta foreground color (95)
575 func (c Color) BrightMagenta() Color {
576 return (c &^ maskFg) | BrightFg | MagentaFg
577 }
578
579 // BrightCyan foreground color (96)
580 func (c Color) BrightCyan() Color {
581 return (c &^ maskFg) | BrightFg | CyanFg
582 }
583
584 // BrightWhite foreground color (97)
585 func (c Color) BrightWhite() Color {
586 return (c &^ maskFg) | BrightFg | WhiteFg
587 }
588
589 // Other
590 //
591 // Index of pre-defined 8-bit foreground color
592 // from 0 to 255 (38;5;n).
593 //
594 // 0- 7: standard colors (as in ESC [ 30–37 m)
595 // 8- 15: high intensity colors (as in ESC [ 90–97 m)
596 // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
597 // 232-255: grayscale from black to white in 24 steps
598 func (c Color) Index(ci ColorIndex) Color {
599 return (c &^ maskFg) | (Color(ci) << shiftFg) | flagFg
600 }
601
602 // Gray from 0 to 23.
603 func (c Color) Gray(n GrayIndex) Color {
604 if n > 23 {
605 n = 23
606 }
607 return (c &^ maskFg) | (Color(232+n) << shiftFg) | flagFg
608 }
609
610 // Background colors
611 //
612 // BgBlack background color (40)
613 func (c Color) BgBlack() Color {
614 return (c &^ maskBg) | BlackBg
615 }
616
617 // BgRed background color (41)
618 func (c Color) BgRed() Color {
619 return (c &^ maskBg) | RedBg
620 }
621
622 // BgGreen background color (42)
623 func (c Color) BgGreen() Color {
624 return (c &^ maskBg) | GreenBg
625 }
626
627 // BgYellow background color (43)
628 func (c Color) BgYellow() Color {
629 return (c &^ maskBg) | YellowBg
630 }
631
632 // BgBlue background color (44)
633 func (c Color) BgBlue() Color {
634 return (c &^ maskBg) | BlueBg
635 }
636
637 // BgMagenta background color (45)
638 func (c Color) BgMagenta() Color {
639 return (c &^ maskBg) | MagentaBg
640 }
641
642 // BgCyan background color (46)
643 func (c Color) BgCyan() Color {
644 return (c &^ maskBg) | CyanBg
645 }
646
647 // BgWhite background color (47)
648 func (c Color) BgWhite() Color {
649 return (c &^ maskBg) | WhiteBg
650 }
651
652 // Bright background colors
653 //
654 // BgBrightBlack background color (100)
655 func (c Color) BgBrightBlack() Color {
656 return (c &^ maskBg) | BrightBg | BlackBg
657 }
658
659 // BgBrightRed background color (101)
660 func (c Color) BgBrightRed() Color {
661 return (c &^ maskBg) | BrightBg | RedBg
662 }
663
664 // BgBrightGreen background color (102)
665 func (c Color) BgBrightGreen() Color {
666 return (c &^ maskBg) | BrightBg | GreenBg
667 }
668
669 // BgBrightYellow background color (103)
670 func (c Color) BgBrightYellow() Color {
671 return (c &^ maskBg) | BrightBg | YellowBg
672 }
673
674 // BgBrightBlue background color (104)
675 func (c Color) BgBrightBlue() Color {
676 return (c &^ maskBg) | BrightBg | BlueBg
677 }
678
679 // BgBrightMagenta background color (105)
680 func (c Color) BgBrightMagenta() Color {
681 return (c &^ maskBg) | BrightBg | MagentaBg
682 }
683
684 // BgBrightCyan background color (106)
685 func (c Color) BgBrightCyan() Color {
686 return (c &^ maskBg) | BrightBg | CyanBg
687 }
688
689 // BgBrightWhite background color (107)
690 func (c Color) BgBrightWhite() Color {
691 return (c &^ maskBg) | BrightBg | WhiteBg
692 }
693
694 // Other
695 //
696 // BgIndex of 8-bit pre-defined background color
697 // from 0 to 255 (48;5;n).
698 //
699 // 0- 7: standard colors (as in ESC [ 40–47 m)
700 // 8- 15: high intensity colors (as in ESC [100–107 m)
701 // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
702 // 232-255: grayscale from black to white in 24 steps
703 func (c Color) BgIndex(n ColorIndex) Color {
704 return (c &^ maskBg) | (Color(n) << shiftBg) | flagBg
705 }
706
707 // BgGray from 0 to 23.
708 func (c Color) BgGray(n GrayIndex) Color {
709 if n > 23 {
710 n = 23
711 }
712 return (c &^ maskBg) | (Color(232+n) << shiftBg) | flagBg
713 }