| 1 | //! Traits for working with stimulus colors and values, such as RGB and XYZ. |
| 2 | |
| 3 | use craterust-analyzer cargo palette 0.7.6 crate/::{ |
| 4 | clamprust-analyzer cargo palette 0.7.6 clamp()., |
| 5 | numrust-analyzer cargo palette 0.7.6 num/::{Onerust-analyzer cargo palette 0.7.6 num/One#, Realrust-analyzer cargo palette 0.7.6 num/Real#, Roundrust-analyzer cargo palette 0.7.6 num/Round#, Zerorust-analyzer cargo palette 0.7.6 num/Zero#}, |
| 6 | }; |
| 7 | |
| 8 | /// Color components that represent a stimulus intensity. |
| 9 | /// |
| 10 | /// The term "stimulus" comes from "tristimulus", literally a set of three |
| 11 | /// stimuli, which is a term for color spaces that measure the intensity of |
| 12 | /// three primary color values. Classic examples of tristimulus color space are |
| 13 | /// XYZ and RGB. |
| 14 | /// |
| 15 | /// Stimulus values are expected to have these properties: |
| 16 | /// * Has a typical range from `0` to some finite maximum, the "max intensity". |
| 17 | /// This represents a range from `0%` to `100%`. For example `0u8` to |
| 18 | /// `255u8`, `0.0f32` to `1.0f32`. |
| 19 | /// * Values below `0` are considered invalid for display purposes, but may |
| 20 | /// still be used in calculations. |
| 21 | /// * Values above the "max intensity" are sometimes supported, depending on |
| 22 | /// the application. For example in 3D rendering, where high values represent |
| 23 | /// intense light. |
| 24 | /// * Unsigned integer values (`u8`, `u16`, `u32`, etc.) have a range from `0` |
| 25 | /// to their largest representable value. For example `0u8` to `255u8` or |
| 26 | /// `0u16` to `65535u16`. |
| 27 | /// * Real values (`f32`, `f64`, fixed point types, etc.) have a range from |
| 28 | /// `0.0` to `1.0`. |
| 29 | pub trait Stimulusrust-analyzer cargo palette 0.7.6 stimulus/Stimulus#: Zerorust-analyzer cargo palette 0.7.6 num/Zero# { |
| 30 | /// The highest displayable value this component type can reach. Integers |
| 31 | /// types are expected to return their maximum value, while real numbers |
| 32 | /// (like floats) return 1.0. Higher values are allowed, but they may be |
| 33 | /// lowered to this before converting to another format. |
| 34 | #[must_use] |
| 35 | fn max_intensityrust-analyzer cargo palette 0.7.6 stimulus/Stimulus#max_intensity().() -> Self; |
| 36 | } |
| 37 | |
| 38 | impl<T> Stimulusrust-analyzer cargo palette 0.7.6 stimulus/Stimulus# for T |
| 39 | where |
| 40 | T: Realrust-analyzer cargo palette 0.7.6 num/Real# + Onerust-analyzer cargo palette 0.7.6 num/One# + Zerorust-analyzer cargo palette 0.7.6 num/Zero#, |
| 41 | { |
| 42 | #[inline] |
| 43 | fn max_intensityrust-analyzer cargo palette 0.7.6 stimulus/impl#[T][Stimulus]max_intensity().() -> Selfrust-analyzer cargo palette 0.7.6 stimulus/impl#[T][Stimulus] { |
| 44 | Selfrust-analyzer cargo palette 0.7.6 stimulus/impl#[T][Stimulus]::onerust-analyzer cargo palette 0.7.6 num/One#one().() |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | macro_rules! impl_uint_componentsrust-analyzer cargo palette 0.7.6 stimulus/impl_uint_components! { |
| 49 | ($($ty: ident),+) => { |
| 50 | $( |
| 51 | impl Stimulus for $ty { |
| 52 | #[inline] |
| 53 | fn max_intensity() -> Self { |
| 54 | $ty::MAX |
| 55 | } |
| 56 | } |
| 57 | )* |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | impl_uint_componentsrust-analyzer cargo palette 0.7.6 stimulus/impl_uint_components!!(u8, u16, u32, u64, u128); |
| 62 | |
| 63 | /// A marker trait for colors where all components are stimuli. |
| 64 | /// |
| 65 | /// Typical stimulus colors are RGB and XYZ. |
| 66 | pub trait StimulusColorrust-analyzer cargo palette 0.7.6 stimulus/StimulusColor# {} |
| 67 | |
| 68 | /// Converts from a stimulus color component type, while performing the |
| 69 | /// appropriate scaling, rounding and clamping. |
| 70 | /// |
| 71 | /// ``` |
| 72 | /// use palette::stimulus::FromStimulus; |
| 73 | /// |
| 74 | /// // Scales the value up to u8::MAX while converting. |
| 75 | /// let u8_component = u8::from_stimulus(1.0f32); |
| 76 | /// assert_eq!(u8_component, 255); |
| 77 | /// ``` |
| 78 | pub trait FromStimulusrust-analyzer cargo palette 0.7.6 stimulus/FromStimulus#<T> { |
| 79 | /// Converts `other` into `Self`, while performing the appropriate scaling, |
| 80 | /// rounding and clamping. |
| 81 | #[must_use] |
| 82 | fn from_stimulusrust-analyzer cargo palette 0.7.6 stimulus/FromStimulus#from_stimulus().(other: T) -> Self; |
| 83 | } |
| 84 | |
| 85 | impl<T, U: IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<T>> FromStimulusrust-analyzer cargo palette 0.7.6 stimulus/FromStimulus#<U> for T { |
| 86 | #[inline] |
| 87 | fn from_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[T][`FromStimulus<U>`]from_stimulus().(other: U) -> T { |
| 88 | other.into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#into_stimulus().() |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Converts into a stimulus color component type, while performing the |
| 93 | /// appropriate scaling, rounding and clamping. |
| 94 | /// |
| 95 | /// ``` |
| 96 | /// use palette::stimulus::IntoStimulus; |
| 97 | /// |
| 98 | /// // Scales the value up to u8::MAX while converting. |
| 99 | /// let u8_component: u8 = 1.0f32.into_stimulus(); |
| 100 | /// assert_eq!(u8_component, 255); |
| 101 | /// ``` |
| 102 | pub trait IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<T> { |
| 103 | /// Converts `self` into `T`, while performing the appropriate scaling, |
| 104 | /// rounding and clamping. |
| 105 | #[must_use] |
| 106 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#into_stimulus().(self) -> T; |
| 107 | } |
| 108 | |
| 109 | impl<T> IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<T> for T { |
| 110 | #[inline] |
| 111 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[T][`IntoStimulus<Self>`]into_stimulus().(self) -> T { |
| 112 | self |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // C23 = 2^23, in f32 |
| 117 | // C52 = 2^52, in f64 |
| 118 | const C23rust-analyzer cargo palette 0.7.6 stimulus/C23.: u32 = 0x4b00_0000; |
| 119 | const C52rust-analyzer cargo palette 0.7.6 stimulus/C52.: u64 = 0x4330_0000_0000_0000; |
| 120 | |
| 121 | // Float to uint conversion with rounding to nearest even number. Formula |
| 122 | // follows the form (x_f32 + C23_f32) - C23_u32, where x is the component. From |
| 123 | // Hacker's Delight, p. 378-380. |
| 124 | // Works on the range of [-0.25, 2^23] for f32, [-0.25, 2^52] for f64. |
| 125 | // |
| 126 | // Special cases: |
| 127 | // NaN -> uint::MAX |
| 128 | // inf -> uint::MAX |
| 129 | // -inf -> 0 |
| 130 | // Greater than 2^23 for f64, 2^52 for f64 -> uint::MAX |
| 131 | macro_rules! convert_float_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_float_to_uint! { |
| 132 | ($float: ident; direct ($($direct_target: ident),+); $(via $temporary: ident ($($target: ident),+);)*) => { |
| 133 | $( |
| 134 | impl IntoStimulus<$direct_target> for $float { |
| 135 | #[inline] |
| 136 | fn into_stimulus(self) -> $direct_target { |
| 137 | let max = $direct_target::max_intensity() as $float; |
| 138 | let scaled = (self * max).min(max); |
| 139 | let f = scaled + f32::from_bits(C23); |
| 140 | (f.to_bits().saturating_sub(C23)) as $direct_target |
| 141 | } |
| 142 | } |
| 143 | )+ |
| 144 | |
| 145 | $( |
| 146 | $( |
| 147 | impl IntoStimulus<$target> for $float { |
| 148 | #[inline] |
| 149 | fn into_stimulus(self) -> $target { |
| 150 | let max = $target::max_intensity() as $temporary; |
| 151 | let scaled = (self as $temporary * max).min(max); |
| 152 | let f = scaled + f64::from_bits(C52); |
| 153 | (f.to_bits().saturating_sub(C52)) as $target |
| 154 | } |
| 155 | } |
| 156 | )+ |
| 157 | )* |
| 158 | }; |
| 159 | } |
| 160 | |
| 161 | // Double to uint conversion with rounding to nearest even number. Formula |
| 162 | // follows the form (x_f64 + C52_f64) - C52_u64, where x is the component. |
| 163 | macro_rules! convert_double_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_double_to_uint! { |
| 164 | ($double: ident; direct ($($direct_target: ident),+);) => { |
| 165 | $( |
| 166 | impl IntoStimulus<$direct_target> for $double { |
| 167 | #[inline] |
| 168 | fn into_stimulus(self) -> $direct_target { |
| 169 | let max = $direct_target::max_intensity() as $double; |
| 170 | let scaled = (self * max).min(max); |
| 171 | let f = scaled + f64::from_bits(C52); |
| 172 | (f.to_bits().saturating_sub(C52)) as $direct_target |
| 173 | } |
| 174 | } |
| 175 | )+ |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | // Uint to float conversion with the formula (x_u32 + C23_u32) - C23_f32, where |
| 180 | // x is the component. We convert the component to f32 then multiply it by the |
| 181 | // reciprocal of the float representation max value for u8. |
| 182 | // Works on the range of [0, 2^23] for f32, [0, 2^52 - 1] for f64. |
| 183 | impl IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<f32> for u8 { |
| 184 | #[inline] |
| 185 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[u8][`IntoStimulus<f32>`]into_stimulus().(self) -> f32 { |
| 186 | let comp_u = u32::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u32][`From<u8>`]from().(self) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().C23rust-analyzer cargo palette 0.7.6 stimulus/C23.; |
| 187 | let comp_f = f32::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]from_bits().(comp_u) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().f32::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]from_bits().(C23rust-analyzer cargo palette 0.7.6 stimulus/C23.); |
| 188 | let max_u = u32::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u32][`From<u8>`]from().(u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u32][`Add<Self>`]add().C23rust-analyzer cargo palette 0.7.6 stimulus/C23.; |
| 189 | let max_f = (f32::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]from_bits().(max_u) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Sub<Self>`]sub().f32::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]from_bits().(C23rust-analyzer cargo palette 0.7.6 stimulus/C23.)).reciprust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]recip().(); |
| 190 | comp_f rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Mul<Self>`]mul().max_f |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // Uint to f64 conversion with the formula (x_u64 + C23_u64) - C23_f64. |
| 195 | impl IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<f64> for u8 { |
| 196 | #[inline] |
| 197 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[u8][`IntoStimulus<f64>`]into_stimulus().(self) -> f64 { |
| 198 | let comp_u = u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(self) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().C52rust-analyzer cargo palette 0.7.6 stimulus/C52.; |
| 199 | let comp_f = f64::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]from_bits().(comp_u) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().f64::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]from_bits().(C52rust-analyzer cargo palette 0.7.6 stimulus/C52.); |
| 200 | let max_u = u64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[u64][`From<u8>`]from().(u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX.) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u64][`Add<Self>`]add().C52rust-analyzer cargo palette 0.7.6 stimulus/C52.; |
| 201 | let max_f = (f64::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]from_bits().(max_u) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Sub<Self>`]sub().f64::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]from_bits().(C52rust-analyzer cargo palette 0.7.6 stimulus/C52.)).reciprust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]recip().(); |
| 202 | comp_f rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul().max_f |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | macro_rules! convert_uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_float! { |
| 207 | ($uint: ident; $(via $temporary: ident ($($target: ident),+);)*) => { |
| 208 | $( |
| 209 | $( |
| 210 | impl IntoStimulus<$target> for $uint { |
| 211 | #[inline] |
| 212 | fn into_stimulus(self) -> $target { |
| 213 | let max = $uint::max_intensity() as $temporary; |
| 214 | let scaled = self as $temporary / max; |
| 215 | scaled as $target |
| 216 | } |
| 217 | } |
| 218 | )+ |
| 219 | )* |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | macro_rules! convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint! { |
| 224 | ($uint: ident; $(via $temporary: ident ($($target: ident),+);)*) => { |
| 225 | $( |
| 226 | $( |
| 227 | impl IntoStimulus<$target> for $uint { |
| 228 | #[inline] |
| 229 | fn into_stimulus(self) -> $target { |
| 230 | let target_max = $target::max_intensity() as $temporary; |
| 231 | let own_max = $uint::max_intensity() as $temporary; |
| 232 | let scaled = (self as $temporary / own_max) * target_max; |
| 233 | clamp(Round::round(scaled), 0.0, target_max) as $target |
| 234 | } |
| 235 | } |
| 236 | )+ |
| 237 | )* |
| 238 | }; |
| 239 | } |
| 240 | |
| 241 | impl IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<f64> for f32 { |
| 242 | #[inline] |
| 243 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[f32][`IntoStimulus<f64>`]into_stimulus().(self) -> f64 { |
| 244 | f64::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[f64][`From<f32>`]from().(self) |
| 245 | } |
| 246 | } |
| 247 | convert_float_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_float_to_uint!!(f32; direct (u8, u16); via f64 (u32, u64, u128);); |
| 248 | |
| 249 | impl IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#<f32> for f64 { |
| 250 | #[inline] |
| 251 | fn into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[f64][`IntoStimulus<f32>`]into_stimulus().(self) -> f32 { |
| 252 | self as f32 |
| 253 | } |
| 254 | } |
| 255 | convert_double_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_double_to_uint!!(f64; direct (u8, u16, u32, u64, u128);); |
| 256 | |
| 257 | convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint!!(u8; via f32 (u16); via f64 (u32, u64, u128);); |
| 258 | |
| 259 | convert_uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_float!!(u16; via f32 (f32); via f64 (f64);); |
| 260 | convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint!!(u16; via f32 (u8); via f64 (u32, u64, u128);); |
| 261 | |
| 262 | convert_uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_float!!(u32; via f64 (f32, f64);); |
| 263 | convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint!!(u32; via f64 (u8, u16, u64, u128);); |
| 264 | |
| 265 | convert_uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_float!!(u64; via f64 (f32, f64);); |
| 266 | convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint!!(u64; via f64 (u8, u16, u32, u128);); |
| 267 | |
| 268 | convert_uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_float!!(u128; via f64 (f32, f64);); |
| 269 | convert_uint_to_uintrust-analyzer cargo palette 0.7.6 stimulus/convert_uint_to_uint!!(u128; via f64 (u8, u16, u32, u64);); |
| 270 | |
| 271 | #[cfg(test)] |
| 272 | mod testrust-analyzer cargo palette 0.7.6 stimulus/test/ { |
| 273 | use craterust-analyzer cargo palette 0.7.6 crate/::stimulusrust-analyzer cargo palette 0.7.6 stimulus/::IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#; |
| 274 | |
| 275 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 276 | fn float_to_uintrust-analyzer cargo palette 0.7.6 stimulus/test/float_to_uint().() { |
| 277 | let data = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 278 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().800.0, |
| 279 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.3, |
| 280 | 0.0, |
| 281 | 0.005, |
| 282 | 0.024983, |
| 283 | 0.01, |
| 284 | 0.15, |
| 285 | 0.3, |
| 286 | 0.5, |
| 287 | 0.6, |
| 288 | 0.7, |
| 289 | 0.8, |
| 290 | 0.8444, |
| 291 | 0.9, |
| 292 | 0.955, |
| 293 | 0.999, |
| 294 | 1.0, |
| 295 | 1.4, |
| 296 | f32::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]from_bits().(0x4b44_0000), |
| 297 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/MAX., |
| 298 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/MIN., |
| 299 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/NAN., |
| 300 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/INFINITY., |
| 301 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f32rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/::NEG_INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/NEG_INFINITY., |
| 302 | ]; |
| 303 | |
| 304 | let expected = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 305 | 0u8, 0, 0, 1, 6, 3, 38, 76, 128, 153, 178, 204, 215, 230, 244, 255, 255, 255, 255, 255, |
| 306 | 0, 255, 255, 0, |
| 307 | ]; |
| 308 | |
| 309 | for (d, e) in data.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(expected) { |
| 310 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#::<u8>::into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[f32][`IntoStimulus<u8>`]into_stimulus().(d), e); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 315 | fn double_to_uintrust-analyzer cargo palette 0.7.6 stimulus/test/double_to_uint().() { |
| 316 | let data = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 317 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().800.0, |
| 318 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][Neg]neg().0.3, |
| 319 | 0.0, |
| 320 | 0.005, |
| 321 | 0.024983, |
| 322 | 0.01, |
| 323 | 0.15, |
| 324 | 0.3, |
| 325 | 0.5, |
| 326 | 0.6, |
| 327 | 0.7, |
| 328 | 0.8, |
| 329 | 0.8444, |
| 330 | 0.9, |
| 331 | 0.955, |
| 332 | 0.999, |
| 333 | 1.0, |
| 334 | 1.4, |
| 335 | f64::from_bitsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/impl#[f64]from_bits().(0x4334_0000_0000_0000), |
| 336 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/MAX., |
| 337 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/::MINrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/MIN., |
| 338 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/::NANrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NAN., |
| 339 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/::INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/INFINITY., |
| 340 | corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::f64rust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/::NEG_INFINITYrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f64/NEG_INFINITY., |
| 341 | ]; |
| 342 | |
| 343 | let expected = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ |
| 344 | 0u8, 0, 0, 1, 6, 3, 38, 76, 128, 153, 178, 204, 215, 230, 244, 255, 255, 255, 255, 255, |
| 345 | 0, 255, 255, 0, |
| 346 | ]; |
| 347 | |
| 348 | for (d, e) in data.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(expected) { |
| 349 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#::<u8>::into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[f64][`IntoStimulus<u8>`]into_stimulus().(d), e); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | #[cfg(feature = "approx")] |
| 354 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 355 | fn uint_to_floatrust-analyzer cargo palette 0.7.6 stimulus/test/uint_to_float().() { |
| 356 | fn into_stimulus_oldrust-analyzer cargo palette 0.7.6 stimulus/test/into_stimulus_old().(n: u8) -> f32 { |
| 357 | let max = u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX. as f32; |
| 358 | n as f32 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Div<Self>`]div().max |
| 359 | } |
| 360 | |
| 361 | for n in (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255).step_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#step_by().(5) { |
| 362 | assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#::<f32>::into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[u8][`IntoStimulus<f32>`]into_stimulus().(n), into_stimulus_oldrust-analyzer cargo palette 0.7.6 stimulus/test/into_stimulus_old().(n)) |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | #[cfg(feature = "approx")] |
| 367 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 368 | fn uint_to_doublerust-analyzer cargo palette 0.7.6 stimulus/test/uint_to_double().() { |
| 369 | fn into_stimulus_oldrust-analyzer cargo palette 0.7.6 stimulus/test/into_stimulus_old().(n: u8) -> f64 { |
| 370 | let max = u8::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]MAX. as f64; |
| 371 | n as f64 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Div<Self>`]div().max |
| 372 | } |
| 373 | |
| 374 | for n in (0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255).step_byrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#step_by().(5) { |
| 375 | assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(IntoStimulusrust-analyzer cargo palette 0.7.6 stimulus/IntoStimulus#::<f64>::into_stimulusrust-analyzer cargo palette 0.7.6 stimulus/impl#[u8][`IntoStimulus<f64>`]into_stimulus().(n), into_stimulus_oldrust-analyzer cargo palette 0.7.6 stimulus/test/into_stimulus_old().(n)) |
| 376 | } |
| 377 | } |
| 378 | } |