pub struct Rgb { /* private fields */ }Expand description
The RGB color model.
Has red, green, blue and optional alpha channel fields.
Red, green, blue values are stored between 0.0 and 255.0, alpha is between 0.0 and 1.0.
If inputted or received values are exceeds the allowed value, or is less than zero
it will be equalize to limit.
§Example
use colorsys::{Rgb, Hsl, prelude::*};
let mut rgb1 = Rgb::from((100.0, 255.0, 17.0));
// Rgb { r: 100.0, g: 255.0, b: 17.0, a: None }
let green = rgb1.green();
// 255.0
rgb1.set_red(108.3);
// Rgb { r: 108.3, g: 255.0, b: 17.0, .. }
let mut hsl: Hsl = rgb1.into();
// ~Hsl { h: 96.98, s: 100.0, l: 53.333, .. }
hsl.saturate( SaturationInSpace::Hsl(-57.901) );
// ~Hsl { h: 96.98, s: 42.099, l: 53.333, .. }
let mut rgb2 = Rgb::from(&hsl);
// ~Rgb { r: 124.34, g: 186.1, b: 85.9, .. }
let rgb2tuple: (f64,f64,f64) = rgb2.as_ref().into();
// (124.34, 186.1,85.9)
rgb2 += Rgb::from_hex_str("#35f15b").unwrap();;
// ~Rgb { r: 177.33, g: 255.0, b: 176.902, .. }
rgb2.set_green(-150.0);
assert_eq!(rgb2.green(), 0.0);
rgb2.lighten(-13.123);
// ~Rgb { r: 110.41, g: 0.0, b: 110.1, .. }
rgb2.grayscale_simple();
// ~Rgb { r: 55.2, g: 55.2, b: 55.2, .. }
let css_string = rgb2.to_css_string();
assert_eq!(css_string, "rgb(55,55,55)");Implementations§
Source§impl Rgb
impl Rgb
pub fn new(r: f64, g: f64, b: f64, a: Option<f64>) -> Rgb
pub fn from_hex_str(s: &str) -> Result<Rgb, ParseError>
pub fn to_hex_string(&self) -> String
pub fn red(&self) -> f64
pub fn green(&self) -> f64
pub fn blue(&self) -> f64
pub fn get_red(&self) -> f64
👎Deprecated since 0.7.0: Please use
red insteadpub fn get_green(&self) -> f64
👎Deprecated since 0.7.0: Please use
green insteadpub fn get_blue(&self) -> f64
👎Deprecated since 0.7.0: Please use
blue insteadpub fn set_red(&mut self, val: f64)
pub fn set_green(&mut self, val: f64)
pub fn set_blue(&mut self, val: f64)
Sourcepub fn to_css_string(&self) -> String
pub fn to_css_string(&self) -> String
Returns a String that can be used in CSS.
§Example
use colorsys::{Rgb};
let rgb = Rgb::from([55.0,31.1, 201.9]);
assert_eq!(rgb.to_css_string(), "rgb(55,31,202)");pub fn grayscale(&mut self, method: GrayScaleMethod)
Trait Implementations§
Source§impl AddAssign for Rgb
impl AddAssign for Rgb
Source§fn add_assign(&mut self, rhs: Rgb)
fn add_assign(&mut self, rhs: Rgb)
Performs the
+= operation. Read moreSource§impl ColorTransform for Rgb
impl ColorTransform for Rgb
Source§fn lighten(&mut self, amt: f64)
fn lighten(&mut self, amt: f64)
Lighten or darken color. amt is a percent with negative values - -100..100
§Example
use colorsys::{Rgb,ColorTransform, ColorTuple};
let tuple = (30.0, 108.0, 77.0);
let mut rgb = Rgb::from(&tuple);
rgb.lighten(20.0);
assert_eq!(rgb.to_css_string(), "rgb(52,188,134)" );
rgb.lighten(-20.0);
assert_eq!(rgb.to_css_string(), "rgb(30,108,77)" );
rgb.lighten(-20.0);
assert_eq!(rgb.to_css_string(), "rgb(8,28,20)" );
rgb.lighten(301.123);
assert_eq!(rgb.to_css_string(), "rgb(255,255,255)" );Source§fn saturate(&mut self, sat: SaturationInSpace)
fn saturate(&mut self, sat: SaturationInSpace)
Saturate/desaturate color.
Value is percent:
-100..100.
You need specify in what color space you want to increase/decrease saturation.Source§fn adjust_hue(&mut self, hue: f64)
fn adjust_hue(&mut self, hue: f64)
increase/decrease color tone. Value is degree -
-360..360.Source§fn grayscale_simple(&mut self)
fn grayscale_simple(&mut self)
Brings color to a shade of gray. For more specific grayscale methods see
Rgb.grayscaleSource§impl<'a> IntoIterator for &'a Rgb
impl<'a> IntoIterator for &'a Rgb
Source§impl IntoIterator for Rgb
impl IntoIterator for Rgb
Source§impl SubAssign for Rgb
impl SubAssign for Rgb
Source§fn sub_assign(&mut self, rhs: Rgb)
fn sub_assign(&mut self, rhs: Rgb)
Performs the
-= operation. Read moreimpl StructuralPartialEq for Rgb
Auto Trait Implementations§
impl Freeze for Rgb
impl RefUnwindSafe for Rgb
impl Send for Rgb
impl Sync for Rgb
impl Unpin for Rgb
impl UnwindSafe for Rgb
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more