boxen 0.4.0

A Rust library for creating styled terminal boxes around text with performance optimizations
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! # Color System
//!
//! This module provides comprehensive color functionality for terminal boxes, including
//! color parsing, validation, and application to text content. It supports multiple
//! color specification formats and provides robust error handling with helpful recommendations.
//!
//! ## Overview
//!
//! The color system supports three main color specification formats:
//! - **Named Colors**: Standard terminal color names (red, blue, green, etc.)
//! - **Hex Colors**: CSS-style hexadecimal color codes (#FF0000, #F00)
//! - **RGB Colors**: Direct RGB component specification (255, 0, 0)
//!
//! ## Quick Start
//!
//! ```rust
//! use ::boxen::color::{parse_color, apply_colors, validate_color};
//! use ::boxen::Color;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Named colors
//! let red = Color::Named("red".to_string());
//! let blue = Color::Named("blue".to_string());
//!
//! // Hex colors (both 3 and 6 digit formats)
//! let orange = Color::Hex("#FF8000".to_string());
//! let green = Color::Hex("#0F0".to_string());
//!
//! // RGB colors
//! let purple = Color::Rgb(128, 0, 128);
//!
//! // Validate colors before use
//! validate_color(&red)?;
//!
//! // Apply colors to text
//! let styled = apply_colors("Hello", Some(&red), Some(&blue))?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Supported Named Colors
//!
//! ### Standard Colors
//! - `black`, `red`, `green`, `yellow`, `blue`, `magenta`/`purple`, `cyan`, `white`
//!
//! ### Bright Colors
//! - `bright_black`/`gray`/`grey`, `bright_red`, `bright_green`, `bright_yellow`
//! - `bright_blue`, `bright_magenta`/`bright_purple`, `bright_cyan`, `bright_white`
//!
//! ### Alternative Formats
//! - Underscore format: `bright_red`, `bright_blue`
//! - Compact format: `brightred`, `brightblue`
//! - Case insensitive: `RED`, `Blue`, `GREEN`
//!
//! ## Hex Color Formats
//!
//! ### 6-Digit Format
//! ```rust
//! use ::boxen::Color;
//!
//! let red = Color::Hex("#FF0000".to_string());
//! let green = Color::Hex("#00FF00".to_string());
//! let blue = Color::Hex("#0000FF".to_string());
//! ```
//!
//! ### 3-Digit Format (Shorthand)
//! ```rust
//! use ::boxen::Color;
//!
//! let red = Color::Hex("#F00".to_string());    // Expands to #FF0000
//! let green = Color::Hex("#0F0".to_string());  // Expands to #00FF00
//! let blue = Color::Hex("#00F".to_string());   // Expands to #0000FF
//! ```
//!
//! ### Optional Hash Prefix
//! ```rust
//! use ::boxen::Color;
//!
//! let with_hash = Color::Hex("#FF0000".to_string());
//! let without_hash = Color::Hex("FF0000".to_string());  // Also valid
//! ```
//!
//! ## RGB Color Format
//!
//! ```rust
//! use ::boxen::Color;
//!
//! let red = Color::Rgb(255, 0, 0);
//! let green = Color::Rgb(0, 255, 0);
//! let blue = Color::Rgb(0, 0, 255);
//! let black = Color::Rgb(0, 0, 0);       // Pure black
//! let white = Color::Rgb(255, 255, 255); // Pure white
//! ```
//!
//! ## Color Application
//!
//! ### Foreground Colors
//! ```rust
//! use ::boxen::color::apply_foreground_color;
//! use ::boxen::Color;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let red_text = apply_foreground_color("Error", &Color::Named("red".to_string()))?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Background Colors
//! ```rust
//! use ::boxen::color::apply_background_color;
//! use ::boxen::Color;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let highlighted = apply_background_color("Important", &Color::Named("yellow".to_string()))?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Combined Colors
//! ```rust
//! use ::boxen::color::apply_colors;
//! use ::boxen::Color;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let fg = Color::Named("white".to_string());
//! let bg = Color::Named("red".to_string());
//! let styled = apply_colors("Alert", Some(&fg), Some(&bg))?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Dim Styling
//! ```rust
//! use ::boxen::color::{apply_dim, apply_color_with_dim};
//! use ::boxen::Color;
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Apply only dim effect
//! let dimmed = apply_dim("Subtle text");
//!
//! // Apply color with optional dim effect
//! let dim_red = apply_color_with_dim(
//!     "Warning",
//!     Some(&Color::Named("red".to_string())),
//!     true  // Enable dim
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Error Handling
//!
//! The color system provides detailed error messages with actionable recommendations:
//!
//! ```rust
//! use ::boxen::color::validate_color;
//! use ::boxen::Color;
//!
//! # fn main() {
//! let invalid = Color::Named("invalid_color".to_string());
//! match validate_color(&invalid) {
//!     Ok(_) => println!("Color is valid"),
//!     Err(e) => {
//!         println!("Error: {}", e);
//!         for rec in e.recommendations() {
//!             println!("Suggestion: {}", rec.suggestion);
//!             if let Some(fix) = &rec.auto_fix {
//!                 println!("Auto-fix: {}", fix);
//!             }
//!         }
//!     }
//! }
//! # }
//! ```
//!
//! ## Performance Considerations
//!
//! - Color parsing is cached internally by the `colored` crate
//! - Named color lookup is O(1) using match statements
//! - Hex parsing is optimized for both 3 and 6 digit formats
//! - RGB colors have no parsing overhead
//! - Color validation is performed once during creation
//!
//! ## Terminal Compatibility
//!
//! - Named colors work on all terminals supporting basic ANSI colors
//! - RGB/Hex colors require true color terminal support
//! - Graceful fallback to nearest colors on limited terminals
//! - Dim styling is widely supported across terminal emulators
//!
//! ## Thread Safety
//!
//! All color operations are thread-safe and can be used concurrently
//! without synchronization concerns.

use crate::ErrorRecommendation;
use crate::error::{BoxenError, BoxenResult};
use crate::options::Color;
use colored::{ColoredString, Colorize};

/// Parse and validate a color specification
///
/// # Errors
///
/// Returns an error if the color specification is invalid.
pub fn parse_color(color: &Color) -> BoxenResult<colored::Color> {
    match color {
        Color::Named(name) => parse_named_color(name),
        Color::Hex(hex) => parse_hex_color(hex),
        Color::Rgb(r, g, b) => Ok(colored::Color::TrueColor {
            r: *r,
            g: *g,
            b: *b,
        }),
    }
}

/// Parse a named color into a `colored::Color`
///
/// # Errors
///
/// Returns an error if the color name is not recognized.
pub fn parse_named_color(name: &str) -> BoxenResult<colored::Color> {
    let normalized = name.to_lowercase();
    match normalized.as_str() {
        // Standard terminal colors
        "black" => Ok(colored::Color::Black),
        "red" => Ok(colored::Color::Red),
        "green" => Ok(colored::Color::Green),
        "yellow" => Ok(colored::Color::Yellow),
        "blue" => Ok(colored::Color::Blue),
        "magenta" | "purple" => Ok(colored::Color::Magenta),
        "cyan" => Ok(colored::Color::Cyan),
        "white" => Ok(colored::Color::White),

        // Bright colors
        "bright_black" | "brightblack" | "gray" | "grey" => Ok(colored::Color::BrightBlack),
        "bright_red" | "brightred" => Ok(colored::Color::BrightRed),
        "bright_green" | "brightgreen" => Ok(colored::Color::BrightGreen),
        "bright_yellow" | "brightyellow" => Ok(colored::Color::BrightYellow),
        "bright_blue" | "brightblue" => Ok(colored::Color::BrightBlue),
        "bright_magenta" | "brightmagenta" | "bright_purple" | "brightpurple" => {
            Ok(colored::Color::BrightMagenta)
        }
        "bright_cyan" | "brightcyan" => Ok(colored::Color::BrightCyan),
        "bright_white" | "brightwhite" => Ok(colored::Color::BrightWhite),

        _ => Err(BoxenError::invalid_color(
            format!("Unknown color name: {name}"),
            name.to_string(),
            vec![
                ErrorRecommendation::suggestion_only(
                    "Unknown color name".to_string(),
                    "Use a standard terminal color name like 'red', 'blue', 'green', etc."
                        .to_string(),
                ),
                ErrorRecommendation::with_auto_fix(
                    "Use standard color".to_string(),
                    "Try using 'red' as a common color".to_string(),
                    "\"red\"".to_string(),
                ),
                ErrorRecommendation::suggestion_only(
                    "Alternative: Use hex color".to_string(),
                    "You can also use hex colors like '#FF0000' for red".to_string(),
                ),
            ],
        )),
    }
}

