Struct Color

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

A color in the linear sRGB color space with an alpha component.

This struct represents a color using four floating-point components: red, green, blue, and alpha (transparency). Values are typically in the range [0.0, 1.0], where:

  • 0.0 represents no intensity (black for RGB, fully transparent for alpha)
  • 1.0 represents full intensity (full color for RGB, fully opaque for alpha)

The struct is designed to be GPU-friendly with a C-compatible memory layout, making it suitable for direct use in shaders and graphics pipelines.

§Memory Layout

The struct uses #[repr(C)] to ensure a predictable memory layout that matches the expected format for GPU buffers and shader uniforms.

§Examples

use tessera_ui::Color;

// Using predefined colors
let red = Color::RED;
let white = Color::WHITE;
let transparent = Color::TRANSPARENT;

// Creating custom colors
let purple = Color::new(0.5, 0.0, 0.5, 1.0);
let semi_transparent_blue = Color::new(0.0, 0.0, 1.0, 0.5);

Fields§

§r: f32

Red component (0.0 to 1.0)

§g: f32

Green component (0.0 to 1.0)

§b: f32

Blue component (0.0 to 1.0)

§a: f32

Alpha (transparency) component (0.0 = fully transparent, 1.0 = fully opaque)

Implementations§

Source§

impl Color

Source

pub const TRANSPARENT: Color

Fully transparent color (0, 0, 0, 0).

This color is completely invisible and is often used as a default or for creating transparent backgrounds.

Source

pub const BLACK: Color

Pure black color (0, 0, 0, 1).

Represents the absence of all light, fully opaque.

Source

pub const WHITE: Color

Pure white color (1, 1, 1, 1).

Represents the presence of all light at full intensity, fully opaque.

Source

pub const RED: Color

Pure red color (1, 0, 0, 1).

Full intensity red with no green or blue components, fully opaque.

Source

pub const GREEN: Color

Pure green color (0, 1, 0, 1).

Full intensity green with no red or blue components, fully opaque.

Source

pub const BLUE: Color

Pure blue color (0, 0, 1, 1).

Full intensity blue with no red or green components, fully opaque.

Source

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

Creates a new Color from four f32 values (red, green, blue, alpha).

§Parameters
  • r - Red component, typically in range [0.0, 1.0]
  • g - Green component, typically in range [0.0, 1.0]
  • b - Blue component, typically in range [0.0, 1.0]
  • a - Alpha (transparency) component, typically in range [0.0, 1.0]
§Examples
use tessera_ui::Color;

let red = Color::new(1.0, 0.0, 0.0, 1.0);
let semi_transparent_blue = Color::new(0.0, 0.0, 1.0, 0.5);
let custom_color = Color::new(0.3, 0.7, 0.2, 0.8);
Source

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

Creates a new opaque Color from three f32 values (red, green, blue).

The alpha component is automatically set to 1.0 (fully opaque).

§Parameters
  • r - Red component, typically in range [0.0, 1.0]
  • g - Green component, typically in range [0.0, 1.0]
  • b - Blue component, typically in range [0.0, 1.0]
§Examples
use tessera_ui::Color;

let purple = Color::from_rgb(0.5, 0.0, 0.5);
let orange = Color::from_rgb(1.0, 0.5, 0.0);
Source

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

Creates a new Color from four u8 values (red, green, blue, alpha).

This is convenient for working with traditional 8-bit color values commonly used in image formats and color pickers.

§Parameters
  • r - Red component in range [0, 255]
  • g - Green component in range [0, 255]
  • b - Blue component in range [0, 255]
  • a - Alpha component in range [0, 255] (0 = transparent, 255 = opaque)
§Examples
use tessera_ui::Color;

let red = Color::from_rgba_u8(255, 0, 0, 255);
let semi_transparent_blue = Color::from_rgba_u8(0, 0, 255, 128);
let custom_color = Color::from_rgba_u8(76, 178, 51, 204);
Source

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

Creates a new opaque Color from three u8 values (red, green, blue).

The alpha component is automatically set to 255 (fully opaque). This is convenient for working with traditional RGB color values.

§Parameters
  • r - Red component in range [0, 255]
  • g - Green component in range [0, 255]
  • b - Blue component in range [0, 255]
§Examples
use tessera_ui::Color;

let purple = Color::from_rgb_u8(128, 0, 128);
let orange = Color::from_rgb_u8(255, 165, 0);
let dark_green = Color::from_rgb_u8(0, 100, 0);
Source

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

Converts the color to an array of [f32; 4].

This is useful for interfacing with graphics APIs and shaders that expect color data in array format.

§Returns

An array [r, g, b, a] where each component is an f32 value.

