| 1 | use crate::{ |
| 2 | bool_mask::LazySelect, |
| 3 | num::{Arithmetics, Cbrt, One, PartialCmp, Powi, Real, Sqrt}, |
| 4 | }; |
| 5 | |
| 6 | // Based on https://stackoverflow.com/q/4778147 and https://math.stackexchange.com/q/18686, |
| 7 | // picking A = (0, 0), B = (0, 1), C = (1, 1) gives us: |
| 8 | // |
| 9 | // ( sqrt(r1) * r2 , sqrt(r1) * (1 - r2) + sqrt(r1) * r2 ) = |
| 10 | // ( sqrt(r1) * r2 , sqrt(r1) - sqrt(r1) * r2 + sqrt(r1) * r2 ) = |
| 11 | // ( sqrt(r1) * r2 , sqrt(r1) ) |
| 12 | // |
| 13 | // `sqrt(r1)` gives us the scale of the triangle, `r2` the radius. |
| 14 | // Substituting, we get `x = scale * radius, y = scale` and thus for cone |
| 15 | // sampling: `scale = powf(r1, 1.0/3.0)` and `radius = sqrt(r2)`. |
| 16 | |
| 17 | #[derive(Debug, PartialEq)] |
| 18 | pub(crate) struct HsvSample<T> { |
| 19 | pub(crate) value: T, |
| 20 | pub(crate) saturation: T, |
| 21 | } |
| 22 | |
| 23 | #[inline] |
| 24 | pub(crate) fn sample_hsv<T>(r1: T, r2: T) -> HsvSample<T> |
| 25 | where |
| 26 | T: Cbrt + Sqrt, |
| 27 | { |
| 28 | HsvSample { |
| 29 | value: r1.cbrt(), |
| 30 | saturation: r2.sqrt(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[inline] |
| 35 | pub(crate) fn invert_hsv_sample<T>(sample: HsvSample<T>) -> (T, T) |
| 36 | where |
| 37 | T: Powi, |
| 38 | { |
| 39 | (sample.value.powi(3), sample.saturation.powi(2)) |
| 40 | } |
| 41 | |
| 42 | #[derive(Debug, PartialEq)] |
| 43 | pub(crate) struct HslSample<T> { |
| 44 | pub(crate) saturation: T, |
| 45 | pub(crate) lightness: T, |
| 46 | } |
| 47 | |
| 48 | #[inline] |
| 49 | pub(crate) fn sample_hsl<T>(r1: T, r2: T) -> HslSample<T> |
| 50 | where |
| 51 | T: Real + One + Cbrt + Sqrt + Arithmetics + PartialCmp + Clone, |
| 52 | T::Mask: LazySelect<T> + Clone, |
| 53 | { |
| 54 | HslSample { |
| 55 | saturation: r2.sqrt(), |
| 56 | lightness: sample_bicone_height(r1), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[inline] |
| 61 | fn sample_bicone_height<T>(r1: T) -> T |
| 62 | where |
| 63 | T: Real + One + Cbrt + Arithmetics + PartialCmp + Clone, |
| 64 | T::Mask: LazySelect<T> + Clone, |
| 65 | { |
| 66 | let mask = r1.lt_eq(&T::from_f64(0.5)); |
| 67 | |
| 68 | // Scale it up to [0, 1] or [0, 1) |
| 69 | let r1 = lazy_select! { |
| 70 | if mask.clone() => r1.clone(), |
| 71 | else => T::one() - &r1, |
| 72 | } * T::from_f64(2.0); |
| 73 | |
| 74 | let height = r1.cbrt(); |
| 75 | |
| 76 | // Turn the height back to [0, 0.5] or (0.5, 1.0] |
| 77 | let height = height * T::from_f64(0.5); |
| 78 | lazy_select! { |
| 79 | if mask => height.clone(), |
| 80 | else => T::one() - &height, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[inline] |
| 85 | pub(crate) fn invert_hsl_sample<T>(sample: HslSample<T>) -> (T, T) |
| 86 | where |
| 87 | T: Real + Powi + Arithmetics + PartialCmp + Clone, |
| 88 | T::Mask: LazySelect<T>, |
| 89 | { |
| 90 | let HslSample { |
| 91 | saturation, |
| 92 | lightness, |
| 93 | } = sample; |
| 94 | |
| 95 | let r1 = invert_bicone_height_sample(lightness); |
| 96 | |
| 97 | // saturation is first multiplied, then divided by h before squaring. |
| 98 | // h can be completely eliminated, leaving only the saturation. |
| 99 | let r2 = saturation.powi(2); |
| 100 | |
| 101 | (r1, r2) |
| 102 | } |
| 103 | |
| 104 | fn invert_bicone_height_sample<T>(height: T) -> T |
| 105 | where |
| 106 | T: Real + Powi + Arithmetics + PartialCmp + Clone, |
| 107 | T::Mask: LazySelect<T>, |
| 108 | { |
| 109 | lazy_select! { |
| 110 | if height.lt_eq(&T::from_f64(0.5)) => { |
| 111 | // ((x * 2)^3) / 2 = x^3 * 4. |
| 112 | // height is multiplied by 2 to scale it up to [0, 1], becoming h. |
| 113 | // h is cubed to make it r1. r1 is divided by 2 to take it back to [0, 0.5]. |
| 114 | height.clone().powi(3) * T::from_f64(4.0) |
| 115 | }, |
| 116 | else => { |
| 117 | let x = height.clone() - T::from_f64(1.0); |
| 118 | x.powi(3) * T::from_f64(4.0) + T::from_f64(1.0) |
| 119 | }, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod test { |
| 125 | use super::{sample_hsl, sample_hsv, HslSample, HsvSample}; |
| 126 | |
| 127 | #[cfg(feature = "random")] |
| 128 | #[test] |
| 129 | fn sample_max_min() { |
| 130 | let a = sample_hsv(0.0, 0.0); |
| 131 | let b = sample_hsv(1.0, 1.0); |
| 132 | assert_eq!( |
| 133 | HsvSample { |
| 134 | saturation: 0.0, |
| 135 | value: 0.0 |
| 136 | }, |
| 137 | a |
| 138 | ); |
| 139 | assert_eq!( |
| 140 | HsvSample { |
| 141 | saturation: 1.0, |
| 142 | value: 1.0 |
| 143 | }, |
| 144 | b |
| 145 | ); |
| 146 | let a = sample_hsl(0.0, 0.0); |
| 147 | let b = sample_hsl(1.0, 1.0); |
| 148 | assert_eq!( |
| 149 | HslSample { |
| 150 | saturation: 0.0, |
| 151 | lightness: 0.0 |
| 152 | }, |
| 153 | a |
| 154 | ); |
| 155 | assert_eq!( |
| 156 | HslSample { |
| 157 | saturation: 1.0, |
| 158 | lightness: 1.0 |
| 159 | }, |
| 160 | b |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | #[cfg(all(feature = "random", feature = "approx"))] |
| 165 | #[allow(clippy::excessive_precision)] |
| 166 | #[test] |
| 167 | fn hsl_sampling() { |
| 168 | use super::invert_hsl_sample; |
| 169 | |
| 170 | // Sanity check that sampling and inverting from sample are equivalent |
| 171 | macro_rules! test_hsl { |
| 172 | ( $x:expr, $y:expr ) => {{ |
| 173 | let hsl = sample_hsl($x, $y); |
| 174 | let a = invert_hsl_sample(hsl); |
| 175 | assert_relative_eq!(a.0, $x); |
| 176 | assert_relative_eq!(a.1, $y); |
| 177 | }}; |
| 178 | } |
| 179 | |
| 180 | test_hsl!(0.8464721407, 0.8271899200); |
| 181 | test_hsl!(0.8797234442, 0.4924621591); |
| 182 | test_hsl!(0.9179406120, 0.8771350605); |
| 183 | test_hsl!(0.5458023108, 0.1154283005); |
| 184 | test_hsl!(0.2691241774, 0.7881780600); |
| 185 | test_hsl!(0.2085030453, 0.9975406626); |
| 186 | test_hsl!(0.8483632811, 0.4955013942); |
| 187 | test_hsl!(0.0857919040, 0.0652214785); |
| 188 | test_hsl!(0.7152662838, 0.2788421565); |
| 189 | test_hsl!(0.2973598808, 0.5585230243); |
| 190 | test_hsl!(0.0936619602, 0.7289450731); |
| 191 | test_hsl!(0.4364395449, 0.9362269009); |
| 192 | test_hsl!(0.9802381158, 0.9742974964); |
| 193 | test_hsl!(0.1666129293, 0.4396910574); |
| 194 | test_hsl!(0.6190216210, 0.7175675180); |
| 195 | } |
| 196 | } |