StyleBuilder

Struct StyleBuilder 

Source
pub struct StyleBuilder { /* private fields */ }
Expand description

Builder for creating Style instances

Implementations§

Source§

impl StyleBuilder

Source

pub fn foreground(&mut self, color: Color) -> &mut Self

Set the foreground (text) color

Examples found in repository?
examples/error_messages.rs (line 8)
5fn print_status(status: &str, message: &str) {
6    match status {
7        "error" => {
8            let style = Style::builder().foreground(Color::Red).bold().build();
9            println!("{}: {}", "ERROR".style(style), message);
10        }
11        "warning" => {
12            let style = Style::builder().foreground(Color::Yellow).bold().build();
13            println!("{}: {}", "WARNING".style(style), message);
14        }
15        "success" => {
16            let style = Style::builder().foreground(Color::Green).bold().build();
17            println!("{}: {}", "SUCCESS".style(style), message);
18        }
19        _ => println!("{}: {}", status, message),
20    }
21}
More examples
Hide additional examples
examples/rainbow_text.rs (line 23)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let text = "Rainbow Text Example";
7    let colors = [
8        (255, 0, 0),   // Red
9        (255, 127, 0), // Orange
10        (255, 255, 0), // Yellow
11        (0, 255, 0),   // Green
12        (0, 0, 255),   // Blue
13        (75, 0, 130),  // Indigo
14        (148, 0, 211), // Violet
15    ];
16
17    // Print each character with a different color
18    for (i, c) in text.chars().enumerate() {
19        let color_idx = i % colors.len();
20        let (r, g, b) = colors[color_idx];
21
22        let style = Style::builder()
23            .foreground(Color::new_rgb(r, g, b)?)
24            .bold()
25            .build();
26
27        print!("{}", c.to_string().style(style));
28    }
29    println!();
30
31    Ok(())
32}
examples/table.rs (line 7)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let header_style = Style::builder()
7        .foreground(Color::White)
8        .background(Color::Blue)
9        .bold()
10        .build();
11
12    let alt_row_style = Style::builder()
13        .background(Color::new_rgb(240, 240, 240)?)
14        .build();
15
16    // Print header
17    println!(
18        "{}",
19        format!("{:<20} {:<15} {:<10}", "Name", "Role", "Status").style(header_style)
20    );
21
22    // Print rows
23    let data = [
24        ("Alice Smith", "Developer", "Active"),
25        ("Bob Johnson", "Designer", "Away"),
26        ("Carol White", "Manager", "Busy"),
27    ];
28
29    for (i, (name, role, status)) in data.iter().enumerate() {
30        let row = format!("{:<20} {:<15} {:<10}", name, role, status);
31        if i % 2 == 1 {
32            println!("{}", row.style(alt_row_style));
33        } else {
34            println!("{}", row);
35        }
36    }
37
38    Ok(())
39}
examples/progress_bar.rs (line 25)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    let total = 50;
18    // More vibrant and pleasing colors
19    let start_color = (86, 171, 255); // Light blue
20    let mid_color = (255, 135, 255); // Pink/Purple
21    let end_color = (98, 255, 161); // Mint green
22
23    // Styles for different parts of the progress bar
24    let empty_style = Style::builder()
25        .foreground(Color::new_rgb(70, 70, 70)?) // Darker gray for empty section
26        .build();
27
28    let border_style = Style::builder()
29        .foreground(Color::new_rgb(150, 150, 150)?) // Lighter gray for borders
30        .bold()
31        .build();
32
33    let percent_style = Style::builder().foreground(Color::White).bold().build();
34
35    print!("\n"); // Start with a newline for better spacing
36
37    for i in 0..=total {
38        let progress = i as f32 / total as f32;
39        let filled = (progress * 30.0) as usize; // Slightly shorter bar
40        let empty = 30 - filled;
41
42        // Two-stage color interpolation for smoother gradient
43        let (r, g, b) = if progress < 0.5 {
44            lerp_color(start_color, mid_color, progress * 2.0)
45        } else {
46            lerp_color(mid_color, end_color, (progress - 0.5) * 2.0)
47        };
48
49        let bar_style = Style::builder()
50            .foreground(Color::new_rgb(r, g, b)?)
51            .bold()
52            .build();
53
54        // Using better characters for the progress bar
55        print!("\r  "); // Add some left padding
56        print!("{}", "├".style(border_style));
57        print!("{}", "━".repeat(filled).style(bar_style));
58        print!("{}", "─".repeat(empty).style(empty_style));
59        print!("{}", "┤".style(border_style));
60
61        // Percentage with padding to prevent jitter
62        print!(
63            " {}%",
64            format!("{:>3}", (progress * 100.0) as u8).style(percent_style)
65        );
66
67        // Existing spinner for visual feedback
68        let spinner = match i % 10 {
69            0 => "⠋",
70            1 => "⠙",
71            2 => "⠹",
72            3 => "⠸",
73            4 => "⠼",
74            5 => "⠴",
75            6 => "⠦",
76            7 => "⠧",
77            8 => "⠇",
78            9 => "⠏",
79            _ => "",
80        };
81        print!(" {}", spinner.style(bar_style));
82
83        // Added spinning circle indicator on the right
84        let circle_spinner = match i % 4 {
85            0 => "◐",
86            1 => "◓",
87            2 => "◑",
88            3 => "◒",
89            _ => "",
90        };
91        print!(" {}", circle_spinner.style(bar_style));
92
93        std::io::stdout().flush()?;
94        thread::sleep(Duration::from_millis(50));
95    }
96    println!("\n"); // Add final newlines for spacing
97
98    Ok(())
99}
examples/print_color.rs (line 24)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
examples/color_conversion.rs (line 26)
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}
Source

