/// Any ANSI color code scheme #[allow(clippy::exhaustive_enums)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pubenum Color { /// Available 4-bit ANSI color palette codes /// /// The user's terminal defines the meaning of the each palette code.
Ansi(AnsiColor), /// 256 (8-bit) color support /// /// - `0..16` are [`AnsiColor`] palette codes /// - `0..232` map to [`RgbColor`] color values /// - `232..` map to [`RgbColor`] gray-scale values
Ansi256(Ansi256Color), /// 24-bit ANSI RGB color codes
Rgb(RgbColor),
}
impl Color { /// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubfn on(self, background: impl Into<Color>) -> crate::Style { crate::Style::new()
.fg_color(Some(self))
.bg_color(Some(background.into()))
}
/// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubconstfn on_default(self) -> crate::Style { crate::Style::new().fg_color(Some(self))
}
/// Render the ANSI code for a foreground color #[inline] pubfn render_fg(self) -> impl core::fmt::Display + Copy { matchself { Self::Ansi(color) => color.as_fg_buffer(), Self::Ansi256(color) => color.as_fg_buffer(), Self::Rgb(color) => color.as_fg_buffer(),
}
}
impl AnsiColor { /// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubfn on(self, background: impl Into<Color>) -> crate::Style { crate::Style::new()
.fg_color(Some(self.into()))
.bg_color(Some(background.into()))
}
/// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubconstfn on_default(self) -> crate::Style { crate::Style::new().fg_color(Some(Color::Ansi(self)))
}
/// Render the ANSI code for a foreground color #[inline] pubfn render_fg(self) -> impl core::fmt::Display + Copy {
NullFormatter(self.as_fg_str())
}
/// 256 (8-bit) color support /// /// - `0..16` are [`AnsiColor`] palette codes /// - `0..232` map to [`RgbColor`] color values /// - `232..` map to [`RgbColor`] gray-scale values #[allow(clippy::exhaustive_structs)] #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] pubstruct Ansi256Color(pub u8);
impl Ansi256Color { /// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubfn on(self, background: impl Into<Color>) -> crate::Style { crate::Style::new()
.fg_color(Some(self.into()))
.bg_color(Some(background.into()))
}
/// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubconstfn on_default(self) -> crate::Style { crate::Style::new().fg_color(Some(Color::Ansi256(self)))
}
/// Get the raw value #[inline] pubconstfn index(self) -> u8 { self.0
}
impl RgbColor { /// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubfn on(self, background: impl Into<Color>) -> crate::Style { crate::Style::new()
.fg_color(Some(self.into()))
.bg_color(Some(background.into()))
}
/// Create a [`Style`][crate::Style] with this as the foreground #[inline] pubconstfn on_default(self) -> crate::Style { crate::Style::new().fg_color(Some(Color::Rgb(self)))
}
/// Red #[inline] pubconstfn r(self) -> u8 { self.0
}
/// Green #[inline] pubconstfn g(self) -> u8 { self.1
}
/// Blue #[inline] pubconstfn b(self) -> u8 { self.2
}
/// Render the ANSI code for a foreground color #[inline] pubfn render_fg(self) -> impl core::fmt::Display + Copy { self.as_fg_buffer()
}
letmut printed = true; if c1 != 0 {
printed = true; self.buffer[self.len] = b'0' + c1; self.len += 1;
} if c2 != 0 || printed { self.buffer[self.len] = b'0' + c2; self.len += 1;
} // If we received a zero value we must still print a value. self.buffer[self.len] = b'0' + c3; self.len += 1;
self
}
#[inline] fn as_str(&self) -> &str { // SAFETY: Only `&str` can be written to the buffer #[allow(unsafe_code)] unsafe {
core::str::from_utf8_unchecked(&self.buffer[0..self.len])
}
}
impl<D: core::fmt::Display> core::fmt::Display for NullFormatter<D> { #[inline] fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { let d = &self.0;
write!(f, "{d}")
}
}
#[cfg(test)] #[cfg(feature = "std")] mod test { usesuper::*;
#[test] fn max_display_buffer() { let c = RgbColor(255, 255, 255); let actual = c.render_fg().to_string();
assert_eq!(actual, "\u{1b}[38;2;255;255;255m");
assert_eq!(actual.len(), DISPLAY_BUFFER_CAPACITY);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.