-
Notifications
You must be signed in to change notification settings - Fork 7
/
TilePaletteKey.go
62 lines (53 loc) · 1.43 KB
/
TilePaletteKey.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
package compression
import "math/bits"
type tilePaletteKey struct {
usedColors [4]uint64
size int
}
func tilePaletteKeyFrom(colors []byte) tilePaletteKey {
var key tilePaletteKey
for _, c := range colors {
key.useColor(c)
}
return key
}
func (key *tilePaletteKey) buffer() []byte {
result := make([]byte, 0, key.size)
for i := 0; i < 256; i++ {
if key.hasColor(byte(i)) {
result = append(result, byte(i))
}
}
return result
}
func (key *tilePaletteKey) useColor(index byte) {
if !key.hasColor(index) {
key.usedColors[index/64] |= 1 << uint(index%64)
key.size++
}
}
func (key *tilePaletteKey) hasColor(index byte) bool {
return (key.usedColors[index/64] & (1 << uint(index%64))) != 0
}
func (key *tilePaletteKey) contains(other *tilePaletteKey) bool {
return ((^key.usedColors[0] & other.usedColors[0]) == 0) &&
((^key.usedColors[1] & other.usedColors[1]) == 0) &&
((^key.usedColors[2] & other.usedColors[2]) == 0) &&
((^key.usedColors[3] & other.usedColors[3]) == 0)
}
func (key *tilePaletteKey) without(other *tilePaletteKey) tilePaletteKey {
var result tilePaletteKey
for i := 0; i < 4; i++ {
result.usedColors[i] = key.usedColors[i] & ^other.usedColors[i]
result.size += bits.OnesCount64(result.usedColors[i])
}
return result
}
func (key *tilePaletteKey) lessThan(other *tilePaletteKey) bool {
for i := 0; i < 4; i++ {
if key.usedColors[i] < other.usedColors[i] {
return true
}
}
return false
}