-
Notifications
You must be signed in to change notification settings - Fork 7
/
TilePaletteKey.go
70 lines (61 loc) · 1.94 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
63
64
65
66
67
68
69
70
package compression
import "math/bits"
// TilePaletteKey marks use of colors of a 256 palette.
type TilePaletteKey struct {
usedColors [4]uint64
size int
}
// TilePaletteKeyFrom returns a key for the given slice of colors.
func TilePaletteKeyFrom(colors []byte) TilePaletteKey {
var key TilePaletteKey
for _, c := range colors {
key.UseColor(c)
}
return key
}
// Buffer returns the color indices marked in use by this 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
}
// UseColor marks the given color index in use.
func (key *TilePaletteKey) UseColor(index byte) {
if !key.HasColor(index) {
key.usedColors[index/64] |= 1 << uint(index%64)
key.size++
}
}
// HasColor returns true if the given color index is in use by this key.
func (key *TilePaletteKey) HasColor(index byte) bool {
return (key.usedColors[index/64] & (1 << uint(index%64))) != 0
}
// Contains returns true if this key is equal to, or a superset of, the given key.
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)
}
// Without returns a new key instance that has all remaining colors marked in use.
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
}
// LessThan is a sorting function to order keys.
func (key *TilePaletteKey) LessThan(other *TilePaletteKey) bool {
for i := 0; i < 4; i++ {
if key.usedColors[i] < other.usedColors[i] {
return true
}
}
return false
}