diff --git a/apgbpal.go b/apgbpal.go index 2a30e50..4f1ca4e 100644 --- a/apgbpal.go +++ b/apgbpal.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "os" + "strconv" "strings" ) @@ -15,11 +16,11 @@ type RGBA struct { } 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) { - 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) } big, err := strconv.ParseUint(str[index:index+1], 16, 8) @@ -29,7 +30,7 @@ func HexByteAt(str string, index int) (byte, error) { return byte(big), nil } -func MustParseRGBA(str string) RGBA { +func ParseRGBA(str string) RGBA, error { s := strings.TrimSpace(str) s = strings.ToLower(s) s, _ = strings.CutPrefix(s, "#") @@ -41,20 +42,22 @@ func MustParseRGBA(str string) RGBA { var err error ret.A, err = HexByteAt(s, 6) if err != nil { - log.Fatalf("can't parse alpha channel: %v", err) + return RGBA{}, fmt.Errorf("can't parse alpha channel: %v", err) } 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 { - log.Fatalf("can't parse red channel: %v", err) + var err error + 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 { - log.Fatalf("can't parse green channel: %v", err) + if ret.G, err = HexByteAt(s, 2); err != nil { + return RGBA{}, fmt.Errorf("can't parse green channel: %v", err) } - if ret.B, err := HexByteAt(s, 4); err != nil { - log.Fatalf("can't parse blue channel: %v", err) + if ret.B, err = HexByteAt(s, 4); err != nil { + return RGBA{}, fmt.Errorf("can't parse blue channel: %v", err) } + return ret, nil } type BGPal [4]RGBA @@ -80,7 +83,7 @@ func MustParseBGPal(strs []string) BGPal { } var ret BGPal for i := 0; i < 4; i++ { - ret[i], err := MustParseRGBA(strs[i]) + ret[i], err := ParseRGBA(strs[i]) if err != nil { log.Fatalf("MustParseBGPal can't parse at %d: %v", i, err) } @@ -109,7 +112,7 @@ func (o OBJPal) Bytes(indexchar byte) [18]byte { func MustParseOBJPal(strs []string) OBJPal { if len(strs) == 4 { - log.Info("Discarding index 0 of OBJ pal") + log.Println("Discarding index 0 of OBJ pal") strs = strs[1:] } if len(strs) != 3 { @@ -117,7 +120,7 @@ func MustParseOBJPal(strs []string) OBJPal { } var ret OBJPal for i := 0; i < 3; i++ { - ret[i], err := MustParseRGBA(strs[i]) + ret[i], err := ParseRGBA(strs[i]) if err != nil { log.Fatalf("MustParseOBJPal can't parse at %d: %v", i, err) }