Skip to main content

Color

Struct Color 

Source
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}
Expand description

RGBA color representation with 8-bit components

Fields§

§r: u8

Red component

§g: u8

Green component

§b: u8

Blue component

§a: u8

Alpha component

Implementations§

Source§

impl Color

Source

pub const WHITE: Self

White (FFFFFF)

Source

pub const BLACK: Self

Black (000000)

Source

pub const RED: Self

Red (FF0000)

Source

pub const GREEN: Self

Green (00FF00)

Source

pub const BLUE: Self

Blue (0000FF)

Source

pub const CYAN: Self

Cyan (00FFFF)

Source

pub const MAGENTA: Self

Magenta (FF00FF)

Source

pub const YELLOW: Self

Yellow (FFFF00)

Source

pub const fn gray(shade: u8) -> Self

Create a grayscale color from a single intensity value

§Arguments
  • shade - Gray intensity
§Example
use simple_color::Color;

let gray = Color::gray(0x80);
let dark_gray = Color::gray(0x40);
let light_gray = Color::gray(0xC0);
let black = Color::gray(0);
let white = Color::gray(0xFF);

assert_eq!(gray, Color::rgb(0x80, 0x80, 0x80));
assert_eq!(black, Color::BLACK);
assert_eq!(white, Color::WHITE);
Source

pub const fn rgb(r: u8, g: u8, b: u8) -> Self

Create an opaque RGB color

§Arguments
  • r - Red component
  • g - Green component
  • b - Blue component
§Example
use simple_color::Color;

let cyan = Color::rgb(0, 0xFF, 0xFF);
assert_eq!(cyan, Color::CYAN);
Source

pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self

Create a translucent RGBA color

§Arguments
  • r - Red component
  • g - Green component
  • b - Blue component
  • a - Alpha component
§Example
use simple_color::Color;

let semi_transparent_red = Color::rgba(0xFF, 0x00, 0x00, 0x80);
assert_eq!(semi_transparent_red, Color::RED.a(0x80));
Source

pub const fn r(self, r: u8) -> Self

Create a copy with modified red component

§Arguments
  • r - New red value
§Example
use simple_color::Color;

let dark_red = Color::BLACK.r(0x80);
assert_eq!(dark_red, Color::rgb(0x80, 0, 0));
Source

pub const fn g(self, g: u8) -> Self

Create a copy with modified green component

§Arguments
  • g - New green value
§Example
use simple_color::Color;

let yellow_green = Color::RED.g(0x80);
assert_eq!(yellow_green, Color::rgb(0xFF, 0x80, 0));
Source

pub const fn b(self, b: u8) -> Self

Create a copy with modified blue component

§Arguments
  • b - New blue value
§Example
use simple_color::Color;

let light_yellow = Color::WHITE.b(0x80);
assert_eq!(light_yellow, Color::rgb(0xFF, 0xFF, 0x80));
Source

pub const fn a(self, a: u8) -> Self

Create a copy with modified alpha component

§Arguments
  • a - New alpha value
§Example
use simple_color::Color;

let semi_transparent_blue = Color::BLUE.a(0x80);
assert_eq!(semi_transparent_blue, Color::rgba(0x00, 0x00, 0xFF, 0x80));

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Color

Source§

impl Debug for Color

Source§

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

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

impl Default for Color

Source§

fn default() -> Color

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

impl Eq for Color

Source§

impl FromStr for Color

Source§

fn from_str(name: &str) -> Result<Self, Self::Err>

Parse color from hexadecimal string.

Supported formats:

  • Grayscale - 1 or 2 characters representing brightness
  • RGB - “RGB” or “RRGGBB”
  • RGBA - “RGBA” or “RRGGBBAA”
§Example
use simple_color::{Color, ColorParsingError};

let red: Color = "FF0000".parse().unwrap();
assert_eq!(red, Color::RED);

let semi_transparent_orange: Color = "ff800080".parse().unwrap();
assert_eq!(semi_transparent_orange, Color::rgba(0xFF, 0x80, 0, 0x80));

let gray: Color = "80".parse().unwrap();
assert_eq!(gray, Color::gray(0x80));

let invalid_char: Result<Color, _> = "80FG00".parse();
assert_eq!(invalid_char, Err(ColorParsingError::InvalidChar('G')));

let invalid_length: Result<Color, _> = "FF80000".parse();
assert_eq!(invalid_length, Err(ColorParsingError::InvalidLength(7)));
Source§

type Err = ColorParsingError

The associated error which can be returned from parsing.
Source§

impl PartialEq for Color

Source§

fn eq(&self, other: &Color) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Color

Auto Trait Implementations§

§

impl Freeze for Color

§

impl RefUnwindSafe for Color

§

impl Send for Color

§

impl Sync for Color

§

impl Unpin for Color

§

impl UnsafeUnpin 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, 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.