-
Notifications
You must be signed in to change notification settings - Fork 8
/
slice_matrix.go
66 lines (61 loc) · 1.36 KB
/
slice_matrix.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
package colors
import (
"fmt"
"image/color"
"sort"
)
func MatrixColumn(m [][]color.Color, colIdx uint) ([]color.Color, error) {
colIdxInt := int(colIdx)
clrs := []color.Color{}
for i, row := range m {
if colIdxInt >= len(row) {
return clrs, fmt.Errorf("col [%d] not found on row [%d]", colIdx, i)
}
clrs = append(clrs, row[colIdxInt])
}
return clrs, nil
}
func MatrixUnique(c [][]color.Color) []color.Color {
if len(c) == 0 {
return []color.Color{}
}
uniques := map[string]color.Color{}
keys := []string{}
for _, row := range c {
for _, cx := range row {
cxs := ColorString(cx)
if _, ok := uniques[cxs]; !ok {
uniques[cxs] = cx
keys = append(keys, cxs)
}
}
}
return uniqueColorsToSlice(keys, uniques)
}
func SliceUnique(c []color.Color) []color.Color {
if len(c) == 0 {
return c
}
uniques := map[string]color.Color{}
keys := []string{}
for _, cx := range c {
cxs := ColorString(cx)
if _, ok := uniques[cxs]; !ok {
uniques[cxs] = cx
keys = append(keys, cxs)
}
}
return uniqueColorsToSlice(keys, uniques)
}
func uniqueColorsToSlice(keys []string, uniques map[string]color.Color) []color.Color {
sort.Strings(keys)
cslice := []color.Color{}
for _, cxs := range keys {
if c, ok := uniques[cxs]; ok {
cslice = append(cslice, c)
} else {
panic(fmt.Sprintf("color [%s] not found", cxs))
}
}
return cslice
}