Skip to main content

Color

Enum Color 

Source
pub enum Color {
Show 20 variants Black, Red, Green, Yellow, Blue, Magenta, Cyan, White, DarkGray, LightRed, LightGreen, LightYellow, LightBlue, LightMagenta, LightCyan, LightGray, Rgb { r: u8, g: u8, b: u8, }, AnsiValue(u8), Reset, Transparent,
}
Expand description

A color that can be displayed in the terminal.

Supports named colors, RGB values, and ANSI color codes.

Variants§

§

Black

Standard black color (ANSI 0)

§

Red

Standard red color (ANSI 1)

§

Green

Standard green color (ANSI 2)

§

Yellow

Standard yellow color (ANSI 3)

§

Blue

Standard blue color (ANSI 4)

§

Magenta

Standard magenta color (ANSI 5)

§

Cyan

Standard cyan color (ANSI 6)

§

White

Standard white color (ANSI 7)

§

DarkGray

Bright black/dark gray color (ANSI 8)

§

LightRed

Bright red color (ANSI 9)

§

LightGreen

Bright green color (ANSI 10)

§

LightYellow

Bright yellow color (ANSI 11)

§

LightBlue

Bright blue color (ANSI 12)

§

LightMagenta

Bright magenta color (ANSI 13)

§

LightCyan

Bright cyan color (ANSI 14)

§

LightGray

Bright white/light gray color (ANSI 15)

§

Rgb

Custom RGB color with red, green, and blue components (0-255 each)

Fields

§r: u8
§g: u8
§b: u8
§

AnsiValue(u8)

ANSI color code from the 256-color palette (0-255)

§

Reset

Terminal’s default foreground color

§

Transparent

No color change - maintains the current color

Implementations§

Source§

impl Color

Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Create an RGB color. Each component should be 0-255.

Examples found in repository?
examples/custom_colors.rs (line 83)
72fn draw_rainbow_gradient(window: &mut dyn Window, start_y: u16, width: u16) -> minui::Result<()> {
73    window.write_str(start_y, 0, "Rainbow Gradient:")?;
74
75    let gradient_y = start_y + 1;
76    let gradient_width = (width - 2).min(60); // Limit width for better display
77
78    for x in 0..gradient_width {
79        // Create a rainbow effect by cycling through hue
80        let hue = (x as f32 / gradient_width as f32) * 360.0;
81        let (r, g, b) = hsl_to_rgb(hue, 1.0, 0.5);
82
83        let color = ColorPair::new(Color::rgb(r, g, b), Color::Black);
84        window.write_str_colored(gradient_y, x + 1, "█", color)?;
85    }
86
87    Ok(())
88}
89
90fn draw_color_themes(window: &mut dyn Window, start_y: u16) -> minui::Result<()> {
91    window.write_str(start_y, 0, "Color Themes:")?;
92
93    // Sunset theme
94    window.write_str(start_y + 1, 0, "Sunset: ")?;
95    window.write_str_colored(start_y + 1, 8, "█████", SUNSET_ORANGE)?;
96    window.write_str_colored(start_y + 1, 13, "█████", SUNSET_RED)?;
97    window.write_str_colored(start_y + 1, 18, "█████", SUNSET_PURPLE)?;
98
99    // Ocean theme
100    window.write_str(start_y + 2, 0, "Ocean:  ")?;
101    window.write_str_colored(start_y + 2, 8, "█████", OCEAN_DEEP)?;
102    window.write_str_colored(start_y + 2, 13, "█████", OCEAN_MEDIUM)?;
103    window.write_str_colored(start_y + 2, 18, "█████", OCEAN_LIGHT)?;
104
105    // Forest theme
106    window.write_str(start_y + 3, 0, "Forest: ")?;
107    window.write_str_colored(start_y + 3, 8, "█████", FOREST_DARK)?;
108    window.write_str_colored(start_y + 3, 13, "█████", FOREST_BRIGHT)?;
109
110    // Neon theme
111    window.write_str(start_y + 4, 0, "Neon:   ")?;
112    window.write_str_colored(start_y + 4, 8, "█████", NEON_PINK)?;
113    window.write_str_colored(start_y + 4, 13, "█████", NEON_CYAN)?;
114    window.write_str_colored(start_y + 4, 18, "█████", NEON_GREEN)?;
115
116    Ok(())
117}
118
119fn draw_vertical_gradient(
120    window: &mut dyn Window,
121    start_y: u16,
122    width: u16,
123    height: u16,
124) -> minui::Result<()> {
125    if start_y + 10 > height {
126        return Ok(()); // Not enough space
127    }
128
129    window.write_str(start_y, 0, "Vertical Blue-to-Red Gradient:")?;
130
131    let gradient_height = (height - start_y - 2).min(8);
132    let gradient_width = (width - 2).min(30);
133
134    for y in 0..gradient_height {
135        let ratio = y as f32 / gradient_height as f32;
136
137        // Interpolate from blue to red
138        let r = (ratio * 255.0) as u8;
139        let g = 0;
140        let b = ((1.0 - ratio) * 255.0) as u8;
141
142        let color = ColorPair::new(Color::rgb(r, g, b), Color::Black);
143
144        for x in 0..gradient_width {
145            window.write_str_colored(start_y + 1 + y, x + 1, "█", color)?;
146        }
147    }
148
149    Ok(())
150}
151
152fn draw_color_blocks(window: &mut dyn Window, height: u16) -> minui::Result<()> {
153    let start_y = height.saturating_sub(6);
154
155    window.write_str(start_y, 0, "RGB Color Examples:")?;
156
157    // Show some specific RGB values with labels
158    let colors = [
159        (Color::rgb(255, 0, 0), "Pure Red (255,0,0)"),
160        (Color::rgb(0, 255, 0), "Pure Green (0,255,0)"),
161        (Color::rgb(0, 0, 255), "Pure Blue (0,0,255)"),
162        (Color::rgb(255, 255, 0), "Yellow (255,255,0)"),
163        (Color::rgb(255, 0, 255), "Magenta (255,0,255)"),
164        (Color::rgb(0, 255, 255), "Cyan (0,255,255)"),
165    ];
166
167    for (i, (color, label)) in colors.iter().enumerate() {
168        let y = start_y + 1 + (i as u16 / 3);
169        let x = (i % 3) * 25;
170
171        let color_pair = ColorPair::new(*color, Color::Black);
172        window.write_str_colored(y, x as u16, "███", color_pair)?;
173        window.write_str(y, x as u16 + 4, label)?;
174    }
175
176    Ok(())
177}
Source

