/
colors.go
191 lines (164 loc) · 3.67 KB
/
colors.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
package main
import (
"encoding/json"
"image"
"image/color"
"image/png"
"log"
"math"
"os"
"sort"
)
type FloatColor struct {
R, G, B float64
}
type IntColor struct {
R, G, B int
}
type Palette []IntColor
//===== FLOAT COLOR =======
func clipFloat(val float64) float64 {
if val > 1.0 {
return 1.0
}
if val < 0.0 {
return 0.0
}
return val
}
func (color FloatColor) Normalized() FloatColor {
return FloatColor{
clipFloat(color.R),
clipFloat(color.G),
clipFloat(color.B)}
}
func (color FloatColor) Distance(other FloatColor) float64 {
return (color.R-other.R)*(color.R-other.R) +
(color.G-other.G)*(color.G-other.G) +
(color.B-other.B)*(color.B-other.B)
}
func (color FloatColor) ToIntColor() IntColor {
norm := color.Normalized()
return IntColor{int(norm.R * 255), int(norm.G * 255), int(norm.B * 255)}
}
func (color FloatColor) LevelConvert(level uint, levelsTotal uint) FloatColor {
if level == levelsTotal {
return color
}
if level == 0 {
return FloatColor{0.0, 0.0, 0.0}
}
k := float64(level) / float64(levelsTotal)
return FloatColor{
color.R * k,
color.G * k,
color.B * k}
}
//===== INT COLOR =======
func clipInt(val int) int {
if val > 255 {
return 255
}
if val < 0 {
return 0
}
return val
}
func (color IntColor) Normalized() IntColor {
return IntColor{
clipInt(color.R),
clipInt(color.G),
clipInt(color.B)}
}
func (color IntColor) Distance(other IntColor) uint64 {
return (uint64(color.R)-uint64(other.R))*(uint64(color.R)-uint64(other.R)) +
(uint64(color.G)-uint64(other.G))*(uint64(color.G)-uint64(other.G)) +
(uint64(color.B)-uint64(other.B))*(uint64(color.B)-uint64(other.B))
}
func (color IntColor) Luma() float32 {
return 0.2126*float32(color.R) + 0.7152*float32(color.G) + 0.0722*float32(color.B)
}
func (color IntColor) ToFloatColor() FloatColor {
return FloatColor{float64(color.R) / 255.0, float64(color.G) / 255.0, float64(color.B) / 255.0}
}
//===== PALETTE =======
func NewPalette(colors int) Palette {
return make([]IntColor, colors)
}
func (pal Palette) Len() int {
return len(pal)
}
func (pal Palette) Less(i, j int) bool {
return pal[i].Luma() < pal[j].Luma()
}
func (pal Palette) Swap(i, j int) {
pal[i], pal[j] = pal[j], pal[i]
}
func (pal Palette) Sort() {
sort.Sort(pal)
}
func (pal *Palette) Save(filename string) {
pal.Sort()
fo, err := os.Create(filename)
if err != nil {
panic(err)
}
defer fo.Close()
data, err := json.MarshalIndent(pal, "", " ")
if err != nil {
panic(err)
}
_, err = fo.Write(data)
if err != nil {
panic(err)
}
}
func (pal Palette) GetIntColorIndex(color IntColor) (index int) {
var mindist uint64 = math.MaxUint64
index = 0
for i, c := range pal {
dist := color.Distance(c)
if dist < mindist {
mindist = dist
index = i
}
}
return
}
func (pal Palette) GetFloatColorIndex(color FloatColor) (index int) {
return pal.GetIntColorIndex(color.ToIntColor())
}
func PaletteLoad(filename string) Palette {
fi, err := os.ReadFile(filename)
if err != nil {
panic(err)
}
result := &Palette{}
err = json.Unmarshal(fi, result)
if err != nil {
panic(err)
}
return *result
}
func (pal Palette) SavePreview(width int, filename string) {
height := pal.Len()/width + 1
oimg := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{width, height}})
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
index := y*width + x
if index >= pal.Len() {
break
}
coli := pal[index]
oimg.SetRGBA(x, y, color.RGBA{uint8(coli.R), uint8(coli.G), uint8(coli.B), 255})
}
}
outf, err := os.Create(filename)
if err != nil {
panic(err)
}
defer outf.Close()
if err = png.Encode(outf, oimg); err != nil {
log.Printf("failed to encode: %v", err)
}
}