2020-02-26 08:39:38 -05:00
|
|
|
package d2dat
|
|
|
|
|
2021-02-08 03:49:43 -05:00
|
|
|
import (
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
)
|
2020-07-05 13:01:44 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
// index offset helpers
|
|
|
|
b = iota
|
|
|
|
g
|
|
|
|
r
|
|
|
|
o
|
|
|
|
)
|
|
|
|
|
2020-06-28 22:32:34 -04:00
|
|
|
// Load loads a DAT file.
|
2020-07-05 13:01:44 -04:00
|
|
|
func Load(data []byte) (d2interface.Palette, error) {
|
2020-02-26 08:39:38 -05:00
|
|
|
palette := &DATPalette{}
|
|
|
|
|
|
|
|
for i := 0; i < 256; i++ {
|
2020-07-05 13:01:44 -04:00
|
|
|
// offsets look like i*3+n, where n is 0,1,2
|
|
|
|
palette.colors[i] = &DATColor{b: data[i*o+b], g: data[i*o+g], r: data[i*o+r]}
|
2020-02-26 08:39:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return palette, nil
|
|
|
|
}
|
2021-02-08 03:49:43 -05:00
|
|
|
|
|
|
|
// Marshal encodes data palettte back into byte slice
|
2021-02-08 04:03:34 -05:00
|
|
|
func (p *DATPalette) Marshal() []byte {
|
|
|
|
result := make([]byte, len(p.colors))
|
2021-02-08 03:49:43 -05:00
|
|
|
|
2021-02-08 04:03:34 -05:00
|
|
|
for _, i := range &p.colors {
|
2021-02-08 03:49:43 -05:00
|
|
|
result = append(result, i.B(), i.G(), i.R())
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|