Struct tetra::graphics::Color

source ·
#[repr(C)]
pub struct Color { pub r: f32, pub g: f32, pub b: f32, pub a: f32, }
Expand description

An RGBA color.

The components are stored as f32 values, which will generally be in the range of 0.0 to 1.0.

If your data is made up of bytes or hex values, this type provides constructors that will carry out the conversion for you.

The std arithmetic traits are implemented for this type, which allows you to add/subtract/multiply/divide colors.

Serde

Serialization and deserialization of this type (via Serde) can be enabled via the serde_support feature.

Fields§

§r: f32

The red component of the color.

§g: f32

The green component of the color.

§b: f32

The blue component of the color.

§a: f32

The alpha component of the color.

Implementations§

source§

impl Color

source

pub const fn rgb(r: f32, g: f32, b: f32) -> Color

Creates a new Color, with the specified RGB values and the alpha set to 1.0.

source

pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color

Creates a new Color, with the specified RGBA values.

source

pub fn rgb8(r: u8, g: u8, b: u8) -> Color

Creates a new Color, with the specified RGB integer (0-255) values and the alpha set to 255.

source

pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Color

Creates a new Color, with the specified RGBA (0-255) integer values.

source

pub fn hex(hex: &str) -> Color

Creates a new Color using a hexidecimal color code, panicking if the input is invalid.

Six and eight digit codes can be used - the former will be interpreted as RGB, and the latter as RGBA. The # prefix (commonly used on the web) will be stripped if present.

source

pub fn try_hex(hex: &str) -> Result<Color>

Creates a new Color using a hexidecimal color code, returning an error if the input is invalid.

Six and eight digit codes can be used - the former will be interpreted as RGB, and the latter as RGBA. The # prefix (commonly used on the web) will be stripped if present.

Errors
source

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

Returns the color with the red component set to the specified value.

source

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

Returns the color with the green component set to the specified value.

source

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

Returns the color with the blue component set to the specified value.

source

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

Returns the color with the alpha component set to the specified value.

source

pub fn clamp(self) -> Self

Returns the color with all components clamped between 0.0 and 1.0.

source

pub fn to_premultiplied(self) -> Color

Returns the color with the RGB components multiplied by the alpha component.

This can be useful when working with premultiplied alpha blending, if you want to convert a non-premultiplied color into its premultiplied version.

source

pub const BLACK: Color = _

source

pub const WHITE: Color = _

source

pub const RED: Color = _

source

pub const GREEN: Color = _

source

pub const BLUE: Color = _

Shortcut for Color::rgb(0.0, 0.0, 1.0)`](Self::rgb).

Trait Implementations§

source§

impl Add<Color> for Color

§

type Output = Color

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl Add<f32> for Color

§

type Output = Color

The resulting type after applying the + operator.
source§

fn add(self, rhs: f32) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<Color> for Color

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl AddAssign<f32> for Color

source§

fn add_assign(&mut self, rhs: f32)

Performs the += operation. Read more
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 Default for Color

source§

fn default() -> Color

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

impl Div<Color> for Color

§

type Output = Color

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
source§

impl Div<f32> for Color

§

type Output = Color

The resulting type after applying the / operator.
source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
source§

impl DivAssign<Color> for Color

source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
source§

impl DivAssign<f32> for Color

source§

fn div_assign(&mut self, rhs: f32)

Performs the /= operation. Read more
source§

impl From<[f32; 4]> for Color

source§

fn from(v: [f32; 4]) -> Self

Converts to this type from the input type.
source§

impl From<[u8; 4]> for Color

source§

fn from(v: [u8; 4]) -> Self

Converts to this type from the input type.
source§

impl From<Color> for [f32; 4]

source§

fn from(color: Color) -> Self

Converts to this type from the input type.
source§

impl From<Color> for [u8; 4]

source§

fn from(color: Color) -> Self

Converts to this type from the input type.
source§

impl From<Color> for Vec4<f32>

source§

fn from(color: Color) -> Vec4<f32>

Converts to this type from the input type.
source§

impl From<Vec4<f32>> for Color

source§

fn from(v: Vec4<f32>) -> Self

Converts to this type from the input type.
source§

impl Mul<Color> for Color

§

type Output = Color

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl Mul<f32> for Color

§

type Output = Color

The resulting type after applying the * operator.
source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
source§

impl MulAssign<Color> for Color

source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
source§

impl MulAssign<f32> for Color

source§

fn mul_assign(&mut self, rhs: f32)

Performs the *= operation. Read more
source§

impl PartialEq<Color> for Color

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Sub<Color> for Color

§

type Output = Color

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl Sub<f32> for Color

§

type Output = Color

The resulting type after applying the - operator.
source§

fn sub(self, rhs: f32) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<Color> for Color

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl SubAssign<f32> for Color

source§

fn sub_assign(&mut self, rhs: f32)

Performs the -= operation. Read more
source§

impl Copy for Color

source§

impl StructuralPartialEq for Color

source§

impl UniformValue for Color

Can be accessed as a vec4 in your shader.

Auto Trait Implementations§

§

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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<S> FromSample<S> for S

§

fn from_sample_(s: S) -> S

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.

§

impl<F, T> IntoSample<T> for Fwhere T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> ToOwned for Twhere T: Clone,

§

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
§

impl<T, U> ToSample<U> for Twhere U: FromSample<T>,

§

fn to_sample_(self) -> U

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<S, T> Duplex<S> for Twhere T: FromSample<S> + ToSample<S>,