#[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
impl Color
Sourcepub const TRANSPARENT: Color
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.
Sourcepub const BLACK: Color
pub const BLACK: Color
Pure black color (0, 0, 0, 1).
Represents the absence of all light, fully opaque.
Sourcepub const WHITE: Color
pub const WHITE: Color
Pure white color (1, 1, 1, 1).
Represents the presence of all light at full intensity, fully opaque.
Sourcepub const RED: Color
pub const RED: Color
Pure red color (1, 0, 0, 1).
Full intensity red with no green or blue components, fully opaque.
Sourcepub const GREEN: Color
pub const GREEN: Color
Pure green color (0, 1, 0, 1).
Full intensity green with no red or blue components, fully opaque.
Sourcepub const BLUE: Color
pub const BLUE: Color
Pure blue color (0, 0, 1, 1).
Full intensity blue with no red or green components, fully opaque.
Sourcepub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self
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);
Sourcepub const fn from_rgb(r: f32, g: f32, b: f32) -> Self
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);
Sourcepub fn from_rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Self
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);
Sourcepub fn from_rgb_u8(r: u8, g: u8, b: u8) -> Self
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);
Sourcepub fn to_array(self) -> [f32; 4]
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]);
Sourcepub fn with_alpha(self, alpha: f32) -> Self
pub fn with_alpha(self, alpha: f32) -> Self
Sourcepub fn lerp(&self, other: &Self, t: f32) -> Self
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 Default for Color
The default color is fully transparent.
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§impl From<[f32; 3]> for Color
Converts from a 3-element f32
array [r, g, b]
to an opaque Color
.
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§impl From<[f32; 4]> for Color
Converts from a 4-element f32
array [r, g, b, a]
to a Color
.
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§impl From<[u8; 3]> for Color
Converts from a 3-element u8
array [r, g, b]
to an opaque Color
.
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§impl From<[u8; 4]> for Color
Converts from a 4-element u8
array [r, g, b, a]
to a Color
.
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§impl From<Color> for [f32; 4]
Converts from a Color
to a 4-element f32
array [r, g, b, a]
.
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]);
impl Copy for Color
impl Pod for Color
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
§type Bits = T
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
fn is_valid_bit_pattern(_bits: &T) -> bool
bits
as &Self
.Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.