-
Notifications
You must be signed in to change notification settings - Fork 8
/
convert.go
197 lines (168 loc) · 4.47 KB
/
convert.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package colors
import (
"fmt"
"image"
"image/color"
"math"
"regexp"
"strconv"
"strings"
"github.com/grokify/mogo/errors/errorsutil"
"github.com/grokify/mogo/type/stringsutil"
"golang.org/x/image/colornames"
)
var (
rxColorHex = regexp.MustCompile(`^(?:#|0x)?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$`)
rxGoogleCol = regexp.MustCompile(`^google([0-9]+)$`)
)
// Parse returns a `color.RGBA` given a color name or hex color code.
func Parse(colorName string) (color.RGBA, error) {
colorName = strings.ToLower(strings.TrimSpace(colorName))
if col, ok := colornames.Map[colorName]; ok {
return col, nil
}
colorHTML, err := ParseHex(colorName)
if err == nil {
return colorHTML, nil
}
colorGoog, err := ParseGoogle(colorName)
if err == nil {
return colorGoog, nil
}
return color.RGBA{}, fmt.Errorf("E_COLOR_NOT_FOUND [%s]", colorName)
}
// MustParse returns a `color.RGBA` given a hex color code or Google color string.
// It panics if the input string cannot be parsed.
func MustParse(input string) color.RGBA {
c, err := Parse(input)
if err != nil {
panic(err)
}
return c
}
func CanonicalHex(hexRGB string, upperCase, addHash bool) (string, error) {
rgb, err := ParseHex(hexRGB)
if err != nil {
return "", err
}
hex := ColorRGBAToHex(rgb)
if upperCase {
hex = strings.ToUpper(hex)
}
if addHash {
hex = "#" + hex
}
return hex, nil
}
func CanonicalHexes(hexRGBs []string, upperCase, addHash, dedupeResults, sortResults bool) ([]string, error) {
var canonical []string
for _, h := range hexRGBs {
can, err := CanonicalHex(h, upperCase, addHash)
if err != nil {
return canonical, err
}
canonical = append(canonical, can)
}
if dedupeResults || sortResults {
return stringsutil.SliceCondenseSpace(canonical, dedupeResults, sortResults), nil
}
return canonical, nil
}
// ParseHex returns a `color.RGBA` given a hex color code.
func ParseHex(hexRGB string) (color.RGBA, error) {
m := rxColorHex.FindStringSubmatch(strings.ToLower(strings.TrimSpace(hexRGB)))
if len(m) == 0 {
return color.RGBA{}, fmt.Errorf("E_COLOR_NOT_HEX_STRING [%s]", hexRGB)
}
rdecimal, errR := strconv.ParseUint(m[1], 16, 64)
gdecimal, errG := strconv.ParseUint(m[2], 16, 64)
bdecimal, errB := strconv.ParseUint(m[3], 16, 64)
err := errorsutil.Join(false, errR, errG, errB)
if err != nil {
return color.RGBA{}, fmt.Errorf("E_COLOR_NOT_HEX_PARSE [%s]", err.Error())
}
return color.RGBA{
R: uint8(rdecimal),
G: uint8(gdecimal),
B: uint8(bdecimal),
A: 0xff}, nil
}
func ParseGoogle(googString string) (color.RGBA, error) {
m := rxGoogleCol.FindStringSubmatch(googString)
if len(m) == 0 {
return color.RGBA{},
fmt.Errorf("E_COLOR_NOT_GOOG_MATCH [%s]", googString)
}
idxInt, err := strconv.Atoi(m[1])
if err != nil {
panic(err)
}
col := GoogleChartColorX(uint64(idxInt))
return col, nil
}
func ColorRGBAToHex(c color.RGBA) string {
return fmt.Sprintf("%02x%02x%02x", c.R, c.G, c.B)
}
// ColorToHex returns 6 byte hex code in lower case.
func ColorToHex(c color.Color) string {
r, g, b, _ := c.RGBA()
return fmt.Sprintf("%02x%02x%02x", uint8(r), uint8(g), uint8(b))
}
// ColorString returns a full 16-bit color representation.
func ColorString(c color.Color) string {
r, g, b, a := c.RGBA()
five0fmt := "%05d"
return strings.Join(
[]string{
fmt.Sprintf(five0fmt, int(r)),
fmt.Sprintf(five0fmt, int(g)),
fmt.Sprintf(five0fmt, int(b)),
fmt.Sprintf(five0fmt, int(a)),
}, ".")
}
func ColorAverageImage(i image.Image) color.Color {
var r, g, b uint64
bounds := i.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
pr, pg, pb, _ := i.At(x, y).RGBA()
r += uint64(pr * pr)
g += uint64(pg * pg)
b += uint64(pb * pg)
}
}
d := uint64(bounds.Dy() * bounds.Dx())
r /= d
g /= d
b /= d
return color.RGBA{
uint8(math.Sqrt(float64(r)) / 0x101),
uint8(math.Sqrt(float64(g)) / 0x101),
uint8(math.Sqrt(float64(b)) / 0x101),
uint8(255)}
}
/*
https://jimsaunders.net/2015/05/22/manipulating-colors-in-go.html
https://sighack.com/post/averaging-rgb-colors-the-right-way
*/
func ColorAverage(c ...color.Color) color.Color {
if len(c) == 0 {
return color.Black
}
var r, g, b uint64
for _, ci := range c {
pr, pg, pb, _ := ci.RGBA()
r += uint64(pr * pr)
g += uint64(pg * pg)
b += uint64(pb * pg)
}
d := uint64(len(c))
r /= d
g /= d
b /= d
return color.RGBA{
uint8(math.Sqrt(float64(r)) / 0x101),
uint8(math.Sqrt(float64(g)) / 0x101),
uint8(math.Sqrt(float64(b)) / 0x101),
uint8(255)}
}