-
Notifications
You must be signed in to change notification settings - Fork 38
/
shortencolor.go
23 lines (21 loc) · 903 Bytes
/
shortencolor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package png2svg
// shortenColorLossy optimizes hexadecimal color strings in a lossy way
func shortenColorLossy(hexColorBytes []byte) []byte {
if len(hexColorBytes) != 7 { // Only accept colors in the format #aabbcc
return hexColorBytes
}
// Just keep the first digits of each 2 digit hex number
return []byte{'#', hexColorBytes[1], hexColorBytes[3], hexColorBytes[5]}
}
// shortenColorLossless optimizes hexadecimal color strings in a lossless way
func shortenColorLossless(hexColorBytes []byte) []byte {
if len(hexColorBytes) != 7 { // Only accept colors in the format #aabbcc
return hexColorBytes
}
// Check for lossless compression
if hexColorBytes[1] == hexColorBytes[2] && hexColorBytes[3] == hexColorBytes[4] && hexColorBytes[5] == hexColorBytes[6] {
return []byte{'#', hexColorBytes[1], hexColorBytes[3], hexColorBytes[5]}
}
// Return the original color
return hexColorBytes
}