pub const fn ansi(value: u8) -> Self

Create an ANSI color from the 256-color palette (0-255).

Source

pub const fn fg(self) -> ColorPair

Create a ColorPair with this color as foreground and transparent background.

§Examples
use minui::{Color, ColorPair};

let red_text = Color::Red.fg();
assert_eq!(red_text, ColorPair::new(Color::Red, Color::Transparent));
Examples found in repository?
examples/syntax_profile_demo.rs (line 65)
60fn draw_header(window: &mut dyn Window) -> minui::Result<()> {
61    window.write_str_colored(
62        0,
63        0,
64        "MinUI syntax + frame profiling demo (press q to quit)",
65        Color::LightCyan.fg(),
66    )?;
67    window.write_str_colored(
68        1,
69        0,
70        "write_spans_colored() is used below for token-based colouring.",
71        Color::DarkGray.fg(),
72    )?;
73    Ok(())
74}
75
76fn draw_code(window: &mut dyn Window) -> minui::Result<()> {
77    let kw = Color::LightMagenta.fg();
78    let ident = Color::LightBlue.fg();
79    let punct = Color::LightGray.fg();
80    let number = Color::LightYellow.fg();
81    let string = Color::LightGreen.fg();
82    let comment = Color::DarkGray.fg();
83    let plain = Color::White.fg();
84
85    window.write_spans_colored(
86        3,
87        0,
88        &[
89            ColoredSpan::new("fn", kw),
90            ColoredSpan::new(" ", plain),
91            ColoredSpan::new("render_status", ident),
92            ColoredSpan::new("(", punct),
93            ColoredSpan::new("fps", ident),
94            ColoredSpan::new(": ", punct),
95            ColoredSpan::new("u32", ident),
96            ColoredSpan::new(") {", punct),
97        ],
98    )?;
99
100    window.write_spans_colored(
101        4,
102        0,
103        &[
104            ColoredSpan::new("    ", plain),
105            ColoredSpan::new("let", kw),
106            ColoredSpan::new(" budget_ms = ", plain),
107            ColoredSpan::new("16", number),
108            ColoredSpan::new(";", punct),
109            ColoredSpan::new(" // ~60 FPS target", comment),
110        ],
111    )?;
112
113    window.write_spans_colored(
114        5,
115        0,
116        &[
117            ColoredSpan::new("    ", plain),
118            ColoredSpan::new("println!", ident),
119            ColoredSpan::new("(", punct),
120            ColoredSpan::new("\"fps={} budget={}ms\"", string),
121            ColoredSpan::new(", fps, budget_ms", plain),
122            ColoredSpan::new(");", punct),
123        ],
124    )?;
125
126    window.write_spans_colored(6, 0, &[ColoredSpan::new("}", punct)])?;
127    Ok(())
128}
129
130fn draw_profile_footer(state: &DemoState, window: &mut dyn Window) -> minui::Result<()> {
131    let (w, h) = window.get_size();
132    if h == 0 {
133        return Ok(());
134    }
135
136    let y = h.saturating_sub(1);
137    let profile = state.latest_profile.lock().ok().and_then(|g| *g);
138
139    if let Some(p) = profile {
140        let colour = if p.over_budget {
141            Color::LightRed.fg()
142        } else {
143            Color::LightGreen.fg()
144        };
145        let budget_ms = p.budget.map(|d| d.as_millis()).unwrap_or(0);
146        let line = format!(
147            "frame={} events={} total={}ms input={}ms update={}ms draw={}ms budget={}ms over_budget={}",
148            state.frame_count,
149            p.events_processed,
150            p.frame_time.as_millis(),
151            p.input_poll_time.as_millis(),
152            p.update_time.as_millis(),
153            p.draw_time.as_millis(),
154            budget_ms,
155            p.over_budget
156        );
157        let clipped = clip_to_cells(&line, w, TabPolicy::SingleCell);
158        window.write_str_colored(y, 0, &clipped, colour)?;
159    } else {
160        window.write_str_colored(y, 0, "Waiting for frame profile...", Color::DarkGray.fg())?;
161    }
162
163    Ok(())
164}
Source

pub const fn bg(self) -> ColorPair

Create a ColorPair with this color as background and default foreground.

§Examples
use minui::{Color, ColorPair};

let white_bg = Color::White.bg();
assert_eq!(white_bg, ColorPair::new(Color::Reset, Color::White));
Source

pub fn to_crossterm(self) -> CrosstermColor

Converts to crossterm color type (used internally).

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 Hash for Color

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 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 UnsafeUnpin 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.