grimoire_css_color_toolkit 1.0.0

CSS Color Module Level 4 compliant color manipulation toolkit
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
mod color;

use clap::{Args, Parser, Subcommand};
use color::Color;
use std::process;

#[derive(Parser)]
#[command(name = "grimoire_css_color_toolkit")]
#[command(about = "CSS Color Module Level 4 compliant color manipulation toolkit")]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Create RGBA color from components
    New(NewArgs),
    /// Create color from RGB values
    FromRgb(FromRgbArgs),
    /// Create color from HSL values
    FromHsl(FromHslArgs),
    /// Create color from HWB values
    FromHwb(FromHwbArgs),
    /// Parse hex color string
    FromHex(FromHexArgs),
    /// Parse any CSS color string
    Parse(ParseArgs),
    /// Get red channel value
    Red(ColorArgs),
    /// Get green channel value
    Green(ColorArgs),
    /// Get blue channel value
    Blue(ColorArgs),
    /// Get alpha channel value
    Alpha(ColorArgs),
    /// Get opacity value (same as alpha)
    Opacity(ColorArgs),
    /// Convert to HSL format
    ToHsl(ColorArgs),
    /// Convert to RGBA format
    ToRgba(ColorArgs),
    /// Convert to hex format
    ToHex(ColorArgs),
    /// Convert to named color if possible
    ToNamed(ColorArgs),
    /// Convert to grayscale
    Grayscale(ColorArgs),
    /// Get complement color
    Complement(ColorArgs),
    /// Invert color
    Invert(InvertArgs),
    /// Mix two colors
    Mix(MixArgs),
    /// Adjust hue by degrees
    AdjustHue(AdjustHueArgs),
    /// Adjust color properties by delta
    AdjustColor(AdjustColorArgs),
    /// Change color properties to absolute values
    ChangeColor(ChangeColorArgs),
    /// Scale color properties by percentage
    ScaleColor(ScaleColorArgs),
    /// Set alpha channel
    Rgba(RgbaArgs),
    /// Increase lightness
    Lighten(AmountArgs),
    /// Decrease lightness
    Darken(AmountArgs),
    /// Increase saturation
    Saturate(AmountArgs),
    /// Decrease saturation
    Desaturate(AmountArgs),
    /// Increase opacity
    Opacify(AmountArgs),
    /// Increase opacity (alias for opacify)
    FadeIn(AmountArgs),
    /// Decrease opacity
    Transparentize(AmountArgs),
    /// Decrease opacity (alias for transparentize)
    FadeOut(AmountArgs),
}

#[derive(Args)]
struct NewArgs {
    /// Red component (0-255)
    r: u8,
    /// Green component (0-255)
    g: u8,
    /// Blue component (0-255)
    b: u8,
    /// Alpha component (0.0-1.0)
    #[arg(default_value = "1.0")]
    a: f32,
}

#[derive(Args)]
struct FromRgbArgs {
    /// Red component (0-255)
    r: u8,
    /// Green component (0-255)
    g: u8,
    /// Blue component (0-255)
    b: u8,
    /// Alpha component (0.0-1.0)
    #[arg(default_value = "1.0")]
    a: f32,
}

#[derive(Args)]
struct FromHslArgs {
    /// Hue in degrees (0-360)
    h: f32,
    /// Saturation percentage (0-100)
    s: f32,
    /// Lightness percentage (0-100)
    l: f32,
    /// Alpha component (0.0-1.0)
    #[arg(default_value = "1.0")]
    a: f32,
}

#[derive(Args)]
struct FromHwbArgs {
    /// Hue in degrees (0-360)
    h: f32,
    /// Whiteness percentage (0-100)
    w: f32,
    /// Blackness percentage (0-100)
    b: f32,
    /// Alpha component (0.0-1.0)
    #[arg(default_value = "1.0")]
    a: f32,
}

#[derive(Args)]
struct FromHexArgs {
    /// Hex color string (e.g., #ff0000, #f00)
    hex: String,
}

#[derive(Args)]
struct ParseArgs {
    /// CSS color string (hex, named, rgb, hsl, hwb)
    color: String,
}

#[derive(Args)]
struct ColorArgs {
    /// CSS color string
    color: String,
}

#[derive(Args)]
struct InvertArgs {
    /// CSS color string
    color: String,
    /// Inversion weight percentage (0-100)
    #[arg(short, long)]
    weight: Option<f32>,
}

