use crate::tween::Interpolate;
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Color {
r: f32,
g: f32,
b: f32,
a: f32,
}
impl Color {
pub const TRANSPARENT: Self = Self::rgba(0.0, 0.0, 0.0, 0.0);
pub const WHITE: Self = Self::rgb(1.0, 1.0, 1.0);
pub const BLACK: Self = Self::rgb(0.0, 0.0, 0.0);
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self::rgba(r, g, b, 1.0)
}
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub const fn red(self) -> f32 {
self.r
}
pub const fn green(self) -> f32 {
self.g
}
pub const fn blue(self) -> f32 {
self.b
}
pub const fn alpha(self) -> f32 {
self.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::rgba(
srgb_channel_to_linear(r as f32 / 255.0),
srgb_channel_to_linear(g as f32 / 255.0),
srgb_channel_to_linear(b as f32 / 255.0),
a as f32 / 255.0,
)
}
pub fn with_alpha(self, alpha: f32) -> Self {
Self { a: alpha, ..self }
}
pub fn scale_alpha(self, factor: f32) -> Self {
Self {
a: self.a * factor,
..self
}
}
pub fn is_finite(self) -> bool {
self.r.is_finite() && self.g.is_finite() && self.b.is_finite() && self.a.is_finite()
}
pub fn is_normalized(self) -> bool {
self.is_finite()
&& [self.r, self.g, self.b, self.a]
.into_iter()
.all(|channel| (0.0..=1.0).contains(&channel))
}
pub fn clamp(self) -> Self {
Self::rgba(
sanitize_channel(self.r),
sanitize_channel(self.g),
sanitize_channel(self.b),
sanitize_channel(self.a),
)
}
pub fn to_array(self) -> [f32; 4] {
let color = self.clamp();
[color.r, color.g, color.b, color.a]
}
#[cfg(feature = "wgpu")]
pub fn to_wgpu(self) -> wgpu::Color {
let color = self.clamp();
wgpu::Color {
r: color.r as f64,
g: color.g as f64,
b: color.b as f64,
a: color.a as f64,
}
}
}
fn sanitize_channel(channel: f32) -> f32 {
if channel.is_nan() {
0.0
} else {
channel.clamp(0.0, 1.0)
}
}
fn srgb_channel_to_linear(channel: f32) -> f32 {
if channel <= 0.04045 {
channel / 12.92
} else {
((channel + 0.055) / 1.055).powf(2.4)
}
}
impl Interpolate for Color {
fn interpolate(self, end: Self, amount: f32) -> Self {
Self::rgba(
self.r.interpolate(end.r, amount),
self.g.interpolate(end.g, amount),
self.b.interpolate(end.b, amount),
self.a.interpolate(end.a, amount),
)
}
fn is_valid_interpolation_value(self) -> bool {
self.is_finite()
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Palette {
background: Color,
surface: Color,
grid: Color,
axis: Color,
primary: Color,
secondary: Color,
accent: Color,
warning: Color,
}
impl Palette {
pub const fn from_colors(colors: [Color; 8]) -> Self {
Self {
background: colors[0],
surface: colors[1],
grid: colors[2],
axis: colors[3],
primary: colors[4],
secondary: colors[5],
accent: colors[6],
warning: colors[7],
}
}
pub const fn background(self) -> Color {
self.background
}
pub const fn surface(self) -> Color {
self.surface
}
pub const fn grid(self) -> Color {
self.grid
}
pub const fn axis(self) -> Color {
self.axis
}
pub const fn primary(self) -> Color {
self.primary
}
pub const fn secondary(self) -> Color {
self.secondary
}
pub const fn accent(self) -> Color {
self.accent
}
pub const fn warning(self) -> Color {
self.warning
}
pub fn sim() -> Self {
Self {
background: Color::rgb8(12, 14, 18),
surface: Color::rgb8(27, 31, 39),
grid: Color::rgba8(140, 158, 180, 38),
axis: Color::rgba8(214, 224, 238, 100),
primary: Color::rgb8(86, 195, 255),
secondary: Color::rgb8(128, 228, 167),
accent: Color::rgb8(255, 190, 94),
warning: Color::rgb8(255, 105, 120),
}
}
}
#[cfg(test)]
mod tests {
use super::Color;
#[test]
fn clamp_sanitizes_non_finite_channels() {
let color = Color::rgba(f32::NAN, f32::INFINITY, f32::NEG_INFINITY, 1.5).clamp();
assert_eq!(color, Color::rgba(0.0, 1.0, 0.0, 1.0));
}
#[test]
fn rgb8_converts_srgb_to_linear_light() {
let middle_gray = Color::rgb8(128, 128, 128);
assert!((middle_gray.red() - 0.21586).abs() < 0.0001);
assert_eq!(middle_gray.red(), middle_gray.green());
assert_eq!(middle_gray.green(), middle_gray.blue());
}
}