Skip to content

Commit

Permalink
Fixes lalphabeta
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Aug 4, 2024
1 parent a9fa623 commit 657b51e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
11 changes: 5 additions & 6 deletions src/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

use std::time::Instant;

use image::io::Reader as ImageReader;
use image::{EncodableLayout, GenericImageView};
use image::{EncodableLayout, GenericImageView, ImageReader};

use colorutils_rs::*;

Expand All @@ -21,11 +20,11 @@ pub const fn shuffle(z: u32, y: u32, x: u32, w: u32) -> i32 {
}

fn main() {
let r = 0;
let g = 127;
let b = 255;
let r = 1;
let g = 75;
let b = 1;
let rgb = Rgb::<u8>::new(r, g, b);
let xyb = Oklab::from_rgb(rgb, TransferFunction::Srgb);
let xyb = LAlphaBeta::from_rgb(rgb, TransferFunction::Srgb);
println!("XYB {:?}", xyb);
println!("Rgb {:?}", xyb.to_rgb(TransferFunction::Srgb));
// let restored = lalphabeta.to_rgb(TransferFunction::Srgb);
Expand Down
35 changes: 21 additions & 14 deletions src/lalphabeta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* // license that can be found in the LICENSE file.
*/
use crate::{Rgb, TransferFunction, Xyz, SRGB_TO_XYZ_D65, XYZ_TO_SRGB_D65};
use erydanos::{Exponential, Logarithmic};

/// Represents l-alpha-beta (lαβ) colorspace
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq)]
Expand Down Expand Up @@ -35,27 +34,35 @@ impl LAlphaBeta {
let l_lms = 0.3897 * xyz.x + 0.6890 * xyz.y - 0.0787 * xyz.z;
let m = -0.2298 * xyz.x + 1.1834 * xyz.y + 0.0464 * xyz.z;
let s = xyz.z;
let lp = if l_lms > 0. { l_lms.eln() } else { 0. };
let mp = if m > 0. { m.eln() } else { 0. };
let sp = if s > 0. { s.eln() } else { 0. };
let l = 0.5774 * lp + 0.5774 * mp + 0.5774 * sp;
let alpha = 0.4082 * lp + 0.4082 * mp - 0.8165 * sp;
let beta = 1.4142 * lp - 1.4142 * mp;
let lp = if l_lms > 0. { l_lms.log10() } else { 0. };
let mp = if m > 0. { m.log10() } else { 0. };
let sp = if s > 0. { s.log10() } else { 0. };
const ONE_F_SQRT3: f32 = 0.57735026918962576f32;
const ONE_F_SQRT6: f32 = 0.40824829046386301f32;
const TWO_F_SQRT6: f32 = 0.816496580927726032f32;
const ONE_F_SQRT2: f32 = std::f32::consts::FRAC_1_SQRT_2;
let l = ONE_F_SQRT3 * lp + ONE_F_SQRT3 * mp + ONE_F_SQRT3 * sp;
let alpha = ONE_F_SQRT6 * lp + ONE_F_SQRT6 * mp - TWO_F_SQRT6 * sp;
let beta = ONE_F_SQRT2 * lp - ONE_F_SQRT2 * mp;
LAlphaBeta::new(l, alpha, beta)
}

/// Converts l-alpha-beta to XYZ
#[inline]
pub fn to_xyz(&self) -> Xyz {
let l_a = self.l * 0.577324;
let s_a = 0.408263 * self.alpha;
let p_b = 0.353557 * self.beta;
const ONE_F_SQRT3: f32 = 0.57735026918962576f32;
const ONE_F_SQRT6: f32 = 0.40824829046386301f32;
const ONE_F_SQRT2: f32 = std::f32::consts::FRAC_1_SQRT_2;
const TWO_F_SQRT6: f32 = 0.816496580927726032f32;
let l_a = self.l * ONE_F_SQRT3;
let s_a = ONE_F_SQRT6 * self.alpha;
let p_b = ONE_F_SQRT2 * self.beta;
let lp = l_a + s_a + p_b;
let mp = l_a + s_a - p_b;
let sp = self.l * 0.577253 - 0.816526 * self.alpha;
let l = lp.eexp();
let m = mp.eexp();
let s = sp.eexp();
let sp = l_a - TWO_F_SQRT6 * self.alpha;
let l = if lp == 0. { 0. } else { 10f32.powf(lp) };
let m = if mp == 0. { 0. } else { 10f32.powf(mp) };
let s = if sp == 0. { 0. } else { 10f32.powf(sp) };
let x = 1.91024 * l - 1.11218 * m + 0.201941 * s;
let y = 0.370942 * l + 0.62905 * m + 5.13315e-6 * s;
let z = s;
Expand Down

0 comments on commit 657b51e

Please sign in to comment.