pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//! Integration tests for CSS variable support
//!
//! Tests the full palette parsing flow with CSS custom properties:
//! - Variable definition (`--name: value`)
//! - Variable resolution (`var(--name)` and `var(--name, fallback)`)
//! - Lenient vs strict mode handling
//! - Integration with color parsing

use image::Rgba;
use pixelsrc::palette_parser::{PaletteParser, ParseMode, MAGENTA};
use pixelsrc::variables::VariableRegistry;
use std::collections::HashMap;

fn make_palette(entries: &[(&str, &str)]) -> HashMap<String, String> {
    entries.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()
}

// ========== End-to-end palette parsing tests ==========

#[test]
fn test_realistic_palette_with_variables() {
    // A realistic palette using CSS variables for theming
    let raw = make_palette(&[
        // Theme variables
        ("--primary", "#4169E1"),
        ("--secondary", "#8B4513"),
        ("--accent", "#FFD700"),
        ("--bg", "#2D2D2D"),
        ("--fg", "#FFFFFF"),
        // Derived colors using variables
        ("{_}", "transparent"),
        ("{outline}", "var(--bg)"),
        ("{skin}", "#FFCC99"),
        ("{hair}", "var(--secondary)"),
        ("{shirt}", "var(--primary)"),
        ("{highlight}", "var(--accent)"),
        ("{text}", "var(--fg)"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Check resolved colors
    assert_eq!(result.colors.get("{outline}"), Some(&Rgba([45, 45, 45, 255])));
    assert_eq!(result.colors.get("{hair}"), Some(&Rgba([139, 69, 19, 255])));
    assert_eq!(result.colors.get("{shirt}"), Some(&Rgba([65, 105, 225, 255])));
    assert_eq!(result.colors.get("{highlight}"), Some(&Rgba([255, 215, 0, 255])));
    assert_eq!(result.colors.get("{text}"), Some(&Rgba([255, 255, 255, 255])));

    // No warnings for valid palette
    assert!(result.warnings.is_empty());

    // Variables should be preserved in registry
    assert!(result.variables.contains("--primary"));
    assert!(result.variables.contains("--secondary"));
}

#[test]
fn test_variable_with_css_color_functions() {
    // Variables can contain partial values for use in CSS functions
    let raw = make_palette(&[
        ("--hue", "240"),
        ("--sat", "100%"),
        ("--light", "50%"),
        ("{blue}", "hsl(var(--hue), var(--sat), var(--light))"),
        // RGB components
        ("--r", "255"),
        ("--g", "128"),
        ("--b", "0"),
        ("{orange}", "rgb(var(--r), var(--g), var(--b))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{blue}"), Some(&Rgba([0, 0, 255, 255])));
    assert_eq!(result.colors.get("{orange}"), Some(&Rgba([255, 128, 0, 255])));
}

#[test]
fn test_fallback_for_optional_theming() {
    // Fallbacks enable optional theme overrides
    let raw = make_palette(&[
        // Only some theme variables defined
        ("--primary", "#FF0000"),
        // Use fallback for undefined optional override
        ("{main}", "var(--primary)"),
        ("{alt}", "var(--secondary, #00FF00)"), // --secondary not defined
        ("{accent}", "var(--accent-override, var(--primary))"), // Nested fallback
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{main}"), Some(&Rgba([255, 0, 0, 255])));
    assert_eq!(result.colors.get("{alt}"), Some(&Rgba([0, 255, 0, 255]))); // Uses fallback
    assert_eq!(result.colors.get("{accent}"), Some(&Rgba([255, 0, 0, 255]))); // Nested to --primary
    assert!(result.warnings.is_empty());
}

// ========== External variable inheritance tests ==========

#[test]
fn test_external_theme_variables() {
    // Simulate a theme file providing base variables
    let mut theme_vars = VariableRegistry::new();
    theme_vars.define("--bg", "#1A1A2E");
    theme_vars.define("--fg", "#EAEAEA");
    theme_vars.define("--accent", "#E94560");

    // Local palette uses theme variables
    let raw = make_palette(&[
        ("{background}", "var(--bg)"),
        ("{text}", "var(--fg)"),
        ("{highlight}", "var(--accent)"),
        ("{_}", "transparent"),
    ]);

    let parser = PaletteParser::with_external_vars(theme_vars);
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{background}"), Some(&Rgba([26, 26, 46, 255])));
    assert_eq!(result.colors.get("{text}"), Some(&Rgba([234, 234, 234, 255])));
    assert_eq!(result.colors.get("{highlight}"), Some(&Rgba([233, 69, 96, 255])));
}

#[test]
fn test_local_overrides_external() {
    // External theme
    let mut theme_vars = VariableRegistry::new();
    theme_vars.define("--primary", "#FF0000"); // Red in theme

    // Local palette overrides theme
    let raw = make_palette(&[
        ("--primary", "#0000FF"), // Blue override
        ("{color}", "var(--primary)"),
    ]);

    let parser = PaletteParser::with_external_vars(theme_vars);
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Local should win
    assert_eq!(result.colors.get("{color}"), Some(&Rgba([0, 0, 255, 255])));
}

// ========== Lenient mode error recovery tests ==========

#[test]
fn test_lenient_undefined_variable_uses_magenta() {
    let raw = make_palette(&[
        ("{valid}", "#FF0000"),
        ("{undefined}", "var(--nonexistent)"), // No fallback
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{valid}"), Some(&Rgba([255, 0, 0, 255])));
    assert_eq!(result.colors.get("{undefined}"), Some(&MAGENTA));
    assert_eq!(result.warnings.len(), 1);
    assert!(result.warnings[0].message.contains("undefined"));
}

#[test]
fn test_lenient_circular_reference_uses_magenta() {
    let raw = make_palette(&[("--a", "var(--b)"), ("--b", "var(--a)"), ("{color}", "var(--a)")]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{color}"), Some(&MAGENTA));
    assert!(!result.warnings.is_empty());
}

#[test]
fn test_lenient_mixed_valid_and_invalid() {
    let raw = make_palette(&[
        ("--valid", "#00FF00"),
        ("{good1}", "var(--valid)"),
        ("{bad1}", "var(--missing)"),
        ("{good2}", "#0000FF"),
        ("{bad2}", "not-a-color"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Valid colors parsed correctly
    assert_eq!(result.colors.get("{good1}"), Some(&Rgba([0, 255, 0, 255])));
    assert_eq!(result.colors.get("{good2}"), Some(&Rgba([0, 0, 255, 255])));

    // Invalid colors become magenta
    assert_eq!(result.colors.get("{bad1}"), Some(&MAGENTA));
    assert_eq!(result.colors.get("{bad2}"), Some(&MAGENTA));

    // Two warnings
    assert_eq!(result.warnings.len(), 2);
}

// ========== Strict mode tests ==========

#[test]
fn test_strict_fails_on_undefined_variable() {
    let raw = make_palette(&[("{color}", "var(--undefined)")]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Strict);

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("undefined"));
}

#[test]
fn test_strict_fails_on_circular_reference() {
    let raw = make_palette(&[("--self", "var(--self)"), ("{color}", "var(--self)")]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Strict);

    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("circular"));
}

#[test]
fn test_strict_succeeds_with_valid_palette() {
    let raw = make_palette(&[
        ("--primary", "#FF0000"),
        ("{main}", "var(--primary)"),
        ("{alt}", "var(--missing, #00FF00)"), // Fallback makes this valid
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Strict);

    assert!(result.is_ok());
    let parsed = result.unwrap();
    assert!(parsed.warnings.is_empty());
}

// ========== Variable registry preservation tests ==========

#[test]
fn test_variable_registry_available_for_reuse() {
    let raw = make_palette(&[
        ("--brand-primary", "#4169E1"),
        ("--brand-secondary", "#8B4513"),
        ("{color}", "var(--brand-primary)"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Can use the registry for further resolution
    let registry = &result.variables;
    assert!(registry.contains("--brand-primary"));
    assert!(registry.contains("--brand-secondary"));

    // Can resolve additional values
    assert_eq!(registry.resolve("var(--brand-primary)").unwrap(), "#4169E1");
}

// ========== resolve_to_strings tests ==========

#[test]
fn test_resolve_to_strings_for_serialization() {
    let raw = make_palette(&[
        ("--primary", "#FF0000"),
        ("{main}", "var(--primary)"),
        ("{static}", "#00FF00"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.resolve_to_strings(&raw, ParseMode::Lenient).unwrap();

    // Get resolved color strings (not RGBA)
    assert_eq!(result.colors.get("{main}"), Some(&"#FF0000".to_string()));
    assert_eq!(result.colors.get("{static}"), Some(&"#00FF00".to_string()));
}

// ========== Edge case tests ==========

#[test]
fn test_deep_nesting_chain() {
    let raw = make_palette(&[
        ("--l1", "#FF0000"),
        ("--l2", "var(--l1)"),
        ("--l3", "var(--l2)"),
        ("--l4", "var(--l3)"),
        ("--l5", "var(--l4)"),
        ("{color}", "var(--l5)"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{color}"), Some(&Rgba([255, 0, 0, 255])));
}

#[test]
fn test_complex_fallback_chain() {
    let raw = make_palette(&[
        ("--final", "#00FF00"),
        ("{color}", "var(--a, var(--b, var(--c, var(--final))))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{color}"), Some(&Rgba([0, 255, 0, 255])));
}

#[test]
fn test_variables_with_all_color_formats() {
    let raw = make_palette(&[
        ("--hex", "#FF0000"),
        ("--rgb", "rgb(0, 255, 0)"),
        ("--hsl", "hsl(240, 100%, 50%)"),
        ("--named", "coral"),
        ("{hex_ref}", "var(--hex)"),
        ("{rgb_ref}", "var(--rgb)"),
        ("{hsl_ref}", "var(--hsl)"),
        ("{named_ref}", "var(--named)"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{hex_ref}"), Some(&Rgba([255, 0, 0, 255])));
    assert_eq!(result.colors.get("{rgb_ref}"), Some(&Rgba([0, 255, 0, 255])));
    assert_eq!(result.colors.get("{hsl_ref}"), Some(&Rgba([0, 0, 255, 255])));
    assert_eq!(result.colors.get("{named_ref}"), Some(&Rgba([255, 127, 80, 255])));
}

#[test]
fn test_whitespace_in_var_reference() {
    let raw = make_palette(&[
        ("--color", "#FF0000"),
        ("{a}", "var(  --color  )"),
        ("{b}", "var(--color,   #00FF00   )"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert_eq!(result.colors.get("{a}"), Some(&Rgba([255, 0, 0, 255])));
    // {b} uses defined --color, not fallback
    assert_eq!(result.colors.get("{b}"), Some(&Rgba([255, 0, 0, 255])));
}

// ========== CSS-12: color-mix() with var() tests ==========

#[test]
fn test_color_mix_with_var_references() {
    // color-mix() using CSS variables for colors
    let raw = make_palette(&[
        ("--primary", "red"),
        ("--secondary", "blue"),
        ("{mixed}", "color-mix(in oklch, var(--primary), var(--secondary))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Should successfully parse color-mix with variable-resolved colors
    assert!(result.warnings.is_empty(), "Should have no warnings: {:?}", result.warnings);
    let mixed = result.colors.get("{mixed}").expect("Should have {mixed} color");
    // Should be a purple-ish color (red + blue)
    assert!(mixed.0[0] > 100, "Should have red component: {:?}", mixed);
    assert!(mixed.0[2] > 100, "Should have blue component: {:?}", mixed);
}

#[test]
fn test_color_mix_with_var_percentages() {
    // color-mix() with variable colors and percentages
    let raw = make_palette(&[
        ("--bg", "#000000"),
        ("--fg", "#ffffff"),
        ("{light_bg}", "color-mix(in oklch, var(--bg) 70%, var(--fg))"),
        ("{dark_fg}", "color-mix(in oklch, var(--fg) 30%, var(--bg))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert!(result.warnings.is_empty(), "Should have no warnings: {:?}", result.warnings);

    // {light_bg} is 70% black + 30% white = dark gray
    let light_bg = result.colors.get("{light_bg}").expect("Should have {light_bg}");
    assert!(light_bg.0[0] < 128, "70% black should be dark: {:?}", light_bg);

    // {dark_fg} is 30% white + 70% black = very dark gray
    let dark_fg = result.colors.get("{dark_fg}").expect("Should have {dark_fg}");
    assert!(dark_fg.0[0] < 128, "30% white should be dark: {:?}", dark_fg);
}

#[test]
fn test_color_mix_with_hex_vars() {
    let raw = make_palette(&[
        ("--color1", "#ff6347"), // coral/tomato
        ("--color2", "#4682b4"), // steelblue
        ("{blend}", "color-mix(in srgb, var(--color1), var(--color2))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert!(result.warnings.is_empty());
    assert!(result.colors.contains_key("{blend}"));
}

#[test]
fn test_color_mix_with_fallback_vars() {
    // color-mix() with undefined variable using fallback
    let raw = make_palette(&[
        ("--known", "red"),
        ("{mixed}", "color-mix(in oklch, var(--known), var(--unknown, blue))"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Fallback should work - var(--unknown, blue) resolves to blue
    assert!(result.warnings.is_empty(), "Fallback should work: {:?}", result.warnings);
    let mixed = result.colors.get("{mixed}").expect("Should have {mixed}");
    // Should be purple (red + blue)
    assert!(mixed.0[0] > 100 && mixed.0[2] > 100, "Should be purple: {:?}", mixed);
}

#[test]
fn test_color_mix_realistic_shadow_generation() {
    // Real-world use case: generating shadow/highlight variants from base colors
    let raw = make_palette(&[
        ("--base", "#4169E1"), // Royal blue
        ("{base}", "var(--base)"),
        ("{shadow}", "color-mix(in oklch, var(--base) 70%, black)"),
        ("{highlight}", "color-mix(in oklch, var(--base) 70%, white)"),
    ]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    assert!(result.warnings.is_empty());

    let base = result.colors.get("{base}").unwrap();
    let shadow = result.colors.get("{shadow}").unwrap();
    let highlight = result.colors.get("{highlight}").unwrap();

    // Shadow should be darker than base
    let base_brightness = (base.0[0] as u32 + base.0[1] as u32 + base.0[2] as u32) / 3;
    let shadow_brightness = (shadow.0[0] as u32 + shadow.0[1] as u32 + shadow.0[2] as u32) / 3;
    assert!(
        shadow_brightness < base_brightness,
        "Shadow should be darker: base={} shadow={}",
        base_brightness,
        shadow_brightness
    );

    // Highlight should be lighter than base
    let highlight_brightness =
        (highlight.0[0] as u32 + highlight.0[1] as u32 + highlight.0[2] as u32) / 3;
    assert!(
        highlight_brightness > base_brightness,
        "Highlight should be lighter: base={} highlight={}",
        base_brightness,
        highlight_brightness
    );
}

#[test]
fn test_color_mix_undefined_var_strict_mode() {
    // Strict mode should fail if var() in color-mix is undefined
    let raw = make_palette(&[("{mixed}", "color-mix(in oklch, var(--undefined), blue)")]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Strict);

    assert!(result.is_err(), "Should fail in strict mode for undefined variable");
}

#[test]
fn test_color_mix_undefined_var_lenient_mode() {
    // Lenient mode should use magenta when var() is undefined (no fallback)
    let raw = make_palette(&[("{mixed}", "color-mix(in oklch, var(--undefined), blue)")]);

    let parser = PaletteParser::new();
    let result = parser.parse(&raw, ParseMode::Lenient).unwrap();

    // Should have a warning
    assert!(!result.warnings.is_empty(), "Should have warning for undefined var");
    // Color should be magenta fallback
    assert_eq!(result.colors.get("{mixed}"), Some(&MAGENTA));
}