use lgwks_deps::libm;
const M1_LONG_RED: f32 = f32::from_bits(0x3ED3_0EB1);
const M1_LONG_GREEN: f32 = f32::from_bits(0x3F09_4D17);
const M1_LONG_BLUE: f32 = f32::from_bits(0x3D52_B909);
const M1_MEDIUM_RED: f32 = f32::from_bits(0x3E58_FD3B);
const M1_MEDIUM_GREEN: f32 = f32::from_bits(0x3F2E_4253);
const M1_MEDIUM_BLUE: f32 = f32::from_bits(0x3DDB_F2F0);
const M1_SHORT_RED: f32 = f32::from_bits(0x3DB4_D7EC);
const M1_SHORT_GREEN: f32 = f32::from_bits(0x3E90_3D74);
const M1_SHORT_BLUE: f32 = f32::from_bits(0x3F21_4649);
const M2_LIGHT_LONG: f32 = f32::from_bits(0x3E57_8152);
const M2_LIGHT_MEDIUM: f32 = f32::from_bits(0x3F4B_2A89);
const M2_LIGHT_SHORT: f32 = f32::from_bits(0x3B85_6ECE);
const M2_AXIS_LONG: f32 = f32::from_bits(0x3FFD_2F0E);
const M2_AXIS_MEDIUM: f32 = f32::from_bits(0x401B_6E0E);
const M2_AXIS_SHORT: f32 = f32::from_bits(0x3EE6_B438);
const M2_BLUE_LONG: f32 = f32::from_bits(0x3CD4_34B4);
const M2_BLUE_MEDIUM: f32 = f32::from_bits(0x3F48_63BB);
const M2_BLUE_SHORT: f32 = f32::from_bits(0x3F4F_0560);
const INV_M2_LONG_LIGHT: f32 = f32::from_bits(0x3ECA_ECC9);
const INV_M2_LONG_AXIS: f32 = f32::from_bits(0x3E5C_FBA9);
const INV_M2_MEDIUM_LIGHT: f32 = f32::from_bits(0x3DD8_308C);
const INV_M2_MEDIUM_AXIS: f32 = f32::from_bits(0x3D82_C5FA);
const INV_M2_SHORT_LIGHT: f32 = f32::from_bits(0x3DB7_437B);
const INV_M2_SHORT_BLUE: f32 = f32::from_bits(0x3FA5_4F66);
const INV_M1_RED_LONG: f32 = f32::from_bits(0x4082_74AB);
const INV_M1_RED_MEDIUM: f32 = f32::from_bits(0x4053_B18C);
const INV_M1_RED_SHORT: f32 = f32::from_bits(0x3E6C_8362);
const INV_M1_GREEN_LONG: f32 = f32::from_bits(0x3FA2_5C2D);
const INV_M1_GREEN_MEDIUM: f32 = f32::from_bits(0x4027_0644);
const INV_M1_GREEN_SHORT: f32 = f32::from_bits(0x3EAE_C16A);
const INV_M1_BLUE_LONG: f32 = f32::from_bits(0x3B89_7F53);
const INV_M1_BLUE_MEDIUM: f32 = f32::from_bits(0x3F34_133E);
const INV_M1_BLUE_SHORT: f32 = f32::from_bits(0x3FDA_931E);
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct OkLab {
lightness: f32,
axis_a: f32,
axis_b: f32,
}
impl OkLab {
#[must_use]
pub const fn new(lightness: f32, axis_a: f32, axis_b: f32) -> Self {
Self {
lightness,
axis_a,
axis_b,
}
}
#[must_use]
pub const fn lightness(&self) -> f32 {
self.lightness
}
#[must_use]
pub const fn axis_a(&self) -> f32 {
self.axis_a
}
#[must_use]
pub const fn axis_b(&self) -> f32 {
self.axis_b
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub struct SRgb {
red: f32,
green: f32,
blue: f32,
}
impl SRgb {
#[must_use]
pub const fn new(red: f32, green: f32, blue: f32) -> Self {
Self { red, green, blue }
}
#[must_use]
pub const fn red(&self) -> f32 {
self.red
}
#[must_use]
pub const fn green(&self) -> f32 {
self.green
}
#[must_use]
pub const fn blue(&self) -> f32 {
self.blue
}
#[must_use]
pub const fn is_in_srgb_gamut(self) -> bool {
self.red >= 0.0
&& self.red <= 1.0
&& self.green >= 0.0
&& self.green <= 1.0
&& self.blue >= 0.0
&& self.blue <= 1.0
}
#[must_use]
pub fn clamp(self) -> Self {
Self::new(
self.red.clamp(0.0, 1.0),
self.green.clamp(0.0, 1.0),
self.blue.clamp(0.0, 1.0),
)
}
}
#[must_use]
pub fn interp_oklab(from: SRgb, to: SRgb, mix: f32) -> SRgb {
let from_lab = srgb_to_oklab(from);
let to_lab = srgb_to_oklab(to);
map_to_srgb_gamut(lerp_oklab(from_lab, to_lab, mix))
}
#[must_use]
pub fn map_to_srgb_gamut(colour: OkLab) -> SRgb {
let candidate = oklab_to_srgb(colour);
if candidate.is_in_srgb_gamut() {
return candidate.clamp();
}
let chroma = libm::sqrtf(colour.axis_a * colour.axis_a + colour.axis_b * colour.axis_b);
if chroma < 1e-6 {
let lightness_clamped = colour.lightness.clamp(0.0, 1.0);
return oklab_to_srgb(OkLab::new(lightness_clamped, 0.0, 0.0)).clamp();
}
let hue_a = colour.axis_a / chroma;
let hue_b = colour.axis_b / chroma;
let mut low: f32 = 0.0;
let mut high: f32 = chroma;
for _ in 0..8 {
let mid = (low + high) / 2.0;
let trial = OkLab::new(colour.lightness, hue_a * mid, hue_b * mid);
if oklab_to_srgb(trial).is_in_srgb_gamut() {
low = mid;
} else {
high = mid;
}
}
oklab_to_srgb(OkLab::new(colour.lightness, hue_a * low, hue_b * low)).clamp()
}
#[must_use]
pub const fn lerp_oklab(from: OkLab, to: OkLab, mix: f32) -> OkLab {
OkLab::new(
from.lightness + (to.lightness - from.lightness) * mix,
from.axis_a + (to.axis_a - from.axis_a) * mix,
from.axis_b + (to.axis_b - from.axis_b) * mix,
)
}
#[must_use]
pub fn srgb_to_oklab(colour: SRgb) -> OkLab {
let red = srgb_channel_to_linear(colour.red);
let green = srgb_channel_to_linear(colour.green);
let blue = srgb_channel_to_linear(colour.blue);
let cone_light = M1_LONG_RED * red + M1_LONG_GREEN * green + M1_LONG_BLUE * blue;
let cone_medium = M1_MEDIUM_RED * red + M1_MEDIUM_GREEN * green + M1_MEDIUM_BLUE * blue;
let cone_short = M1_SHORT_RED * red + M1_SHORT_GREEN * green + M1_SHORT_BLUE * blue;
let cone_light_nonlinear = libm::cbrtf(cone_light);
let cone_medium_nonlinear = libm::cbrtf(cone_medium);
let cone_short_nonlinear = libm::cbrtf(cone_short);
OkLab::new(
M2_LIGHT_LONG * cone_light_nonlinear + M2_LIGHT_MEDIUM * cone_medium_nonlinear
- M2_LIGHT_SHORT * cone_short_nonlinear,
M2_AXIS_LONG * cone_light_nonlinear - M2_AXIS_MEDIUM * cone_medium_nonlinear
+ M2_AXIS_SHORT * cone_short_nonlinear,
M2_BLUE_LONG * cone_light_nonlinear + M2_BLUE_MEDIUM * cone_medium_nonlinear
- M2_BLUE_SHORT * cone_short_nonlinear,
)
}
#[must_use]
pub fn oklab_to_srgb(colour: OkLab) -> SRgb {
let cone_light_nonlinear =
colour.lightness + INV_M2_LONG_LIGHT * colour.axis_a + INV_M2_LONG_AXIS * colour.axis_b;
let cone_medium_nonlinear =
colour.lightness - INV_M2_MEDIUM_LIGHT * colour.axis_a - INV_M2_MEDIUM_AXIS * colour.axis_b;
let cone_short_nonlinear =
colour.lightness - INV_M2_SHORT_LIGHT * colour.axis_a - INV_M2_SHORT_BLUE * colour.axis_b;
let cone_light = cone_light_nonlinear * cone_light_nonlinear * cone_light_nonlinear;
let cone_medium = cone_medium_nonlinear * cone_medium_nonlinear * cone_medium_nonlinear;
let cone_short = cone_short_nonlinear * cone_short_nonlinear * cone_short_nonlinear;
let red_linear = INV_M1_RED_LONG * cone_light - INV_M1_RED_MEDIUM * cone_medium
+ INV_M1_RED_SHORT * cone_short;
let green_linear = -INV_M1_GREEN_LONG * cone_light + INV_M1_GREEN_MEDIUM * cone_medium
- INV_M1_GREEN_SHORT * cone_short;
let blue_linear = -INV_M1_BLUE_LONG * cone_light - INV_M1_BLUE_MEDIUM * cone_medium
+ INV_M1_BLUE_SHORT * cone_short;
SRgb::new(
linear_to_srgb_channel(red_linear),
linear_to_srgb_channel(green_linear),
linear_to_srgb_channel(blue_linear),
)
}
fn srgb_channel_to_linear(channel: f32) -> f32 {
if channel <= 0.04045 {
channel / 12.92
} else {
libm::powf((channel + 0.055) / 1.055, 2.4)
}
}
fn linear_to_srgb_channel(channel: f32) -> f32 {
if channel <= 0.0031308 {
channel * 12.92
} else {
1.055 * libm::powf(channel, 1.0 / 2.4) - 0.055
}
}