use std::sync::LazyLock;
static SRGB_U8_TO_LINEAR: LazyLock<[f32; 256]> =
LazyLock::new(|| std::array::from_fn(|component| srgb_to_linear(component as f32 / 255.0)));
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub const TRANSPARENT: Self = Self::linear(0.0, 0.0, 0.0, 0.0);
pub const BLACK: Self = Self::linear(0.0, 0.0, 0.0, 1.0);
pub const WHITE: Self = Self::linear(1.0, 1.0, 1.0, 1.0);
pub const fn linear(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub fn rgb8(r: u8, g: u8, b: u8) -> Self {
Self::rgba8(r, g, b, 255)
}
pub fn rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
Self {
r: SRGB_U8_TO_LINEAR[r as usize],
g: SRGB_U8_TO_LINEAR[g as usize],
b: SRGB_U8_TO_LINEAR[b as usize],
a: a as f32 / 255.0,
}
}
pub fn with_alpha(self, alpha: f32) -> Self {
Self {
a: alpha.clamp(0.0, 1.0),
..self
}
}
pub(crate) fn multiply_alpha(self, opacity: f32) -> Self {
let opacity = if opacity.is_finite() {
opacity.clamp(0.0, 1.0)
} else {
1.0
};
self.with_alpha(self.a * opacity)
}
pub(crate) fn interpolate_premultiplied(from: Self, to: Self, phase: f32) -> Self {
if phase == 0.0 {
return from;
}
if phase == 1.0 {
return to;
}
let alpha = from.a + (to.a - from.a) * phase;
if alpha.abs() <= f32::EPSILON {
return Self::linear(0.0, 0.0, 0.0, alpha);
}
let component = |from_component: f32, to_component: f32| {
let from_premultiplied = from_component * from.a;
let to_premultiplied = to_component * to.a;
(from_premultiplied + (to_premultiplied - from_premultiplied) * phase) / alpha
};
Self::linear(
component(from.r, to.r),
component(from.g, to.g),
component(from.b, to.b),
alpha,
)
}
pub(crate) fn as_array(self) -> [f32; 4] {
[self.r, self.g, self.b, self.a]
}
pub(crate) fn to_srgba8(self) -> [u8; 4] {
[
(linear_to_srgb(self.r) * 255.0).round() as u8,
(linear_to_srgb(self.g) * 255.0).round() as u8,
(linear_to_srgb(self.b) * 255.0).round() as u8,
(self.a.clamp(0.0, 1.0) * 255.0).round() as u8,
]
}
}
impl Default for Color {
fn default() -> Self {
Self::TRANSPARENT
}
}
impl From<quickgui_system::SystemColor> for Color {
fn from(color: quickgui_system::SystemColor) -> Self {
Self::rgba8(color.red, color.green, color.blue, color.alpha)
}
}
fn srgb_to_linear(value: f32) -> f32 {
if value <= 0.04045 {
value / 12.92
} else {
((value + 0.055) / 1.055).powf(2.4)
}
}
fn linear_to_srgb(value: f32) -> f32 {
let value = value.clamp(0.0, 1.0);
if value <= 0.003_130_8 {
value * 12.92
} else {
1.055 * value.powf(1.0 / 2.4) - 0.055
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn srgb_round_trip_is_exact_to_one_byte() {
for value in 0..=255 {
let color = Color::rgba8(value, value, value, value);
assert_eq!(color.to_srgba8(), [value, value, value, value]);
}
}
}