Color

Trait Color 

Source
pub trait Color
where Self: Sized,
{ // Required methods fn blend(color_top: &Self, color_bottom: &Self) -> Self; fn color( text: &str, foreground_color: &Self, background_color: &Self, ) -> String; fn distance(color1: &Self, color2: &Self) -> f32; fn mix(colors: &[Self]) -> Self; // Provided method fn group<const N: usize>(colors: &[Self; N]) -> [bool; N] { ... } }
Expand description

Defines a color used to color text.

Required Methods§

Source

fn blend(color_top: &Self, color_bottom: &Self) -> Self

Blends the top with the bottom color. In the case of opaque colors this simply returns the top color. If the top color is completely transparent it returns the bottom color. For a translucent top color this returns a mix of both colors weighted by their opacity.

§Examples
use console_display::color::{
    Color,
    RGBColor,
};
use rand::{
    Rng,
    thread_rng,
};

let top = RGBColor {
    r: thread_rng().gen_range(0..=255),
    g: thread_rng().gen_range(0..=255),
    b: thread_rng().gen_range(0..=255),
};
let bottom = RGBColor {
    r: thread_rng().gen_range(0..=255),
    g: thread_rng().gen_range(0..=255),
    b: thread_rng().gen_range(0..=255),
};

// Opaque top color is returned.
assert_eq!(top, RGBColor::blend(&top, &bottom));
Source

fn color(text: &str, foreground_color: &Self, background_color: &Self) -> String

Colors a text with a foreground and background color.

§Examples
use console_display::color::{
    Color,
    RGBColor,
};

let msg =
    RGBColor::color("test", &RGBColor::RED, &RGBColor::BLACK);

// msg contains escape sequences containing color information.
assert!(msg.chars().count() > 4);
Source

fn distance(color1: &Self, color2: &Self) -> f32

Calculates the relative distance between two colors. For smaller distances the returned value is smaller. There are no guarantees for the exact return value, so this has only meaning in comparisons.

§Examples
use console_display::color::{
    Color,
    RGBColor,
    TerminalColor,
};

let dist_prio = TerminalColor::distance(
    &RGBColor::BLACK.into(),
    &TerminalColor::Default,
);
let dist_min = TerminalColor::distance(
    &RGBColor::BLACK.into(),
    &RGBColor::BLACK.into(),
);
let dist = TerminalColor::distance(
    &RGBColor::BLACK.into(),
    &RGBColor::RED.into(),
);
let dist_max = TerminalColor::distance(
    &RGBColor::BLACK.into(),
    &RGBColor::WHITE.into(),
);

assert!(dist_prio <= dist_min);
assert!(dist_min < dist);
assert!(dist < dist_max);
Source

fn mix(colors: &[Self]) -> Self

Mixes a list of colors into one.

§Examples
use console_display::color::{
    Color,
    RGBColor,
    TerminalColor,
};

let colors = [
    RGBColor::BLACK.into(),
    TerminalColor::Default,
    RGBColor::WHITE.into(),
];

let mix = TerminalColor::mix(&colors);

assert_eq!(
    mix,
    // gray, Default is ignored in calculation
    RGBColor {
        r: 255 / 2,
        g: 255 / 2,
        b: 255 / 2
    }
    .into()
);

Provided Methods§

Source

fn group<const N: usize>(colors: &[Self; N]) -> [bool; N]

Groups a list of colors into two groups. The groups are formed by finding the most distant colors. All other colors are then put into a group with one of them depending on which distance is smaller. Returns an array of the size of the input. True and false are used to map each color into one of two groups.

§Examples
use console_display::color::{
    Color,
    RGBColor,
};

let colors =
    [RGBColor::BLACK, RGBColor::WHITE, RGBColor::BLACK];

let grouping = RGBColor::group(&colors);

assert_ne!(grouping[0], grouping[1]);
assert_ne!(grouping[1], grouping[2]);
assert_eq!(grouping[0], grouping[2]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§