Struct Color

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

Represents an RGB color.

Implementations§

Source§

impl Color

Source

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

Creates a new Color instance with the given RGB values.

§Arguments
  • r - Red component (0-255)
  • g - Green component (0-255)
  • b - Blue component (0-255)
Examples found in repository?
examples/dlog.rs (line 40)
20fn showcase_log_levels() {
21    println!("\n{}", "Log Levels Demonstration:".style(Style::Bold).style(Style::Italic));
22
23    // Helper function to set log level, print color-coded messages, and log all levels
24    fn demonstrate_log_level(level: Level, color: Color, description: &str) {
25        println!("\n{}", format!("Log Level: {}", description).style(Style::Bold).color(color));
26        set_max_level(level);
27        log_all_levels();
28    }
29
30    // Function to log messages at all levels
31    fn log_all_levels() {
32        trace!("Most detailed level, useful for step-by-step debugging");
33        debug!("Useful for diagnosing issues");
34        info!("General operational messages about program execution");
35        warn!("Something unexpected happened, but the program can still continue");
36        error!("A serious problem occurred, indicating potential a failure");
37    }
38
39    // Demonstrate log levels with different settings
40    demonstrate_log_level(Level::Trace, Color::new(180,   0, 158), "Trace (all levels visible)");
41    demonstrate_log_level(Level::Debug, Color::new( 97, 214, 214), "Debug (Trace hidden, Debug and above visible)");
42    demonstrate_log_level(Level::Info,  Color::new( 22, 198,  12), "Info (Trace and Debug hidden, Info and above visible)");
43    demonstrate_log_level(Level::Warn,  Color::new(245, 245,  57), "Warn (Only Warn and Error visible)");
44    demonstrate_log_level(Level::Error, Color::new(231,  72,  86), "Error (Only Error logs visible)");
45
46    // Restore Trace level at the end
47    set_max_level(Level::Trace);
48    println!("\n{}", "Log Level restored to Trace.".style(Style::Bold).color(GREEN));
49}
Source

pub fn to_rgb(&self) -> (u8, u8, u8)

Returns the RGB components as a tuple.

Examples found in repository?
examples/formatting.rs (line 66)
63    fn create_gradient(start: Color, end: Color, steps: usize) -> String {
64        (0..steps).map(|i| {
65            let t = i as f32 / (steps - 1) as f32;
66            let r = (start.to_rgb().0 as f32 * (1.0 - t) + end.to_rgb().0 as f32 * t) as u8;
67            let g = (start.to_rgb().1 as f32 * (1.0 - t) + end.to_rgb().1 as f32 * t) as u8;
68            let b = (start.to_rgb().2 as f32 * (1.0 - t) + end.to_rgb().2 as f32 * t) as u8;
69            "■".color(Color::from((r, g, b)))
70        }).collect()
71    }
Source

pub fn as_fg(&self) -> String

Returns the ANSI escape code for setting this color as the foreground color.

§Examples
use dev_utils::format::Color;
 
let red = Color::new(255, 0, 0);
println!("{}Red text\x1b[0m", red.as_fg());
Source

pub fn as_bg(&self) -> String

Returns the ANSI escape code for setting this color as the background color.

§Examples
use dev_utils::format::Color;
 
let blue = Color::new(0, 0, 255);
println!("{}Text with blue background\x1b[0m", blue.as_bg());

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a copy 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 From<(u8, u8, u8)> for Color

Source§

fn from(rgb: (u8, u8, u8)) -> Self

Creates a Color from an RGB tuple.

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.