Color

Enum Color 

Source
pub enum Color {
Show 14 variants Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, Empty, RGB(u8, u8, u8), Color256(u8), HEX(&'static str), HSV(u16, u8, u8), HSL(u16, u8, u8),
}

Variants§

§

Black

§

Red

§

Green

§

Yellow

§

Blue

§

Magenta

§

Cyan

§

White

§

Empty

§

RGB(u8, u8, u8)

§

Color256(u8)

§

HEX(&'static str)

§

HSV(u16, u8, u8)

§

HSL(u16, u8, u8)

Implementations§

Source§

impl Color

Source

pub fn new_rgb(r: u8, g: u8, b: u8) -> Result<Self, ColorError>

Create a new RGB color

§Arguments
  • r - Red component (0-255)
  • g - Green component (0-255)
  • b - Blue component (0-255)
§Returns
  • Ok(Color) - RGB color
§Examples
use inksac::Color;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let orange = Color::new_rgb(255, 165, 0)?;
    let purple = Color::new_rgb(128, 0, 128)?;
    Ok(())
}
Examples found in repository?
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}
More examples
Hide additional examples
examples/table.rs (line 13)
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 43)
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 new_hex(hex: &'static str) -> Result<Self, ColorError>

Create a new color from a hexadecimal color code

The hex code must start with ‘#’ and be followed by exactly 6 hexadecimal digits (e.g., “#FF0000” for red).

§Arguments
  • hex - Hexadecimal color code (e.g., “#FF0000”)
§Returns
  • Ok(Color) if the hex code is valid
  • Err(ColorError) if the hex code is invalid
§Examples
use inksac::Color;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let red = Color::new_hex("#FF0000")?;
    let green = Color::new_hex("#00FF00")?;
    Ok(())
}
Source

pub fn new_hsv(h: u16, s: u8, v: u8) -> Result<Self, ColorError>

Create a new HSV color

§Arguments
  • h - Hue (0-360)
  • s - Saturation (0-100)
  • v - Value (0-100)
§Returns
  • Ok(Color) if the values are valid
  • Err(ColorError) if the values are out of range
Source

pub fn new_hsl(h: u16, s: u8, l: u8) -> Result<Self, ColorError>

Create a new HSL color

§Arguments
  • h - Hue (0-360)
  • s - Saturation (0-100)
  • l - Lightness (0-100)
§Returns
  • Ok(Color) if the values are valid
  • Err(ColorError) if the values are out of range
Source§

impl Color

Source

pub fn rgb_to_256(r: u8, g: u8, b: u8) -> u8

Convert RGB color values to the nearest 256-color code

This function maps RGB colors to the 256-color palette used by many terminals. It handles both the 6x6x6 color cube (216 colors) and the grayscale ramp (24 levels).

§Arguments
  • r - Red component (0-255)
  • g - Green component (0-255)
  • b - Blue component (0-255)
§Returns
  • u8 - The nearest 256-color code (16-255)
Examples found in repository?
examples/color_conversion.rs (line 31)
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 code_to_rgb(code: u8) -> (u8, u8, u8)

Convert 256-color code to RGB color values

§Arguments
  • code - The 256-color code (16-255)
§Returns
  • (u8, u8, u8) - RGB color components (0-255)
Source

pub fn rgb_to_basic(r: u8, g: u8, b: u8) -> Color

Convert RGB color values to the nearest basic ANSI color

This function maps RGB colors to the 8 basic ANSI colors by analyzing the relative luminance and dominant color components.

§Arguments
  • r - Red component (0-255)
  • g - Green component (0-255)
  • b - Blue component (0-255)
§Returns
  • Color - The nearest basic ANSI color
Examples found in repository?
examples/color_conversion.rs (line 36)
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§

impl Color

Source

pub fn lighten(self, percent: u8) -> Result<Self, ColorError>

Lighten a color by a percentage

§Arguments
  • percent - Amount to lighten (0-100)
§Returns
  • Ok(Color) - Lightened color
  • Err(ColorError) - If color manipulation fails
§Examples
use inksac::Color;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let color = Color::new_rgb(255, 100, 0)?;
    let lighter = color.lighten(30)?;
    Ok(())
}
Examples found in repository?
examples/print_color.rs (line 57)
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 darken(self, percent: u8) -> Result<Self, ColorError>

Darken a color by a percentage

§Arguments
  • percent - Amount to darken (0-100)
§Returns
  • Ok(Color) - Darkened color
  • Err(ColorError) - If color manipulation fails
§Examples
use inksac::Color;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let color = Color::new_rgb(255, 100, 0)?;
    let darker = color.darken(30)?;
    Ok(())
}
Examples found in repository?
examples/print_color.rs (line 63)
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}

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Color

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Color

Source§

fn default() -> Color

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

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for Color

Source§

impl Eq for Color

Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnwindSafe for Color

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.