Debian Codegraph @

drawing/color.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "strings"
8 )
9
10 // Basic Colors from:
11 // https://www.w3.org/wiki/CSS/Properties/color/keywords
12 var (
13 // ColorTransparent is a fully transparent color.
14 ColorTransparent = Color{R: 255, G: 255, B: 255, A: 0}
15 // ColorWhite is white.
16 ColorWhite = Color{R: 255, G: 255, B: 255, A: 255}
17 // ColorBlack is black.
18 ColorBlack = Color{R: 0, G: 0, B: 0, A: 255}
19 // ColorRed is red.
20 ColorRed = Color{R: 255, G: 0, B: 0, A: 255}
21 // ColorGreen is green.
22 ColorGreen = Color{R: 0, G: 128, B: 0, A: 255}
23 // ColorBlue is blue.
24 ColorBlue = Color{R: 0, G: 0, B: 255, A: 255}
25 // ColorSilver is a known color.
26 ColorSilver = Color{R: 192, G: 192, B: 192, A: 255}
27 // ColorMaroon is a known color.
28 ColorMaroon = Color{R: 128, G: 0, B: 0, A: 255}
29 // ColorPurple is a known color.
30 ColorPurple = Color{R: 128, G: 0, B: 128, A: 255}
31 // ColorFuchsia is a known color.
32 ColorFuchsia = Color{R: 255, G: 0, B: 255, A: 255}
33 // ColorLime is a known color.
34 ColorLime = Color{R: 0, G: 255, B: 0, A: 255}
35 // ColorOlive is a known color.
36 ColorOlive = Color{R: 128, G: 128, B: 0, A: 255}
37 // ColorYellow is a known color.
38 ColorYellow = Color{R: 255, G: 255, B: 0, A: 255}
39 // ColorNavy is a known color.
40 ColorNavy = Color{R: 0, G: 0, B: 128, A: 255}
41 // ColorTeal is a known color.
42 ColorTeal = Color{R: 0, G: 128, B: 128, A: 255}
43 // ColorAqua is a known color.
44 ColorAqua = Color{R: 0, G: 255, B: 255, A: 255}
45 )
46
47 func parseHex(hex string) uint8 {
48 v, _ := strconv.ParseInt(hex, 16, 16)
49 return uint8(v)
50 }
51
52 // ParseColor parses a color from a string.
53 func ParseColor(rawColor string) Color {
54 if strings.HasPrefix(rawColor, "rgba") {
55 return ColorFromRGBA(rawColor)
56 }
57 if strings.HasPrefix(rawColor, "rgb") {
58 return ColorFromRGB(rawColor)
59 }
60 if strings.HasPrefix(rawColor, "#") {
61 return ColorFromHex(rawColor)
62 }
63 return ColorFromKnown(rawColor)
64 }
65
66 var rgbaexpr = regexp.MustCompile(`rgba\((?P<R>.+),(?P<G>.+),(?P<B>.+),(?P<A>.+)\)`)
67
68 // ColorFromRGBA returns a color from an `rgba()` css function.
69 func ColorFromRGBA(rgba string) (output Color) {
70 values := rgbaexpr.FindStringSubmatch(rgba)
71 for i, name := range rgbaexpr.SubexpNames() {
72 if i == 0 {
73 continue
74 }
75 if i >= len(values) {
76 break
77 }
78 switch name {
79 case "R":
80 value := strings.TrimSpace(values[i])
81 parsed, _ := strconv.ParseInt(value, 10, 16)
82 output.R = uint8(parsed)
83 case "G":
84 value := strings.TrimSpace(values[i])
85 parsed, _ := strconv.ParseInt(value, 10, 16)
86 output.G = uint8(parsed)
87 case "B":
88 value := strings.TrimSpace(values[i])
89 parsed, _ := strconv.ParseInt(value, 10, 16)
90 output.B = uint8(parsed)
91 case "A":
92 value := strings.TrimSpace(values[i])
93 parsed, _ := strconv.ParseFloat(value, 32)
94 if parsed > 1 {
95 parsed = 1
96 } else if parsed < 0 {
97 parsed = 0
98 }
99 output.A = uint8(parsed * 255)
100 }
101 }
102 return
103 }
104
105 var rgbexpr = regexp.MustCompile(`rgb\((?P<R>.+),(?P<G>.+),(?P<B>.+)\)`)
106
107 // ColorFromRGB returns a color from an `rgb()` css function.
108 func ColorFromRGB(rgb string) (output Color) {
109 output.A = 255
110 values := rgbexpr.FindStringSubmatch(rgb)
111 for i, name := range rgbaexpr.SubexpNames() {
112 if i == 0 {
113 continue
114 }
115 if i >= len(values) {
116 break
117 }
118 switch name {
119 case "R":
120 value := strings.TrimSpace(values[i])
121 parsed, _ := strconv.ParseInt(value, 10, 16)
122 output.R = uint8(parsed)
123 case "G":
124 value := strings.TrimSpace(values[i])
125 parsed, _ := strconv.ParseInt(value, 10, 16)
126 output.G = uint8(parsed)
127 case "B":
128 value := strings.TrimSpace(values[i])
129 parsed, _ := strconv.ParseInt(value, 10, 16)
130 output.B = uint8(parsed)
131 }
132 }
133 return
134 }
135
136 // ColorFromHex returns a color from a css hex code.
137 //
138 // NOTE: it will trim a leading '#' character if present.
139 func ColorFromHex(hex string) Color {
140 if strings.HasPrefix(hex, "#") {
141 hex = strings.TrimPrefix(hex, "#")
142 }
143 var c Color
144 if len(hex) == 3 {
145 c.R = parseHex(string(hex[0])) * 0x11
146 c.G = parseHex(string(hex[1])) * 0x11
147 c.B = parseHex(string(hex[2])) * 0x11
148 } else {
149 c.R = parseHex(string(hex[0:2]))
150 c.G = parseHex(string(hex[2:4]))
151 c.B = parseHex(string(hex[4:6]))
152 }
153 c.A = 255
154 return c
155 }
156
157 // ColorFromKnown returns an internal color from a known (basic) color name.
158 func ColorFromKnown(known string) Color {
159 switch strings.ToLower(known) {
160 case "transparent":
161 return ColorTransparent
162 case "white":
163 return ColorWhite
164 case "black":
165 return ColorBlack
166 case "red":
167 return ColorRed
168 case "blue":
169 return ColorBlue
170 case "green":
171 return ColorGreen
172 case "silver":
173 return ColorSilver
174 case "maroon":
175 return ColorMaroon
176 case "purple":
177 return ColorPurple
178 case "fuchsia":
179 return ColorFuchsia
180 case "lime":
181 return ColorLime
182 case "olive":
183 return ColorOlive
184 case "yellow":
185 return ColorYellow
186 case "navy":
187 return ColorNavy
188 case "teal":
189 return ColorTeal
190 case "aqua":
191 return ColorAqua
192 default:
193 return Color{}
194 }
195 }
196
197 // ColorFromAlphaMixedRGBA returns the system alpha mixed rgba values.
198 func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color {
199 fa := float64(a) / 255.0
200 var c Color
201 c.R = uint8(float64(r) / fa)
202 c.G = uint8(float64(g) / fa)
203 c.B = uint8(float64(b) / fa)
204 c.A = uint8(a | (a >> 8))
205 return c
206 }
207
208 // ColorChannelFromFloat returns a normalized byte from a given float value.
209 func ColorChannelFromFloat(v float64) uint8 {
210 return uint8(v * 255)
211 }
212
213 // Color is our internal color type because color.Color is bullshit.
214 type Color struct {
215 R, G, B, A uint8
216 }
217
218 // RGBA returns the color as a pre-alpha mixed color set.
219 func (c Color) RGBA() (r, g, b, a uint32) {
220 fa := float64(c.A) / 255.0
221 r = uint32(float64(uint32(c.R)) * fa)
222 r |= r << 8
223 g = uint32(float64(uint32(c.G)) * fa)
224 g |= g << 8
225 b = uint32(float64(uint32(c.B)) * fa)
226 b |= b << 8
227 a = uint32(c.A)
228 a |= a << 8
229 return
230 }
231
232 // IsZero returns if the color has been set or not.
233 func (c Color) IsZero() bool {
234 return c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0
235 }
236
237 // IsTransparent returns if the colors alpha channel is zero.
238 func (c Color) IsTransparent() bool {
239 return c.A == 0
240 }
241
242 // WithAlpha returns a copy of the color with a given alpha.
243 func (c Color) WithAlpha(a uint8) Color {
244 return Color{
245 R: c.R,
246 G: c.G,
247 B: c.B,
248 A: a,
249 }
250 }
251
252 // Equals returns true if the color equals another.
253 func (c Color) Equals(other Color) bool {
254 return c.R == other.R &&
255 c.G == other.G &&
256 c.B == other.B &&
257 c.A == other.A
258 }
259
260 // AverageWith averages two colors.
261 func (c Color) AverageWith(other Color) Color {
262 return Color{
263 R: (c.R + other.R) >> 1,
264 G: (c.G + other.G) >> 1,
265 B: (c.B + other.B) >> 1,
266 A: c.A,
267 }
268 }
269
270 // String returns a css string representation of the color.
271 func (c Color) String() string {
272 fa := float64(c.A) / float64(255)
273 return fmt.Sprintf("rgba(%v,%v,%v,%.1f)", c.R, c.G, c.B, fa)
274 }