1+ // Package aseprite provides types and utilities for interacting with Aseprite.
2+ package aseprite
3+
4+ import (
5+ "fmt"
6+ "regexp"
7+ "strconv"
8+ "strings"
9+ )
10+
11+ // Color represents an RGBA color value.
12+ type Color struct {
13+ R uint8 `json:"r"`
14+ G uint8 `json:"g"`
15+ B uint8 `json:"b"`
16+ A uint8 `json:"a"`
17+ }
18+
19+ var hexColorPattern = regexp .MustCompile (`^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$` )
20+
21+ // NewColor creates a new Color with the specified RGBA values.
22+ func NewColor (r , g , b , a uint8 ) Color {
23+ return Color {R : r , G : g , B : b , A : a }
24+ }
25+
26+ // NewColorRGB creates a new opaque Color with the specified RGB values.
27+ func NewColorRGB (r , g , b uint8 ) Color {
28+ return Color {R : r , G : g , B : b , A : 255 }
29+ }
30+
31+ // FromHex parses a hex color string in the format "#RRGGBB" or "#RRGGBBAA".
32+ // The "#" prefix is optional.
33+ func (c * Color ) FromHex (hex string ) error {
34+ hex = strings .TrimPrefix (hex , "#" )
35+
36+ if ! hexColorPattern .MatchString ("#" + hex ) {
37+ return fmt .Errorf ("invalid hex color format: %q (expected #RRGGBB or #RRGGBBAA)" , hex )
38+ }
39+
40+ // Parse RGB
41+ r , _ := strconv .ParseUint (hex [0 :2 ], 16 , 8 )
42+ g , _ := strconv .ParseUint (hex [2 :4 ], 16 , 8 )
43+ b , _ := strconv .ParseUint (hex [4 :6 ], 16 , 8 )
44+
45+ c .R = uint8 (r )
46+ c .G = uint8 (g )
47+ c .B = uint8 (b )
48+
49+ // Parse alpha if present
50+ if len (hex ) == 8 {
51+ a , _ := strconv .ParseUint (hex [6 :8 ], 16 , 8 )
52+ c .A = uint8 (a )
53+ } else {
54+ c .A = 255 // Opaque by default
55+ }
56+
57+ return nil
58+ }
59+
60+ // ToHex converts the color to a hex string in the format "#RRGGBBAA".
61+ func (c Color ) ToHex () string {
62+ return fmt .Sprintf ("#%02X%02X%02X%02X" , c .R , c .G , c .B , c .A )
63+ }
64+
65+ // ToHexRGB converts the color to a hex string in the format "#RRGGBB" (ignoring alpha).
66+ func (c Color ) ToHexRGB () string {
67+ return fmt .Sprintf ("#%02X%02X%02X" , c .R , c .G , c .B )
68+ }
69+
70+ // Point represents a 2D coordinate.
71+ type Point struct {
72+ X int `json:"x"`
73+ Y int `json:"y"`
74+ }
75+
76+ // Rectangle represents a rectangular region.
77+ type Rectangle struct {
78+ X int `json:"x"`
79+ Y int `json:"y"`
80+ Width int `json:"width"`
81+ Height int `json:"height"`
82+ }
83+
84+ // Pixel represents a single pixel with color and position.
85+ type Pixel struct {
86+ Point
87+ Color Color `json:"color"`
88+ }
89+
90+ // SpriteInfo contains metadata about a sprite.
91+ type SpriteInfo struct {
92+ Width int `json:"width"`
93+ Height int `json:"height"`
94+ ColorMode string `json:"color_mode"`
95+ FrameCount int `json:"frame_count"`
96+ LayerCount int `json:"layer_count"`
97+ Layers []string `json:"layers"`
98+ }
99+
100+ // ColorMode represents the color mode of a sprite.
101+ type ColorMode string
102+
103+ const (
104+ ColorModeRGB ColorMode = "rgb"
105+ ColorModeGrayscale ColorMode = "grayscale"
106+ ColorModeIndexed ColorMode = "indexed"
107+ )
108+
109+ // String returns the string representation of the color mode.
110+ func (cm ColorMode ) String () string {
111+ return string (cm )
112+ }
113+
114+ // ToLua returns the Lua constant for the color mode.
115+ func (cm ColorMode ) ToLua () string {
116+ switch cm {
117+ case ColorModeRGB :
118+ return "ColorMode.RGB"
119+ case ColorModeGrayscale :
120+ return "ColorMode.GRAYSCALE"
121+ case ColorModeIndexed :
122+ return "ColorMode.INDEXED"
123+ default :
124+ return "ColorMode.RGB"
125+ }
126+ }
0 commit comments