pub fn background(&mut self, color: Color) -> &mut Self

Set the background color

Examples found in repository?
examples/table.rs (line 8)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let header_style = Style::builder()
7        .foreground(Color::White)
8        .background(Color::Blue)
9        .bold()
10        .build();
11
12    let alt_row_style = Style::builder()
13        .background(Color::new_rgb(240, 240, 240)?)
14        .build();
15
16    // Print header
17    println!(
18        "{}",
19        format!("{:<20} {:<15} {:<10}", "Name", "Role", "Status").style(header_style)
20    );
21
22    // Print rows
23    let data = [
24        ("Alice Smith", "Developer", "Active"),
25        ("Bob Johnson", "Designer", "Away"),
26        ("Carol White", "Manager", "Busy"),
27    ];
28
29    for (i, (name, role, status)) in data.iter().enumerate() {
30        let row = format!("{:<20} {:<15} {:<10}", name, role, status);
31        if i % 2 == 1 {
32            println!("{}", row.style(alt_row_style));
33        } else {
34            println!("{}", row);
35        }
36    }
37
38    Ok(())
39}
More examples
Hide additional examples
examples/print_color.rs (line 90)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
Source

pub fn bold(&mut self) -> &mut Self

Enable bold text

Examples found in repository?
examples/error_messages.rs (line 8)
5fn print_status(status: &str, message: &str) {
6    match status {
7        "error" => {
8            let style = Style::builder().foreground(Color::Red).bold().build();
9            println!("{}: {}", "ERROR".style(style), message);
10        }
11        "warning" => {
12            let style = Style::builder().foreground(Color::Yellow).bold().build();
13            println!("{}: {}", "WARNING".style(style), message);
14        }
15        "success" => {
16            let style = Style::builder().foreground(Color::Green).bold().build();
17            println!("{}: {}", "SUCCESS".style(style), message);
18        }
19        _ => println!("{}: {}", status, message),
20    }
21}
More examples
Hide additional examples
examples/rainbow_text.rs (line 24)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let text = "Rainbow Text Example";
7    let colors = [
8        (255, 0, 0),   // Red
9        (255, 127, 0), // Orange
10        (255, 255, 0), // Yellow
11        (0, 255, 0),   // Green
12        (0, 0, 255),   // Blue
13        (75, 0, 130),  // Indigo
14        (148, 0, 211), // Violet
15    ];
16
17    // Print each character with a different color
18    for (i, c) in text.chars().enumerate() {
19        let color_idx = i % colors.len();
20        let (r, g, b) = colors[color_idx];
21
22        let style = Style::builder()
23            .foreground(Color::new_rgb(r, g, b)?)
24            .bold()
25            .build();
26
27        print!("{}", c.to_string().style(style));
28    }
29    println!();
30
31    Ok(())
32}
examples/table.rs (line 9)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let header_style = Style::builder()
7        .foreground(Color::White)
8        .background(Color::Blue)
9        .bold()
10        .build();
11
12    let alt_row_style = Style::builder()
13        .background(Color::new_rgb(240, 240, 240)?)
14        .build();
15
16    // Print header
17    println!(
18        "{}",
19        format!("{:<20} {:<15} {:<10}", "Name", "Role", "Status").style(header_style)
20    );
21
22    // Print rows
23    let data = [
24        ("Alice Smith", "Developer", "Active"),
25        ("Bob Johnson", "Designer", "Away"),
26        ("Carol White", "Manager", "Busy"),
27    ];
28
29    for (i, (name, role, status)) in data.iter().enumerate() {
30        let row = format!("{:<20} {:<15} {:<10}", name, role, status);
31        if i % 2 == 1 {
32            println!("{}", row.style(alt_row_style));
33        } else {
34            println!("{}", row);
35        }
36    }
37
38    Ok(())
39}
examples/progress_bar.rs (line 30)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    let total = 50;
18    // More vibrant and pleasing colors
19    let start_color = (86, 171, 255); // Light blue
20    let mid_color = (255, 135, 255); // Pink/Purple
21    let end_color = (98, 255, 161); // Mint green
22
23    // Styles for different parts of the progress bar
24    let empty_style = Style::builder()
25        .foreground(Color::new_rgb(70, 70, 70)?) // Darker gray for empty section
26        .build();
27
28    let border_style = Style::builder()
29        .foreground(Color::new_rgb(150, 150, 150)?) // Lighter gray for borders
30        .bold()
31        .build();
32
33    let percent_style = Style::builder().foreground(Color::White).bold().build();
34
35    print!("\n"); // Start with a newline for better spacing
36
37    for i in 0..=total {
38        let progress = i as f32 / total as f32;
39        let filled = (progress * 30.0) as usize; // Slightly shorter bar
40        let empty = 30 - filled;
41
42        // Two-stage color interpolation for smoother gradient
43        let (r, g, b) = if progress < 0.5 {
44            lerp_color(start_color, mid_color, progress * 2.0)
45        } else {
46            lerp_color(mid_color, end_color, (progress - 0.5) * 2.0)
47        };
48
49        let bar_style = Style::builder()
50            .foreground(Color::new_rgb(r, g, b)?)
51            .bold()
52            .build();
53
54        // Using better characters for the progress bar
55        print!("\r  "); // Add some left padding
56        print!("{}", "├".style(border_style));
57        print!("{}", "━".repeat(filled).style(bar_style));
58        print!("{}", "─".repeat(empty).style(empty_style));
59        print!("{}", "┤".style(border_style));
60
61        // Percentage with padding to prevent jitter
62        print!(
63            " {}%",
64            format!("{:>3}", (progress * 100.0) as u8).style(percent_style)
65        );
66
67        // Existing spinner for visual feedback
68        let spinner = match i % 10 {
69            0 => "⠋",
70            1 => "⠙",
71            2 => "⠹",
72            3 => "⠸",
73            4 => "⠼",
74            5 => "⠴",
75            6 => "⠦",
76            7 => "⠧",
77            8 => "⠇",
78            9 => "⠏",
79            _ => "",
80        };
81        print!(" {}", spinner.style(bar_style));
82
83        // Added spinning circle indicator on the right
84        let circle_spinner = match i % 4 {
85            0 => "◐",
86            1 => "◓",
87            2 => "◑",
88            3 => "◒",
89            _ => "",
90        };
91        print!(" {}", circle_spinner.style(bar_style));
92
93        std::io::stdout().flush()?;
94        thread::sleep(Duration::from_millis(50));
95    }
96    println!("\n"); // Add final newlines for spacing
97
98    Ok(())
99}
examples/print_color.rs (line 24)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
examples/color_conversion.rs (line 27)
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}
Source

