-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolor.go
executable file
·310 lines (251 loc) · 6.43 KB
/
color.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package ase
import (
"bytes"
"encoding/binary"
"errors"
"io"
"strings"
"unicode/utf16"
)
var (
ErrInvalidColorType = errors.New("ase: invalid color type")
ErrInvalidColorValue = errors.New("ase: invalid color value")
ErrInvalidColorModel = errors.New("ase: invalid color model")
)
type Color struct {
nameLen uint16
Name string
Model string // CMYK, RGB, LAB or Gray
Values []float32
Type string // Global, Spot, Normal
}
// Decode an ASE color.
func (color *Color) read(r io.Reader) (err error) {
if err = color.readNameLen(r); err != nil {
return
}
if err = color.readName(r); err != nil {
return
}
if err = color.readModel(r); err != nil {
return
}
if err = color.readValues(r); err != nil {
return
}
if err = color.readType(r); err != nil {
return
}
return
}
// Decode the color's name length.
func (color *Color) readNameLen(r io.Reader) error {
return binary.Read(r, binary.BigEndian, &color.nameLen)
}
// Decode the color's name.
func (color *Color) readName(r io.Reader) (err error) {
if color.nameLen == 0 {
return
}
// make array for our color name based on block length
name := make([]uint16, color.nameLen) // assumes the nameLen was already defined.
if err = binary.Read(r, binary.BigEndian, &name); err != nil {
return
}
// decode our name. we trim off the last byte since it's zero terminated
// utf16.Decode returns a slice of runes from an input of []uint16
color.Name = string(utf16.Decode(name[:len(name)-1]))
return
}
// Decode the color's model.
func (color *Color) readModel(r io.Reader) (err error) {
// make array for our color model, where four is the max possible
// amount of characters (RGB, LAB, CMYK, Gray).
colorModel := make([]uint8, 4)
if err = binary.Read(r, binary.BigEndian, colorModel); err != nil {
return
}
// Assign the string version of the `colorModel`
color.Model = strings.TrimSpace(string(colorModel[0:]))
return
}
// Decode the color's values.
func (color *Color) readValues(r io.Reader) (err error) {
switch color.Model {
case "RGB":
rgb := make([]float32, 3)
// read into rbg array
if err = binary.Read(r, binary.BigEndian, &rgb); err != nil {
return
}
color.Values = rgb
break
case "LAB":
lab := make([]float32, 3)
// read into lab array
if err = binary.Read(r, binary.BigEndian, &lab); err != nil {
return
}
color.Values = lab
break
case "CMYK":
cmyk := make([]float32, 4)
// read into cmyk array
if err = binary.Read(r, binary.BigEndian, &cmyk); err != nil {
return
}
color.Values = cmyk
break
case "Gray":
gray := make([]float32, 1)
// read into gray array
if err = binary.Read(r, binary.BigEndian, &gray); err != nil {
return
}
color.Values = gray
break
default:
return ErrInvalidColorValue
}
return
}
// Decode the color's type.
func (color *Color) readType(r io.Reader) (err error) {
colorType := make([]int16, 1)
// read into colorType array
if err = binary.Read(r, binary.BigEndian, colorType); err != nil {
return
}
switch colorType[0] {
case 0:
color.Type = "Global"
break
case 1:
color.Type = "Spot"
break
case 2:
color.Type = "Normal"
break
default:
return ErrInvalidColorType
}
return
}
// Encodes a color's attributes according to the ASE specification.
func (color *Color) write(w io.Writer) (err error) {
// Write the block type
if err = color.writeBlockType(w); err != nil {
return
}
// Write the block length
if err = color.writeBlockLength(w); err != nil {
return
}
// Write the color data
if err = color.writeNameLen(w); err != nil {
return
}
if err = color.writeName(w); err != nil {
return
}
if err = color.writeModel(w); err != nil {
return
}
if err = color.writeValues(w); err != nil {
return
}
if err = color.writeType(w); err != nil {
return
}
return
}
// Write color's block length as a part of the ASE encoding.
func (color *Color) writeBlockLength(w io.Writer) (err error) {
blockLength, err := color.calculateBlockLength()
if err != nil {
return
}
if err = binary.Write(w, binary.BigEndian, blockLength); err != nil {
return
}
return
}
// Calculates the block length to be written based on the color's attributes.
func (color *Color) calculateBlockLength() (blockLength int32, err error) {
buf := new(bytes.Buffer)
if err = color.writeNameLen(buf); err != nil {
return
}
if err = color.writeName(buf); err != nil {
return
}
if err = color.writeModel(buf); err != nil {
return
}
if err = color.writeValues(buf); err != nil {
return
}
if err = color.writeType(buf); err != nil {
return
}
blockLength = int32(buf.Len())
return
}
// Encode the color's name length.
func (color *Color) writeNameLen(w io.Writer) (err error) {
// Adding one to the name length accounts for the zero-terminated character.
return binary.Write(w, binary.BigEndian, color.NameLen()+1)
}
// Encode the color's name as a slice of uint16.
func (color *Color) writeName(w io.Writer) (err error) {
name := utf16.Encode([]rune(color.Name))
name = append(name, uint16(0))
return binary.Write(w, binary.BigEndian, name)
}
// Encode the color's model as a of slice of uint8.
func (color *Color) writeModel(w io.Writer) (err error) {
model := make([]uint8, 4)
// Populate model with the uint8 version of the each character in the string.
for i, _ := range model {
colorModel := color.Model
// Our iterator over model needs to match the number of elements in color.Model.
// If color.Model has a length less than four, append an empty space.
if len(color.Model) < 4 {
for len(colorModel) < 4 {
colorModel += " "
}
}
model[i] = uint8([]rune(colorModel)[i])
}
return binary.Write(w, binary.BigEndian, model)
}
// Encode color's values.
func (color *Color) writeValues(w io.Writer) (err error) {
return binary.Write(w, binary.BigEndian, color.Values)
}
// Encode the color's type.
func (color *Color) writeType(w io.Writer) (err error) {
var colorType int16
switch color.Type {
case "Global":
colorType = 0
break
case "Spot":
colorType = 1
break
case "Normal":
colorType = 2
break
default:
return ErrInvalidColorType
}
return binary.Write(w, binary.BigEndian, []int16{colorType})
}
// Helper function that returns the length of a color's name.
func (color *Color) NameLen() uint16 {
return uint16(len(color.Name))
}
// Write color's block header as a part of the ASE encoding.
func (color *Color) writeBlockType(w io.Writer) (err error) {
return binary.Write(w, binary.BigEndian, colorEntry)
}