| 1 |
package drawing |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"regexp" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
var ( |
| 13 |
|
| 14 |
ColorTransparent = Color{R: 255, G: 255, B: 255, A: 0} |
| 15 |
|
| 16 |
ColorWhite = Color{R: 255, G: 255, B: 255, A: 255} |
| 17 |
|
| 18 |
ColorBlack = Color{R: 0, G: 0, B: 0, A: 255} |
| 19 |
|
| 20 |
ColorRed = Color{R: 255, G: 0, B: 0, A: 255} |
| 21 |
|
| 22 |
ColorGreen = Color{R: 0, G: 128, B: 0, A: 255} |
| 23 |
|
| 24 |
ColorBlue = Color{R: 0, G: 0, B: 255, A: 255} |
| 25 |
|
| 26 |
ColorSilver = Color{R: 192, G: 192, B: 192, A: 255} |
| 27 |
|
| 28 |
ColorMaroon = Color{R: 128, G: 0, B: 0, A: 255} |
| 29 |
|
| 30 |
ColorPurple = Color{R: 128, G: 0, B: 128, A: 255} |
| 31 |
|
| 32 |
ColorFuchsia = Color{R: 255, G: 0, B: 255, A: 255} |
| 33 |
|
| 34 |
ColorLime = Color{R: 0, G: 255, B: 0, A: 255} |
| 35 |
|
| 36 |
ColorOlive = Color{R: 128, G: 128, B: 0, A: 255} |
| 37 |
|
| 38 |
ColorYellow = Color{R: 255, G: 255, B: 0, A: 255} |
| 39 |
|
| 40 |
ColorNavy = Color{R: 0, G: 0, B: 128, A: 255} |
| 41 |
|
| 42 |
ColorTeal = Color{R: 0, G: 128, B: 128, A: 255} |
| 43 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 209 |
func ColorChannelFromFloat(v float64) uint8 { |
| 210 |
return uint8(v * 255) |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
type Color struct { |
| 215 |
R, G, B, A uint8 |
| 216 |
} |
| 217 |
|
| 218 |
|
| 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 |
|
| 233 |
func (c Color) IsZero() bool { |
| 234 |
return c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0 |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
func (c Color) IsTransparent() bool { |
| 239 |
return c.A == 0 |
| 240 |
} |
| 241 |
|
| 242 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |