color_conversion/
color_conversion.rs

1//! Example demonstrating color conversion between RGB, 256-color, and basic ANSI colors
2
3use inksac::prelude::*;
4
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    println!("Color Conversion Comparison (RGB → 256 → Basic):");
7    println!("----------------------------------------------");
8
9    let test_colors = [
10        ((0, 0, 0), "Black"),
11        ((255, 255, 255), "White"),
12        ((128, 128, 128), "Mid Gray"),
13        ((255, 0, 0), "Red"),
14        ((0, 255, 0), "Green"),
15        ((0, 0, 255), "Blue"),
16        ((255, 255, 0), "Yellow"),
17        ((255, 0, 255), "Magenta"),
18        ((0, 255, 255), "Cyan"),
19        ((128, 64, 32), "Brown"),
20        ((70, 130, 180), "Steel Blue"),
21    ];
22
23    for ((r, g, b), name) in test_colors {
24        // Original RGB color
25        let rgb_style = Style::builder()
26            .foreground(Color::new_rgb(r, g, b)?)
27            .bold()
28            .build();
29
30        // Get the 256-color code
31        let code_256 = Color::rgb_to_256(r, g, b);
32        let color_256 = Color::Color256(code_256);
33        let style_256 = Style::builder().foreground(color_256).bold().build();
34
35        // Get the basic ANSI color
36        let basic = Color::rgb_to_basic(r, g, b);
37        let basic_style = Style::builder().foreground(basic).bold().build();
38
39        println!(
40            "{:<15} RGB({:>3},{:>3},{:>3}): {} | 256({:>3}): {} | Basic: {}",
41            name,
42            r,
43            g,
44            b,
45            "■■■■".style(rgb_style),
46            code_256,
47            "■■■■".style(style_256),
48            "■■■■".style(basic_style),
49        );
50    }
51
52    // Show some edge cases
53    println!("\nEdge Cases and Special Colors:");
54    println!("-----------------------------");
55
56    let edge_cases = [
57        // Brown variations
58        ((139, 69, 19), "Saddle Brown"),
59        ((128, 64, 0), "Brown"),
60        ((165, 42, 42), "Brown 2"),
61        ((160, 82, 45), "Sienna"),
62        ((210, 105, 30), "Chocolate"),
63        ((184, 134, 11), "Dark Goldenrod"),
64        ((153, 76, 0), "Darker Brown"),
65        ((102, 51, 0), "Deep Brown"),
66        // Very dark gray (specific test case)
67        ((32, 32, 32), "Very Dark Gray"),
68        // Near-boundary cases
69        ((51, 51, 51), "Dark Gray"),
70        ((102, 102, 102), "Medium Gray"),
71        ((204, 204, 204), "Light Gray"),
72        ((254, 254, 254), "Almost White"),
73        ((1, 1, 1), "Almost Black"),
74        // Almost-primary colors
75        ((254, 0, 0), "Near Red"),
76        ((0, 254, 0), "Near Green"),
77        ((0, 0, 254), "Near Blue"),
78        // Web colors
79        ((147, 112, 219), "Medium Purple"),
80        ((64, 224, 208), "Turquoise"),
81        ((250, 128, 114), "Salmon"),
82        ((85, 107, 47), "Dark Olive"),
83        ((219, 112, 147), "Pale Violet"),
84        // Subtle variations
85        ((128, 0, 0), "Maroon"),
86        ((128, 0, 128), "Purple"),
87        ((0, 128, 128), "Teal"),
88        // Mixed intensities
89        ((192, 64, 64), "Light Red"),
90        ((64, 192, 64), "Light Green"),
91        ((64, 64, 192), "Light Blue"),
92        // Color cube edge cases
93        ((51, 0, 0), "Dark Red"),
94        ((102, 0, 0), "Medium Red"),
95        ((204, 0, 0), "Bright Red"),
96        // Pastels
97        ((255, 182, 193), "Light Pink"),
98        ((176, 224, 230), "Powder Blue"),
99        ((255, 218, 185), "Peach Puff"),
100        // Earth tones
101        ((210, 180, 140), "Tan"),
102        // Neon colors
103        ((255, 0, 127), "Neon Pink"),
104        ((127, 255, 0), "Neon Green"),
105        ((0, 127, 255), "Neon Blue"),
106        // Gradient steps
107        ((51, 51, 51), "20% Gray"),
108        ((102, 102, 102), "40% Gray"),
109        ((153, 153, 153), "60% Gray"),
110        ((204, 204, 204), "80% Gray"),
111        // Color blends
112        ((64, 0, 128), "Deep Purple"),
113        ((0, 128, 64), "Sea Green"),
114    ];
115
116    for ((r, g, b), name) in edge_cases {
117        let rgb_style = Style::builder()
118            .foreground(Color::new_rgb(r, g, b)?)
119            .bold()
120            .build();
121
122        let code_256 = Color::rgb_to_256(r, g, b);
123        let style_256 = Style::builder()
124            .foreground(Color::Color256(code_256))
125            .bold()
126            .build();
127
128        let basic = Color::rgb_to_basic(r, g, b);
129        let basic_style = Style::builder().foreground(basic).bold().build();
130
131        println!(
132            "{:<15} RGB({:>3},{:>3},{:>3}): {} | 256({:>3}): {} | Basic: {}",
133            name,
134            r,
135            g,
136            b,
137            "■■■■".style(rgb_style),
138            code_256,
139            "■■■■".style(style_256),
140            "■■■■".style(basic_style),
141        );
142    }
143
144    Ok(())
145}