/// Parse a hex color string into a `colored::Color`
///
/// # Errors
///
/// Returns an error if the hex color format is invalid.
#[allow(clippy::too_many_lines)]
pub fn parse_hex_color(hex: &str) -> BoxenResult<colored::Color> {
    let hex = hex.trim_start_matches('#');

    // Validate hex string length
    if hex.len() != 3 && hex.len() != 6 {
        return Err(BoxenError::invalid_color(
            format!("Invalid hex color format: #{hex}"),
            format!("#{hex}"),
            vec![
                ErrorRecommendation::suggestion_only(
                    "Invalid hex length".to_string(),
                    "Hex colors must be 3 or 6 characters long (e.g., #F00 or #FF0000)".to_string(),
                ),
                ErrorRecommendation::with_auto_fix(
                    "Use 6-digit format".to_string(),
                    "Try using the full 6-digit hex format".to_string(),
                    "\"#FF0000\"".to_string(),
                ),
            ],
        ));
    }

    // Validate hex characters
    if !hex.chars().all(|c| c.is_ascii_hexdigit()) {
        return Err(BoxenError::invalid_color(
            format!("Invalid hex color format: #{hex}"),
            format!("#{hex}"),
            vec![
                ErrorRecommendation::suggestion_only(
                    "Invalid hex characters".to_string(),
                    "Hex colors can only contain digits 0-9 and letters A-F".to_string(),
                ),
                ErrorRecommendation::with_auto_fix(
                    "Use valid hex color".to_string(),
                    "Try using a valid hex color".to_string(),
                    "\"#FF0000\"".to_string(),
                ),
            ],
        ));
    }

    let (r, g, b) = if hex.len() == 3 {
        // Short format: #RGB -> #RRGGBB
        let r = u8::from_str_radix(&hex[0..1].repeat(2), 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 3-digit hex color".to_string(),
                    "\"#F00\"".to_string(),
                )],
            )
        })?;
        let g = u8::from_str_radix(&hex[1..2].repeat(2), 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 3-digit hex color".to_string(),
                    "\"#0F0\"".to_string(),
                )],
            )
        })?;
        let b = u8::from_str_radix(&hex[2..3].repeat(2), 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 3-digit hex color".to_string(),
                    "\"#00F\"".to_string(),
                )],
            )
        })?;
        (r, g, b)
    } else {
        // Long format: #RRGGBB
        let r = u8::from_str_radix(&hex[0..2], 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 6-digit hex color".to_string(),
                    "\"#FF0000\"".to_string(),
                )],
            )
        })?;
        let g = u8::from_str_radix(&hex[2..4], 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 6-digit hex color".to_string(),
                    "\"#00FF00\"".to_string(),
                )],
            )
        })?;
        let b = u8::from_str_radix(&hex[4..6], 16).map_err(|_| {
            BoxenError::invalid_color(
                format!("Invalid hex color: #{hex}"),
                format!("#{hex}"),
                vec![ErrorRecommendation::with_auto_fix(
                    "Invalid hex format".to_string(),
                    "Use a valid 6-digit hex color".to_string(),
                    "\"#0000FF\"".to_string(),
                )],
            )
        })?;
        (r, g, b)
    };

    Ok(colored::Color::TrueColor { r, g, b })
}

/// Validate that a color specification is valid
///
/// # Errors
///
/// Returns an error if the color specification is invalid.
pub fn validate_color(color: &Color) -> BoxenResult<()> {
    parse_color(color).map(|_| ())
}

/// Apply foreground color to text
///
/// # Errors
///
/// Returns an error if the color specification is invalid.
pub fn apply_foreground_color(text: &str, color: &Color) -> BoxenResult<ColoredString> {
    let parsed_color = parse_color(color)?;
    Ok(text.color(parsed_color))
}

/// Apply background color to text
///
/// # Errors
///
/// Returns an error if the color specification is invalid.
pub fn apply_background_color(text: &str, color: &Color) -> BoxenResult<ColoredString> {
    let parsed_color = parse_color(color)?;
    Ok(text.on_color(parsed_color))
}

/// Apply both foreground and background colors to text
///
/// # Errors
///
/// Returns an error if any color specification is invalid.
pub fn apply_colors(
    text: &str,
    fg_color: Option<&Color>,
    bg_color: Option<&Color>,
) -> BoxenResult<ColoredString> {
    let mut styled = ColoredString::from(text);

    if let Some(fg) = fg_color {
        let parsed_fg = parse_color(fg)?;
        styled = styled.color(parsed_fg);
    }

    if let Some(bg) = bg_color {
        let parsed_bg = parse_color(bg)?;
        styled = styled.on_color(parsed_bg);
    }

    Ok(styled)
}

