FloatColor

Struct FloatColor 

Source
pub struct FloatColor { /* private fields */ }

Implementations§

Source§

impl FloatColor

Source

pub fn set_r(&mut self, new_value: f64)

Sets the red value of the color. Values range from 0.0 (no red) to 1.0 (fully red).

The provided color value is clamped between 0.0 and 1.0.

§Panics

This function will panic if the provided argument is NaN.

§Examples
let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_r(0.5);
assert_eq!(color.get_r(), 0.5);
let mut color_clamped_lower = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_lower.set_r(-20.0);
assert_eq!(color_clamped_lower.get_r(), 0.0);
let mut color_clamped_upper = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_upper.set_r(15.0);
assert_eq!(color_clamped_upper.get_r(), 1.0);

This example will panic:

let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_r(std::f64::NAN);
Source

pub fn set_g(&mut self, new_value: f64)

Sets the green value of the color. Values range from 0.0 (no green) to 1.0 (fully green).

The provided color value is clamped between 0.0 and 1.0.

§Panics

This function will panic if the provided argument is NaN.

§Examples
let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_g(0.6);
assert_eq!(color.get_g(), 0.6);
let mut color_clamped_lower = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_lower.set_g(-20.0);
assert_eq!(color_clamped_lower.get_g(), 0.0);
let mut color_clamped_upper = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_upper.set_g(15.0);
assert_eq!(color_clamped_upper.get_g(), 1.0);

This example will panic:

let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_g(std::f64::NAN);
Source

pub fn set_b(&mut self, new_value: f64)

Sets the blue value of the color. Values range from 0.0 (no blue) to 1.0 (fully blue).

The provided color value is clamped between 0.0 and 1.0.

§Panics

This function will panic if the provided argument is NaN.

§Examples
let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_b(0.7);
assert_eq!(color.get_b(), 0.7);
let mut color_clamped_lower = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_lower.set_b(-20.0);
assert_eq!(color_clamped_lower.get_b(), 0.0);
let mut color_clamped_upper = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_upper.set_b(15.0);
assert_eq!(color_clamped_upper.get_b(), 1.0);

This example will panic:

let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_b(std::f64::NAN);
Source

pub fn set_a(&mut self, new_value: f64)

Sets the alpha (opacity) value of the color. Values range from 0.0 (fully transparent) to 1.0 (fully opaque).

The provided color value is clamped between 0.0 and 1.0.

§Panics

This function will panic if the provided argument is NaN.

§Examples
let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_a(0.8);
assert_eq!(color.get_a(), 0.8);
let mut color_clamped_lower = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_lower.set_a(-20.0);
assert_eq!(color_clamped_lower.get_a(), 0.0);
let mut color_clamped_upper = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color_clamped_upper.set_a(15.0);
assert_eq!(color_clamped_upper.get_a(), 1.0);

This example will panic:

let mut color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
color.set_a(std::f64::NAN);
Source

pub fn get_r(&self) -> f64

Returns the red value of the color. Values range from 0.0 (no red) to 1.0 (fully red).

§Examples
let color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
assert_eq!(color.get_r(), 0.1);
Source

pub fn get_g(&self) -> f64

Returns the green value of the color. Values range from 0.0 (no green) to 1.0 (fully green).

§Examples
let color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
assert_eq!(color.get_g(), 0.2);
Source

pub fn get_b(&self) -> f64

Returns the blue value of the color. Values range from 0.0 (no blue) to 1.0 (fully blue).

§Examples
let color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
assert_eq!(color.get_b(), 0.3);
Source

pub fn get_a(&self) -> f64

Returns the alpha (opacity) value of the color. Values range from 0.0 (fully transparent) to 1.0 (fully opaque).

§Examples
let color = FloatColor::new(0.1, 0.2, 0.3, 0.4);
assert_eq!(color.get_a(), 0.4);
Source

pub fn new(r: f64, g: f64, b: f64, a: f64) -> Self

Constructs a new FloatColor structure from four f64 values. The color values provided via arguments are clamped between 0.0 and 1.0.

§Panics

This function will panic if any of the arguments is NaN.

§Examples
let color1 = FloatColor::new(0.4, 0.0, -2.9, std::f64::NEG_INFINITY);
assert_eq!(color1.get_r(), 0.4);
assert_eq!(color1.get_g(), 0.0);
assert_eq!(color1.get_b(), 0.0);
assert_eq!(color1.get_a(), 0.0);

let color2 = FloatColor::new(0.6, 1.0, 2.9, std::f64::INFINITY);
assert_eq!(color2.get_r(), 0.6);
assert_eq!(color2.get_g(), 1.0);
assert_eq!(color2.get_b(), 1.0);
assert_eq!(color2.get_a(), 1.0);

These examples will panic:

FloatColor::new(std::f64::NAN, 0.2, 0.3, 0.4);
FloatColor::new(0.1, std::f64::NAN, 0.3, 0.4);
FloatColor::new(0.1, 0.2, std::f64::NAN, 0.4);
FloatColor::new(0.1, 0.2, 0.3, std::f64::NAN);

Trait Implementations§

Source§

impl Clone for FloatColor

Source§

fn clone(&self) -> FloatColor

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 Color for FloatColor

Source§

fn hex_rgba_string(&self) -> String

Creates a hexadecimal color string in the format #rrggbbaa.

This function converts float values to bytes by first multiplying the value by 255.0 and then casting the result to u8 (rounding towards 0).

§Examples
let color = FloatColor::new(0.2, 0.901, 0.0, 1.0);
assert_eq!(color.hex_rgba_string(), "#33e500ff");
// 0.901 gets converted to 229.755 which rounds down to 229 (0xe5)
Source§

fn lightness(&self) -> f64

Returns an arithmetic mean of the red, green and blue values. The alpha value is not taken into account.

Note that this is different from the perceived brightness of the color, as high values on some color channels look more bright to the human eye than the same values on other color channels.

§Examples
let color = FloatColor::new(0.1, 0.4, 0.7, 0.2);
assert!((color.lightness() - 0.4).abs() < 0.00001)
Source§

impl Debug for FloatColor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for FloatColor

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for FloatColor

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl From<ByteColor> for FloatColor

Source§

fn from(value: ByteColor) -> Self

Convert a ByteColor into a FloatColor.

This function converts bytes to float values by first casting them to f64 and then dividing the value by 255.0.

§Examples
let byte_color = ByteColor::new(0, 32, 53, 255);
let float_color: FloatColor = byte_color.into();
assert_eq!(float_color.get_r(), 0.0);
assert!((float_color.get_g() - 0.125490).abs() < 0.00001);
assert!((float_color.get_b() - 0.207843).abs() < 0.00001);
assert_eq!(float_color.get_a(), 1.0);
Source§

impl From<FloatColor> for ByteColor

Source§

fn from(value: FloatColor) -> Self

Converts to this type from the input type.
Source§

impl Serialize for FloatColor

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for FloatColor

Auto Trait Implementations§

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,