forked from viamrobotics/rdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_utils.go
118 lines (100 loc) · 2.52 KB
/
color_utils.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
package rimage
import (
"html/template"
"image/color"
"os"
"sort"
"strings"
)
// ConvertToNRGBA converts a go color to an NRGBA color.
func ConvertToNRGBA(c color.Color) color.NRGBA {
return color.NRGBAModel.Convert(c).(color.NRGBA)
}
// TheColorModel represents our Color type as a model to be
// used for color conversions in color.Color.
type TheColorModel struct{}
// Convert converts the given color into our Color type but
// still returns it as a go color.
func (cm *TheColorModel) Convert(c color.Color) color.Color {
return NewColorFromColor(c)
}
// ColorDiff TODO.
type ColorDiff struct {
Left Color
Right Color
Diff float64
}
// ColorDiffs TODO.
type ColorDiffs struct {
all []ColorDiff
seenCombos map[uint64]bool // this a + b for now, which is wrong, but..
}
// Len returns the number of diffs.
func (x *ColorDiffs) Len() int {
return len(x.all)
}
// Less returns if one diff is less than another based on its diff value.
func (x *ColorDiffs) Less(i, j int) bool {
return x.all[i].Diff < x.all[j].Diff
}
// Swap swaps two diffs positionally.
func (x *ColorDiffs) Swap(i, j int) {
x.all[i], x.all[j] = x.all[j], x.all[i]
}
// Sort sorts the diffs based on satisfying the Sort interface above.
func (x *ColorDiffs) Sort() {
sort.Sort(x)
}
// AddD TODO.
func (x *ColorDiffs) AddD(a, b Color, d float64) {
if x.seenCombos == nil {
x.seenCombos = map[uint64]bool{}
}
t := uint64(a) + uint64(b)
if x.seenCombos[t] {
return
}
x.all = append(x.all, ColorDiff{a, b, d})
x.seenCombos[t] = true
}
// Add TODO.
func (x *ColorDiffs) Add(a, b Color) {
d := a.Distance(b)
x.AddD(a, b, d)
}
func (x *ColorDiffs) output() string {
t := "<html><body><table>" +
"{{ range .}}" +
"<tr>" +
"<td style='background-color:{{.Left.Hex}}'>{{ .Left.Hex }} </td>" +
"<td style='background-color:{{.Right.Hex}}'>{{ .Right.Hex }} </td>" +
"<td>{{ .Diff }}</td>" +
"</tr>" +
"{{end}}" +
"</table></body></html>"
tt, err := template.New("temp").Parse(t)
if err != nil {
panic(err)
}
w := strings.Builder{}
err = tt.Execute(&w, x.all)
if err != nil {
panic(err)
}
return w.String()
}
// WriteTo writes the diff information out to a file.
func (x *ColorDiffs) WriteTo(fn string) error {
//nolint:gosec
return os.WriteFile(fn, []byte(x.output()), 0o640)
}
// ComputeColorDiffs computes the different between the all of the colors given.
func ComputeColorDiffs(all []Color) ColorDiffs {
diffs := ColorDiffs{}
for i, c := range all {
for _, c2 := range all[i+1:] {
diffs.Add(c, c2)
}
}
return diffs
}