/// Apply dim styling to text (for dim borders)
#[must_use]
pub fn apply_dim(text: &str) -> ColoredString {
    text.dimmed()
}

/// Apply color and dim styling to text
///
/// # Errors
///
/// Returns an error if the color specification is invalid.
pub fn apply_color_with_dim(
    text: &str,
    color: Option<&Color>,
    dim: bool,
) -> BoxenResult<ColoredString> {
    let mut styled = if let Some(c) = color {
        apply_foreground_color(text, c)?
    } else {
        ColoredString::from(text)
    };

    if dim {
        styled = styled.dimmed();
    }

    Ok(styled)
}

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

    #[test]
    fn test_parse_named_colors_basic() {
        assert!(parse_named_color("red").is_ok());
        assert!(parse_named_color("green").is_ok());
        assert!(parse_named_color("blue").is_ok());
        assert!(parse_named_color("black").is_ok());
        assert!(parse_named_color("white").is_ok());
        assert!(parse_named_color("yellow").is_ok());
        assert!(parse_named_color("magenta").is_ok());
        assert!(parse_named_color("cyan").is_ok());
    }

    #[test]
    fn test_parse_named_colors_bright() {
        assert!(parse_named_color("bright_red").is_ok());
        assert!(parse_named_color("brightred").is_ok());
        assert!(parse_named_color("bright_green").is_ok());
        assert!(parse_named_color("bright_blue").is_ok());
        assert!(parse_named_color("gray").is_ok());
        assert!(parse_named_color("grey").is_ok());
        assert!(parse_named_color("bright_black").is_ok());
        assert!(parse_named_color("brightblack").is_ok());
    }

    #[test]
    fn test_parse_named_colors_aliases() {
        assert!(parse_named_color("purple").is_ok());
        assert!(parse_named_color("bright_purple").is_ok());
        assert!(parse_named_color("brightpurple").is_ok());
    }

    #[test]
    fn test_parse_named_colors_case_insensitive() {
        assert!(parse_named_color("RED").is_ok());
        assert!(parse_named_color("Red").is_ok());
        assert!(parse_named_color("rEd").is_ok());
        assert!(parse_named_color("BRIGHT_BLUE").is_ok());
        assert!(parse_named_color("Bright_Blue").is_ok());
    }

    #[test]
    fn test_parse_named_colors_invalid() {
        assert!(parse_named_color("invalid_color").is_err());
        assert!(parse_named_color("").is_err());
        assert!(parse_named_color("orange").is_err());
        assert!(parse_named_color("pink").is_err());
    }

    #[test]
    fn test_parse_hex_colors_long_format() {
        // Valid 6-digit hex colors
        assert!(parse_hex_color("#FF0000").is_ok()); // Red
        assert!(parse_hex_color("#00FF00").is_ok()); // Green
        assert!(parse_hex_color("#0000FF").is_ok()); // Blue
        assert!(parse_hex_color("#FFFFFF").is_ok()); // White
        assert!(parse_hex_color("#000000").is_ok()); // Black
        assert!(parse_hex_color("#123456").is_ok()); // Random valid hex
        assert!(parse_hex_color("#ABCDEF").is_ok()); // With letters
        assert!(parse_hex_color("#abcdef").is_ok()); // Lowercase
    }

    #[test]
    fn test_parse_hex_colors_short_format() {
        // Valid 3-digit hex colors
        assert!(parse_hex_color("#F00").is_ok()); // Red
        assert!(parse_hex_color("#0F0").is_ok()); // Green
        assert!(parse_hex_color("#00F").is_ok()); // Blue
        assert!(parse_hex_color("#FFF").is_ok()); // White
        assert!(parse_hex_color("#000").is_ok()); // Black
        assert!(parse_hex_color("#123").is_ok()); // Random valid hex
        assert!(parse_hex_color("#ABC").is_ok()); // With letters
        assert!(parse_hex_color("#abc").is_ok()); // Lowercase
    }

    #[test]
    fn test_parse_hex_colors_without_hash() {
        // Should work without # prefix
        assert!(parse_hex_color("FF0000").is_ok());
        assert!(parse_hex_color("F00").is_ok());
        assert!(parse_hex_color("123456").is_ok());
        assert!(parse_hex_color("ABC").is_ok());
    }

    #[test]
    fn test_parse_hex_colors_invalid_length() {
        assert!(parse_hex_color("#FF").is_err()); // Too short
        assert!(parse_hex_color("#FFFF").is_err()); // Invalid length
        assert!(parse_hex_color("#FFFFF").is_err()); // Invalid length
        assert!(parse_hex_color("#FFFFFFF").is_err()); // Too long
        assert!(parse_hex_color("").is_err()); // Empty
        assert!(parse_hex_color("#").is_err()); // Just hash
    }

    #[test]
    fn test_parse_hex_colors_invalid_characters() {
        assert!(parse_hex_color("#GGGGGG").is_err()); // Invalid hex chars
        assert!(parse_hex_color("#FF00ZZ").is_err()); // Invalid hex chars
        assert!(parse_hex_color("#FF 000").is_err()); // Space
        assert!(parse_hex_color("#FF-000").is_err()); // Dash
        assert!(parse_hex_color("#FF.000").is_err()); // Dot
    }

    #[test]
    fn test_parse_hex_color_values() {
        // Test that hex parsing produces correct RGB values
        let red = parse_hex_color("#FF0000").unwrap();
        if let colored::Color::TrueColor { r, g, b } = red {
            assert_eq!(r, 255);
            assert_eq!(g, 0);
            assert_eq!(b, 0);
        } else {
            panic!("Expected TrueColor");
        }

        let green = parse_hex_color("#00FF00").unwrap();
        if let colored::Color::TrueColor { r, g, b } = green {
            assert_eq!(r, 0);
            assert_eq!(g, 255);
            assert_eq!(b, 0);
        } else {
            panic!("Expected TrueColor");
        }

        // Test short format expansion
        let short_red = parse_hex_color("#F00").unwrap();
        if let colored::Color::TrueColor { r, g, b } = short_red {
            assert_eq!(r, 255); // F -> FF
            assert_eq!(g, 0); // 0 -> 00
            assert_eq!(b, 0); // 0 -> 00
        } else {
            panic!("Expected TrueColor");
        }
    }

    #[test]
    fn test_parse_color_enum_variants() {
        // Test Color::Named
        let named = Color::Named("red".to_string());
        assert!(parse_color(&named).is_ok());

        // Test Color::Hex
        let hex = Color::Hex("#FF0000".to_string());
        assert!(parse_color(&hex).is_ok());

        // Test Color::Rgb
        let rgb = Color::Rgb(255, 0, 0);
        let parsed = parse_color(&rgb).unwrap();
        if let colored::Color::TrueColor { r, g, b } = parsed {
            assert_eq!(r, 255);
            assert_eq!(g, 0);
            assert_eq!(b, 0);
        } else {
            panic!("Expected TrueColor");
        }
    }

    #[test]
    fn test_validate_color() {
        // Valid colors
        assert!(validate_color(&Color::Named("red".to_string())).is_ok());
        assert!(validate_color(&Color::Hex("#FF0000".to_string())).is_ok());
        assert!(validate_color(&Color::Rgb(255, 0, 0)).is_ok());

        // Invalid colors
        assert!(validate_color(&Color::Named("invalid_color".to_string())).is_err());
        assert!(validate_color(&Color::Hex("#GGGGGG".to_string())).is_err());
    }

    #[test]
    fn test_apply_foreground_color() {
        let color = Color::Named("red".to_string());
        let result = apply_foreground_color("test", &color);
        assert!(result.is_ok());

        // Test with invalid color
        let invalid_color = Color::Named("invalid".to_string());
        let result = apply_foreground_color("test", &invalid_color);
        assert!(result.is_err());
    }

    #[test]
    fn test_apply_background_color() {
        let color = Color::Named("blue".to_string());
        let result = apply_background_color("test", &color);
        assert!(result.is_ok());

        // Test with invalid color
        let invalid_color = Color::Named("invalid".to_string());
        let result = apply_background_color("test", &invalid_color);
        assert!(result.is_err());
    }

    #[test]
    fn test_apply_colors_both() {
        let fg_color = Color::Named("red".to_string());
        let bg_color = Color::Named("blue".to_string());
        let result = apply_colors("test", Some(&fg_color), Some(&bg_color));
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_colors_foreground_only() {
        let fg_color = Color::Named("red".to_string());
        let result = apply_colors("test", Some(&fg_color), None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_colors_background_only() {
        let bg_color = Color::Named("blue".to_string());
        let result = apply_colors("test", None, Some(&bg_color));
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_colors_neither() {
        let result = apply_colors("test", None, None);
        assert!(result.is_ok());
    }

    #[test]
    fn test_apply_colors_invalid() {
        let invalid_color = Color::Named("invalid".to_string());
        let result = apply_colors("test", Some(&invalid_color), None);
        assert!(result.is_err());

        let result = apply_colors("test", None, Some(&invalid_color));
        assert!(result.is_err());
    }

    #[test]
    fn test_apply_dim() {
        let result = apply_dim("test");
        // We can't easily test the actual ANSI codes, but we can verify it doesn't panic
        assert!(!result.to_string().is_empty());
    }

    #[test]
    fn test_apply_color_with_dim() {
        let color = Color::Named("red".to_string());

        // Test with color and dim
        let result = apply_color_with_dim("test", Some(&color), true);
        assert!(result.is_ok());

        // Test with color but no dim
        let result = apply_color_with_dim("test", Some(&color), false);
        assert!(result.is_ok());

        // Test with no color but dim
        let result = apply_color_with_dim("test", None, true);
        assert!(result.is_ok());

        // Test with no color and no dim
        let result = apply_color_with_dim("test", None, false);
        assert!(result.is_ok());

        // Test with invalid color
        let invalid_color = Color::Named("invalid".to_string());
        let result = apply_color_with_dim("test", Some(&invalid_color), false);
        assert!(result.is_err());
    }

    #[test]
    fn test_color_from_implementations() {
        // Test From<String> for Color
        let color1: Color = "red".to_string().into();
        assert!(matches!(color1, Color::Named(_)));

        let color2: Color = "#FF0000".to_string().into();
        assert!(matches!(color2, Color::Hex(_)));

        // Test From<&str> for Color
        let color3: Color = "blue".into();
        assert!(matches!(color3, Color::Named(_)));

        let color4: Color = "#00FF00".into();
        assert!(matches!(color4, Color::Hex(_)));

        // Test From<(u8, u8, u8)> for Color
        let color5: Color = (255, 0, 0).into();
        assert!(matches!(color5, Color::Rgb(255, 0, 0)));
    }

    #[test]
    fn test_edge_cases() {
        // Test empty string
        assert!(parse_named_color("").is_err());

        // Test whitespace
        assert!(parse_named_color(" red ").is_err()); // We don't trim in named colors

        // Test hex with whitespace (should fail)
        assert!(parse_hex_color(" #FF0000 ").is_err());

        // Test very long strings
        let long_name = "a".repeat(1000);
        assert!(parse_named_color(&long_name).is_err());

        // Test RGB edge values
        let rgb_max = Color::Rgb(255, 255, 255);
        assert!(parse_color(&rgb_max).is_ok());

        let rgb_min = Color::Rgb(0, 0, 0);
        assert!(parse_color(&rgb_min).is_ok());
    }

    #[test]
    fn test_hex_color_case_sensitivity() {
        // Both should work
        assert!(parse_hex_color("#ABCDEF").is_ok());
        assert!(parse_hex_color("#abcdef").is_ok());
        assert!(parse_hex_color("#AbCdEf").is_ok());

        // Short format too
        assert!(parse_hex_color("#ABC").is_ok());
        assert!(parse_hex_color("#abc").is_ok());
        assert!(parse_hex_color("#AbC").is_ok());
    }

    #[test]
    fn test_comprehensive_color_validation() {
        // Test a variety of valid colors
        let valid_colors = vec![
            Color::Named("red".to_string()),
            Color::Named("BLUE".to_string()),
            Color::Named("bright_green".to_string()),
            Color::Hex("#FF0000".to_string()),
            Color::Hex("#F00".to_string()),
            Color::Hex("00FF00".to_string()),
            Color::Rgb(128, 64, 192),
            Color::Rgb(0, 0, 0),
            Color::Rgb(255, 255, 255),
        ];

        for color in valid_colors {
            assert!(
                validate_color(&color).is_ok(),
                "Color should be valid: {color:?}"
            );
        }

        // Test a variety of invalid colors
        let invalid_colors = vec![
            Color::Named("invalid_color_name".to_string()),
            Color::Named(String::new()),
            Color::Hex("#GGGGGG".to_string()),
            Color::Hex("#FF".to_string()),
            Color::Hex("#FFFFFFF".to_string()),
            Color::Hex(String::new()),
        ];

        for color in invalid_colors {
            assert!(
                validate_color(&color).is_err(),
                "Color should be invalid: {color:?}"
            );
        }
    }
}