Skip to content

Commit

Permalink
Remove ColorDifference from Oklab and Oklch
Browse files Browse the repository at this point in the history
  • Loading branch information
Ogeon committed Jul 11, 2021
1 parent c84fb4e commit 6d8e602
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 79 deletions.
35 changes: 3 additions & 32 deletions palette/src/oklab.rs
Expand Up @@ -8,8 +8,6 @@ use rand::distributions::{Distribution, Standard};
#[cfg(feature = "random")]
use rand::Rng;

use crate::color_difference::ColorDifference;
use crate::color_difference::{get_ciede_difference, LabColorDiff};
use crate::convert::FromColorUnclamped;
use crate::encoding::pixel::RawPixel;
use crate::matrix::multiply_xyz;
Expand Down Expand Up @@ -61,9 +59,9 @@ pub type Oklaba<T = f32> = Alpha<Oklab<T>, T>;

/// The [Oklab color space](https://bottosson.github.io/posts/oklab/).
///
/// Oklab is a perceptually-uniform color space similar in structure to [L\*a\*b\*](crate::Lab), but
/// with better perceptual uniformity. It assumes a D65 whitepoint and normal well-lit viewing
/// conditions.
/// Oklab is a perceptually-uniform color space similar in structure to
/// [L\*a\*b\*](crate::Lab), but tries to have a better perceptual uniformity.
/// It assumes a D65 whitepoint and normal well-lit viewing conditions.
#[derive(Debug, PartialEq, Pixel, FromColorUnclamped, WithAlpha)]
#[cfg_attr(feature = "serializing", derive(Serialize, Deserialize))]
#[palette(
Expand Down Expand Up @@ -370,33 +368,6 @@ where
}
}

impl<T> ColorDifference for Oklab<T>
where
T: FloatComponent,
{
type Scalar = T;

fn get_color_difference(&self, other: &Self) -> Self::Scalar {
// Color difference calculation requires Lab and chroma components. This
// function handles the conversion into those components which are then
// passed to `get_ciede_difference()` where calculation is completed.
let self_params = LabColorDiff {
l: self.l,
a: self.a,
b: self.b,
chroma: (self.a * self.a + self.b * self.b).sqrt(),
};
let other_params = LabColorDiff {
l: other.l,
a: other.a,
b: other.b,
chroma: (other.a * other.a + other.b * other.b).sqrt(),
};

get_ciede_difference(&self_params, &other_params)
}
}

impl<T> ComponentWise for Oklab<T>
where
T: FloatComponent,
Expand Down
47 changes: 0 additions & 47 deletions palette/src/oklch.rs
Expand Up @@ -8,7 +8,6 @@ use rand::distributions::{Distribution, Standard};
#[cfg(feature = "random")]
use rand::Rng;

use crate::color_difference::{get_ciede_difference, ColorDifference, LabColorDiff};
use crate::convert::{FromColorUnclamped, IntoColorUnclamped};
use crate::encoding::pixel::RawPixel;
use crate::white_point::D65;
Expand Down Expand Up @@ -355,52 +354,6 @@ where
}
}

/// CIEDE2000 distance metric for color difference.
impl<T> ColorDifference for Oklch<T>
where
T: FloatComponent,
{
type Scalar = T;

fn get_color_difference(&self, other: &Oklch<T>) -> Self::Scalar {
// Prepare a* and b* from Oklch components to calculate color difference
let self_a = clamp(
self.chroma.max(T::zero()) * self.hue.to_radians().cos(),
from_f64(0.0),
from_f64(1.0),
);
let self_b = clamp(
self.chroma.max(T::zero()) * self.hue.to_radians().sin(),
from_f64(0.0),
from_f64(1.0),
);
let other_a = clamp(
other.chroma.max(T::zero()) * other.hue.to_radians().cos(),
from_f64(0.0),
from_f64(1.0),
);
let other_b = clamp(
other.chroma.max(T::zero()) * other.hue.to_radians().sin(),
from_f64(0.0),
from_f64(1.0),
);
let self_params = LabColorDiff {
l: self.l,
a: self_a,
b: self_b,
chroma: self.chroma,
};
let other_params = LabColorDiff {
l: other.l,
a: other_a,
b: other_b,
chroma: other.chroma,
};

get_ciede_difference(&self_params, &other_params)
}
}

impl<T> Saturate for Oklch<T>
where
T: FloatComponent,
Expand Down

0 comments on commit 6d8e602

Please sign in to comment.