| 1 | use craterust-analyzer cargo palette 0.7.6 crate/::{ |
| 2 | bool_maskrust-analyzer cargo palette 0.7.6 bool_mask/::LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#, |
| 3 | numrust-analyzer cargo palette 0.7.6 num/::{Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics#, Cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt#, Onerust-analyzer cargo palette 0.7.6 num/One#, PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp#, Powirust-analyzer cargo palette 0.7.6 num/Powi#, Realrust-analyzer cargo palette 0.7.6 num/Real#, Sqrtrust-analyzer cargo palette 0.7.6 num/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 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 18 | pub(craterust-analyzer cargo palette 0.7.6 crate/) struct HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#<T> { |
| 19 | pub(craterust-analyzer cargo palette 0.7.6 crate/) valuerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#value.: T, |
| 20 | pub(craterust-analyzer cargo palette 0.7.6 crate/) saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#saturation.: T, |
| 21 | } |
| 22 | |
| 23 | #[inline] |
| 24 | pub(craterust-analyzer cargo palette 0.7.6 crate/) fn sample_hsvrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsv().<T>(r1: T, r2: T) -> HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#<T> |
| 25 | where |
| 26 | T: Cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt# + Sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt#, |
| 27 | { |
| 28 | HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample# { |
| 29 | valuerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#value.: r1.cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt#cbrt().(), |
| 30 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#saturation.: r2.sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt#sqrt().(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[inline] |
| 35 | pub(craterust-analyzer cargo palette 0.7.6 crate/) fn invert_hsv_samplerust-analyzer cargo palette 0.7.6 random_sampling/cone/invert_hsv_sample().<T>(sample: HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#<T>) -> (T, T) |
| 36 | where |
| 37 | T: Powirust-analyzer cargo palette 0.7.6 num/Powi#, |
| 38 | { |
| 39 | (sample.valuerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#value..powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(3), sample.saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#saturation..powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(2)) |
| 40 | } |
| 41 | |
| 42 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)] |
| 43 | pub(craterust-analyzer cargo palette 0.7.6 crate/) struct HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#<T> { |
| 44 | pub(craterust-analyzer cargo palette 0.7.6 crate/) saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#saturation.: T, |
| 45 | pub(craterust-analyzer cargo palette 0.7.6 crate/) lightnessrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#lightness.: T, |
| 46 | } |
| 47 | |
| 48 | #[inline] |
| 49 | pub(craterust-analyzer cargo palette 0.7.6 crate/) fn sample_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsl().<T>(r1: T, r2: T) -> HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#<T> |
| 50 | where |
| 51 | T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Onerust-analyzer cargo palette 0.7.6 num/One# + Cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt# + Sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 52 | T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T> + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 53 | { |
| 54 | HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample# { |
| 55 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#saturation.: r2.sqrtrust-analyzer cargo palette 0.7.6 num/Sqrt#sqrt().(), |
| 56 | lightnessrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#lightness.: sample_bicone_heightrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_bicone_height().(r1), |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[inline] |
| 61 | fn sample_bicone_heightrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_bicone_height().<T>(r1: T) -> T |
| 62 | where |
| 63 | T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Onerust-analyzer cargo palette 0.7.6 num/One# + Cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 64 | T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T> + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 65 | { |
| 66 | let mask = r1.lt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#lt_eq().(&T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.5)); |
| 67 | |
| 68 | // Scale it up to [0, 1] or [0, 1) |
| 69 | let r1 = lazy_selectrust-analyzer cargo palette 0.7.6 macros/lazy_select/lazy_select!! { |
| 70 | if mask.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() => r1.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(), |
| 71 | else => T::onerust-analyzer cargo palette 0.7.6 num/One#one().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). &r1, |
| 72 | } rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(2.0); |
| 73 | |
| 74 | let height = r1.cbrtrust-analyzer cargo palette 0.7.6 num/Cbrt#cbrt().(); |
| 75 | |
| 76 | // Turn the height back to [0, 0.5] or (0.5, 1.0] |
| 77 | let height = height rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul().T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(0.5); |
| 78 | lazy_selectrust-analyzer cargo palette 0.7.6 macros/lazy_select/lazy_select!! { |
| 79 | if mask => height.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().(), |
| 80 | else => T::onerust-analyzer cargo palette 0.7.6 num/One#one().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). &height, |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | #[inline] |
| 85 | pub(craterust-analyzer cargo palette 0.7.6 crate/) fn invert_hsl_samplerust-analyzer cargo palette 0.7.6 random_sampling/cone/invert_hsl_sample().<T>(sample: HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#<T>) -> (T, T) |
| 86 | where |
| 87 | T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Powirust-analyzer cargo palette 0.7.6 num/Powi# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 88 | T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T>, |
| 89 | { |
| 90 | let HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample# { |
| 91 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#saturation., |
| 92 | lightnessrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#lightness., |
| 93 | } = sample; |
| 94 | |
| 95 | let r1 = invert_bicone_height_samplerust-analyzer cargo palette 0.7.6 random_sampling/cone/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.powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(2); |
| 100 | |
| 101 | (r1, r2) |
| 102 | } |
| 103 | |
| 104 | fn invert_bicone_height_samplerust-analyzer cargo palette 0.7.6 random_sampling/cone/invert_bicone_height_sample().<T>(height: T) -> T |
| 105 | where |
| 106 | T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Powirust-analyzer cargo palette 0.7.6 num/Powi# + Arithmeticsrust-analyzer cargo palette 0.7.6 num/Arithmetics# + PartialCmprust-analyzer cargo palette 0.7.6 num/PartialCmp# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#, |
| 107 | T::Maskrust-analyzer cargo palette 0.7.6 bool_mask/HasBoolMask#Mask#: LazySelectrust-analyzer cargo palette 0.7.6 bool_mask/LazySelect#<T>, |
| 108 | { |
| 109 | lazy_selectrust-analyzer cargo palette 0.7.6 macros/lazy_select/lazy_select!! { |
| 110 | if height.lt_eqrust-analyzer cargo palette 0.7.6 num/PartialCmp#lt_eq().(&T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#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.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().().powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(3) *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(4.0) |
| 115 | }, |
| 116 | else => { |
| 117 | let x = height.clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Sub#sub(). T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(1.0); |
| 118 | x.powirust-analyzer cargo palette 0.7.6 num/Powi#powi().(3) *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Mul#mul(). T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(4.0) +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/Add#add(). T::from_f64rust-analyzer cargo palette 0.7.6 num/Real#from_f64().(1.0) |
| 119 | }, |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | #[cfg(test)] |
| 124 | mod testrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/ { |
| 125 | use superrust-analyzer cargo palette 0.7.6 random_sampling/cone/::{sample_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsl()., sample_hsvrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsv()., HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#, HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#}; |
| 126 | |
| 127 | #[cfg(feature = "random")] |
| 128 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 129 | fn sample_max_minrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/sample_max_min().() { |
| 130 | let a = sample_hsvrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsv().(0.0, 0.0); |
| 131 | let b = sample_hsvrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsv().(1.0, 1.0); |
| 132 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 133 | HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample# { |
| 134 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#saturation.: 0.0, |
| 135 | valuerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#value.: 0.0 |
| 136 | }, |
| 137 | a |
| 138 | ); |
| 139 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 140 | HsvSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample# { |
| 141 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#saturation.: 1.0, |
| 142 | valuerust-analyzer cargo palette 0.7.6 random_sampling/cone/HsvSample#value.: 1.0 |
| 143 | }, |
| 144 | b |
| 145 | ); |
| 146 | let a = sample_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsl().(0.0, 0.0); |
| 147 | let b = sample_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/sample_hsl().(1.0, 1.0); |
| 148 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 149 | HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample# { |
| 150 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#saturation.: 0.0, |
| 151 | lightnessrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#lightness.: 0.0 |
| 152 | }, |
| 153 | a |
| 154 | ); |
| 155 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 156 | HslSamplerust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample# { |
| 157 | saturationrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#saturation.: 1.0, |
| 158 | lightnessrust-analyzer cargo palette 0.7.6 random_sampling/cone/HslSample#lightness.: 1.0 |
| 159 | }, |
| 160 | b |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | #[cfg(all(feature = "random", feature = "approx"))] |
| 165 | #[allow(clippy::excessive_precision)] |
| 166 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 167 | fn hsl_samplingrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/hsl_sampling().() { |
| 168 | use superrust-analyzer cargo palette 0.7.6 random_sampling/cone/::invert_hsl_samplerust-analyzer cargo palette 0.7.6 random_sampling/cone/invert_hsl_sample().; |
| 169 | |
| 170 | // Sanity check that sampling and inverting from sample are equivalent |
| 171 | macro_rules! test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/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_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.8464721407, 0.8271899200); |
| 181 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.8797234442, 0.4924621591); |
| 182 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.9179406120, 0.8771350605); |
| 183 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.5458023108, 0.1154283005); |
| 184 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.2691241774, 0.7881780600); |
| 185 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.2085030453, 0.9975406626); |
| 186 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.8483632811, 0.4955013942); |
| 187 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.0857919040, 0.0652214785); |
| 188 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.7152662838, 0.2788421565); |
| 189 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.2973598808, 0.5585230243); |
| 190 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.0936619602, 0.7289450731); |
| 191 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.4364395449, 0.9362269009); |
| 192 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.9802381158, 0.9742974964); |
| 193 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.1666129293, 0.4396910574); |
| 194 | test_hslrust-analyzer cargo palette 0.7.6 random_sampling/cone/test/test_hsl!!(0.6190216210, 0.7175675180); |
| 195 | } |
| 196 | } |