Struct Color32

Source
#[repr(C)]
pub struct Color32(/* private fields */);
Expand description

This format is used for space-efficient color representation (32 bits).

Instead of manipulating this directly it is often better to first convert it to either Rgba or crate::Hsva.

Internally this uses 0-255 gamma space sRGBA color with premultiplied alpha. Alpha channel is in linear space.

The special value of alpha=0 means the color is to be treated as an additive color.

Implementations§

Source§

impl Color32

Source

pub const TRANSPARENT: Self

Source

pub const BLACK: Self

Source

pub const DARK_GRAY: Self

Source

pub const GRAY: Self

Source

pub const LIGHT_GRAY: Self

Source

pub const WHITE: Self

Source

pub const BROWN: Self

Source

pub const DARK_RED: Self

Source

pub const RED: Self

Source

pub const LIGHT_RED: Self

Source

pub const CYAN: Self

Source

pub const MAGENTA: Self

Source

pub const YELLOW: Self

Source

pub const ORANGE: Self

Source

pub const LIGHT_YELLOW: Self

Source

pub const KHAKI: Self

Source

pub const DARK_GREEN: Self

Source

pub const GREEN: Self

Source

pub const LIGHT_GREEN: Self

Source

pub const DARK_BLUE: Self

Source

pub const BLUE: Self

Source

pub const LIGHT_BLUE: Self

Source

pub const PURPLE: Self

Source

pub const GOLD: Self

Source

pub const DEBUG_COLOR: Self

Source

pub const PLACEHOLDER: Self

An ugly color that is planned to be replaced before making it to the screen.

This is an invalid color, in that it does not correspond to a valid multiplied color, nor to an additive color.

This is used as a special color key, i.e. often taken to mean “no color”.

Source

pub const TEMPORARY_COLOR: Self = Self::PLACEHOLDER

👎Deprecated: Renamed to PLACEHOLDER
Source

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

Source

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

Source

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

From sRGBA with premultiplied alpha.

Source

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

From sRGBA WITHOUT premultiplied alpha.

Source

pub const fn from_gray(l: u8) -> Self

Source

pub const fn from_black_alpha(a: u8) -> Self

Source

pub fn from_white_alpha(a: u8) -> Self

Source

pub const fn from_additive_luminance(l: u8) -> Self

Source

pub const fn is_opaque(&self) -> bool

Source

pub const fn r(&self) -> u8

Source

pub const fn g(&self) -> u8

Source

pub const fn b(&self) -> u8

Source

pub const fn a(&self) -> u8

Source

pub fn to_opaque(self) -> Self

Returns an opaque version of self

Source

pub const fn additive(self) -> Self

Returns an additive version of self

Source

pub fn is_additive(self) -> bool

Is the alpha=0 ?

Source

pub const fn to_array(&self) -> [u8; 4]

Premultiplied RGBA

Source

pub const fn to_tuple(&self) -> (u8, u8, u8, u8)

Premultiplied RGBA

Source

pub fn to_srgba_unmultiplied(&self) -> [u8; 4]

Source

pub fn gamma_multiply(self, factor: f32) -> Self

Multiply with 0.5 to make color half as opaque, perceptually.

Fast multiplication in gamma-space.

This is perceptually even, and faster that Self::linear_multiply.

Source

pub fn gamma_multiply_u8(self, factor: u8) -> Self

Multiply with 127 to make color half as opaque, perceptually.

Fast multiplication in gamma-space.

This is perceptually even, and faster that Self::linear_multiply.

Source

pub fn linear_multiply(self, factor: f32) -> Self

Multiply with 0.5 to make color half as opaque in linear space.

This is using linear space, which is not perceptually even. You likely want to use Self::gamma_multiply instead.

Source

pub fn to_normalized_gamma_f32(self) -> [f32; 4]

Converts to floating point values in the range 0-1 without any gamma space conversion.

Use this with great care! In almost all cases, you want to convert to crate::Rgba instead in order to obtain linear space color values.

Source

pub fn lerp_to_gamma(&self, other: Self, t: f32) -> Self

Lerp this color towards other by t in gamma space.

Source

pub fn blend(self, on_top: Self) -> Self

Blend two colors, so that self is behind the argument.

Source§

impl Color32

Source

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

Parses a color from a hex string.

Supports the 3, 4, 6, and 8-digit formats, according to the specification in https://drafts.csswg.org/css-color-4/#hex-color

To parse hex colors from string literals with compile-time checking, use the macro [crate::hex_color!] instead.

§Example
use ecolor::Color32;
assert_eq!(Ok(Color32::RED), Color32::from_hex("#ff0000"));
assert_eq!(Ok(Color32::GREEN), Color32::from_hex("#00ff00ff"));
assert_eq!(Ok(Color32::BLUE), Color32::from_hex("#00f"));
assert_eq!(Ok(Color32::TRANSPARENT), Color32::from_hex("#0000"));
§Errors

Returns an error if the string doesn’t start with the hash # character, if the remaining length does not correspond to one of the standard formats (3, 4, 6, or 8), if it contains non-hex characters.

Source

pub fn to_hex(&self) -> String

Formats the color as a hex string.

§Example
use ecolor::Color32;
assert_eq!(Color32::RED.to_hex(), "#ff0000ff");
assert_eq!(Color32::GREEN.to_hex(), "#00ff00ff");
assert_eq!(Color32::BLUE.to_hex(), "#0000ffff");
assert_eq!(Color32::TRANSPARENT.to_hex(), "#00000000");

Uses the 8-digit format described in https://drafts.csswg.org/css-color-4/#hex-color, as that is the only format that is lossless. For other formats, see HexColor.

Trait Implementations§

Source§

impl Add for Color32

Source§

type Output = Color32

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self

Performs the + operation. Read more
Source§

impl Clone for Color32

Source§

fn clone(&self) -> Color32

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 Color32

Source§

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

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

impl Default for Color32

Source§

fn default() -> Color32

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

impl From<Color32> for Hsva

Source§

fn from(srgba: Color32) -> Self

Converts to this type from the input type.
Source§

impl From<Color32> for HsvaGamma

Source§

fn from(srgba: Color32) -> Self

Converts to this type from the input type.
Source§

impl From<Color32> for Rgba

Source§

fn from(srgba: Color32) -> Self

Converts to this type from the input type.
Source§

impl From<Hsva> for Color32

Source§

fn from(hsva: Hsva) -> Self

Converts to this type from the input type.
Source§

impl From<HsvaGamma> for Color32

Source§

fn from(hsvag: HsvaGamma) -> Self

Converts to this type from the input type.
Source§

impl From<Rgba> for Color32

Source§

fn from(rgba: Rgba) -> Self

Converts to this type from the input type.
Source§

impl Hash for Color32

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Index<usize> for Color32

Source§

type Output = u8

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &u8

Performs the indexing (container[index]) operation. Read more
Source§

impl IndexMut<usize> for Color32

Source§

fn index_mut(&mut self, index: usize) -> &mut u8

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl Mul for Color32

Source§

fn mul(self, other: Self) -> Self

Fast gamma-space multiplication.

Source§

type Output = Color32

The resulting type after applying the * operator.
Source§

impl PartialEq for Color32

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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 Copy for Color32

Source§

impl Eq for Color32

Source§

impl StructuralPartialEq for Color32

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.