1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
use std::{fmt::Display, marker::PhantomData};
use num_traits::clamp;
use crate::{
color::{white_point::WhitePoint, LCHab, D65, XYZ},
math::FloatNumber,
};
/// The CIE L*a*b* color representation.
///
/// See the following for more details:
/// [CIELAB color space - Wikipedia](https://en.wikipedia.org/wiki/CIELAB_color_space)
///
/// # Type Parameters
/// * `T` - The floating point type.
/// * `W` - The white point type.
///
/// # Fields
/// * `l` - The L component.
/// * `a` - The a component.
/// * `b` - The b component.
///
/// # Examples
/// ```
/// use auto_palette::color::{LCHab, Lab, D65, XYZ};
///
/// let xyz = XYZ::new(0.3576, 0.7152, 0.1192);
/// let lab = Lab::<_>::from(&xyz);
/// assert_eq!(format!("{}", lab), "Lab(87.74, -86.18, 83.18)");
///
/// let lchab: LCHab<_> = (&lab).into();
/// assert_eq!(format!("{}", lchab), "LCH(ab)(87.74, 119.78, 136.02)");
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Lab<T, W = D65>
where
T: FloatNumber,
W: WhitePoint,
{
pub l: T,
pub a: T,
pub b: T,
_marker: PhantomData<W>,
}
impl<T, W> Lab<T, W>
where
T: FloatNumber,
W: WhitePoint,
{
/// Creates a new `Lab` instance.
///
/// # Arguments
/// * `l` - The L component.
/// * `a` - The a component.
/// * `b` - The b component.
///
/// # Returns
/// A new `Lab` instance.
#[must_use]
pub fn new(l: T, a: T, b: T) -> Self {
Self {
l: clamp(l, Lab::<T, W>::min_l(), Lab::<T, W>::max_l()),
a: clamp(a, Lab::<T, W>::min_a(), Lab::<T, W>::max_a()),
b: clamp(b, Lab::<T, W>::min_b(), Lab::<T, W>::max_b()),
_marker: PhantomData,
}
}
/// Returns the minimum value of the L component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The minimum value of the L component.
#[inline]
#[must_use]
pub(crate) fn min_l() -> T {
T::zero()
}
/// Returns the maximum value of the L component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The maximum value of the L component.
#[inline]
#[must_use]
pub(crate) fn max_l() -> T {
T::from_f32(100.0)
}
/// Returns the minimum value of the a component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The minimum value of the a component.
#[inline]
#[must_use]
pub(crate) fn min_a() -> T {
T::from_f32(-128.0)
}
/// Returns the maximum value of the a component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The maximum value of the a component.
#[inline]
#[must_use]
pub(crate) fn max_a() -> T {
T::from_f32(127.0)
}
/// Returns the minimum value of the b component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The minimum value of the b component.
#[inline]
#[must_use]
pub(crate) fn min_b() -> T {
T::from_f32(-128.0)
}
/// Returns the maximum value of the b component.
///
/// # Type Parameters
/// * `T` - The floating point type.
///
/// # Returns
/// The maximum value of the b component.
#[inline]
#[must_use]
pub(crate) fn max_b() -> T {
T::from_f32(127.0)
}
}
impl<T> Display for Lab<T>
where
T: FloatNumber,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Lab({:.2}, {:.2}, {:.2})", self.l, self.a, self.b)
}
}
impl<T, W> From<&XYZ<T>> for Lab<T, W>
where
T: FloatNumber,
W: WhitePoint,
{
fn from(xyz: &XYZ<T>) -> Self {
let (l, a, b) = xyz_to_lab::<T, W>(xyz.x, xyz.y, xyz.z);
Lab::new(l, a, b)
}
}
impl<T, W> From<&LCHab<T, W>> for Lab<T, W>
where
T: FloatNumber,
W: WhitePoint,
{
fn from(lch: &LCHab<T, W>) -> Self {
// This implementation is based on the formulae from the following sources:
// http://www.brucelindbloom.com/index.html?Eqn_Lab_to_LCH.html
let l = lch.l;
let c = lch.c;
let h = lch.h.to_radians();
let a = c * h.cos();
let b = c * h.sin();
Lab::new(l, a, b)
}
}
/// Converts the CIE XYZ color space to the CIE L*a*b* color space.
///
/// # Type Parameters
/// * `T` - The floating point type.
/// * `W` - The white point type.
///
/// # Arguments
/// * `x` - The X component of the XYZ color.
/// * `y` - The Y component of the XYZ color.
/// * `z` - The Z component of the XYZ color.
///
/// # Returns
/// The L*a*b* color space representation of the XYZ color. The tuple contains the L, a, and b components.
#[inline]
#[must_use]
pub fn xyz_to_lab<T, W>(x: T, y: T, z: T) -> (T, T, T)
where
T: FloatNumber,
W: WhitePoint,
{
let epsilon = T::from_f64(6.0 / 29.0).powi(3);
let kappa = T::from_f64(841.0 / 108.0); // ((29.0 / 6.0) ^ 2) / 3.0
let delta = T::from_f64(4.0 / 29.0);
let f = |t: T| -> T {
if t > epsilon {
t.cbrt()
} else {
kappa * t + delta
}
};
let fx = f(x / W::x());
let fy = f(y / W::y());
let fz = f(z / W::z());
let l = T::from_f32(116.0) * fy - T::from_f32(16.0);
let a = T::from_f32(500.0) * (fx - fy);
let b = T::from_f32(200.0) * (fy - fz);
(
clamp(l, Lab::<T, W>::min_l(), Lab::<T, W>::max_l()),
clamp(a, Lab::<T, W>::min_a(), Lab::<T, W>::max_a()),
clamp(b, Lab::<T, W>::min_b(), Lab::<T, W>::max_b()),
)
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
use crate::color::white_point::D65;
#[test]
fn test_new() {
// Act
let actual = Lab::<_>::new(53.2437, 80.09315, 67.2388);
// Assert
assert_eq!(actual.l, 53.2437);
assert_eq!(actual.a, 80.09315);
assert_eq!(actual.b, 67.2388);
}
#[test]
fn test_fmt() {
// Act
let lab = Lab::<_>::new(53.2437, 80.09315, 67.2388);
let actual = format!("{}", lab);
// Assert
assert_eq!(actual, "Lab(53.24, 80.09, 67.24)");
}
#[test]
fn test_from_xyz() {
// Act
let xyz: XYZ<f64> = XYZ::new(0.3576, 0.7152, 0.1192);
let actual: Lab<f64> = Lab::<_>::from(&xyz);
// Assert
assert!((actual.l - 87.7376).abs() < 1e-3);
assert!((actual.a + 86.1846).abs() < 1e-3);
assert!((actual.b - 83.1813).abs() < 1e-3);
}
#[test]
fn test_from_lchab() {
// Act
let lchab: LCHab<f64> = LCHab::new(54.617, 92.151, 27.756);
let actual = Lab::from(&lchab);
// Assert
assert_eq!(actual.l, 54.617);
assert!((actual.a - 81.549).abs() < 1e-3);
assert!((actual.b - 42.915).abs() < 1e-3);
}
#[rstest]
#[case::black((0.0, 0.0, 0.0), (0.0, 0.0, 0.0))]
#[case::white((0.9505, 1.0000, 1.0886), (100.0, 0.0052, 0.0141))]
#[case::red((0.4125, 0.2127, 0.0193), (53.2437, 80.09315, 67.2388))]
#[case::green((0.3576, 0.7152, 0.1192), (87.7376, - 86.1846, 83.1813))]
#[case::blue((0.1804, 0.0722, 0.9502), (32.3026, 79.1436, - 107.8436))]
#[case::cyan((0.53802, 0.7873, 1.0698), (91.1120, - 48.0806, - 14.1521))]
#[case::magenta((0.5928, 0.2848, 0.9699), (60.3199, 98.2302, - 60.8496))]
#[case::yellow((0.7700, 0.9278, 0.1385), (97.1382, - 21.5551, 94.4825))]
fn test_xyz_to_lab(#[case] xyz: (f32, f32, f32), #[case] lab: (f32, f32, f32)) {
// Act
let (l, a, b) = xyz_to_lab::<f32, D65>(xyz.0, xyz.1, xyz.2);
// Assert
assert!((l - lab.0).abs() < 1e-3);
assert!((a - lab.1).abs() < 1e-3);
assert!((b - lab.2).abs() < 1e-3);
}
}