#[derive(Args)]
struct MixArgs {
    /// First color to mix
    color1: String,
    /// Second color to mix
    color2: String,
    /// Mix weight percentage (0-100, higher values favor color2)
    weight: f32,
}

#[derive(Args)]
struct AdjustHueArgs {
    color: String,
    /// Degrees to shift hue (-360 to 360)
    degrees: f32,
}

#[derive(Args)]
struct AdjustColorArgs {
    color: String,
    /// RGB delta (-255 to 255)
    #[arg(long)]
    red_delta: Option<i32>,
    #[arg(long)]
    green_delta: Option<i32>,
    #[arg(long)]
    blue_delta: Option<i32>,
    /// HSL deltas
    #[arg(long)]
    hue_delta: Option<f32>,
    #[arg(long)]
    sat_delta: Option<f32>,
    #[arg(long)]
    light_delta: Option<f32>,
    #[arg(long)]
    alpha_delta: Option<f32>,
}

#[derive(Args)]
struct ChangeColorArgs {
    color: String,
    #[arg(long)]
    red: Option<u8>,
    #[arg(long)]
    green: Option<u8>,
    #[arg(long)]
    blue: Option<u8>,
    #[arg(long)]
    hue: Option<f32>,
    #[arg(long)]
    saturation: Option<f32>,
    #[arg(long)]
    lightness: Option<f32>,
    #[arg(long)]
    alpha: Option<f32>,
}

#[derive(Args)]
struct ScaleColorArgs {
    color: String,
    /// Scale percentages (-100 to 100, negative values decrease)
    #[arg(long)]
    red_scale: Option<f32>,
    #[arg(long)]
    green_scale: Option<f32>,
    #[arg(long)]
    blue_scale: Option<f32>,
    #[arg(long)]
    saturation_scale: Option<f32>,
    #[arg(long)]
    lightness_scale: Option<f32>,
    #[arg(long)]
    alpha_scale: Option<f32>,
}

#[derive(Args)]
struct RgbaArgs {
    color: String,
    /// Alpha value (0.0-1.0)
    alpha: f32,
}

#[derive(Args)]
struct AmountArgs {
    color: String,
    /// Percentage amount (0-100)
    amount: f32,
}

