[][src]Struct scarlet::color::RGBColor

pub struct RGBColor {
    pub r: f64,
    pub g: f64,
    pub b: f64,
}

A color with red, green, and blue primaries of specified intensity, specifically in the sRGB gamut: most computer screens use this to display colors. The attributes r, g, and b are floating-point numbers from 0 to 1 for visible colors, allowing the avoidance of rounding errors or clamping errors when converting to and from RGB. Many conveniences are afforded so that working with RGB as if it were instead three integers from 0-255 is painless. Note that the integers generated from the underlying floating-point numbers round away from 0.

Examples of this abound: this is used ubiquitously in Scarlet. Check the Color documentation for plenty.

Fields

r: f64

The red component. Ranges from 0 to 1 for numbers displayable by sRGB machines.

g: f64

The green component. Ranges from 0 to 1 for numbers displayable by sRGB machines.

b: f64

The blue component. Ranges from 0 to 1 for numbers displayable by sRGB machines.

Methods

impl RGBColor[src]

pub fn int_r(&self) -> u8[src]

Gets an 8-byte version of the red component, as a u8. Clamps values outside of the range 0-1 and discretizes, so this may not correspond to the exact values kept internally.

Example

let super_red = RGBColor{r: 1.2, g: 0., b: 0.};
let non_integral_red = RGBColor{r: 0.999, g: 0., b: 0.};
// the first one will get clamped in range, the second one will be rounded
assert_eq!(super_red.int_r(), non_integral_red.int_r());
assert_eq!(super_red.int_r(), 255);

pub fn int_g(&self) -> u8[src]

Gets an 8-byte version of the green component, as a u8. Clamps values outside of the range 0-1 and discretizes, so this may not correspond to the exact values kept internally.

Example

let super_green = RGBColor{r: 0., g: 1.2, b: 0.};
let non_integral_green = RGBColor{r: 0., g: 0.999, b: 0.};
// the first one will get clamped in range, the second one will be rounded
assert_eq!(super_green.int_g(), non_integral_green.int_g());
assert_eq!(super_green.int_g(), 255);

pub fn int_b(&self) -> u8[src]

Gets an 8-byte version of the blue component, as a u8. Clamps values outside of the range 0-1 and discretizes, so this may not correspond to the exact values kept internally.

Example

let super_blue = RGBColor{r: 0., g: 0., b: 1.2};
let non_integral_blue = RGBColor{r: 0., g: 0., b: 0.999};
// the first one will get clamped in range, the second one will be rounded
assert_eq!(super_blue.int_b(), non_integral_blue.int_b());
assert_eq!(super_blue.int_b(), 255);

pub fn int_rgb_tup(&self) -> (u8, u8, u8)[src]

Purely for convenience: gives a tuple with the three integer versions of the components. Used over standard conversion traits to avoid ambiguous operations.

Example

let color = RGBColor{r: 0.3, g: 0.6, b: 0.7};
assert_eq!(color.int_rgb_tup(), (color.int_r(), color.int_g(), color.int_b()));

impl RGBColor[src]

pub fn from_hex_code(hex: &str) -> Result<RGBColor, RGBParseError>[src]

Given a string that represents a hex code, returns the RGB color that the given hex code represents. Four formats are accepted: "#rgb" as a shorthand for "#rrggbb", #rrggbb by itself, and either of those formats without #: "rgb" or "rrggbb" are acceptable. Returns a ColorParseError if the given string does not follow one of these formats.

Example

let fuchsia = RGBColor::from_hex_code("#ff00ff")?;
// if 3 digits, interprets as doubled
let fuchsia2 = RGBColor::from_hex_code("f0f")?;
assert_eq!(fuchsia.int_rgb_tup(), fuchsia2.int_rgb_tup());
assert_eq!(fuchsia.int_rgb_tup(), (255, 0, 255));
let err = RGBColor::from_hex_code("#afafa");
let err2 = RGBColor::from_hex_code("#gafd22");
assert_eq!(err, err2);

pub fn from_color_name(name: &str) -> Result<RGBColor, RGBParseError>[src]

Gets the RGB color corresponding to an X11 color name. Case is ignored.

Example

let fuchsia = RGBColor::from_color_name("fuchsia")?;
let fuchsia2 = RGBColor::from_color_name("FuCHSiA")?;
assert_eq!(fuchsia.int_rgb_tup(), fuchsia2.int_rgb_tup());
assert_eq!(fuchsia.int_rgb_tup(), (255, 0, 255));
let err = RGBColor::from_color_name("fuccshai");
let err2 = RGBColor::from_color_name("foobar");
assert_eq!(err, err2);

impl RGBColor[src]

pub fn from_material_palette(prim: MaterialPrimary) -> RGBColor[src]

Gets a Color from the Material palette, given a specification of such a color.

Example

let red_400 = RGBColor::from_material_palette(MaterialPrimary::Red(MaterialTone::Neutral(NeutralTone::W400)));
let amber_a100 = RGBColor::from_material_palette(MaterialPrimary::Amber(MaterialTone::Accent(AccentTone::A100)));
assert_eq!(red_400.to_string(), "#EF5350");
assert_eq!(amber_a100.to_string(), "#FFE57F");

Trait Implementations

impl Bound for RGBColor[src]

impl Clone for RGBColor[src]

impl Color for RGBColor[src]

impl Copy for RGBColor[src]

impl Debug for RGBColor[src]

impl From<(u8, u8, u8)> for RGBColor[src]

impl From<Coord> for RGBColor[src]

impl FromStr for RGBColor[src]

type Err = RGBParseError

The associated error which can be returned from parsing.

impl Into<(u8, u8, u8)> for RGBColor[src]

impl Into<Coord> for RGBColor[src]

impl PartialEq<RGBColor> for RGBColor[src]

impl ToString for RGBColor[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> ColorPoint for T where
    T: Color + Clone + Into<Coord> + From<Coord> + Copy
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.