lazarus/formats/dat/dat.go

43 lines
712 B
Go
Raw Permalink Normal View History

2018-12-16 22:09:18 +00:00
package dat
import (
"encoding/binary"
"io"
2018-12-31 18:54:33 +00:00
"github.com/FooSoft/lazarus/math"
2018-12-16 22:09:18 +00:00
)
2019-01-09 02:45:19 +00:00
type Palette struct {
2018-12-31 18:54:33 +00:00
Colors [256]math.Color3b
}
type color struct {
B byte
G byte
R byte
2018-12-16 22:09:18 +00:00
}
2019-01-09 02:45:19 +00:00
func NewFromReader(reader io.Reader) (*Palette, error) {
var colors [256]color
if err := binary.Read(reader, binary.LittleEndian, &colors); err != nil {
2018-12-16 22:09:18 +00:00
return nil, err
}
2019-01-09 02:45:19 +00:00
palette := new(Palette)
for i, color := range colors {
2018-12-31 18:54:33 +00:00
palette.Colors[i] = math.Color3b{R: color.R, G: color.G, B: color.B}
}
return palette, nil
2018-12-16 22:09:18 +00:00
}
2019-01-09 02:45:19 +00:00
func NewFromGrayscale() *Palette {
palette := new(Palette)
for i := 0; i < 256; i++ {
value := uint8(i)
2018-12-31 18:54:33 +00:00
palette.Colors[i] = math.Color3b{R: value, G: value, B: value}
}
return palette
}