use crate::{Border, Color, CornerRadius, Gradient, Padding, Rect, Shadow, Transform, Vec2};
pub trait Interpolate: Clone + PartialEq + 'static {
fn interpolate(&self, to: &Self, t: f32) -> Self;
}
impl Interpolate for f32 {
fn interpolate(&self, to: &Self, t: f32) -> Self {
self + (to - self) * t
}
}
impl Interpolate for f64 {
fn interpolate(&self, to: &Self, t: f32) -> Self {
self + (to - self) * f64::from(t)
}
}
impl Interpolate for Vec2 {
fn interpolate(&self, to: &Self, t: f32) -> Self {
self.lerp(*to, t)
}
}
impl Interpolate for Color {
fn interpolate(&self, to: &Self, t: f32) -> Self {
if t == 0.0 {
return *self;
}
if t == 1.0 {
return *to;
}
let from = self.linear();
let to = to.linear();
let mut rgba = [0; 4];
for i in 0..4 {
let value = from[i].interpolate(&to[i], t).clamp(0.0, 1.0);
let encoded = if i == 3 { value } else { encode_srgb(value) };
rgba[i] = (encoded * 255.0).round() as u8;
}
Self(rgba)
}
}
fn encode_srgb(linear: f32) -> f32 {
if linear <= 0.0031308 {
linear * 12.92
} else {
1.055 * linear.powf(1.0 / 2.4) - 0.055
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Oklab(pub Color);
#[allow(clippy::excessive_precision)]
impl Oklab {
fn lab(color: Color) -> [f32; 3] {
let [r, g, b, _] = color.linear();
let l = (0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b).cbrt();
let m = (0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b).cbrt();
let s = (0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b).cbrt();
[
0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s,
0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s,
]
}
fn color(lab: [f32; 3], alpha: f32) -> Color {
let [lightness, a, b] = lab;
let l = (lightness + 0.3963377774 * a + 0.2158037573 * b).powi(3);
let m = (lightness - 0.1055613458 * a - 0.0638541728 * b).powi(3);
let s = (lightness - 0.0894841775 * a - 1.2914855480 * b).powi(3);
let linear = [
4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
-1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
-0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
];
let byte = |v: f32| (encode_srgb(v.clamp(0.0, 1.0)) * 255.0).round() as u8;
Color([
byte(linear[0]),
byte(linear[1]),
byte(linear[2]),
(alpha.clamp(0.0, 1.0) * 255.0).round() as u8,
])
}
}
impl Interpolate for Oklab {
fn interpolate(&self, to: &Self, t: f32) -> Self {
if t == 0.0 {
return *self;
}
if t == 1.0 {
return *to;
}
let (from_lab, to_lab) = (Self::lab(self.0), Self::lab(to.0));
let lab = from_lab.interpolate(&to_lab, t);
let alpha = f32::from(self.0 .0[3]) / 255.0;
let alpha = alpha.interpolate(&(f32::from(to.0 .0[3]) / 255.0), t);
Self(Self::color(lab, alpha))
}
}
impl From<Color> for Oklab {
fn from(color: Color) -> Self {
Self(color)
}
}
impl From<Oklab> for Color {
fn from(color: Oklab) -> Self {
color.0
}
}
#[macro_export]
macro_rules! impl_interpolate {
($ty:ty { $($field:ident),+ $(,)? }) => {
impl $crate::Interpolate for $ty {
fn interpolate(&self, to: &Self, t: f32) -> Self {
Self {
$($field: $crate::Interpolate::interpolate(&self.$field, &to.$field, t)),+
}
}
}
};
}
macro_rules! fields {
($ty:ty, $($field:ident),+ $(,)?) => {
impl Interpolate for $ty {
fn interpolate(&self, to: &Self, t: f32) -> Self {
Self { $($field: self.$field.interpolate(&to.$field, t)),+ }
}
}
};
}
fields!(Border, width, color);
fields!(CornerRadius, top_left, top_right, bottom_left, bottom_right);
fields!(Shadow, color, offset, blur_radius, spread);
fields!(Rect, min, max);
fields!(Padding, left, right, top, bottom);
fields!(Transform, scale, translation, angle);
impl Interpolate for Gradient {
fn interpolate(&self, to: &Self, t: f32) -> Self {
Self {
start: self.start.interpolate(&to.start, t),
end: self.end.interpolate(&to.end, t),
direction: if t >= 1.0 {
to.direction
} else {
self.direction
},
}
}
}
impl<T: Interpolate, const N: usize> Interpolate for [T; N] {
fn interpolate(&self, to: &Self, t: f32) -> Self {
std::array::from_fn(|i| self[i].interpolate(&to[i], t))
}
}
impl<A: Interpolate, B: Interpolate> Interpolate for (A, B) {
fn interpolate(&self, to: &Self, t: f32) -> Self {
(self.0.interpolate(&to.0, t), self.1.interpolate(&to.1, t))
}
}
impl<A: Interpolate, B: Interpolate, C: Interpolate> Interpolate for (A, B, C) {
fn interpolate(&self, to: &Self, t: f32) -> Self {
(
self.0.interpolate(&to.0, t),
self.1.interpolate(&to.1, t),
self.2.interpolate(&to.2, t),
)
}
}
impl<T: Interpolate> Interpolate for Option<T> {
fn interpolate(&self, to: &Self, t: f32) -> Self {
match (self, to) {
(Some(a), Some(b)) => Some(a.interpolate(b, t)),
_ if t >= 1.0 => to.clone(),
_ => self.clone(),
}
}
}
impl<T: Interpolate> Interpolate for Vec<T> {
fn interpolate(&self, to: &Self, t: f32) -> Self {
if self.len() != to.len() {
return if t >= 1.0 { to.clone() } else { self.clone() };
}
self.iter()
.zip(to)
.map(|(a, b)| a.interpolate(b, t))
.collect()
}
}