Skip to main content

oa/core/
color.rs

1//! Backend-neutral RGBA color value.
2//!
3//! Donor: `oacpp/source/cpp/include/oa/core/color.h` and `fnColor.h`.
4//! `Color` carries four floating-point components but no encoded transfer
5//! function or gamut. Domain values such as video colorimetry and HDR emission
6//! retain their own typed metadata.
7
8use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
9
10/// Four-component RGBA color used by UI, plotting, overlays, and ordinary
11/// normalized render colors.
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct Color {
14	pub r: f32,
15	pub g: f32,
16	pub b: f32,
17	pub a: f32,
18}
19
20impl Color {
21	pub const fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
22		Self { r, g, b, a }
23	}
24
25	pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
26		Self::new(r, g, b, 1.0)
27	}
28
29	/// Pack to `0xRRGGBBAA`, clamping each component and rounding to nearest.
30	pub fn to_u32(self) -> u32 {
31		let to_u8 = |value: f32| (value.clamp(0.0, 1.0) * 255.0).round() as u32;
32		(to_u8(self.r) << 24) | (to_u8(self.g) << 16) | (to_u8(self.b) << 8) | to_u8(self.a)
33	}
34
35	/// Unpack one `0xRRGGBBAA` word.
36	pub const fn new_from_u32(rgba: u32) -> Self {
37		Self::new(
38			((rgba >> 24) & 0xff) as f32 / 255.0,
39			((rgba >> 16) & 0xff) as f32 / 255.0,
40			((rgba >> 8) & 0xff) as f32 / 255.0,
41			(rgba & 0xff) as f32 / 255.0,
42		)
43	}
44
45	pub const fn with_alpha(self, alpha: f32) -> Self {
46		Self::new(self.r, self.g, self.b, alpha)
47	}
48
49	pub const fn lerp(self, other: Self, fraction: f32) -> Self {
50		Self::new(
51			self.r + (other.r - self.r) * fraction,
52			self.g + (other.g - self.g) * fraction,
53			self.b + (other.b - self.b) * fraction,
54			self.a + (other.a - self.a) * fraction,
55		)
56	}
57
58	pub fn clamp(self) -> Self {
59		Self::new(
60			self.r.clamp(0.0, 1.0),
61			self.g.clamp(0.0, 1.0),
62			self.b.clamp(0.0, 1.0),
63			self.a.clamp(0.0, 1.0),
64		)
65	}
66
67	pub const fn to_array(self) -> [f32; 4] {
68		[self.r, self.g, self.b, self.a]
69	}
70
71	pub const fn accent() -> Self {
72		Self::new(0.388, 0.400, 0.945, 1.0)
73	}
74	pub const fn accent_hover() -> Self {
75		Self::new(0.506, 0.549, 0.973, 1.0)
76	}
77	pub const fn success() -> Self {
78		Self::new(0.188, 0.820, 0.345, 1.0)
79	}
80	pub const fn warning() -> Self {
81		Self::new(0.961, 0.620, 0.043, 1.0)
82	}
83	pub const fn error() -> Self {
84		Self::new(1.000, 0.271, 0.227, 1.0)
85	}
86	pub const fn orange() -> Self {
87		Self::new(1.000, 0.420, 0.208, 1.0)
88	}
89	pub const fn purple() -> Self {
90		Self::new(0.659, 0.333, 0.969, 1.0)
91	}
92	pub const fn cyan() -> Self {
93		Self::new(0.133, 0.827, 0.933, 1.0)
94	}
95	pub const fn pink() -> Self {
96		Self::new(0.925, 0.282, 0.600, 1.0)
97	}
98	pub const fn yellow() -> Self {
99		Self::warning()
100	}
101	pub const fn text_primary() -> Self {
102		Self::new(0.961, 0.961, 0.961, 1.0)
103	}
104}
105
106impl Default for Color {
107	fn default() -> Self {
108		Self::new(0.0, 0.0, 0.0, 1.0)
109	}
110}
111
112macro_rules! component_binary {
113	($trait:ident, $method:ident, $op:tt) => {
114		impl $trait for Color {
115			type Output = Self;
116			fn $method(self, rhs: Self) -> Self { Self::new(self.r $op rhs.r, self.g $op rhs.g, self.b $op rhs.b, self.a $op rhs.a) }
117		}
118	};
119}
120component_binary!(Add, add, +);
121component_binary!(Sub, sub, -);
122component_binary!(Mul, mul, *);
123
124impl Mul<f32> for Color {
125	type Output = Self;
126	fn mul(self, rhs: f32) -> Self {
127		Self::new(self.r * rhs, self.g * rhs, self.b * rhs, self.a * rhs)
128	}
129}
130impl Mul<Color> for f32 {
131	type Output = Color;
132	fn mul(self, rhs: Color) -> Color {
133		rhs * self
134	}
135}
136impl Div<f32> for Color {
137	type Output = Self;
138	fn div(self, rhs: f32) -> Self {
139		Self::new(self.r / rhs, self.g / rhs, self.b / rhs, self.a / rhs)
140	}
141}
142
143macro_rules! assign_op {
144	($trait:ident, $method:ident, $op:tt, $rhs:ty) => {
145		impl $trait<$rhs> for Color {
146			fn $method(&mut self, rhs: $rhs) { *self = *self $op rhs; }
147		}
148	};
149}
150assign_op!(AddAssign, add_assign, +, Color);
151assign_op!(SubAssign, sub_assign, -, Color);
152assign_op!(MulAssign, mul_assign, *, Color);
153assign_op!(MulAssign, mul_assign, *, f32);
154assign_op!(DivAssign, div_assign, /, f32);