Color

Struct Color 

Source
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}
Expand description

A struct representing a 24-bit RGB color with alpha

Fields§

§r: u8

The red component of the color

§g: u8

The green component of the color

§b: u8

The blue component of the color

§a: u8

The opacity of the color

Implementations§

Source§

impl Color

Source

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

Returns a new Color from RGB values.

§Parameters
  • r - The color’s amount of red.
  • g - The color’s amount of green.
  • b - The color’s amount of blue.
§Example
let white = Color::new(255, 255, 255);
Source

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

Returns a new Color from RGB values with an opacity.

§Parameters
  • r - The color’s amount of red.
  • g - The color’s amount of green.
  • b - The color’s amount of blue.
  • a - The color’s opacity.
§Example
let translucent_white = Color::new_with_alpha(255, 255, 255, 127);
Source

pub fn new_hsv(hue: f32, saturation: f32, value: f32) -> Self

Returns a new Color from HSV values.

The saturation and value parameters are automatically clamped to 0 and 1.

Use set_hsv() to fill an existing struct with HSV values.

§Parameters
  • hue - The color’s hue in degrees.
  • saturation - The color’s saturation, from 0 to 1.
  • value - The color’s value, from 0 to 1.
§Example
let light_blue = Color::new_hsv(240.0, 0.75, 1.0);
Source

pub fn new_hsv_with_opacity( hue: f32, saturation: f32, value: f32, opacity: f32, ) -> Self

Returns a new Color from HSV values with the given opacity.

The saturation, value and opacity parameters are automatically clamped to 0 and 1.

Use set_hsv() to fill an existing struct with HSV values.

§Parameters
  • hue - The color’s hue in degrees.
  • saturation - The color’s saturation, from 0 to 1.
  • value - The color’s value, from 0 to 1.
  • opacity - The color’s opacity, from 0 to 1.
§Example
let translucent_light_blue = Color::new_hsv_with_opacity(240.0, 0.75, 1.0, 0.5);
Source

pub fn set_hsv(&mut self, hue: f32, saturation: f32, value: f32)

Sets a colors values from HSV values.

§Parameters
  • hue - The color’s hue in degrees.
  • saturation - The color’s saturation, from 0 to 1.
  • value - The color’s value, from 0 to 1.

Values outside the given ranges are clipped to fit within the allowed range.

Source

pub fn get_hsv(self) -> (f32, f32, f32)

Get a tuple of HSV values from a color.

Source

pub fn set_hue(&mut self, hue: f32)

Change a color’s hue.

§Parameters
  • hue - The color’s hue in degrees. Values outside the given range loop around to fit within the allowed range.
§Examples
// Sets the hue of the color to 16 degrees.
color.set_hue(16.);
// Values outside the range of 0-360 will be clipped:
color.set_hue(420.);

// The hue is actually set to 60 degrees.
let hue = color.get_hue();
assert!((hue - 60.).abs() < 1.);

color.set_hue(-90.);

// The hue is actually set to 270 degrees.
let hue = color.get_hue();
assert!((hue - 270.).abs() < 1.);
Source

pub fn get_hue(self) -> f32

Return a color’s hue in degrees. See set_hue for examples.

Source

pub fn get_saturation(self) -> f32

Returns a color’s saturation in the range [0, 1]. See set_saturation for examples.

Source

pub fn set_saturation(&mut self, saturation: f32)

Change a color’s saturation.

§Parameters
  • saturation - The color’s saturation, from 0 to 1. Values outside the given range are clipped to fit within the allowed range.
§Examples
// Sets the saturation of the color to 0.75.
color.set_saturation(0.75);
// Values outside the range of 0-1 will be clipped:
color.set_saturation(2.);

// The saturation is actually set to 1.
let saturation = color.get_saturation();
assert!((saturation - 1.).abs() < 0.001);

color.set_saturation(-2.);

// The saturation is actually set to 1.
let saturation = color.get_saturation();
assert!((saturation - 0.).abs() < 0.001);
Source

