#[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: f32Implementations§
Source§impl Color
impl Color
Sourcepub const fn new(r: f32, g: f32, b: f32, a: f32) -> Color
pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Color
Stores raw channels without validation, clamping or gamma conversion.
Sourcepub fn try_new(r: f32, g: f32, b: f32, a: f32) -> Result<Color, ColorError>
pub fn try_new(r: f32, g: f32, b: f32, a: f32) -> Result<Color, 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));Sourcepub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color
pub const fn from_rgba(r: u8, g: u8, b: u8, a: u8) -> Color
Normalizes bytes to raw channels; does not decode sRGB.
pub const fn to_vec(self) -> Vec4
pub const fn to_array(self) -> [f32; 4]
Sourcepub fn is_finite(self) -> bool
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());Sourcepub fn try_clamped(self) -> Result<Color, ColorError>
pub fn try_clamped(self) -> Result<Color, 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));Sourcepub fn try_srgb_to_linear(self) -> Result<Color, ColorError>
pub fn try_srgb_to_linear(self) -> Result<Color, 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]);Sourcepub fn try_linear_to_srgb(self) -> Result<Color, ColorError>
pub fn try_linear_to_srgb(self) -> Result<Color, 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]);Sourcepub fn to_rgba(self) -> [u8; 4]
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.
pub const fn with_alpha(self, alpha: f32) -> Color
Sourcepub fn lerp(self, other: Color, t: f32) -> Color
pub fn lerp(self, other: Color, t: f32) -> Color
Interpolates raw RGBA channels, with no gamma conversion or clamping of t.
Decode sRGB endpoints first when linear-light interpolation is desired.