1// Package theme provides GTK CSS styling for UI components.
2package theme
3
4import (
5 "fmt"
6 "regexp"
7 "strings"
8
9 "github.com/bnema/dumber/internal/domain/entity"
10)
11
12// Palette holds semantic color tokens for theming.
13type Palette struct {
14 Background string // Main background color
15 Surface string // Elevated surfaces (cards, popups)
16 SurfaceVariant string // Secondary surfaces
17 Text string // Primary text color
18 Muted string // Secondary/disabled text
19 Accent string // Primary accent color (actions, highlights)
20 Border string // Border and divider lines
21 // Semantic status colors (not user-editable, derived defaults)
22 Success string // Success/positive feedback
23 Warning string // Warning/caution feedback
24 Destructive string // Error/destructive actions
25}
26
27// ModeColors holds colors for modal mode indicators (borders and toasters).
28type ModeColors struct {
29 PaneMode string // Color for pane mode (default: #4A90E2 blue)
30 TabMode string // Color for tab mode (default: #FFA500 orange)
31 SessionMode string // Color for session mode (default: #9B59B6 purple)
32 ResizeMode string // Color for resize mode (default: #00D4AA teal)
33}
34
35// DefaultModeColors returns the default mode indicator colors.
36func DefaultModeColors() ModeColors {
37 return ModeColors{
38 PaneMode: "#4A90E2",
39 TabMode: "#FFA500",
40 SessionMode: "#9B59B6",
41 ResizeMode: "#00D4AA",
42 }
43}
44
45// ToCSSVars generates CSS custom property declarations for mode colors.
46func (m ModeColors) ToCSSVars() string {
47 var sb strings.Builder
48 sb.WriteString(" --pane-mode-color: " + m.PaneMode + ";\n")
49 sb.WriteString(" --tab-mode-color: " + m.TabMode + ";\n")
50 sb.WriteString(" --session-mode-color: " + m.SessionMode + ";\n")
51 sb.WriteString(" --resize-mode-color: " + m.ResizeMode + ";\n")
52 return sb.String()
53}
54
55// DefaultDarkPalette returns the default dark theme palette.
56func DefaultDarkPalette() Palette {
57 return Palette{
58 Background: "#0a0a0b",
59 Surface: "#1a1a1b",
60 SurfaceVariant: "#2d2d2d",
61 Text: "#ffffff",
62 Muted: "#909090",
63 Accent: "#4ade80",
64 Border: "#333333",
65 Success: "#4ade80",
66 Warning: "#fbbf24",
67 Destructive: "#ef4444",
68 }
69}
70
71// DefaultLightPalette returns the default light theme palette.
72func DefaultLightPalette() Palette {
73 return Palette{
74 Background: "#fafafa",
75 Surface: "#ffffff",
76 SurfaceVariant: "#f0f0f0",
77 Text: "#1a1a1a",
78 Muted: "#666666",
79 Accent: "#22c55e",
80 Border: "#dddddd",
81 Success: "#22c55e",
82 Warning: "#f59e0b",
83 Destructive: "#dc2626",
84 }
85}
86
87// PaletteFromConfig creates a Palette from config values, filling missing values with defaults.
88func PaletteFromConfig(cfg *entity.ColorPalette, isDark bool) Palette {
89 var defaults Palette
90 if isDark {
91 defaults = DefaultDarkPalette()
92 } else {
93 defaults = DefaultLightPalette()
94 }
95
96 if cfg == nil {
97 return defaults
98 }
99
100 return Palette{
101 Background: Coalesce(cfg.Background, defaults.Background),
102 Surface: Coalesce(cfg.Surface, defaults.Surface),
103 SurfaceVariant: Coalesce(cfg.SurfaceVariant, defaults.SurfaceVariant),
104 Text: Coalesce(cfg.Text, defaults.Text),
105 Muted: Coalesce(cfg.Muted, defaults.Muted),
106 Accent: Coalesce(cfg.Accent, defaults.Accent),
107 Border: Coalesce(cfg.Border, defaults.Border),
108 // Semantic colors always use defaults (not user-editable)
109 Success: defaults.Success,
110 Warning: defaults.Warning,
111 Destructive: defaults.Destructive,
112 }
113}
114
115// Coalesce returns the first non-empty string.
116func Coalesce(values ...string) string {
117 for _, v := range values {
118 if v != "" {
119 return v
120 }
121 }
122 return ""
123}
124
125// hexColorRegex matches valid hex colors (#RGB, #RRGGBB, #RRGGBBAA).
126var hexColorRegex = regexp.MustCompile(`^#([0-9A-Fa-f]{3}|[0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$`)
127
128// Hex format length constants.
129const (
130 hexLenShort = 3 // #RGB
131 hexLenStandard = 6 // #RRGGBB
132 hexLenWithAlha = 8 // #RRGGBBAA
133 maxColorValue = 255 // Maximum value for 8-bit color channel
134)
135
136// HexToRGBA converts a hex color string to RGBA float32 values (0.0-1.0).
137// Supports #RGB, #RRGGBB, and #RRGGBBAA formats.
138// Returns opaque black (0,0,0,1) if parsing fails.
139func HexToRGBA(hex string) (r, g, b, a float32) {
140 // Default to opaque black
141 if hex == "" || hex[0] != '#' {
142 return 0, 0, 0, 1
143 }
144 hex = hex[1:]
145
146 var ri, gi, bi, ai uint64
147 switch len(hex) {
148 case hexLenShort: // #RGB
149 ri, _ = parseHex(hex[0:1])
150 gi, _ = parseHex(hex[1:2])
151 bi, _ = parseHex(hex[2:3])
152 ri *= 17 // 0xF -> 0xFF
153 gi *= 17
154 bi *= 17
155 ai = maxColorValue
156 case hexLenStandard: // #RRGGBB
157 ri, _ = parseHex(hex[0:2])
158 gi, _ = parseHex(hex[2:4])
159 bi, _ = parseHex(hex[4:6])
160 ai = maxColorValue
161 case hexLenWithAlha: // #RRGGBBAA
162 ri, _ = parseHex(hex[0:2])
163 gi, _ = parseHex(hex[2:4])
164 bi, _ = parseHex(hex[4:6])
165 ai, _ = parseHex(hex[6:8])
166 default:
167 return 0, 0, 0, 1
168 }
169
170 return float32(ri) / maxColorValue, float32(gi) / maxColorValue, float32(bi) / maxColorValue, float32(ai) / maxColorValue
171}
172
173// parseHex parses a hex string to uint64.
174func parseHex(s string) (uint64, error) {
175 var result uint64
176 for _, c := range s {
177 result *= 16
178 switch {
179 case c >= '0' && c <= '9':
180 result += uint64(c - '0')
181 case c >= 'a' && c <= 'f':
182 result += uint64(c - 'a' + 10)
183 case c >= 'A' && c <= 'F':
184 result += uint64(c - 'A' + 10)
185 default:
186 return 0, fmt.Errorf("invalid hex char: %c", c)
187 }
188 }
189 return result, nil
190}
191
192// ValidateHexColor checks if a string is a valid hex color.
193func ValidateHexColor(color string) error {
194 if color == "" {
195 return nil // Empty is valid (will use default)
196 }
197 if !hexColorRegex.MatchString(color) {
198 return fmt.Errorf("invalid hex color: %s", color)
199 }
200 return nil
201}
202
203// Validate checks all palette colors are valid hex values.
204func (p Palette) Validate() error {
205 colors := map[string]string{
206 "background": p.Background,
207 "surface": p.Surface,
208 "surface_variant": p.SurfaceVariant,
209 "text": p.Text,
210 "muted": p.Muted,
211 "accent": p.Accent,
212 "border": p.Border,
213 }
214
215 for name, color := range colors {
216 if err := ValidateHexColor(color); err != nil {
217 return fmt.Errorf("%s: %w", name, err)
218 }
219 }
220 return nil
221}
222
223// ToCSSVars generates CSS custom property declarations for GTK.
224func (p Palette) ToCSSVars() string {
225 var sb strings.Builder
226 sb.WriteString(" --bg: " + p.Background + ";\n")
227 sb.WriteString(" --surface: " + p.Surface + ";\n")
228 sb.WriteString(" --surface-variant: " + p.SurfaceVariant + ";\n")
229 sb.WriteString(" --text: " + p.Text + ";\n")
230 sb.WriteString(" --muted: " + p.Muted + ";\n")
231 sb.WriteString(" --accent: " + p.Accent + ";\n")
232 sb.WriteString(" --border: " + p.Border + ";\n")
233 sb.WriteString(" --success: " + p.Success + ";\n")
234 sb.WriteString(" --warning: " + p.Warning + ";\n")
235 sb.WriteString(" --destructive: " + p.Destructive + ";\n")
236 return sb.String()
237}
238
239// ToWebCSSVars generates CSS custom properties for web UI (Tailwind compatibility).
240// Maps GTK palette names to Tailwind variable names used in the WebUI homepage.
241func (p Palette) ToWebCSSVars() string {
242 var sb strings.Builder
243 // Tailwind-compatible variables
244 sb.WriteString(" --background: " + p.Background + " !important;\n")
245 sb.WriteString(" --foreground: " + p.Text + " !important;\n")
246 sb.WriteString(" --card: " + p.Surface + " !important;\n")
247 sb.WriteString(" --card-foreground: " + p.Text + " !important;\n")
248 sb.WriteString(" --primary: " + p.Accent + " !important;\n")
249 sb.WriteString(" --primary-foreground: " + p.Background + " !important;\n")
250 sb.WriteString(" --muted: " + p.SurfaceVariant + " !important;\n")
251 sb.WriteString(" --muted-foreground: " + p.Muted + " !important;\n")
252 sb.WriteString(" --border: " + p.Border + " !important;\n")
253 // Ring follows primary (accent)
254 sb.WriteString(" --ring: " + p.Accent + " !important;\n")
255 // Semantic colors from palette defaults
256 sb.WriteString(" --success: " + p.Success + " !important;\n")
257 sb.WriteString(" --success-foreground: " + p.Background + " !important;\n")
258 sb.WriteString(" --warning: " + p.Warning + " !important;\n")
259 sb.WriteString(" --warning-foreground: " + p.Text + " !important;\n")
260 sb.WriteString(" --destructive: " + p.Destructive + " !important;\n")
261 sb.WriteString(" --destructive-foreground: " + p.Background + " !important;\n")
262 // Keep GTK names for compatibility
263 sb.WriteString(" --bg: " + p.Background + " !important;\n")
264 sb.WriteString(" --surface: " + p.Surface + " !important;\n")
265 sb.WriteString(" --surface-variant: " + p.SurfaceVariant + " !important;\n")
266 sb.WriteString(" --text: " + p.Text + " !important;\n")
267 sb.WriteString(" --accent: " + p.Accent + " !important;\n")
268 return sb.String()
269}