pub fn get_value(self) -> f32

Returns a color’s value in the range [0, 1].

Source

pub fn set_value(&mut self, value: f32)

Change a color’s value.

§Parameters
  • value - The color’s value, from 0 to 1.
§Examples
// Sets the value of the color to 0.25.
color.set_value(0.25);
// Values outside the range of 0-1 will be clipped:
color.set_value(2.);

// The saturation is actually set to 1.
let value = color.get_value();
assert!((value - 1.).abs() < 0.001);

color.set_value(-2.);

// The saturation is actually set to 0.
let value = color.get_value();
assert!((value - 0.).abs() < 0.001);
Source

pub fn shift_hue(&mut self, hue_shift: f32)

Shift a color’s hue by an amount.

§Parameters
  • hue_shift - The distance to shift the hue, in degrees.
§Example
// Shift the hue by 10 degrees
color.shift_hue(10.);

// Shift the hue back to what it was originally
color.shift_hue(-10.);
Source

pub fn scale_hsv(&mut self, saturation_coefficient: f32, value_coefficient: f32)

Scale a color’s saturation and value.

§Parameters
  • saturation_coefficient - Multiplier for this color’s saturation.
  • value_coefficient - Multiplier for this color’s value.
§Example

// Let's make a color with a saturation of 0.5 and a value of 0.75
let mut color = Color::new_hsv(10., 0.5, 0.75);

// Scale them by 1/2 and 1/3, respectively
color.scale_hsv(0.5, 1. / 3.);

// They should both be 0.25 now
assert!((color.get_saturation() - 0.25).abs() < 0.001);
assert!((color.get_value() - 0.25).abs() < 0.001);
Source

pub fn generate_gradient_rgb( key_colors: &[Self], gradient_spans: &[usize], ) -> Vec<Self>

Generates an interpolated gradient of colors using RGB interpolation.

Using RGB interpolation between colors is almost always the wrong choice and tends to produce really ugly results. You almost certainly don’t want to use this; use generate_gradient_hsv() instead.

§Parameters
  • key_colors - The colors to make gradients between.
  • gradient_spans - How many interpolated colors to generate between each pair of key colors.
§Panics
  • If gradient_spans’ length isn’t one less than key_colors’ length.
§Example
// Generates no colors at all
let none = Color::generate_gradient_rgb(&[], &[]);

assert!(none.is_empty());
// Generates only the given color
let one = Color::generate_gradient_rgb(&[Color::WHITE], &[]);

assert_eq!(one.len(), 1);
assert_eq!(one[0], Color::WHITE);
// Generates every grayscale color between black and white
let grayscale = Color::generate_gradient_rgb(&[Color::BLACK, Color::WHITE], &[254]);

assert_eq!(grayscale.len(), 256);
Source

pub fn generate_gradient_hsv( key_colors: &[Self], gradient_spans: &[usize], ) -> Vec<Self>

Generates an interpolated gradient of colors using HSV interpolation.

§Parameters
  • key_colors - The colors to make gradients between.
  • gradient_spans - How many interpolated colors to generate between each pair of key colors.
§Panics
  • If gradient_spans’ length isn’t one less than key_colors’ length.
§Examples
// Generates no colors at all
let none = Color::generate_gradient_hsv(&[], &[]);

assert!(none.is_empty());
// Generates only the given color
let one = Color::generate_gradient_hsv(&[Color::WHITE], &[]);

assert_eq!(one.len(), 1);
assert_eq!(one[0], Color::WHITE);
// Generates every grayscale color between black and white
let grayscale = Color::generate_gradient_hsv(&[Color::BLACK, Color::WHITE], &[254]);
Source

pub fn lerp_rgb(self, other: Self, coefficient: f32) -> Self

Interpolate two colors together using their RGB representation and return the result.

You almost certainly don’t want to use this; use lerp_hsv() instead.