pub fn dim(&mut self) -> &mut Self

Enable dim text

Examples found in repository?
examples/print_color.rs (line 84)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
Source

pub fn italic(&mut self) -> &mut Self

Enable italic text

Examples found in repository?
examples/print_color.rs (line 78)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
Source

pub fn underline(&mut self) -> &mut Self

Enable underlined text

Examples found in repository?
examples/print_color.rs (line 81)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
Source

pub fn build(&self) -> Style

Build the final Style

Examples found in repository?
examples/error_messages.rs (line 8)
5fn print_status(status: &str, message: &str) {
6    match status {
7        "error" => {
8            let style = Style::builder().foreground(Color::Red).bold().build();
9            println!("{}: {}", "ERROR".style(style), message);
10        }
11        "warning" => {
12            let style = Style::builder().foreground(Color::Yellow).bold().build();
13            println!("{}: {}", "WARNING".style(style), message);
14        }
15        "success" => {
16            let style = Style::builder().foreground(Color::Green).bold().build();
17            println!("{}: {}", "SUCCESS".style(style), message);
18        }
19        _ => println!("{}: {}", status, message),
20    }
21}
More examples
Hide additional examples
examples/rainbow_text.rs (line 25)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let text = "Rainbow Text Example";
7    let colors = [
8        (255, 0, 0),   // Red
9        (255, 127, 0), // Orange
10        (255, 255, 0), // Yellow
11        (0, 255, 0),   // Green
12        (0, 0, 255),   // Blue
13        (75, 0, 130),  // Indigo
14        (148, 0, 211), // Violet
15    ];
16
17    // Print each character with a different color
18    for (i, c) in text.chars().enumerate() {
19        let color_idx = i % colors.len();
20        let (r, g, b) = colors[color_idx];
21
22        let style = Style::builder()
23            .foreground(Color::new_rgb(r, g, b)?)
24            .bold()
25            .build();
26
27        print!("{}", c.to_string().style(style));
28    }
29    println!();
30
31    Ok(())
32}
examples/table.rs (line 10)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    let header_style = Style::builder()
7        .foreground(Color::White)
8        .background(Color::Blue)
9        .bold()
10        .build();
11
12    let alt_row_style = Style::builder()
13        .background(Color::new_rgb(240, 240, 240)?)
14        .build();
15
16    // Print header
17    println!(
18        "{}",
19        format!("{:<20} {:<15} {:<10}", "Name", "Role", "Status").style(header_style)
20    );
21
22    // Print rows
23    let data = [
24        ("Alice Smith", "Developer", "Active"),
25        ("Bob Johnson", "Designer", "Away"),
26        ("Carol White", "Manager", "Busy"),
27    ];
28
29    for (i, (name, role, status)) in data.iter().enumerate() {
30        let row = format!("{:<20} {:<15} {:<10}", name, role, status);
31        if i % 2 == 1 {
32            println!("{}", row.style(alt_row_style));
33        } else {
34            println!("{}", row);
35        }
36    }
37
38    Ok(())
39}
examples/progress_bar.rs (line 26)
16fn main() -> Result<(), Box<dyn std::error::Error>> {
17    let total = 50;
18    // More vibrant and pleasing colors
19    let start_color = (86, 171, 255); // Light blue
20    let mid_color = (255, 135, 255); // Pink/Purple
21    let end_color = (98, 255, 161); // Mint green
22
23    // Styles for different parts of the progress bar
24    let empty_style = Style::builder()
25        .foreground(Color::new_rgb(70, 70, 70)?) // Darker gray for empty section
26        .build();
27
28    let border_style = Style::builder()
29        .foreground(Color::new_rgb(150, 150, 150)?) // Lighter gray for borders
30        .bold()
31        .build();
32
33    let percent_style = Style::builder().foreground(Color::White).bold().build();
34
35    print!("\n"); // Start with a newline for better spacing
36
37    for i in 0..=total {
38        let progress = i as f32 / total as f32;
39        let filled = (progress * 30.0) as usize; // Slightly shorter bar
40        let empty = 30 - filled;
41
42        // Two-stage color interpolation for smoother gradient
43        let (r, g, b) = if progress < 0.5 {
44            lerp_color(start_color, mid_color, progress * 2.0)
45        } else {
46            lerp_color(mid_color, end_color, (progress - 0.5) * 2.0)
47        };
48
49        let bar_style = Style::builder()
50            .foreground(Color::new_rgb(r, g, b)?)
51            .bold()
52            .build();
53
54        // Using better characters for the progress bar
55        print!("\r  "); // Add some left padding
56        print!("{}", "├".style(border_style));
57        print!("{}", "━".repeat(filled).style(bar_style));
58        print!("{}", "─".repeat(empty).style(empty_style));
59        print!("{}", "┤".style(border_style));
60
61        // Percentage with padding to prevent jitter
62        print!(
63            " {}%",
64            format!("{:>3}", (progress * 100.0) as u8).style(percent_style)
65        );
66
67        // Existing spinner for visual feedback
68        let spinner = match i % 10 {
69            0 => "⠋",
70            1 => "⠙",
71            2 => "⠹",
72            3 => "⠸",
73            4 => "⠼",
74            5 => "⠴",
75            6 => "⠦",
76            7 => "⠧",
77            8 => "⠇",
78            9 => "⠏",
79            _ => "",
80        };
81        print!(" {}", spinner.style(bar_style));
82
83        // Added spinning circle indicator on the right
84        let circle_spinner = match i % 4 {
85            0 => "◐",
86            1 => "◓",
87            2 => "◑",
88            3 => "◒",
89            _ => "",
90        };
91        print!(" {}", circle_spinner.style(bar_style));
92
93        std::io::stdout().flush()?;
94        thread::sleep(Duration::from_millis(50));
95    }
96    println!("\n"); // Add final newlines for spacing
97
98    Ok(())
99}
examples/print_color.rs (line 24)
5fn print_color_demo() -> Result<(), Box<dyn std::error::Error>> {
6    // First, check color support
7    let support = check_color_support()?;
8    println!("Color Support Level: {}\n", support);
9
10    // Basic colors
11    let basic_colors = [
12        ("Black", Color::Black),
13        ("Red", Color::Red),
14        ("Green", Color::Green),
15        ("Yellow", Color::Yellow),
16        ("Blue", Color::Blue),
17        ("Magenta", Color::Magenta),
18        ("Cyan", Color::Cyan),
19        ("White", Color::White),
20    ];
21
22    println!("Basic Colors:");
23    for (name, color) in basic_colors {
24        let style = Style::builder().foreground(color).bold().build();
25        println!("{:<8}: {}", name, "■■■■".style(style));
26    }
27    println!();
28
29    // RGB Colors (if supported)
30    if matches!(support, ColorSupport::TrueColor) {
31        println!("RGB Colors:");
32        let rgb_colors = [
33            (255, 100, 0),
34            (100, 255, 0),
35            (0, 255, 100),
36            (0, 100, 255),
37            (100, 0, 255),
38            (255, 0, 100),
39        ];
40
41        for (r, g, b) in rgb_colors {
42            let style = Style::builder()
43                .foreground(Color::new_rgb(r, g, b)?)
44                .build();
45            println!("RGB({}, {}, {}): {}", r, g, b, "■■■■".style(style));
46        }
47        println!();
48
49        // Color manipulation
50        let base_color = Color::new_rgb(255, 100, 0)?;
51        println!("Color Manipulation:");
52        println!(
53            "Original: {}",
54            "■■■■".style(Style::builder().foreground(base_color).build())
55        );
56
57        let lighter = base_color.lighten(30)?;
58        println!(
59            "Lighter : {}",
60            "■■■■".style(Style::builder().foreground(lighter).build())
61        );
62
63        let darker = base_color.darken(30)?;
64        println!(
65            "Darker  : {}",
66            "■■■■".style(Style::builder().foreground(darker).build())
67        );
68        println!();
69    }
70
71    // Text styles
72    println!("Text Styles:");
73    let text = "Styled Text";
74
75    let bold = Style::builder().bold().build();
76    println!("Bold:      {}", text.style(bold));
77
78    let italic = Style::builder().italic().build();
79    println!("Italic:    {}", text.style(italic));
80
81    let underline = Style::builder().underline().build();
82    println!("Underline: {}", text.style(underline));
83
84    let dim = Style::builder().dim().build();
85    println!("Dim:       {}", text.style(dim));
86
87    // Combined styles
88    let combined = Style::builder()
89        .foreground(Color::Green)
90        .background(Color::Black)
91        .bold()
92        .italic()
93        .build();
94    println!("\nCombined Styles:");
95    println!("{}", "Multiple styles combined!".style(combined));
96
97    Ok(())
98}
examples/color_conversion.rs (line 28)
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}

Trait Implementations§

Source§

impl Default for StyleBuilder

Source§

fn default() -> StyleBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.