| 1 |
#[cfg(feature = "random")] |
| 2 |
#[cfg(test)] |
| 3 |
macro_rules! assert_uniform_distribution { |
| 4 |
($bins:expr) => {{ |
| 5 |
let bins = &$bins; |
| 6 |
|
| 7 |
for (i, &bin) in bins.iter().enumerate() { |
| 8 |
if bin < 5 { |
| 9 |
panic!("{}[{}] < 5: {:?}", stringify!($bins), i, bins); |
| 10 |
} |
| 11 |
} |
| 12 |
const P_LIMIT: f64 = 0.01; // Keeping it low to account for the RNG noise |
| 13 |
let p_value = crate::random_sampling::test_utils::uniform_distribution_test(bins); |
| 14 |
if p_value < P_LIMIT { |
| 15 |
panic!( |
| 16 |
"distribution of {} is not uniform enough (p-value {} < {}): {:?}", |
| 17 |
stringify!($bins), |
| 18 |
p_value, |
| 19 |
P_LIMIT, |
| 20 |
bins |
| 21 |
); |
| 22 |
} |
| 23 |
}}; |
| 24 |
} |
| 25 |
|
| 26 |
#[cfg(test)] |
| 27 |
macro_rules! test_uniform_distribution { |
| 28 |
( |
| 29 |
$ty:path $(as $base_ty:path)? { |
| 30 |
$($component:ident: ($component_min:expr, $component_max:expr)),+$(,)? |
| 31 |
}, |
| 32 |
min: $min:expr, |
| 33 |
max: $max:expr$(,)? |
| 34 |
) => { |
| •35 |
/* |
| 36 |
#[cfg(feature = "random")] |
| 37 |
#[test] |
| 38 |
fn uniform_distribution_rng_gen() { |
| 39 |
use rand::Rng; |
| 40 |
|
| 41 |
const BINS: usize = crate::random_sampling::test_utils::BINS; |
| 42 |
const SAMPLES: usize = crate::random_sampling::test_utils::SAMPLES; |
| 43 |
|
| 44 |
$(let mut $component = [0; BINS];)+ |
| 45 |
|
| 46 |
let mut rng = rand_mt::Mt::new(1234); // We want the same seed on every run to avoid random fails |
| 47 |
|
| 48 |
for _ in 0..SAMPLES { |
| 49 |
let color: $ty = rng.gen(); |
| 50 |
$(let color: $base_ty = crate::convert::IntoColorUnclamped::into_color_unclamped(color);)? |
| 51 |
|
| 52 |
if $(color.$component < $component_min || color.$component > $component_max)||+ { |
| 53 |
continue; |
| 54 |
} |
| 55 |
|
| 56 |
$({ |
| 57 |
let min: f32 = $component_min; |
| 58 |
let max: f32 = $component_max; |
| 59 |
let range = max - min; |
| 60 |
let normalized = (color.$component - min) / range; |
| 61 |
$component[((normalized * BINS as f32) as usize).min(BINS - 1)] += 1; |
| 62 |
})+ |
| 63 |
} |
| 64 |
|
| 65 |
$(assert_uniform_distribution!($component);)+ |
| 66 |
} |
| 67 |
|
| 68 |
#[cfg(feature = "random")] |
| 69 |
#[test] |
| 70 |
fn uniform_distribution_uniform_sample() { |
| 71 |
use rand::distributions::uniform::Uniform; |
| 72 |
use rand::Rng; |
| 73 |
|
| 74 |
const BINS: usize = crate::random_sampling::test_utils::BINS; |
| 75 |
const SAMPLES: usize = crate::random_sampling::test_utils::SAMPLES; |
| 76 |
|
| 77 |
$(let mut $component = [0; BINS];)+ |
| 78 |
|
| 79 |
let mut rng = rand_mt::Mt::new(1234); // We want the same seed on every run to avoid random fails |
| 80 |
let uniform_sampler = Uniform::new($min, $max); |
| 81 |
|
| 82 |
for _ in 0..SAMPLES { |
| 83 |
let color: $ty = rng.sample(&uniform_sampler); |
| 84 |
$(let color: $base_ty = crate::convert::IntoColorUnclamped::into_color_unclamped(color);)? |
| 85 |
|
| 86 |
if $(color.$component < $component_min || color.$component > $component_max)||+ { |
| 87 |
continue; |
| 88 |
} |
| 89 |
|
| 90 |
$({ |
| 91 |
let min: f32 = $component_min; |
| 92 |
let max: f32 = $component_max; |
| 93 |
let range = max - min; |
| 94 |
let normalized = (color.$component - min) / range; |
| 95 |
$component[((normalized * BINS as f32) as usize).min(BINS - 1)] += 1; |
| 96 |
})+ |
| 97 |
} |
| 98 |
|
| 99 |
$(assert_uniform_distribution!($component);)+ |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg(feature = "random")] |
| 103 |
#[test] |
| 104 |
fn uniform_distribution_uniform_sample_inclusive() { |
| 105 |
use rand::distributions::uniform::Uniform; |
| 106 |
use rand::Rng; |
| 107 |
|
| 108 |
const BINS: usize = crate::random_sampling::test_utils::BINS; |
| 109 |
const SAMPLES: usize = crate::random_sampling::test_utils::SAMPLES; |
| 110 |
|
| 111 |
$(let mut $component = [0; BINS];)+ |
| 112 |
|
| 113 |
let mut rng = rand_mt::Mt::new(1234); // We want the same seed on every run to avoid random fails |
| 114 |
let uniform_sampler = Uniform::new_inclusive($min, $max); |
| 115 |
|
| 116 |
for _ in 0..SAMPLES { |
| 117 |
let color: $ty = rng.sample(&uniform_sampler); |
| 118 |
$(let color: $base_ty = crate::convert::IntoColorUnclamped::into_color_unclamped(color);)? |
| 119 |
|
| 120 |
if $(color.$component < $component_min || color.$component > $component_max)||+ { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
$({ |
| 125 |
let min: f32 = $component_min; |
| 126 |
let max: f32 = $component_max; |
| 127 |
let range = max - min; |
| 128 |
let normalized = (color.$component - min) / range; |
| 129 |
$component[((normalized * BINS as f32) as usize).min(BINS - 1)] += 1; |
| 130 |
})+ |
| 131 |
} |
| 132 |
|
| 133 |
$(assert_uniform_distribution!($component);)+ |
| 134 |
} |
| •135 |
*/ |
| 136 |
}; |
| 137 |
} |
| 138 |
|
| 139 |
macro_rules! __apply_map_fn { |
| 140 |
($value: expr) => { |
| 141 |
$value |
| 142 |
}; |
| 143 |
($value: expr, $map_fn: expr) => { |
| 144 |
$map_fn($value) |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
macro_rules! impl_rand_traits_cartesian { |
| 149 |
( |
| 150 |
$uniform_ty: ident, |
| 151 |
$ty: ident |
| 152 |
{$($component: ident $(=> [$map_fn: expr])?),+} |
| 153 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 154 |
$(where $($where: tt)+)? |
| 155 |
) => { |
| 156 |
impl_rand_traits_cartesian!( |
| 157 |
$uniform_ty, |
| 158 |
$ty<> |
| 159 |
{$($component $( => [$map_fn])?),+} |
| 160 |
$(phantom: $phantom : PhantomData<$phantom_ty>)? |
| 161 |
$(where $($where)+)?); |
| 162 |
}; |
| 163 |
( |
| 164 |
$uniform_ty: ident, |
| 165 |
$ty: ident <$($ty_param: ident),*> |
| 166 |
{$($component: ident $(=> [$map_fn: expr])?),+} |
| 167 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 168 |
$(where $($where: tt)+)? |
| 169 |
) => { |
| 170 |
#[cfg(feature = "random")] |
| 171 |
impl<$($ty_param,)* T> rand::distributions::Distribution<$ty<$($ty_param,)* T>> for rand::distributions::Standard |
| 172 |
where |
| 173 |
rand::distributions::Standard: rand::distributions::Distribution<T>, |
| 174 |
$($($where)+)? |
| 175 |
{ |
| 176 |
#[allow(clippy::redundant_closure_call)] |
| 177 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 178 |
$ty { |
| 179 |
$($component: __apply_map_fn!(rng.gen::<T>() $(, $map_fn)?),)+ |
| 180 |
$($phantom: core::marker::PhantomData,)? |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
/// Samples colors uniformly. |
| 186 |
#[cfg(feature = "random")] |
| 187 |
pub struct $uniform_ty<$($ty_param,)* T> |
| 188 |
where |
| 189 |
T: rand::distributions::uniform::SampleUniform, |
| 190 |
{ |
| 191 |
$($component: rand::distributions::uniform::Uniform<T>,)+ |
| 192 |
$($phantom: core::marker::PhantomData<$phantom_ty>,)? |
| 193 |
} |
| 194 |
|
| 195 |
#[cfg(feature = "random")] |
| 196 |
impl<$($ty_param,)* T> rand::distributions::uniform::SampleUniform for $ty<$($ty_param,)* T> |
| 197 |
where |
| 198 |
T: rand::distributions::uniform::SampleUniform + Clone, |
| 199 |
{ |
| 200 |
type Sampler = $uniform_ty<$($ty_param,)* T>; |
| 201 |
} |
| 202 |
|
| 203 |
#[cfg(feature = "random")] |
| 204 |
impl<$($ty_param,)* T> rand::distributions::uniform::UniformSampler for $uniform_ty<$($ty_param,)* T> |
| 205 |
where |
| 206 |
T: rand::distributions::uniform::SampleUniform + Clone, |
| 207 |
{ |
| 208 |
type X = $ty<$($ty_param,)* T>; |
| 209 |
|
| 210 |
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 211 |
where |
| 212 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 213 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 214 |
{ |
| 215 |
let low = low_b.borrow(); |
| 216 |
let high = high_b.borrow(); |
| 217 |
|
| 218 |
Self { |
| 219 |
$($component: rand::distributions::uniform::Uniform::new::<_, T>(low.$component.clone(), high.$component.clone()),)+ |
| 220 |
$($phantom: core::marker::PhantomData,)? |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 225 |
where |
| 226 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 227 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 228 |
{ |
| 229 |
let low = low_b.borrow(); |
| 230 |
let high = high_b.borrow(); |
| 231 |
|
| 232 |
Self { |
| 233 |
$($component: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(low.$component.clone(), high.$component.clone()),)+ |
| 234 |
$($phantom: core::marker::PhantomData,)? |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 239 |
use rand::distributions::Distribution; |
| 240 |
|
| 241 |
$ty { |
| 242 |
$($component: self.$component.sample(rng),)+ |
| 243 |
$($phantom: core::marker::PhantomData,)? |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
}; |
| 248 |
} |
| 249 |
|
| 250 |
macro_rules! impl_rand_traits_cylinder { |
| 251 |
( |
| 252 |
$uniform_ty: ident, |
| 253 |
$ty: ident |
| 254 |
{ |
| 255 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 256 |
height: $height: ident $(=> [$height_map_fn: expr])?, |
| 257 |
radius: $radius: ident $(=> [$radius_map_fn: expr])? |
| 258 |
} |
| 259 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 260 |
$(where $($where: tt)+)? |
| 261 |
) => { |
| 262 |
impl_rand_traits_cylinder!( |
| 263 |
$uniform_ty, |
| 264 |
$ty<> |
| 265 |
{ |
| 266 |
hue: $hue_uniform_ty => $hue_ty, |
| 267 |
height: $height $(=> [$height_map_fn])?, |
| 268 |
radius: $radius $(=> [$radius_map_fn])? |
| 269 |
} |
| 270 |
$(phantom: $phantom : PhantomData<$phantom_ty>)? |
| 271 |
$(where $($where)+)?); |
| 272 |
}; |
| 273 |
( |
| 274 |
$uniform_ty: ident, |
| 275 |
$ty: ident <$($ty_param: ident),*> |
| 276 |
{ |
| 277 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 278 |
height: $height: ident $(=> [$height_map_fn: expr])?, |
| 279 |
radius: $radius: ident $(=> [$radius_map_fn: expr])? |
| 280 |
} |
| 281 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 282 |
$(where $($where: tt)+)? |
| 283 |
) => { |
| 284 |
#[cfg(feature = "random")] |
| 285 |
impl<$($ty_param,)* T> rand::distributions::Distribution<$ty<$($ty_param,)* T>> for rand::distributions::Standard |
| 286 |
where |
| 287 |
T: crate::num::Sqrt, |
| 288 |
rand::distributions::Standard: rand::distributions::Distribution<T> + rand::distributions::Distribution<$hue_ty<T>>, |
| 289 |
$($($where)+)? |
| 290 |
{ |
| 291 |
#[allow(clippy::redundant_closure_call)] |
| 292 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 293 |
$ty { |
| 294 |
hue: rng.gen::<$hue_ty<T>>(), |
| 295 |
$height: __apply_map_fn!(rng.gen::<T>() $(, $height_map_fn)?), |
| 296 |
$radius: __apply_map_fn!(rng.gen::<T>().sqrt() $(, $radius_map_fn)?), |
| 297 |
$($phantom: core::marker::PhantomData,)? |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/// Samples colors uniformly. |
| 303 |
#[cfg(feature = "random")] |
| 304 |
pub struct $uniform_ty<$($ty_param,)* T> |
| 305 |
where |
| 306 |
T: rand::distributions::uniform::SampleUniform, |
| 307 |
{ |
| 308 |
hue: crate::hues::$hue_uniform_ty<T>, |
| 309 |
$height: rand::distributions::uniform::Uniform<T>, |
| 310 |
$radius: rand::distributions::uniform::Uniform<T>, |
| 311 |
$($phantom: core::marker::PhantomData<$phantom_ty>,)? |
| 312 |
} |
| 313 |
|
| 314 |
#[cfg(feature = "random")] |
| 315 |
impl<$($ty_param,)* T> rand::distributions::uniform::SampleUniform for $ty<$($ty_param,)* T> |
| 316 |
where |
| 317 |
T: crate::num::Sqrt + core::ops::Mul<Output = T> + Clone + rand::distributions::uniform::SampleUniform, |
| 318 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 319 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 320 |
{ |
| 321 |
type Sampler = $uniform_ty<$($ty_param,)* T>; |
| 322 |
} |
| 323 |
|
| 324 |
#[cfg(feature = "random")] |
| 325 |
impl<$($ty_param,)* T> rand::distributions::uniform::UniformSampler for $uniform_ty<$($ty_param,)* T> |
| 326 |
where |
| 327 |
T: crate::num::Sqrt + core::ops::Mul<Output = T> + Clone + rand::distributions::uniform::SampleUniform, |
| 328 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 329 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 330 |
{ |
| 331 |
type X = $ty<$($ty_param,)* T>; |
| 332 |
|
| 333 |
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 334 |
where |
| 335 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 336 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 337 |
{ |
| 338 |
let low = low_b.borrow().clone(); |
| 339 |
let high = high_b.borrow().clone(); |
| 340 |
|
| 341 |
$uniform_ty { |
| 342 |
$height: rand::distributions::uniform::Uniform::new::<_, T>(low.$height, high.$height), |
| 343 |
$radius: rand::distributions::uniform::Uniform::new::<_, T>( |
| 344 |
low.$radius.clone() * low.$radius, |
| 345 |
high.$radius.clone() * high.$radius, |
| 346 |
), |
| 347 |
hue: crate::hues::$hue_uniform_ty::new(low.hue, high.hue), |
| 348 |
$($phantom: core::marker::PhantomData,)? |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 353 |
where |
| 354 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 355 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 356 |
{ |
| 357 |
let low = low_b.borrow().clone(); |
| 358 |
let high = high_b.borrow().clone(); |
| 359 |
|
| 360 |
$uniform_ty { |
| 361 |
$height: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(low.$height, high.$height), |
| 362 |
$radius: rand::distributions::uniform::Uniform::new_inclusive::<_, T>( |
| 363 |
low.$radius.clone() * low.$radius, |
| 364 |
high.$radius.clone() * high.$radius, |
| 365 |
), |
| 366 |
hue: crate::hues::$hue_uniform_ty::new_inclusive(low.hue, high.hue), |
| 367 |
$($phantom: core::marker::PhantomData,)? |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 372 |
use rand::distributions::Distribution; |
| 373 |
|
| 374 |
$ty { |
| 375 |
$height: self.$height.sample(rng), |
| 376 |
$radius: self.$radius.sample(rng).sqrt(), |
| 377 |
hue: self.hue.sample(rng), |
| 378 |
$($phantom: core::marker::PhantomData,)? |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
}; |
| 383 |
} |
| 384 |
|
| 385 |
macro_rules! impl_rand_traits_hsv_cone { |
| 386 |
( |
| 387 |
$uniform_ty: ident, |
| 388 |
$ty: ident |
| 389 |
{ |
| 390 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 391 |
height: $height: ident, |
| 392 |
radius: $radius: ident |
| 393 |
} |
| 394 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 395 |
$(where $($where: tt)+)? |
| 396 |
) => { |
| 397 |
impl_rand_traits_hsv_cone!( |
| 398 |
$uniform_ty, |
| 399 |
$ty<> |
| 400 |
{ |
| 401 |
hue: $hue_uniform_ty => $hue_ty, |
| 402 |
height: $height, |
| 403 |
radius: $radius |
| 404 |
} |
| 405 |
$(phantom: $phantom : PhantomData<$phantom_ty>)? |
| 406 |
$(where $($where)+)?); |
| 407 |
}; |
| 408 |
( |
| 409 |
$uniform_ty: ident, |
| 410 |
$ty: ident <$($ty_param: ident),*> |
| 411 |
{ |
| 412 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 413 |
height: $height: ident, |
| 414 |
radius: $radius: ident |
| 415 |
} |
| 416 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 417 |
$(where $($where: tt)+)? |
| 418 |
) => { |
| 419 |
#[cfg(feature = "random")] |
| 420 |
impl<$($ty_param,)* T> rand::distributions::Distribution<$ty<$($ty_param,)* T>> for rand::distributions::Standard |
| 421 |
where |
| 422 |
T: crate::num::Cbrt + crate::num::Sqrt, |
| 423 |
rand::distributions::Standard: rand::distributions::Distribution<T> + rand::distributions::Distribution<$hue_ty<T>>, |
| 424 |
{ |
| 425 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 426 |
let hue = rng.gen::<$hue_ty<T>>(); |
| 427 |
let crate::random_sampling::HsvSample { saturation: $radius, value: $height } = |
| 428 |
crate::random_sampling::sample_hsv(rng.gen(), rng.gen()); |
| 429 |
|
| 430 |
$ty { |
| 431 |
hue, |
| 432 |
$radius, |
| 433 |
$height, |
| 434 |
$($phantom: core::marker::PhantomData,)? |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
/// Samples colors uniformly. |
| 440 |
#[cfg(feature = "random")] |
| 441 |
pub struct $uniform_ty<$($ty_param,)* T> |
| 442 |
where |
| 443 |
T: rand::distributions::uniform::SampleUniform, |
| 444 |
{ |
| 445 |
hue: crate::hues::$hue_uniform_ty<T>, |
| 446 |
u1: rand::distributions::uniform::Uniform<T>, |
| 447 |
u2: rand::distributions::uniform::Uniform<T>, |
| 448 |
$($phantom: core::marker::PhantomData<$phantom_ty>,)? |
| 449 |
} |
| 450 |
|
| 451 |
#[cfg(feature = "random")] |
| 452 |
impl<$($ty_param,)* T> rand::distributions::uniform::SampleUniform for $ty<$($ty_param,)* T> |
| 453 |
where |
| 454 |
T: crate::num::Cbrt + crate::num::Sqrt + crate::num::Powi + Clone + rand::distributions::uniform::SampleUniform, |
| 455 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 456 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 457 |
{ |
| 458 |
type Sampler = $uniform_ty<$($ty_param,)* T>; |
| 459 |
} |
| 460 |
|
| 461 |
#[cfg(feature = "random")] |
| 462 |
impl<$($ty_param,)* T> rand::distributions::uniform::UniformSampler for $uniform_ty<$($ty_param,)* T> |
| 463 |
where |
| 464 |
T: crate::num::Cbrt + crate::num::Sqrt + crate::num::Powi + Clone + rand::distributions::uniform::SampleUniform, |
| 465 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 466 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 467 |
{ |
| 468 |
type X = $ty<$($ty_param,)* T>; |
| 469 |
|
| 470 |
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 471 |
where |
| 472 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 473 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 474 |
{ |
| 475 |
let low = low_b.borrow().clone(); |
| 476 |
let high = high_b.borrow().clone(); |
| 477 |
|
| 478 |
let (r1_min, r2_min) = |
| 479 |
crate::random_sampling::invert_hsv_sample(crate::random_sampling::HsvSample { |
| 480 |
value: low.$height, |
| 481 |
saturation: low.$radius, |
| 482 |
}); |
| 483 |
let (r1_max, r2_max) = |
| 484 |
crate::random_sampling::invert_hsv_sample(crate::random_sampling::HsvSample { |
| 485 |
value: high.$height, |
| 486 |
saturation: high.$radius, |
| 487 |
}); |
| 488 |
|
| 489 |
$uniform_ty { |
| 490 |
hue: crate::hues::$hue_uniform_ty::new(low.hue, high.hue), |
| 491 |
u1: rand::distributions::uniform::Uniform::new::<_, T>(r1_min, r1_max), |
| 492 |
u2: rand::distributions::uniform::Uniform::new::<_, T>(r2_min, r2_max), |
| 493 |
$($phantom: core::marker::PhantomData,)? |
| 494 |
} |
| 495 |
} |
| 496 |
|
| 497 |
fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 498 |
where |
| 499 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 500 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 501 |
{ |
| 502 |
let low = low_b.borrow().clone(); |
| 503 |
let high = high_b.borrow().clone(); |
| 504 |
|
| 505 |
let (r1_min, r2_min) = |
| 506 |
crate::random_sampling::invert_hsv_sample(crate::random_sampling::HsvSample { |
| 507 |
value: low.$height, |
| 508 |
saturation: low.$radius, |
| 509 |
}); |
| 510 |
let (r1_max, r2_max) = |
| 511 |
crate::random_sampling::invert_hsv_sample(crate::random_sampling::HsvSample { |
| 512 |
value: high.$height, |
| 513 |
saturation: high.$radius, |
| 514 |
}); |
| 515 |
|
| 516 |
$uniform_ty { |
| 517 |
hue: crate::hues::$hue_uniform_ty::new_inclusive(low.hue, high.hue), |
| 518 |
u1: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(r1_min, r1_max), |
| 519 |
u2: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(r2_min, r2_max), |
| 520 |
$($phantom: core::marker::PhantomData,)? |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 525 |
use rand::distributions::Distribution; |
| 526 |
|
| 527 |
let hue = self.hue.sample(rng); |
| 528 |
let crate::random_sampling::HsvSample { saturation: $radius, value: $height } = |
| 529 |
crate::random_sampling::sample_hsv(self.u1.sample(rng), self.u2.sample(rng)); |
| 530 |
|
| 531 |
$ty { |
| 532 |
hue, |
| 533 |
$radius, |
| 534 |
$height, |
| 535 |
$($phantom: core::marker::PhantomData,)? |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
macro_rules! impl_rand_traits_hsl_bicone { |
| 543 |
( |
| 544 |
$uniform_ty: ident, |
| 545 |
$ty: ident |
| 546 |
{ |
| 547 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 548 |
height: $height: ident $(=> [$height_map_fn: expr, $height_unmap_fn: expr])?, |
| 549 |
radius: $radius: ident $(=> [$radius_map_fn: expr, $radius_unmap_fn: expr])? |
| 550 |
} |
| 551 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 552 |
$(where $($where: tt)+)? |
| 553 |
) => { |
| 554 |
impl_rand_traits_hsl_bicone!( |
| 555 |
$uniform_ty, |
| 556 |
$ty<> |
| 557 |
{ |
| 558 |
hue: $hue_uniform_ty => $hue_ty, |
| 559 |
height: $height $(=> [$height_map_fn, $height_unmap_fn])?, |
| 560 |
radius: $radius $(=> [$radius_map_fn, $radius_unmap_fn])? |
| 561 |
} |
| 562 |
$(phantom: $phantom : PhantomData<$phantom_ty>)? |
| 563 |
$(where $($where)+)?); |
| 564 |
}; |
| 565 |
( |
| 566 |
$uniform_ty: ident, |
| 567 |
$ty: ident <$($ty_param: ident),*> |
| 568 |
{ |
| 569 |
hue: $hue_uniform_ty: ident => $hue_ty: ident, |
| 570 |
height: $height: ident $(=> [$height_map_fn: expr, $height_unmap_fn: expr])?, |
| 571 |
radius: $radius: ident $(=> [$radius_map_fn: expr, $radius_unmap_fn: expr])? |
| 572 |
} |
| 573 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 574 |
$(where $($where: tt)+)? |
| 575 |
) => { |
| 576 |
#[cfg(feature = "random")] |
| 577 |
impl<$($ty_param,)* T> rand::distributions::Distribution<$ty<$($ty_param,)* T>> for rand::distributions::Standard |
| 578 |
where |
| 579 |
T: crate::num::Real + crate::num::One + crate::num::Cbrt + crate::num::Sqrt + crate::num::Arithmetics + crate::num::PartialCmp + Clone, |
| 580 |
T::Mask: crate::bool_mask::LazySelect<T> + Clone, |
| 581 |
rand::distributions::Standard: rand::distributions::Distribution<T> + rand::distributions::Distribution<$hue_ty<T>>, |
| 582 |
{ |
| 583 |
#[allow(clippy::redundant_closure_call)] |
| 584 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 585 |
let hue = rng.gen::<$hue_ty<T>>(); |
| 586 |
let crate::random_sampling::HslSample { saturation, lightness } = |
| 587 |
crate::random_sampling::sample_hsl(rng.gen(), rng.gen()); |
| 588 |
|
| 589 |
$ty { |
| 590 |
hue, |
| 591 |
$radius: __apply_map_fn!(saturation $(, $radius_map_fn)?), |
| 592 |
$height: __apply_map_fn!(lightness $(, $height_map_fn)?), |
| 593 |
$($phantom: core::marker::PhantomData,)? |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
/// Samples colors uniformly. |
| 599 |
#[cfg(feature = "random")] |
| 600 |
pub struct $uniform_ty<$($ty_param,)* T> |
| 601 |
where |
| 602 |
T: rand::distributions::uniform::SampleUniform, |
| 603 |
{ |
| 604 |
hue: crate::hues::$hue_uniform_ty<T>, |
| 605 |
u1: rand::distributions::uniform::Uniform<T>, |
| 606 |
u2: rand::distributions::uniform::Uniform<T>, |
| 607 |
$($phantom: core::marker::PhantomData<$phantom_ty>,)? |
| 608 |
} |
| 609 |
|
| 610 |
#[cfg(feature = "random")] |
| 611 |
impl<$($ty_param,)* T> rand::distributions::uniform::SampleUniform for $ty<$($ty_param,)* T> |
| 612 |
where |
| 613 |
T: crate::num::Real + crate::num::One + crate::num::Cbrt + crate::num::Sqrt + crate::num::Powi + crate::num::Arithmetics + crate::num::PartialCmp + Clone + rand::distributions::uniform::SampleUniform, |
| 614 |
T::Mask: crate::bool_mask::LazySelect<T> + Clone, |
| 615 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 616 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 617 |
{ |
| 618 |
type Sampler = $uniform_ty<$($ty_param,)* T>; |
| 619 |
} |
| 620 |
|
| 621 |
#[cfg(feature = "random")] |
| 622 |
impl<$($ty_param,)* T> rand::distributions::uniform::UniformSampler for $uniform_ty<$($ty_param,)* T> |
| 623 |
where |
| 624 |
T: crate::num::Real + crate::num::One + crate::num::Cbrt + crate::num::Sqrt + crate::num::Powi + crate::num::Arithmetics + crate::num::PartialCmp + Clone + rand::distributions::uniform::SampleUniform, |
| 625 |
T::Mask: crate::bool_mask::LazySelect<T> + Clone, |
| 626 |
$hue_ty<T>: rand::distributions::uniform::SampleBorrow<$hue_ty<T>>, |
| 627 |
crate::hues::$hue_uniform_ty<T>: rand::distributions::uniform::UniformSampler<X = $hue_ty<T>>, |
| 628 |
{ |
| 629 |
type X = $ty<$($ty_param,)* T>; |
| 630 |
|
| 631 |
#[allow(clippy::redundant_closure_call)] |
| 632 |
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 633 |
where |
| 634 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 635 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 636 |
{ |
| 637 |
let low = low_b.borrow().clone(); |
| 638 |
let high = high_b.borrow().clone(); |
| 639 |
|
| 640 |
let (r1_min, r2_min) = |
| 641 |
crate::random_sampling::invert_hsl_sample(crate::random_sampling::HslSample { |
| 642 |
lightness: __apply_map_fn!(low.$height $(, $radius_unmap_fn)?), |
| 643 |
saturation: __apply_map_fn!(low.$radius $(, $height_unmap_fn)?), |
| 644 |
}); |
| 645 |
let (r1_max, r2_max) = |
| 646 |
crate::random_sampling::invert_hsl_sample(crate::random_sampling::HslSample { |
| 647 |
lightness: __apply_map_fn!(high.$height $(, $radius_unmap_fn)?), |
| 648 |
saturation: __apply_map_fn!(high.$radius $(, $height_unmap_fn)?), |
| 649 |
}); |
| 650 |
|
| 651 |
$uniform_ty { |
| 652 |
hue: crate::hues::$hue_uniform_ty::new(low.hue, high.hue), |
| 653 |
u1: rand::distributions::uniform::Uniform::new::<_, T>(r1_min, r1_max), |
| 654 |
u2: rand::distributions::uniform::Uniform::new::<_, T>(r2_min, r2_max), |
| 655 |
$($phantom: core::marker::PhantomData,)? |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
#[allow(clippy::redundant_closure_call)] |
| 660 |
fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 661 |
where |
| 662 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 663 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 664 |
{ |
| 665 |
let low = low_b.borrow().clone(); |
| 666 |
let high = high_b.borrow().clone(); |
| 667 |
|
| 668 |
let (r1_min, r2_min) = |
| 669 |
crate::random_sampling::invert_hsl_sample(crate::random_sampling::HslSample { |
| 670 |
lightness: __apply_map_fn!(low.$height $(, $radius_unmap_fn)?), |
| 671 |
saturation: __apply_map_fn!(low.$radius $(, $height_unmap_fn)?), |
| 672 |
}); |
| 673 |
let (r1_max, r2_max) = |
| 674 |
crate::random_sampling::invert_hsl_sample(crate::random_sampling::HslSample { |
| 675 |
lightness: __apply_map_fn!(high.$height $(, $radius_unmap_fn)?), |
| 676 |
saturation: __apply_map_fn!(high.$radius $(, $height_unmap_fn)?), |
| 677 |
}); |
| 678 |
|
| 679 |
$uniform_ty { |
| 680 |
hue: crate::hues::$hue_uniform_ty::new_inclusive(low.hue, high.hue), |
| 681 |
u1: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(r1_min, r1_max), |
| 682 |
u2: rand::distributions::uniform::Uniform::new_inclusive::<_, T>(r2_min, r2_max), |
| 683 |
$($phantom: core::marker::PhantomData,)? |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
#[allow(clippy::redundant_closure_call)] |
| 688 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 689 |
use rand::distributions::Distribution; |
| 690 |
|
| 691 |
let hue = self.hue.sample(rng); |
| 692 |
let crate::random_sampling::HslSample { saturation, lightness } = |
| 693 |
crate::random_sampling::sample_hsl(self.u1.sample(rng), self.u2.sample(rng)); |
| 694 |
|
| 695 |
$ty { |
| 696 |
hue, |
| 697 |
$radius: __apply_map_fn!(saturation $(, $radius_map_fn)?), |
| 698 |
$height: __apply_map_fn!(lightness $(, $height_map_fn)?), |
| 699 |
$($phantom: core::marker::PhantomData,)? |
| 700 |
} |
| 701 |
} |
| 702 |
} |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
macro_rules! impl_rand_traits_hwb_cone { |
| 707 |
( |
| 708 |
$uniform_ty: ident, |
| 709 |
$ty: ident, |
| 710 |
$hsv_uniform_ty: ident, |
| 711 |
$hsv_ty: ident |
| 712 |
{ |
| 713 |
height: $height: ident, |
| 714 |
radius: $radius: ident |
| 715 |
} |
| 716 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 717 |
$(where $($where: tt)+)? |
| 718 |
) => { |
| 719 |
impl_rand_traits_hwb_cone!( |
| 720 |
$uniform_ty, |
| 721 |
$ty<>, |
| 722 |
$hsv_uniform_ty, |
| 723 |
$hsv_ty |
| 724 |
{ |
| 725 |
height: $height, |
| 726 |
radius: $radius |
| 727 |
} |
| 728 |
$(phantom: $phantom : PhantomData<$phantom_ty>)? |
| 729 |
$(where $($where)+)?); |
| 730 |
}; |
| 731 |
( |
| 732 |
$uniform_ty: ident, |
| 733 |
$ty: ident <$($ty_param: ident),*>, |
| 734 |
$hsv_uniform_ty: ident, |
| 735 |
$hsv_ty: ident |
| 736 |
{ |
| 737 |
height: $height: ident, |
| 738 |
radius: $radius: ident |
| 739 |
} |
| 740 |
$(phantom: $phantom: ident : PhantomData<$phantom_ty: ident>)? |
| 741 |
$(where $($where: tt)+)? |
| 742 |
) => { |
| 743 |
#[cfg(feature = "random")] |
| 744 |
impl<$($ty_param,)* T> rand::distributions::Distribution<$ty<$($ty_param,)* T>> for rand::distributions::Standard |
| 745 |
where |
| 746 |
rand::distributions::Standard: rand::distributions::Distribution<$hsv_ty<$($ty_param,)* T>>, |
| 747 |
$ty<$($ty_param,)* T>: crate::convert::FromColorUnclamped<$hsv_ty<$($ty_param,)* T>>, |
| 748 |
{ |
| 749 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 750 |
use crate::convert::FromColorUnclamped; |
| 751 |
$ty::from_color_unclamped(rng.gen::<$hsv_ty<$($ty_param,)* T>>()) |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
/// Samples colors uniformly. |
| 756 |
#[cfg(feature = "random")] |
| 757 |
pub struct $uniform_ty<$($ty_param,)* T> |
| 758 |
where |
| 759 |
T: rand::distributions::uniform::SampleUniform, |
| 760 |
{ |
| 761 |
sampler: $hsv_uniform_ty<$($ty_param,)* T>, |
| 762 |
$($phantom: core::marker::PhantomData<$phantom_ty>,)? |
| 763 |
} |
| 764 |
|
| 765 |
#[cfg(feature = "random")] |
| 766 |
impl<$($ty_param,)* T> rand::distributions::uniform::SampleUniform for $ty<$($ty_param,)* T> |
| 767 |
where |
| 768 |
T: crate::num::MinMax + Clone + rand::distributions::uniform::SampleUniform, |
| 769 |
$hsv_ty<$($ty_param,)* T>: crate::convert::FromColorUnclamped<$ty<$($ty_param,)* T>> + rand::distributions::uniform::SampleBorrow<$hsv_ty<$($ty_param,)* T>>, |
| 770 |
$ty<$($ty_param,)* T>: crate::convert::FromColorUnclamped<$hsv_ty<$($ty_param,)* T>>, |
| 771 |
$hsv_uniform_ty<$($ty_param,)* T>: rand::distributions::uniform::UniformSampler<X = $hsv_ty<$($ty_param,)* T>>, |
| 772 |
{ |
| 773 |
type Sampler = $uniform_ty<$($ty_param,)* T>; |
| 774 |
} |
| 775 |
|
| 776 |
#[cfg(feature = "random")] |
| 777 |
impl<$($ty_param,)* T> rand::distributions::uniform::UniformSampler for $uniform_ty<$($ty_param,)* T> |
| 778 |
where |
| 779 |
T: crate::num::MinMax + Clone + rand::distributions::uniform::SampleUniform, |
| 780 |
$hsv_ty<$($ty_param,)* T>: crate::convert::FromColorUnclamped<$ty<$($ty_param,)* T>> + rand::distributions::uniform::SampleBorrow<$hsv_ty<$($ty_param,)* T>>, |
| 781 |
$ty<$($ty_param,)* T>: crate::convert::FromColorUnclamped<$hsv_ty<$($ty_param,)* T>>, |
| 782 |
$hsv_uniform_ty<$($ty_param,)* T>: rand::distributions::uniform::UniformSampler<X = $hsv_ty<$($ty_param,)* T>>, |
| 783 |
{ |
| 784 |
type X = $ty<$($ty_param,)* T>; |
| 785 |
|
| 786 |
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 787 |
where |
| 788 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 789 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 790 |
{ |
| 791 |
use crate::convert::FromColorUnclamped; |
| 792 |
let low_input = $hsv_ty::from_color_unclamped(low_b.borrow().clone()); |
| 793 |
let high_input = $hsv_ty::from_color_unclamped(high_b.borrow().clone()); |
| 794 |
|
| 795 |
let (low_saturation, high_saturation) = low_input.saturation.min_max(high_input.saturation); |
| 796 |
let (low_value, high_value) = low_input.value.min_max(high_input.value); |
| 797 |
|
| 798 |
let low = $hsv_ty{ |
| 799 |
hue: low_input.hue, |
| 800 |
$radius: low_saturation, |
| 801 |
$height: low_value, |
| 802 |
$($phantom: core::marker::PhantomData,)? |
| 803 |
}; |
| 804 |
let high = $hsv_ty{ |
| 805 |
hue: high_input.hue, |
| 806 |
$radius: high_saturation, |
| 807 |
$height: high_value, |
| 808 |
$($phantom: core::marker::PhantomData,)? |
| 809 |
}; |
| 810 |
|
| 811 |
let sampler = $hsv_uniform_ty::<$($ty_param,)* T>::new(low, high); |
| 812 |
|
| 813 |
$uniform_ty { |
| 814 |
sampler, |
| 815 |
$($phantom: core::marker::PhantomData,)? |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
fn new_inclusive<B1, B2>(low_b: B1, high_b: B2) -> Self |
| 820 |
where |
| 821 |
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 822 |
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized, |
| 823 |
{ |
| 824 |
use crate::convert::FromColorUnclamped; |
| 825 |
let low_input = $hsv_ty::from_color_unclamped(low_b.borrow().clone()); |
| 826 |
let high_input = $hsv_ty::from_color_unclamped(high_b.borrow().clone()); |
| 827 |
|
| 828 |
let (low_saturation, high_saturation) = low_input.saturation.min_max(high_input.saturation); |
| 829 |
let (low_value, high_value) = low_input.value.min_max(high_input.value); |
| 830 |
|
| 831 |
let low = $hsv_ty{ |
| 832 |
hue: low_input.hue, |
| 833 |
$radius: low_saturation, |
| 834 |
$height: low_value, |
| 835 |
$($phantom: core::marker::PhantomData,)? |
| 836 |
}; |
| 837 |
let high = $hsv_ty{ |
| 838 |
hue: high_input.hue, |
| 839 |
$radius: high_saturation, |
| 840 |
$height: high_value, |
| 841 |
$($phantom: core::marker::PhantomData,)? |
| 842 |
}; |
| 843 |
|
| 844 |
let sampler = $hsv_uniform_ty::<$($ty_param,)* T>::new_inclusive(low, high); |
| 845 |
|
| 846 |
$uniform_ty { |
| 847 |
sampler, |
| 848 |
$($phantom: core::marker::PhantomData,)? |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> $ty<$($ty_param,)* T> { |
| 853 |
use crate::convert::FromColorUnclamped; |
| 854 |
$ty::from_color_unclamped(self.sampler.sample(rng)) |
| 855 |
} |
| 856 |
} |
| 857 |
}; |
| 858 |
} |