§Parameters
  • other - The second color.
  • coefficient - The coefficient. 0 for entirely the first color, 1 for entirely the second.
§Panics

If coefficient is outside the range [0, 1].

Source

pub fn lerp_hsv(self, other: Self, coefficient: f32) -> Self

Interpolate two colors together using their HSV representation and return the result.

§Parameters
  • other - The second color.
  • coefficient - The coefficient. 0 for entirely the first color, 1 for entirely the second.
§Panics

If coefficient is outside the range [0, 1].

Source§

impl Color

Source

pub fn by_name_and_level(name: Name, level: Level) -> Self

Takes a Name and Level value and returns the corresponding color constant.

Source§

impl Color

Source

pub const BLACK: Self

Source

pub const DARKEST_GRAY: Self

Source

pub const DARKER_GRAY: Self

Source

pub const DARK_GRAY: Self

Source

pub const GRAY: Self

Source

pub const LIGHT_GRAY: Self

Source

pub const LIGHTER_GRAY: Self

Source

pub const LIGHTEST_GRAY: Self

Source

pub const DARKEST_GREY: Self = Self::DARKEST_GRAY

Source

pub const DARKER_GREY: Self = Self::DARKER_GRAY

Source

pub const DARK_GREY: Self = Self::DARK_GRAY

Source

pub const GREY: Self = Self::GRAY

Source

pub const LIGHT_GREY: Self = Self::LIGHT_GRAY

Source

pub const LIGHTER_GREY: Self = Self::LIGHTER_GRAY

Source

pub const LIGHTEST_GREY: Self = Self::LIGHTEST_GRAY

Source

pub const WHITE: Self

Source

pub const DARKEST_SEPIA: Self

Source

pub const DARKER_SEPIA: Self

Source

pub const DARK_SEPIA: Self

Source

pub const SEPIA: Self

Source

pub const LIGHT_SEPIA: Self

Source

pub const LIGHTER_SEPIA: Self

Source

pub const LIGHTEST_SEPIA: Self

Source

pub const DESATURATED_RED: Self

Source

pub const DESATURATED_FLAME: Self

Source

pub const DESATURATED_ORANGE: Self

Source

pub const DESATURATED_AMBER: Self

Source

pub const DESATURATED_YELLOW: Self

Source

pub const DESATURATED_LIME: Self

Source

pub const DESATURATED_CHARTREUSE: Self

Source

pub const DESATURATED_GREEN: Self

Source

pub const DESATURATED_SEA: Self

Source

pub const DESATURATED_TURQUOISE: Self

Source

pub const DESATURATED_CYAN: Self

Source

pub const DESATURATED_SKY: Self

Source

pub const DESATURATED_AZURE: Self

Source

pub const DESATURATED_BLUE: Self

Source

pub const DESATURATED_HAN: Self

Source

pub const DESATURATED_VIOLET: Self

Source

pub const DESATURATED_PURPLE: Self

Source

pub const DESATURATED_FUCHSIA: Self

Source

pub const DESATURATED_MAGENTA: Self

Source

pub const DESATURATED_PINK: Self

Source

pub const DESATURATED_CRIMSON: Self

Source

pub const LIGHTEST_RED: Self

Source

pub const LIGHTEST_FLAME: Self

Source

pub const LIGHTEST_ORANGE: Self

Source

pub const LIGHTEST_AMBER: Self

Source

pub const LIGHTEST_YELLOW: Self

Source

pub const LIGHTEST_LIME: Self

Source

pub const LIGHTEST_CHARTREUSE: Self

Source

pub const LIGHTEST_GREEN: Self

Source

pub const LIGHTEST_SEA: Self

Source

pub const LIGHTEST_TURQUOISE: Self

Source

pub const LIGHTEST_CYAN: Self

Source

pub const LIGHTEST_SKY: Self

Source

pub const LIGHTEST_AZURE: Self

Source

pub const LIGHTEST_BLUE: Self

Source

