| 1 | use crate::{ |
| 2 | bool_mask::LazySelect, |
| 3 | cam16::BakedParameters, |
| 4 | num::{Arithmetics, FromScalar, PartialCmp, Real, Sqrt, Zero}, |
| 5 | }; |
| 6 | /// One the apparent luminance metrics of CAM16. |
| 7 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 8 | #[non_exhaustive] |
| 9 | pub(crate) enum LuminanceType<T> { |
| 10 | /// The [lightness](https://en.wikipedia.org/wiki/Lightness) (J) of a color. |
| 11 | Lightness(T), |
| 12 | |
| 13 | /// The [brightness](https://en.wikipedia.org/wiki/Brightness) (Q) of a |
| 14 | /// color. |
| 15 | Brightness(T), |
| 16 | } |
| 17 | |
| 18 | impl<T> LuminanceType<T> { |
| 19 | pub(crate) fn into_cam16<Wp>(self, parameters: BakedParameters<Wp, T::Scalar>) -> (T, T) |
| 20 | where |
| 21 | T: Real + FromScalar + Zero + Arithmetics + Sqrt + PartialCmp + Clone, |
| 22 | T::Mask: LazySelect<T> + Clone, |
| 23 | { |
| 24 | let parameters = parameters.inner; |
| 25 | |
| 26 | match self { |
| 27 | LuminanceType::Lightness(lightness) => { |
| 28 | let is_black = lightness.eq(&T::zero()); |
| 29 | let brightness = lazy_select! { |
| 30 | if is_black => T::zero(), |
| 31 | else => crate::cam16::math::lightness_to_brightness( |
| 32 | lightness.clone(), |
| 33 | T::from_scalar(parameters.c), |
| 34 | T::from_scalar(parameters.a_w), |
| 35 | T::from_scalar(parameters.f_l_4), |
| 36 | ) |
| 37 | }; |
| 38 | |
| 39 | (lightness, brightness) |
| 40 | } |
| 41 | LuminanceType::Brightness(brightness) => { |
| 42 | let is_black = brightness.eq(&T::zero()); |
| 43 | let lightness = lazy_select! { |
| 44 | if is_black => T::zero(), |
| 45 | else => crate::cam16::math::brightness_to_lightness( |
| 46 | brightness.clone(), |
| 47 | T::from_scalar(parameters.c), |
| 48 | T::from_scalar(parameters.a_w), |
| 49 | T::from_scalar(parameters.f_l_4), |
| 50 | ) |
| 51 | }; |
| 52 | |
| 53 | (lightness, brightness) |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |