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: u8The red component of the color
g: u8The green component of the color
b: u8The blue component of the color
a: u8The opacity of the color
Implementations§
Source§impl Color
impl Color
Sourcepub fn new_hsv(hue: f32, saturation: f32, value: f32) -> Self
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);Sourcepub fn new_hsv_with_opacity(
hue: f32,
saturation: f32,
value: f32,
opacity: f32,
) -> Self
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);Sourcepub fn set_hsv(&mut self, hue: f32, saturation: f32, value: f32)
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.
Sourcepub fn set_hue(&mut self, hue: f32)
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.);Sourcepub fn get_saturation(self) -> f32
pub fn get_saturation(self) -> f32
Returns a color’s saturation in the range [0, 1]. See set_saturation for examples.
Sourcepub fn set_saturation(&mut self, saturation: f32)
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);Sourcepub fn set_value(&mut self, value: f32)
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);Sourcepub fn scale_hsv(&mut self, saturation_coefficient: f32, value_coefficient: f32)
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);Sourcepub fn generate_gradient_rgb(
key_colors: &[Self],
gradient_spans: &[usize],
) -> Vec<Self>
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 thankey_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);Sourcepub fn generate_gradient_hsv(
key_colors: &[Self],
gradient_spans: &[usize],
) -> Vec<Self>
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 thankey_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]);Sourcepub fn lerp_rgb(self, other: Self, coefficient: f32) -> Self
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§impl Color
impl Color
Sourcepub fn by_name_and_level(name: Name, level: Level) -> Self
pub fn by_name_and_level(name: Name, level: Level) -> Self
Takes a Name and Level value and returns the corresponding color constant.