pub const LIGHTEST_HAN: Self

Source

pub const LIGHTEST_VIOLET: Self

Source

pub const LIGHTEST_PURPLE: Self

Source

pub const LIGHTEST_FUCHSIA: Self

Source

pub const LIGHTEST_MAGENTA: Self

Source

pub const LIGHTEST_PINK: Self

Source

pub const LIGHTEST_CRIMSON: Self

Source

pub const LIGHTER_RED: Self

Source

pub const LIGHTER_FLAME: Self

Source

pub const LIGHTER_ORANGE: Self

Source

pub const LIGHTER_AMBER: Self

Source

pub const LIGHTER_YELLOW: Self

Source

pub const LIGHTER_LIME: Self

Source

pub const LIGHTER_CHARTREUSE: Self

Source

pub const LIGHTER_GREEN: Self

Source

pub const LIGHTER_SEA: Self

Source

pub const LIGHTER_TURQUOISE: Self

Source

pub const LIGHTER_CYAN: Self

Source

pub const LIGHTER_SKY: Self

Source

pub const LIGHTER_AZURE: Self

Source

pub const LIGHTER_BLUE: Self

Source

pub const LIGHTER_HAN: Self

Source

pub const LIGHTER_VIOLET: Self

Source

pub const LIGHTER_PURPLE: Self

Source

pub const LIGHTER_FUCHSIA: Self

Source

pub const LIGHTER_MAGENTA: Self

Source

pub const LIGHTER_PINK: Self

Source

pub const LIGHTER_CRIMSON: Self

Source

pub const LIGHT_RED: Self

Source

pub const LIGHT_FLAME: Self

Source

pub const LIGHT_ORANGE: Self

Source

pub const LIGHT_AMBER: Self

Source

pub const LIGHT_YELLOW: Self

Source

pub const LIGHT_LIME: Self

Source

pub const LIGHT_CHARTREUSE: Self

Source

pub const LIGHT_GREEN: Self

Source

pub const LIGHT_SEA: Self

Source

pub const LIGHT_TURQUOISE: Self

Source

pub const LIGHT_CYAN: Self

Source

pub const LIGHT_SKY: Self

Source

pub const LIGHT_AZURE: Self

Source

pub const LIGHT_BLUE: Self

Source

pub const LIGHT_HAN: Self

Source

pub const LIGHT_VIOLET: Self

Source

pub const LIGHT_PURPLE: Self

Source

pub const LIGHT_FUCHSIA: Self

Source

pub const LIGHT_MAGENTA: Self

Source

pub const LIGHT_PINK: Self

Source

pub const LIGHT_CRIMSON: Self

Source

pub const RED: Self

Source

pub const FLAME: Self

Source

pub const ORANGE: Self

Source

pub const AMBER: Self

Source

pub const YELLOW: Self

Source

pub const LIME: Self

Source

pub const CHARTREUSE: Self

Source

pub const GREEN: Self

Source

pub const SEA: Self

Source

pub const TURQUOISE: Self

Source

pub const CYAN: Self

Source

pub const SKY: Self

Source

pub const AZURE: Self

Source

pub const BLUE: Self

Source

pub const HAN: Self

Source

pub const VIOLET: Self

Source

pub const PURPLE: Self

Source

pub const FUCHSIA: Self

Source

pub const MAGENTA: Self

Source

pub const PINK: Self

Source

pub const CRIMSON: Self

Source

pub const DARK_RED: Self

Source

pub const DARK_FLAME: Self

Source

pub const DARK_ORANGE: Self

Source

pub const DARK_AMBER: Self

Source

pub const DARK_YELLOW: Self

Source

pub const DARK_LIME: Self

Source

pub const DARK_CHARTREUSE: Self

Source

pub const DARK_GREEN: Self

Source

pub const DARK_SEA: Self

Source

pub const DARK_TURQUOISE: Self

Source

pub const DARK_CYAN: Self

Source

