Compare commits
6 Commits
e3443d57f5
...
9318a0f46c
Author | SHA1 | Date | |
---|---|---|---|
9318a0f46c | |||
8365ee96cd | |||
79dfe7b85d | |||
d78344808a | |||
8bf78d1cc6 | |||
3bd611feab |
93
apgbpal.go
93
apgbpal.go
@ -1,9 +1,32 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
// Binary apgbpal creates a 56-byte file named "test.pal" in the current working
|
||||||
|
// directory. I hope it might be an Analogue Pocket GB Palette file, but I don't
|
||||||
|
// know that for sure yet.
|
||||||
|
|
||||||
|
// apgbpal is licensed under 0BSD.
|
||||||
|
//
|
||||||
|
// Zero-Clause BSD
|
||||||
|
// =============
|
||||||
|
//
|
||||||
|
// Permission to use, copy, modify, and/or distribute this software for
|
||||||
|
// any purpose with or without fee is hereby granted.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
|
||||||
|
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
||||||
|
// OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
|
||||||
|
// FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
|
||||||
|
// DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
|
||||||
|
// AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
||||||
|
// OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RGBA struct {
|
type RGBA struct {
|
||||||
@ -14,11 +37,11 @@ type RGBA struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c RGBA) Bytes() [4]byte {
|
func (c RGBA) Bytes() [4]byte {
|
||||||
return [4]byte {c.R, c.G, cB, c.A}
|
return [4]byte{c.R, c.G, c.B, c.A}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HexByteAt(str string, index int) (byte, error) {
|
func HexByteAt(str string, index int) (byte, error) {
|
||||||
if len(string) < index + 2 {
|
if len(str) < index+2 {
|
||||||
return 0, fmt.Errorf("no hex byte at %d in too short string %q", index, str)
|
return 0, fmt.Errorf("no hex byte at %d in too short string %q", index, str)
|
||||||
}
|
}
|
||||||
big, err := strconv.ParseUint(str[index:index+1], 16, 8)
|
big, err := strconv.ParseUint(str[index:index+1], 16, 8)
|
||||||
@ -28,7 +51,7 @@ func HexByteAt(str string, index int) (byte, error) {
|
|||||||
return byte(big), nil
|
return byte(big), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func MustParseRGBA(str string) RGBA {
|
func ParseRGBA(str string) (RGBA, error) {
|
||||||
s := strings.TrimSpace(str)
|
s := strings.TrimSpace(str)
|
||||||
s = strings.ToLower(s)
|
s = strings.ToLower(s)
|
||||||
s, _ = strings.CutPrefix(s, "#")
|
s, _ = strings.CutPrefix(s, "#")
|
||||||
@ -40,20 +63,22 @@ func MustParseRGBA(str string) RGBA {
|
|||||||
var err error
|
var err error
|
||||||
ret.A, err = HexByteAt(s, 6)
|
ret.A, err = HexByteAt(s, 6)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("can't parse alpha channel: %v", err)
|
return RGBA{}, fmt.Errorf("can't parse alpha channel: %v", err)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
log.Fatalf("%q is not an RGBA value: wrong length after trimming", str)
|
return RGBA{}, fmt.Errorf("%q is not an RGBA value: wrong length after trimming", str)
|
||||||
}
|
}
|
||||||
if ret.R, err := HexByteAt(s, 0); err != nil {
|
var err error
|
||||||
log.Fatalf("can't parse red channel: %v", err)
|
if ret.R, err = HexByteAt(s, 0); err != nil {
|
||||||
|
return RGBA{}, fmt.Errorf("can't parse red channel: %v", err)
|
||||||
}
|
}
|
||||||
if ret.G, err := HexByteAt(s, 2); err != nil {
|
if ret.G, err = HexByteAt(s, 2); err != nil {
|
||||||
log.Fatalf("can't parse green channel: %v", err)
|
return RGBA{}, fmt.Errorf("can't parse green channel: %v", err)
|
||||||
}
|
}
|
||||||
if ret.B, err := HexByteAt(s, 4); err != nil {
|
if ret.B, err = HexByteAt(s, 4); err != nil {
|
||||||
log.Fatalf("can't parse blue channel: %v", err)
|
return RGBA{}, fmt.Errorf("can't parse blue channel: %v", err)
|
||||||
}
|
}
|
||||||
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type BGPal [4]RGBA
|
type BGPal [4]RGBA
|
||||||
@ -66,7 +91,7 @@ func (b BGPal) Bytes() [20]byte {
|
|||||||
for c := 0; c < 4; c++ {
|
for c := 0; c < 4; c++ {
|
||||||
bb := b[c].Bytes()
|
bb := b[c].Bytes()
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
ret[3+i+4*c]=bb[i]
|
ret[3+i+4*c] = bb[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret[19] = '\n'
|
ret[19] = '\n'
|
||||||
@ -79,7 +104,8 @@ func MustParseBGPal(strs []string) BGPal {
|
|||||||
}
|
}
|
||||||
var ret BGPal
|
var ret BGPal
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
ret[i], err := MustParseRGBA(strs[i])
|
var err error
|
||||||
|
ret[i], err = ParseRGBA(strs[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("MustParseBGPal can't parse at %d: %v", i, err)
|
log.Fatalf("MustParseBGPal can't parse at %d: %v", i, err)
|
||||||
}
|
}
|
||||||
@ -99,20 +125,25 @@ func (o OBJPal) Bytes(indexchar byte) [18]byte {
|
|||||||
for c := 0; c < 3; c++ {
|
for c := 0; c < 3; c++ {
|
||||||
oo := o[c].Bytes()
|
oo := o[c].Bytes()
|
||||||
for i := 0; i < 4; i++ {
|
for i := 0; i < 4; i++ {
|
||||||
ret[5+i+4*c]=oo[i]
|
ret[5+i+4*c] = oo[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret[17] = '\n'
|
ret[17] = '\n'
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func MustParseOBJPal {
|
func MustParseOBJPal(strs []string) OBJPal {
|
||||||
|
if len(strs) == 4 {
|
||||||
|
log.Println("Discarding index 0 of OBJ pal")
|
||||||
|
strs = strs[1:]
|
||||||
|
}
|
||||||
if len(strs) != 3 {
|
if len(strs) != 3 {
|
||||||
log.Fatalf("MustParseObjPal requires length 3, got %d", len(strs))
|
log.Fatalf("MustParseObjPal requires length 3, got %d", len(strs))
|
||||||
}
|
}
|
||||||
var ret OBJPal
|
var ret OBJPal
|
||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
ret[i], err := MustParseRGBA(strs[i])
|
var err error
|
||||||
|
ret[i], err = ParseRGBA(strs[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("MustParseOBJPal can't parse at %d: %v", i, err)
|
log.Fatalf("MustParseOBJPal can't parse at %d: %v", i, err)
|
||||||
}
|
}
|
||||||
@ -121,7 +152,7 @@ func MustParseOBJPal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type APGBPal struct {
|
type APGBPal struct {
|
||||||
BG BGPal
|
BG BGPal
|
||||||
OBJ0 OBJPal
|
OBJ0 OBJPal
|
||||||
OBJ1 OBJPal
|
OBJ1 OBJPal
|
||||||
}
|
}
|
||||||
@ -140,3 +171,27 @@ func (a APGBPal) Bytes() [56]byte {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
f, err := os.OpenFile(
|
||||||
|
"test.pal",
|
||||||
|
os.O_WRONLY|os.O_CREATE|os.O_EXCL,
|
||||||
|
0644)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't create test.pal: %v", err)
|
||||||
|
}
|
||||||
|
aUpPal := APGBPal{
|
||||||
|
BG: MustParseBGPal([]string{"ffffff", "ff8484", "943a3a", "000000"}),
|
||||||
|
OBJ0: MustParseOBJPal([]string{"7bff31", "008400", "000000"}),
|
||||||
|
OBJ1: MustParseOBJPal([]string{"63a5ff", "0000ff", "000000"}),
|
||||||
|
}
|
||||||
|
palb := aUpPal.Bytes()
|
||||||
|
n, err := f.Write(palb[:])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("can't save test.pal: %v", err)
|
||||||
|
}
|
||||||
|
if n != 56 {
|
||||||
|
log.Fatalf("unexpected write length: %d", n)
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user