§Examples
use tessera_ui::Color;

let color = Color::new(0.5, 0.3, 0.8, 1.0);
let array = color.to_array();
assert_eq!(array, [0.5, 0.3, 0.8, 1.0]);
Source

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

Sets the alpha (transparency) component of the color.

§Returns

A new Color instance with the updated alpha value.

§Examples
use tessera_ui::Color;

let color = Color::new(0.5, 0.3, 0.8, 1.0);
let semi_transparent_color = color.with_alpha(0.5);

assert_eq!(semi_transparent_color.a, 0.5);
Source

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

Linearly interpolates between two colors.

§Arguments
  • other - The target color to interpolate towards.
  • t - The interpolation factor, typically in the range [0.0, 1.0].
§Returns

A new Color that is the result of the interpolation.

§Examples
use tessera_ui::Color;

let color1 = Color::new(1.0, 0.0, 0.0, 1.0); // Red
let color2 = Color::new(0.0, 0.0, 1.0, 1.0); // Blue
let interpolated = color1.lerp(&color2, 0.5);

assert_eq!(interpolated, Color::new(0.5, 0.0, 0.5, 1.0)); // Purple

Trait Implementations§

Source§

impl Clone for Color

Source§

fn clone(&self) -> Color

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 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

The default color is fully transparent.

This implementation returns Color::TRANSPARENT, which is often the most sensible default for UI elements that may not have an explicit color specified.

§Examples

use tessera_ui::Color;

let default_color = Color::default();
assert_eq!(default_color, Color::TRANSPARENT);
Source§

fn default() -> Self

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

impl From<[f32; 3]> for Color

Converts from a 3-element f32 array [r, g, b] to an opaque Color.

The alpha component is automatically set to 1.0 (fully opaque).

§Examples

use tessera_ui::Color;

let color: Color = [0.5, 0.3, 0.8].into();
assert_eq!(color, Color::new(0.5, 0.3, 0.8, 1.0));
Source§

fn from([r, g, b]: [f32; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<[f32; 4]> for Color

Converts from a 4-element f32 array [r, g, b, a] to a Color.

§Examples

use tessera_ui::Color;

let color: Color = [0.5, 0.3, 0.8, 1.0].into();
assert_eq!(color, Color::new(0.5, 0.3, 0.8, 1.0));
Source§

fn from([r, g, b, a]: [f32; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 3]> for Color

Converts from a 3-element u8 array [r, g, b] to an opaque Color.

Each component is converted from the range [0, 255] to [0.0, 1.0]. The alpha component is automatically set to 1.0 (fully opaque).

§Examples

use tessera_ui::Color;

let color: Color = [255, 128, 64].into();
assert_eq!(color, Color::from_rgb_u8(255, 128, 64));
Source§

fn from([r, g, b]: [u8; 3]) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 4]> for Color

Converts from a 4-element u8 array [r, g, b, a] to a Color.

Each component is converted from the range [0, 255] to [0.0, 1.0].

§Examples

use tessera_ui::Color;

let color: Color = [255, 128, 64, 255].into();
assert_eq!(color, Color::from_rgba_u8(255, 128, 64, 255));
Source§

fn from([r, g, b, a]: [u8; 4]) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for [f32; 4]

Converts from a Color to a 4-element f32 array [r, g, b, a].

§Examples

use tessera_ui::Color;

let color = Color::new(0.5, 0.3, 0.8, 1.0);
let array: [f32; 4] = color.into();
assert_eq!(array, [0.5, 0.3, 0.8, 1.0]);
Source§

fn from(color: Color) -> Self

Converts to this type from the input type.
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 · 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 Zeroable for Color

§

fn zeroed() -> Self

Source§

impl Copy for Color

Source§

impl Pod for Color

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 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> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Returns a reference to the concrete type as &dyn Any.
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
§

impl<T> CheckedBitPattern for T
where T: AnyBitPattern,

§

type Bits = T

Self must have the same layout as the specified Bits except for the possible invalid bit patterns being checked during is_valid_bit_pattern.
§

fn is_valid_bit_pattern(_bits: &T) -> bool

If this function returns true, then it must be valid to reinterpret bits as &Self.
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
§

impl<T> Downcast<T> for T

§

fn downcast(&self) -> &T

§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

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<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
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.
§

impl<T> Upcast<T> for T

§

fn upcast(&self) -> Option<&T>

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> AnyBitPattern for T
where T: Pod,

§

impl<T> NoUninit for T
where T: Pod,

§

impl<T> WasmNotSend for T
where T: Send,

§

impl<T> WasmNotSendSync for T
where T: WasmNotSend + WasmNotSync,

§

impl<T> WasmNotSync for T
where T: Sync,