Struct Color

Source
pub struct Color {
    pub red: u8,
    pub green: u8,
    pub blue: u8,
    pub alpha: f32,
}
Expand description

Represents an RGB color.

Fields§

§red: u8

Red component (0-255)

§green: u8

Green component (0-255)

§blue: u8

Blue component (0-255)

§alpha: f32

Alpha component (0-1)

Implementations§

Source§

impl Color

Source

pub fn new(red: u8, green: u8, blue: u8) -> Self

Creates a new Color instance.

§Arguments
  • red - The red component (0-255)
  • green - The green component (0-255)
  • blue - The blue component (0-255)
§Returns

A new Color instance.

Source

pub fn with_alpha(red: u8, green: u8, blue: u8, alpha: f32) -> Self

Creates a new Color instance with an alpha component. The alpha component is a floating-point value between 0.0 and 1.0. A value of 0.0 represents full transparency, while 1.0 is fully opaque. Values between 0.0 and 1.0 represent varying levels of transparency.

§Arguments
  • red - The red component (0-255)
  • green - The green component (0-255)
  • blue - The blue component (0-255)
  • alpha - The alpha component (0.0-1.0)
§Returns

A new Color instance.

Source

pub fn interpolate(&self, color: &Color, percentage: f32) -> Self

Interpolates between two RGB colors based on percentage.

§Arguments
  • color - The target color to interpolate towards
  • percentage - The interpolation percentage (0.0 to 100.0)
§Returns

A new Color instance representing the interpolated color.

Source

pub fn from_hex(hex: &str) -> Result<Self, ColorError>

Tries to create a Color from a hexadecimal color string.

§Arguments
  • hex - A string slice that holds the hexadecimal color code (e.g., “#FF00FF”)
§Returns

A Result containing either the created Color or a ColorError.

Source

pub fn from_rgba(input: &str) -> Result<Self, ColorError>

Tries to create a Color from a rgba color string. The format is “rgba(r, g, b, a)” where r, g, b are the red, green, blue components (0-255) Or “rgb(r, g, b)” where r, g, b are the red, green, blue components (0-255) and a is the alpha component (0.0-1.0). The spaces are optional. The alpha component is optional and defaults to 1.0.

§Arguments
  • input - A string slice that holds the rgba color code (e.g., “rgba(255, 128, 64, 0.5)”, “rgb(255, 128, 64)”)
§Returns

A Result containing either the created Color or a ColorError.

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a copy 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 Display for Color

Source§

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

Formats the Color as a hexadecimal string.

§Returns

A String representing the color in “#RRGGBB” format.

Source§

impl TryFrom<&str> for Color

Source§

fn try_from(value: &str) -> Result<Self, Self::Error>

Tries to create a Color from various string formats.

§Arguments
  • value - A string slice that holds the color code (e.g., “#FF8040”, “#FF8040FF”, “rgba(255, 128, 64, 0.5)”)
§Returns

A Result containing either the created Color or a ColorError.

Source§

type Error = ColorError

The type returned in the event of a conversion error.

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.