Struct Color

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

Unpremultiplied RGBA floating-point channels. No implicit gamma conversion.

The color space is not stored: callers must track whether RGB is sRGB encoded or linear light. Existing constructors and Self::lerp operate on raw channels. Use Self::try_srgb_to_linear and Self::try_linear_to_srgb to convert explicitly, before premultiplying. Alpha is linear coverage, never gamma encoded; conversions leave it unchanged, even for transparent colors.

Checked construction and conversions require finite channels and alpha in 0..=1. Signed RGB values outside 0..=1 are allowed for HDR and out-of-gamut intermediates. Only Self::try_clamped and byte output explicitly clip; no operation performs tone mapping. Conversions round to f32, so very small values may underflow to zero and round trips are approximate.

Fields§

§r: f32§g: f32§b: f32§a: f32

Implementations§

Source§

impl Color

Source

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

Stores raw channels without validation, clamping or gamma conversion.

Source

pub fn try_new(r: f32, g: f32, b: f32, a: f32) -> Result<Self, ColorError>

Constructs finite raw RGB with alpha in 0..=1, without conversion.

RGB may be negative or greater than one; no channels are clamped. Returns ColorError::NonFinite for any NaN/infinity, otherwise ColorError::AlphaOutOfRange for invalid alpha.

use ferravex_math::{Color, ColorError};
let hdr = Color::try_new(4.0, -0.25, 0.5, 0.75)?;
assert_eq!(hdr.to_array(), [4.0, -0.25, 0.5, 0.75]);
assert_eq!(Color::try_new(0.0, 0.0, 0.0, -0.1), Err(ColorError::AlphaOutOfRange));
Source

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

Normalizes bytes to raw channels; does not decode sRGB.

Source

pub const fn from_hex(rgb: u32) -> Self

Opaque 0xRRGGBB color. Does not decode sRGB.

Source

pub const fn to_vec(self) -> Vec4

Source

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

Source

pub fn is_finite(self) -> bool

Whether all RGBA channels are finite, regardless of their ranges.

use ferravex_math::Color;
assert!(Color::new(4.0, -0.5, 0.0, 2.0).is_finite());
assert!(!Color::new(0.0, 0.0, 0.0, f32::NAN).is_finite());
Source

pub fn try_clamped(self) -> Result<Self, ColorError>

Clips finite RGBA channels to 0..=1 without gamma conversion.

Returns ColorError::NonFinite instead of repairing NaN/infinity. Finite out-of-range alpha is intentionally accepted and clipped here. This discards HDR/out-of-gamut information; it is not tone mapping.

use ferravex_math::{Color, ColorError};
let color = Color::new(2.0, -0.25, 0.5, 1.5).try_clamped()?;
assert_eq!(color.to_array(), [1.0, 0.0, 0.5, 1.0]);
assert_eq!(color.to_rgba(), [255, 0, 128, 255]);
assert_eq!(Color::new(f32::NAN, 0.0, 0.0, 1.0).try_clamped(), Err(ColorError::NonFinite));
Source

pub fn try_srgb_to_linear(self) -> Result<Self, ColorError>

Decodes unpremultiplied sRGB channels to linear-light sRGB.

Uses the piecewise sRGB transfer function, with a linear segment for magnitudes up to 0.04045. Its sign-preserving extension supports negative RGB and values above one without clipping, as in CSS Color 4. Alpha is unchanged and RGB is not multiplied by alpha.

Returns the input errors from Self::try_new, or ColorError::ConversionOverflow if decoded HDR RGB overflows f32. Intermediate calculations use f64; results are not bitwise portable.

use ferravex_math::{Color, ColorError};
let authored = Color::from_rgba(128, 0, 255, 64);
let linear = authored.try_srgb_to_linear()?;
assert!((linear.r - 0.2158605).abs() < 1.0e-6);
assert_eq!(linear.a, authored.a);
assert_eq!(linear.try_linear_to_srgb()?.to_rgba(), [128, 0, 255, 64]);
Source

pub fn try_linear_to_srgb(self) -> Result<Self, ColorError>

Encodes unpremultiplied linear-light sRGB channels to sRGB.

The linear segment covers magnitudes up to 0.0031308. Negative RGB uses the sign-preserving extension described in Self::try_srgb_to_linear. HDR RGB is retained above one; there is no clamping or tone mapping. Alpha is unchanged and RGB is not multiplied by alpha. Returns the input errors from Self::try_new. Finite linear RGB cannot overflow on encode. Intermediate calculations use f64; results are not bitwise portable.

use ferravex_math::{Color, ColorError};
// Interpolate in linear light, then encode for an sRGB destination.
let black = Color::from_hex(0x000000).try_srgb_to_linear()?;
let white = Color::from_hex(0xffffff).try_srgb_to_linear()?;
let middle = black.lerp(white, 0.5).try_linear_to_srgb()?;
assert_eq!(middle.to_rgba(), [188, 188, 188, 255]);
Source

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

Clips and rounds raw channels to bytes without gamma conversion.

Legacy non-finite behavior is preserved: NaN and negative infinity become zero; positive infinity becomes 255. Use Self::try_clamped first to reject non-finite channels. Encode linear RGB explicitly before sRGB output.

Source

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

Source

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

Interpolates raw RGBA channels, with no gamma conversion or clamping of t. Decode sRGB endpoints first when linear-light interpolation is desired.

Source

pub fn premultiplied(self) -> Self

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

Source§

fn default() -> Color

Returns the “default value” for a type. 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<Color> for [f32; 4]

Source§

fn from(v: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Color> for Vec4

Source§

fn from(v: Color) -> Self

Converts to this type from the input type.
Source§

impl From<Vec4> for Color

Source§

fn from(v: Vec4) -> 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 Copy 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> 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.