-
-
Notifications
You must be signed in to change notification settings - Fork 172
/
colorfinder.go
201 lines (165 loc) · 4.45 KB
/
colorfinder.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
198
199
200
201
package colorfinder
// colorfinder takes an image and tries to find its main color.
// It is a liberal port of
// https://pieroxy.net/blog/pages/color-finder/demo.html
import (
"fmt"
"image"
"io"
"log"
"math"
"net/http"
"os"
"strings"
"image/color"
// Load supported image formats
_ "image/gif"
_ "image/jpeg"
_ "image/png"
_ "github.com/mat/besticon/v3/ico"
)
func main() {
arg := os.Args[1]
var imageReader io.ReadCloser
if strings.HasPrefix(arg, "http") {
var err error
response, err := http.Get(arg)
if err != nil {
log.Fatal(err)
}
imageReader = response.Body
} else {
var err error
fmt.Fprintln(os.Stderr, "Reading "+arg+"...")
imageReader, err = os.Open(arg)
if err != nil {
log.Fatal(err)
}
}
defer imageReader.Close()
img, _, err := image.Decode(imageReader)
if err != nil {
log.Fatal(err)
}
cf := ColorFinder{}
c, err := cf.FindMainColor(img)
if err != nil {
log.Fatal(err)
}
fmt.Println("#" + ColorToHex(c))
}
type ColorFinder struct {
img image.Image
}
// FindMainColor tries to identify the most important color in the given logo.
func (cf *ColorFinder) FindMainColor(img image.Image) (color.RGBA, error) {
cf.img = img
colorMap := cf.buildColorMap()
sRGB := cf.findMainColor(colorMap, 6, nil)
sRGB = cf.findMainColor(colorMap, 4, &sRGB)
sRGB = cf.findMainColor(colorMap, 2, &sRGB)
sRGB = cf.findMainColor(colorMap, 0, &sRGB)
return sRGB.rgb, nil
}
const sampleThreshold = 160 * 160
func (cf *ColorFinder) buildColorMap() *map[color.RGBA]colorStats {
colorMap := make(map[color.RGBA]colorStats)
bounds := cf.img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
r, g, b, a := cf.img.At(x, y).RGBA()
rgb := color.RGBA{}
rgb.R = uint8(r >> shiftRGB)
rgb.G = uint8(g >> shiftRGB)
rgb.B = uint8(b >> shiftRGB)
rgb.A = uint8(a >> shiftRGB)
colrStats, exist := colorMap[rgb]
if exist {
colrStats.count++
} else {
colrStats := colorStats{count: 1, weight: weight(&rgb)}
if colrStats.weight <= 0 {
colrStats.weight = 1e-10
}
colorMap[rgb] = colrStats
}
}
}
return &colorMap
}
// Turns out using this is faster than using
// RGBAModel.Convert(img.At(x, y))).(color.RGBA)
const shiftRGB = uint8(8)
func (cf *ColorFinder) findMainColor(colorMap *map[color.RGBA]colorStats, shift uint, targetColor *shiftedRGBA) shiftedRGBA {
colorWeights := make(map[shiftedRGBA]float64)
bounds := cf.img.Bounds()
stepLength := stepLength(bounds)
for y := bounds.Min.Y; y < bounds.Max.Y; y += stepLength {
for x := bounds.Min.X; x < bounds.Max.X; x += stepLength {
r, g, b, a := cf.img.At(x, y).RGBA()
color := color.RGBA{}
color.R = uint8(r >> shiftRGB)
color.G = uint8(g >> shiftRGB)
color.B = uint8(b >> shiftRGB)
color.A = uint8(a >> shiftRGB)
if rgbMatchesTargetColor(targetColor, &color) {
increaseColorWeight(&colorWeights, colorMap, &color, shift)
}
}
}
maxColor := shiftedRGBA{}
maxWeight := 0.0
for sRGB, weight := range colorWeights {
if weight > maxWeight {
maxColor = sRGB
maxWeight = weight
}
}
return maxColor
}
func increaseColorWeight(weightedColors *map[shiftedRGBA]float64, colorMap *map[color.RGBA]colorStats, rgb *color.RGBA, shift uint) {
shiftedColor := color.RGBA{R: rgb.R >> shift, G: rgb.G >> shift, B: rgb.B >> shift}
pixelGroup := shiftedRGBA{rgb: shiftedColor, shift: shift}
colorStats := (*colorMap)[*rgb]
(*weightedColors)[pixelGroup] += colorStats.weight * float64(colorStats.count)
}
type shiftedRGBA struct {
rgb color.RGBA
shift uint
}
func rgbMatchesTargetColor(targetCol *shiftedRGBA, rgb *color.RGBA) bool {
if targetCol == nil {
return true
}
return targetCol.rgb.R == (rgb.R>>targetCol.shift) &&
targetCol.rgb.G == (rgb.G>>targetCol.shift) &&
targetCol.rgb.B == (rgb.B>>targetCol.shift)
}
type colorStats struct {
weight float64
count int64
}
func stepLength(bounds image.Rectangle) int {
width := bounds.Dx()
height := bounds.Dy()
pixelCount := width * height
var stepLength int
if pixelCount > sampleThreshold {
stepLength = 2
} else {
stepLength = 1
}
return stepLength
}
func weight(rgb *color.RGBA) float64 {
rr := float64(rgb.R)
gg := float64(rgb.G)
bb := float64(rgb.B)
return (abs(rr-gg)*abs(rr-gg)+abs(rr-bb)*abs(rr-bb)+abs(gg-bb)*abs(gg-bb))/65535.0*1000.0 + 1
}
func abs(n float64) float64 {
return math.Abs(float64(n))
}
func ColorToHex(c color.RGBA) string {
return fmt.Sprintf("%02x%02x%02x", c.R, c.G, c.B)
}