-
Notifications
You must be signed in to change notification settings - Fork 0
/
colours.go
158 lines (121 loc) · 2.72 KB
/
colours.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
package colours
import (
"errors"
"fmt"
"github.com/lucasb-eyer/go-colorful"
"image"
"strings"
)
type Colour interface {
Name() string
Hex() string
Reference() string
Closest() []Colour
AppendClosest(Colour) error // I don't love this... (20180605/thisisaaronland)
String() string
}
type Palette interface {
Reference() string
Colours() []Colour
}
type Extruder interface {
Colours(image.Image, int) ([]Colour, error)
}
type Grid interface {
Closest(Colour, Palette) (Colour, error)
}
type CommonColour struct {
Colour `json:",omitempty"`
CommonName string `json:"name,omitempty"`
CommonHex string `json:"hex"`
CommonReference string `json:"reference,omitempty"`
CommonClosest []Colour `json:"closest,omitempty"`
}
func (hc *CommonColour) Name() string {
return hc.CommonName
}
func (hc *CommonColour) Hex() string {
return hc.CommonHex
}
func (hc *CommonColour) Reference() string {
return hc.CommonReference
}
func (hc *CommonColour) AppendClosest(c Colour) error {
if hc.CommonClosest == nil {
hc.CommonClosest = make([]Colour, 0)
}
hc.CommonClosest = append(hc.CommonClosest, c)
return nil
}
func (hc *CommonColour) String() string {
name := hc.Name()
hex := hc.Hex()
ref := hc.Reference()
if name == hex {
return hex
}
return fmt.Sprintf("%s (%s, %s)", hex, name, ref)
}
func NewColour(args ...interface{}) (Colour, error) {
hex := "###"
name := "unknown"
ref := "unknown"
var closest []Colour
var err error
switch len(args) {
case 0:
err = errors.New("Insuffient arguments")
case 1:
hex = args[0].(string)
case 2:
hex = args[0].(string)
name = args[1].(string)
case 3:
hex = args[0].(string)
name = args[1].(string)
ref = args[2].(string)
default:
hex = args[0].(string)
name = args[1].(string)
ref = args[2].(string)
closest = make([]Colour, 0)
for i, c := range args[3:] {
closest[i] = c.(Colour)
}
}
if err != nil {
return nil, err
}
if !strings.HasPrefix(hex, "#") {
hex = fmt.Sprintf("#%s", hex)
}
_, err = colorful.Hex(hex)
if err != nil {
return nil, err
}
hc := CommonColour{
CommonName: name,
CommonHex: hex,
CommonReference: ref,
CommonClosest: closest,
}
return &hc, nil
}
type CommonPalette struct {
Palette `json:",omitempty"`
CommonReference string `json:"reference"`
CommonColours []*CommonColour `json:"colours,omitempty"`
}
func (p *CommonPalette) Reference() string {
return p.CommonReference
}
func (p *CommonPalette) Colours() []Colour {
// Y DO I NEED TO DOOOOOOOOOOOOOOOOOOO THIS???
// Y U SO WEIRD GOOOOOOOOOOOOOOOO????
// (20180605/thisisaaronland)
c := make([]Colour, 0)
for _, pc := range p.CommonColours {
c = append(c, pc)
}
return c
}