fn parse_color(color_str: &str) -> Color {
    Color::try_from_str(color_str).unwrap_or_else(|| {
        eprintln!("Invalid color: {color_str}");
        process::exit(1);
    })
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::New(args) => {
            let color = Color::new(args.r, args.g, args.b, args.a);
            println!("{}", color.to_hex_string());
        }

        Commands::FromRgb(args) => {
            let color = Color::from_rgb(args.r, args.g, args.b, args.a);
            println!("{}", color.to_hex_string());
        }

        Commands::FromHsl(args) => {
            let color = Color::from_hsl(args.h, args.s, args.l, args.a);
            println!("{}", color.to_hex_string());
        }

        Commands::FromHwb(args) => {
            let color = Color::from_hwb(args.h, args.w, args.b, args.a);
            println!("{}", color.to_hex_string());
        }

        Commands::FromHex(args) => match Color::from_hex(&args.hex) {
            Some(color) => println!("{}", color.to_hex_string()),
            None => {
                eprintln!("Invalid hex color: {}", args.hex);
                process::exit(1);
            }
        },

        Commands::Parse(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.to_hex_string());
        }

        Commands::Red(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.red());
        }

        Commands::Green(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.green());
        }

        Commands::Blue(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.blue());
        }

        Commands::Alpha(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.alpha());
        }

        Commands::Opacity(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.opacity());
        }

        Commands::ToHsl(args) => {
            let color = parse_color(&args.color);
            let (h, s, l) = color.to_hsl();
            println!("{h} {s} {l}");
        }

        Commands::ToRgba(args) => {
            let color = parse_color(&args.color);
            let (r, g, b, a) = color.to_rgba();
            println!("{r} {g} {b} {a}");
        }

        Commands::ToHex(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.to_hex_string());
        }

        Commands::ToNamed(args) => {
            let color = parse_color(&args.color);
            match color.to_named_color_str() {
                Some(name) => println!("{name}"),
                None => process::exit(1),
            }
        }

        Commands::Grayscale(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.grayscale().to_hex_string());
        }

        Commands::Complement(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.complement().to_hex_string());
        }

        Commands::Invert(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.invert(args.weight).to_hex_string());
        }

        Commands::Mix(args) => {
            let color1 = parse_color(&args.color1);
            let color2 = parse_color(&args.color2);
            println!(
                "{}",
                Color::mix(color1, color2, args.weight).to_hex_string()
            );
        }

        Commands::AdjustHue(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.adjust_hue(args.degrees).to_hex_string());
        }

        Commands::AdjustColor(args) => {
            let color = parse_color(&args.color);
            let result = color.adjust_color(
                args.red_delta,
                args.green_delta,
                args.blue_delta,
                args.hue_delta,
                args.sat_delta,
                args.light_delta,
                args.alpha_delta,
            );
            println!("{}", result.to_hex_string());
        }

        Commands::ChangeColor(args) => {
            let color = parse_color(&args.color);
            let result = color.change_color(
                args.red,
                args.green,
                args.blue,
                args.hue,
                args.saturation,
                args.lightness,
                args.alpha,
            );
            println!("{}", result.to_hex_string());
        }

        Commands::ScaleColor(args) => {
            let color = parse_color(&args.color);
            let result = color.scale_color(
                args.red_scale,
                args.green_scale,
                args.blue_scale,
                args.saturation_scale,
                args.lightness_scale,
                args.alpha_scale,
            );
            println!("{}", result.to_hex_string());
        }

        Commands::Rgba(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.rgba(args.alpha).to_hex_string());
        }

        Commands::Lighten(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.lighten(args.amount).to_hex_string());
        }

        Commands::Darken(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.darken(args.amount).to_hex_string());
        }

        Commands::Saturate(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.saturate(args.amount).to_hex_string());
        }

        Commands::Desaturate(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.desaturate(args.amount).to_hex_string());
        }

        Commands::Opacify(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.opacify(args.amount).to_hex_string());
        }

        Commands::FadeIn(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.fade_in(args.amount).to_hex_string());
        }

        Commands::Transparentize(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.transparentize(args.amount).to_hex_string());
        }

        Commands::FadeOut(args) => {
            let color = parse_color(&args.color);
            println!("{}", color.fade_out(args.amount).to_hex_string());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_constructors() {
        let color = Color::new(255, 0, 0, 1.0);
        assert_eq!(color.to_hex_string(), "#ff0000");

        let color = Color::from_rgb(0, 255, 0, 1.0);
        assert_eq!(color.to_hex_string(), "#00ff00");

        let color = Color::from_hsl(240.0, 100.0, 50.0, 1.0);
        assert_eq!(color.to_hex_string(), "#0000ff");

        let color = Color::from_hwb(120.0, 0.0, 0.0, 1.0);
        assert_eq!(color.to_hex_string(), "#00ff00");

        let color = Color::from_hex("#ff0000").unwrap();
        assert_eq!(color.to_hex_string(), "#ff0000");

        let color = Color::try_from_str("red").unwrap();
        assert_eq!(color.to_hex_string(), "#ff0000");
    }

    #[test]
    fn test_getters() {
        let color = Color::new(255, 128, 64, 0.5);
        assert_eq!(color.red(), 255);
        assert_eq!(color.green(), 128);
        assert_eq!(color.blue(), 64);
        assert_eq!(color.alpha(), 0.5);
        assert_eq!(color.opacity(), 0.5);
    }

    #[test]
    fn test_conversions() {
        let color = Color::new(255, 0, 0, 1.0);

        let (h, s, l) = color.to_hsl();
        assert_eq!(h, 0.0);
        assert_eq!(s, 100.0);
        assert_eq!(l, 50.0);

        let (r, g, b, a) = color.to_rgba();
        assert_eq!(r, 255);
        assert_eq!(g, 0);
        assert_eq!(b, 0);
        assert_eq!(a, 1.0);

        assert_eq!(color.to_hex_string(), "#ff0000");
        assert_eq!(color.to_named_color_str(), Some("red"));
    }

    #[test]
    fn test_color_operations() {
        let red = Color::try_from_str("red").unwrap();

        let gray = red.grayscale();
        assert_eq!(gray.to_hex_string(), "#808080");

        let complement = red.complement();
        assert_eq!(complement.to_hex_string(), "#00ffff");

        let inverted = red.invert(Some(100.0));
        assert_eq!(inverted.to_hex_string(), "#00ffff");

        let mixed = Color::mix(red, Color::try_from_str("blue").unwrap(), 50.0);
        assert_eq!(mixed.to_hex_string(), "#800080");

        let hue_adjusted = red.adjust_hue(120.0);
        assert_eq!(hue_adjusted.to_hex_string(), "#00ff00");
    }

    #[test]
    fn test_color_adjustments() {
        let color = Color::new(128, 128, 128, 1.0);

        let adjusted = color.adjust_color(Some(10), None, None, None, None, None, None);
        assert_eq!(adjusted.red(), 138);

        let changed = color.change_color(Some(255), None, None, None, None, None, None);
        assert_eq!(changed.red(), 255);

        let scaled = color.scale_color(Some(10.0), None, None, None, None, None);
        assert!(scaled.red() > 128);
    }

    #[test]
    fn test_alpha_operations() {
        let color = Color::new(255, 0, 0, 1.0);

        let with_alpha = color.rgba(0.5);
        assert_eq!(with_alpha.to_hex_string(), "#ff000080");

        let more_opaque = with_alpha.opacify(0.3);
        assert_eq!(more_opaque.alpha(), 0.8);

        let more_transparent = color.transparentize(0.5);
        assert_eq!(more_transparent.alpha(), 0.5);

        let fade_in = with_alpha.fade_in(0.2);
        assert_eq!(fade_in.alpha(), 0.7);

        let fade_out = color.fade_out(0.3);
        assert_eq!(fade_out.alpha(), 0.7);
    }

    #[test]
    fn test_lightness_adjustments() {
        let red = Color::try_from_str("red").unwrap();

        let lighter = red.lighten(10.0);
        assert_eq!(lighter.to_hex_string(), "#ff3333");

        let darker = red.darken(10.0);
        assert_eq!(darker.to_hex_string(), "#cc0000");
    }

    #[test]
    fn test_saturation_adjustments() {
        let red = Color::try_from_str("red").unwrap();

        let desaturated = red.desaturate(50.0);
        let (_, s, _) = desaturated.to_hsl();
        assert!((s - 50.0).abs() < 1.0);

        let saturated = desaturated.saturate(25.0);
        let (_, s, _) = saturated.to_hsl();
        assert!((s - 75.0).abs() < 1.0);
    }

    #[test]
    fn test_css_parsing() {
        assert!(Color::try_from_str("rgb(255, 0, 0)").is_some());
        assert!(Color::try_from_str("hsl(0, 100%, 50%)").is_some());
        assert!(Color::try_from_str("hwb(0 0% 0%)").is_some());
        assert!(Color::try_from_str("#ff0000").is_some());
        assert!(Color::try_from_str("red").is_some());
        assert!(Color::try_from_str("invalid_color").is_none());
    }

    #[test]
    fn test_hex_variations() {
        assert_eq!(Color::from_hex("#fff").unwrap().to_hex_string(), "#ffffff");
        assert_eq!(
            Color::from_hex("#ffffff").unwrap().to_hex_string(),
            "#ffffff"
        );
        assert_eq!(
            Color::from_hex("#ffff").unwrap().to_hex_string(),
            "#ffffffff"
        );
        assert_eq!(
            Color::from_hex("#ffffffff").unwrap().to_hex_string(),
            "#ffffffff"
        );
    }

    #[test]
    fn test_named_colors() {
        let red = Color::try_from_str("red").unwrap();
        assert_eq!(red.to_named_color_str(), Some("red"));

        let blue = Color::try_from_str("blue").unwrap();
        assert_eq!(blue.to_named_color_str(), Some("blue"));

        let custom = Color::new(123, 45, 67, 1.0);
        assert_eq!(custom.to_named_color_str(), None);
    }

    #[test]
    fn test_edge_cases() {
        let color = Color::new(255, 255, 255, 2.0);
        assert_eq!(color.alpha(), 1.0);

        let color = Color::new(0, 0, 0, -1.0);
        assert_eq!(color.alpha(), 0.0);

        let (h, s, l) = Color::new(0, 0, 0, 1.0).to_hsl();
        assert_eq!(h, 0.0);
        assert_eq!(s, 0.0);
        assert_eq!(l, 0.0);
    }
}