pub const DARK_SKY: Self

Source

pub const DARK_AZURE: Self

Source

pub const DARK_BLUE: Self

Source

pub const DARK_HAN: Self

Source

pub const DARK_VIOLET: Self

Source

pub const DARK_PURPLE: Self

Source

pub const DARK_FUCHSIA: Self

Source

pub const DARK_MAGENTA: Self

Source

pub const DARK_PINK: Self

Source

pub const DARK_CRIMSON: Self

Source

pub const DARKER_RED: Self

Source

pub const DARKER_FLAME: Self

Source

pub const DARKER_ORANGE: Self

Source

pub const DARKER_AMBER: Self

Source

pub const DARKER_YELLOW: Self

Source

pub const DARKER_LIME: Self

Source

pub const DARKER_CHARTREUSE: Self

Source

pub const DARKER_GREEN: Self

Source

pub const DARKER_SEA: Self

Source

pub const DARKER_TURQUOISE: Self

Source

pub const DARKER_CYAN: Self

Source

pub const DARKER_SKY: Self

Source

pub const DARKER_AZURE: Self

Source

pub const DARKER_BLUE: Self

Source

pub const DARKER_HAN: Self

Source

pub const DARKER_VIOLET: Self

Source

pub const DARKER_PURPLE: Self

Source

pub const DARKER_FUCHSIA: Self

Source

pub const DARKER_MAGENTA: Self

Source

pub const DARKER_PINK: Self

Source

pub const DARKER_CRIMSON: Self

Source

pub const DARKEST_RED: Self

Source

pub const DARKEST_FLAME: Self

Source

pub const DARKEST_ORANGE: Self

Source

pub const DARKEST_AMBER: Self

Source

pub const DARKEST_YELLOW: Self

Source

pub const DARKEST_LIME: Self

Source

pub const DARKEST_CHARTREUSE: Self

Source

pub const DARKEST_GREEN: Self

Source

pub const DARKEST_SEA: Self

Source

pub const DARKEST_TURQUOISE: Self

Source

pub const DARKEST_CYAN: Self

Source

pub const DARKEST_SKY: Self

Source

pub const DARKEST_AZURE: Self

Source

pub const DARKEST_BLUE: Self

Source

pub const DARKEST_HAN: Self

Source

pub const DARKEST_VIOLET: Self

Source

pub const DARKEST_PURPLE: Self

Source

pub const DARKEST_FUCHSIA: Self

Source

pub const DARKEST_MAGENTA: Self

Source

pub const DARKEST_PINK: Self

Source

pub const DARKEST_CRIMSON: Self

Source

pub const BRASS: Self

Source

pub const COPPER: Self

Source

pub const GOLD: Self

Source

pub const SILVER: Self

Source

pub const CELADON: Self

Source

pub const PEACH: Self

Trait Implementations§

Source§

impl Add for Color

Source§

fn add(self, rhs: Self) -> Self::Output

Add two colors together and return the result.

Source§

type Output = Color

The resulting type after applying the + operator.
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 From<(u8, u8, u8)> for Color

Source§

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

Converts to this type from the input type.
Source§

impl From<(u8, u8, u8, u8)> for Color

Source§

fn from((r, g, b, a): Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for (u8, u8, u8)

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Color

Source§

fn from(c: Color) -> Self

Converts to this type from the input type.
Source§

impl Mul<f32> for Color

Source§

fn mul(self, rhs: f32) -> Self::Output

Multiply a color with a scalar value and return the result.

Source§

type Output = Color

The resulting type after applying the * operator.
Source§

impl Mul for Color

Source§

fn mul(self, rhs: Self) -> Self::Output

Multiply two colors together and return the result.

Source§

type Output = Color

The resulting type after applying the * operator.
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 Sub for Color

Source§

fn sub(self, rhs: Self) -> Self::Output

Subtract the right hand side from the left hand side and return the result.

Source§

type Output = Color

The resulting type after applying the - operator.
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.