Debian Codegraph @

input/color.go

raw
1 package input
2
3 import (
4 "fmt"
5 "image/color"
6 "math"
7 )
8
9 // ForegroundColorEvent represents a foreground color event. This event is
10 // emitted when the terminal requests the terminal foreground color using
11 // [ansi.RequestForegroundColor].
12 type ForegroundColorEvent struct{ color.Color }
13
14 // String returns the hex representation of the color.
15 func (e ForegroundColorEvent) String() string {
16 return colorToHex(e.Color)
17 }
18
19 // IsDark returns whether the color is dark.
20 func (e ForegroundColorEvent) IsDark() bool {
21 return isDarkColor(e.Color)
22 }
23
24 // BackgroundColorEvent represents a background color event. This event is
25 // emitted when the terminal requests the terminal background color using
26 // [ansi.RequestBackgroundColor].
27 type BackgroundColorEvent struct{ color.Color }
28
29 // String returns the hex representation of the color.
30 func (e BackgroundColorEvent) String() string {
31 return colorToHex(e)
32 }
33
34 // IsDark returns whether the color is dark.
35 func (e BackgroundColorEvent) IsDark() bool {
36 return isDarkColor(e.Color)
37 }
38
39 // CursorColorEvent represents a cursor color change event. This event is
40 // emitted when the program requests the terminal cursor color using
41 // [ansi.RequestCursorColor].
42 type CursorColorEvent struct{ color.Color }
43
44 // String returns the hex representation of the color.
45 func (e CursorColorEvent) String() string {
46 return colorToHex(e)
47 }
48
49 // IsDark returns whether the color is dark.
50 func (e CursorColorEvent) IsDark() bool {
51 return isDarkColor(e)
52 }
53
54 type shiftable interface {
55 ~uint | ~uint16 | ~uint32 | ~uint64
56 }
57
58 func shift[T shiftable](x T) T {
59 if x > 0xff {
60 x >>= 8
61 }
62 return x
63 }
64
65 func colorToHex(c color.Color) string {
66 if c == nil {
67 return ""
68 }
69 r, g, b, _ := c.RGBA()
70 return fmt.Sprintf("#%02x%02x%02x", shift(r), shift(g), shift(b))
71 }
72
73 func getMaxMin(a, b, c float64) (ma, mi float64) {
74 if a > b {
75 ma = a
76 mi = b
77 } else {
78 ma = b
79 mi = a
80 }
81 if c > ma {
82 ma = c
83 } else if c < mi {
84 mi = c
85 }
86 return ma, mi
87 }
88
89 func round(x float64) float64 {
90 return math.Round(x*1000) / 1000
91 }
92
93 // rgbToHSL converts an RGB triple to an HSL triple.
94 func rgbToHSL(r, g, b uint8) (h, s, l float64) {
95 // convert uint32 pre-multiplied value to uint8
96 // The r,g,b values are divided by 255 to change the range from 0..255 to 0..1:
97 Rnot := float64(r) / 255
98 Gnot := float64(g) / 255
99 Bnot := float64(b) / 255
100 Cmax, Cmin := getMaxMin(Rnot, Gnot, Bnot)
101 Δ := Cmax - Cmin
102 // Lightness calculation:
103 l = (Cmax + Cmin) / 2
104 // Hue and Saturation Calculation:
105 if Δ == 0 {
106 h = 0
107 s = 0
108 } else {
109 switch Cmax {
110 case Rnot:
111 h = 60 * (math.Mod((Gnot-Bnot)/Δ, 6))
112 case Gnot:
113 h = 60 * (((Bnot - Rnot) / Δ) + 2)
114 case Bnot:
115 h = 60 * (((Rnot - Gnot) / Δ) + 4)
116 }
117 if h < 0 {
118 h += 360
119 }
120
121 s = Δ / (1 - math.Abs((2*l)-1))
122 }
123
124 return h, round(s), round(l)
125 }
126
127 // isDarkColor returns whether the given color is dark.
128 func isDarkColor(c color.Color) bool {
129 if c == nil {
130 return true
131 }
132
133 r, g, b, _ := c.RGBA()
134 _, _, l := rgbToHSL(uint8(r>>8), uint8(g>>8), uint8(b>>8)) //nolint:gosec
135 return l < 0.5
136 }