| 1 | #![doc(html_root_url = "https://docs.rs/lab")] |
| 2 | |
| 3 | /*! |
| 4 | |
| 5 | # Lab |
| 6 | |
| 7 | Tools for converting RGB colors to L\*a\*b\* measurements. |
| 8 | |
| 9 | RGB colors, for this crate at least, are considered to be composed of `u8` |
| 10 | values from 0 to 255, while L\*a\*b\* colors are represented by its own struct |
| 11 | that uses `f32` values. |
| 12 | |
| 13 | # Usage |
| 14 | |
| 15 | ## Converting single values |
| 16 | |
| 17 | To convert a single value, use one of the functions |
| 18 | |
| 19 | * `lab::Lab::from_rgb(rgb: &[u8; 3]) -> Lab` |
| 20 | * `lab::Lab::from_rgba(rgba: &[u8; 4]) -> Lab` (drops the fourth alpha byte) |
| 21 | * `lab::Lab::to_rgb(&self) -> [u8; 3]` |
| 22 | |
| 23 | ```rust |
| 24 | extern crate lab; |
| 25 | use lab::Lab; |
| 26 | |
| 27 | let pink_in_lab = Lab::from_rgb(&[253, 120, 138]); |
| 28 | // Lab { l: 66.639084, a: 52.251457, b: 14.860654 } |
| 29 | ``` |
| 30 | |
| 31 | ## Converting multiple values |
| 32 | |
| 33 | To convert slices of values |
| 34 | |
| 35 | * `lab::rgbs_to_labs(rgbs: &[[u8; 3]]) -> Vec<Lab>` |
| 36 | * `lab::labs_to_rgbs(labs: &[Lab]) -> Vec<[u8; 3]>` |
| 37 | * `lab::rgb_bytes_to_labs(bytes: &[u8]) -> Vec<Lab>` |
| 38 | * `lab::labs_to_rgb_bytes(labs: &[Lab]) -> Vec<u8>` |
| 39 | |
| 40 | ```rust |
| 41 | extern crate lab; |
| 42 | use lab::rgbs_to_labs; |
| 43 | |
| 44 | let rgbs = vec![ |
| 45 | [0xFF, 0x69, 0xB6], |
| 46 | [0xE7, 0x00, 0x00], |
| 47 | [0xFF, 0x8C, 0x00], |
| 48 | [0xFF, 0xEF, 0x00], |
| 49 | [0x00, 0x81, 0x1F], |
| 50 | [0x00, 0xC1, 0xC1], |
| 51 | [0x00, 0x44, 0xFF], |
| 52 | [0x76, 0x00, 0x89], |
| 53 | ]; |
| 54 | |
| 55 | let labs = rgbs_to_labs(&rgbs); |
| 56 | ``` |
| 57 | |
| 58 | ```rust |
| 59 | extern crate lab; |
| 60 | use lab::rgb_bytes_to_labs; |
| 61 | |
| 62 | let rgbs = vec![ |
| 63 | 0xFF, 0x69, 0xB6, |
| 64 | 0xE7, 0x00, 0x00, |
| 65 | 0xFF, 0x8C, 0x00, |
| 66 | 0xFF, 0xEF, 0x00, |
| 67 | 0x00, 0x81, 0x1F, |
| 68 | 0x00, 0xC1, 0xC1, |
| 69 | 0x00, 0x44, 0xFF, |
| 70 | 0x76, 0x00, 0x89, |
| 71 | ]; |
| 72 | |
| 73 | let labs = rgb_bytes_to_labs(&rgbs); |
| 74 | ``` |
| 75 | |
| 76 | These functions will use x86_64 AVX2 instructions if compiled to a supported target. |
| 77 | |
| 78 | ## Minimum Rust version |
| 79 | |
| 80 | Lab 0.7.0 requires Rust >= 1.31.0 for the [chunks_exact](https://doc.rust-lang.org/std/primitive.slice.html#method.chunks_exact) slice method |
| 81 | |
| 82 | */ |
| 83 | |
| 84 | #[cfg(test)] |
| 85 | #[macro_use] |
| 86 | extern crate pretty_assertionsrust-analyzer cargo lab 0.11.0 pretty_assertions/; |
| 87 | #[cfg(test)] |
| 88 | extern crate approxrust-analyzer cargo lab 0.11.0 approx/; |
| 89 | #[cfg(test)] |
| 90 | extern crate lazy_staticrust-analyzer cargo lab 0.11.0 lazy_static/; |
| 91 | #[cfg(test)] |
| 92 | extern crate randrust-analyzer cargo lab 0.11.0 rand/; |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod approx_implrust-analyzer cargo lab 0.11.0 approx_impl/; |
| 96 | |
| 97 | #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] |
| 98 | mod simd; |
| 99 | |
| 100 | /// Struct representing a color in CIALab, a.k.a. L\*a\*b\*, color space |
| 101 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 102 | pub struct Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 103 | pub lrust-analyzer cargo lab 0.11.0 Lab#l.: f32, |
| 104 | pub arust-analyzer cargo lab 0.11.0 Lab#a.: f32, |
| 105 | pub brust-analyzer cargo lab 0.11.0 Lab#b.: f32, |
| 106 | } |
| 107 | |
| 108 | /// Struct representing a color in cylindrical CIELCh color space |
| 109 | #[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:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default:)] |
| 110 | pub struct LChrust-analyzer cargo lab 0.11.0 LCh# { |
| 111 | pub lrust-analyzer cargo lab 0.11.0 LCh#l.: f32, |
| 112 | pub crust-analyzer cargo lab 0.11.0 LCh#c.: f32, |
| 113 | pub hrust-analyzer cargo lab 0.11.0 LCh#h.: f32, |
| 114 | } |
| 115 | |
| 116 | // κ and ε parameters used in conversion between XYZ and La*b*. See |
| 117 | // http://www.brucelindbloom.com/LContinuity.html for explanation as to why |
| 118 | // those are different values than those provided by CIE standard. |
| 119 | pub(craterust-analyzer cargo lab 0.11.0 crate/) const KAPPArust-analyzer cargo lab 0.11.0 KAPPA.: f32 = 24389.0 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().27.0; |
| 120 | pub(craterust-analyzer cargo lab 0.11.0 crate/) const EPSILONrust-analyzer cargo lab 0.11.0 EPSILON.: f32 = 216.0 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().24389.0; |
| 121 | pub(craterust-analyzer cargo lab 0.11.0 crate/) const CBRT_EPSILONrust-analyzer cargo lab 0.11.0 CBRT_EPSILON.: f32 = 6.0 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().29.0; |
| 122 | |
| 123 | // S₀ and E₀ thresholds used in sRGB gamma. The latter is scaled for encoded |
| 124 | // value in the range 0..255. |
| 125 | pub(craterust-analyzer cargo lab 0.11.0 crate/) const S_0rust-analyzer cargo lab 0.11.0 S_0.: f32 = 0.003130668442500564; |
| 126 | pub(craterust-analyzer cargo lab 0.11.0 crate/) const E_0_255rust-analyzer cargo lab 0.11.0 E_0_255.: f32 = 3294.6 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().S_0rust-analyzer cargo lab 0.11.0 S_0.; |
| 127 | |
| 128 | // Generated by srgb-matrices.py |
| 129 | const WHITE_Xrust-analyzer cargo lab 0.11.0 WHITE_X.: f32 = 0.9504492182750991; |
| 130 | const WHITE_Zrust-analyzer cargo lab 0.11.0 WHITE_Z.: f32 = 1.0889166484304715; |
| 131 | |
| 132 | fn rgb_to_labrust-analyzer cargo lab 0.11.0 rgb_to_lab().(r: u8, g: u8, b: u8) -> Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 133 | xyz_to_labrust-analyzer cargo lab 0.11.0 xyz_to_lab().(rgb_to_xyzrust-analyzer cargo lab 0.11.0 rgb_to_xyz().(r, g, b)) |
| 134 | } |
| 135 | |
| 136 | fn rgb_to_xyzrust-analyzer cargo lab 0.11.0 rgb_to_xyz().(r: u8, g: u8, b: u8) -> [f32; 3] { |
| 137 | rgb_to_xyz_innerrust-analyzer cargo lab 0.11.0 rgb_to_xyz_inner().(r as f32, g as f32, b as f32) |
| 138 | } |
| 139 | |
| 140 | fn rgb_to_xyz_normalizedrust-analyzer cargo lab 0.11.0 rgb_to_xyz_normalized().(rgb: &[f32; 3]) -> [f32; 3] { |
| 141 | rgb_to_xyz_innerrust-analyzer cargo lab 0.11.0 rgb_to_xyz_inner().(rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). 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().255.0, rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). 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().255.0, rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index(). 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().255.0) |
| 142 | } |
| 143 | |
| 144 | #[inline(always)] |
| 145 | #[cfg(any(target_feature = "fma", test))] |
| 146 | fn mul3rust-analyzer cargo lab 0.11.0 mul3().(a0: f32, a1: f32, a2: f32, b0: f32, b1: f32, b2: f32) -> f32 { |
| 147 | a2.mul_addrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]mul_add().(b2, a1.mul_addrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]mul_add().(b1, a0 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().b0)) |
| 148 | } |
| 149 | |
| 150 | #[inline(always)] |
| 151 | #[cfg(not(any(target_feature = "fma", test)))] |
| 152 | fn mul3(a0: f32, a1: f32, a2: f32, b0: f32, b1: f32, b2: f32) -> f32 { |
| 153 | a0 * b0 + a1 * b1 + a2 * b2 |
| 154 | } |
| 155 | |
| 156 | #[inline] |
| 157 | fn rgb_to_xyz_innerrust-analyzer cargo lab 0.11.0 rgb_to_xyz_inner().(r: f32, g: f32, b: f32) -> [f32; 3] { |
| 158 | #[inline] |
| 159 | fn rgb_to_xyz_maprust-analyzer cargo lab 0.11.0 rgb_to_xyz_map().(c: f32) -> f32 { |
| 160 | if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().E_0_255rust-analyzer cargo lab 0.11.0 E_0_255. { |
| 161 | const Arust-analyzer cargo lab 0.11.0 A.: f32 = 0.055 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().255.0; |
| 162 | const Drust-analyzer cargo lab 0.11.0 D.: f32 = 1.055 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().255.0; |
| 163 | ((c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().Arust-analyzer cargo lab 0.11.0 A.) 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().Drust-analyzer cargo lab 0.11.0 D.).powfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powf().(2.4) |
| 164 | } else { |
| 165 | const Drust-analyzer cargo lab 0.11.0 D.: f32 = 12.92 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().255.0; |
| 166 | c 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().Drust-analyzer cargo lab 0.11.0 D. |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | let r = rgb_to_xyz_maprust-analyzer cargo lab 0.11.0 rgb_to_xyz_map().(r); |
| 171 | let g = rgb_to_xyz_maprust-analyzer cargo lab 0.11.0 rgb_to_xyz_map().(g); |
| 172 | let b = rgb_to_xyz_maprust-analyzer cargo lab 0.11.0 rgb_to_xyz_map().(b); |
| 173 | |
| 174 | // Generated by srgb-matrices.py |
| 175 | let x = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 176 | r, |
| 177 | g, |
| 178 | b, |
| 179 | 0.4124108464885388, |
| 180 | 0.3575845678529519, |
| 181 | 0.18045380393360833, |
| 182 | ); |
| 183 | let y = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 184 | r, |
| 185 | g, |
| 186 | b, |
| 187 | 0.21264934272065283, |
| 188 | 0.7151691357059038, |
| 189 | 0.07218152157344333, |
| 190 | ); |
| 191 | let z = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 192 | r, |
| 193 | g, |
| 194 | b, |
| 195 | 0.019331758429150258, |
| 196 | 0.11919485595098397, |
| 197 | 0.9503900340503373, |
| 198 | ); |
| 199 | |
| 200 | [x, y, z] |
| 201 | } |
| 202 | |
| 203 | fn xyz_to_labrust-analyzer cargo lab 0.11.0 xyz_to_lab().(xyz: [f32; 3]) -> Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 204 | #[inline] |
| 205 | fn xyz_to_lab_maprust-analyzer cargo lab 0.11.0 xyz_to_lab_map().(c: f32) -> f32 { |
| 206 | if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().EPSILONrust-analyzer cargo lab 0.11.0 EPSILON. { |
| 207 | c.powfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powf().(1.0 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().3.0) |
| 208 | } else { |
| 209 | (KAPPArust-analyzer cargo lab 0.11.0 KAPPA. 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().c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().16.0) 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().116.0 |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // It’s tempting to replace the division with a multiplication by inverse, |
| 214 | // however that results in slightly worse test_grey_error benchmark. |
| 215 | let x = xyz_to_lab_maprust-analyzer cargo lab 0.11.0 xyz_to_lab_map().(xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). 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().WHITE_Xrust-analyzer cargo lab 0.11.0 WHITE_X.); |
| 216 | let y = xyz_to_lab_maprust-analyzer cargo lab 0.11.0 xyz_to_lab_map().(xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().); |
| 217 | let z = xyz_to_lab_maprust-analyzer cargo lab 0.11.0 xyz_to_lab_map().(xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). 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().WHITE_Zrust-analyzer cargo lab 0.11.0 WHITE_Z.); |
| 218 | |
| 219 | Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 220 | lrust-analyzer cargo lab 0.11.0 Lab#l.: (116.0 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().y) 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().16.0, |
| 221 | arust-analyzer cargo lab 0.11.0 Lab#a.: 500.0 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().(x 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().y), |
| 222 | brust-analyzer cargo lab 0.11.0 Lab#b.: 200.0 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().(y 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().z), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | fn lab_to_xyzrust-analyzer cargo lab 0.11.0 lab_to_xyz().(lab: &Labrust-analyzer cargo lab 0.11.0 Lab#) -> [f32; 3] { |
| 227 | let fy = (lab.lrust-analyzer cargo lab 0.11.0 Lab#l. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().16.0) 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().116.0; |
| 228 | let fx = (lab.arust-analyzer cargo lab 0.11.0 Lab#a. 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().500.0) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().fy; |
| 229 | let fz = fy 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().(lab.brust-analyzer cargo lab 0.11.0 Lab#b. 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().200.0); |
| 230 | let xr = if fx rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().CBRT_EPSILONrust-analyzer cargo lab 0.11.0 CBRT_EPSILON. { |
| 231 | fx.powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(3) |
| 232 | } else { |
| 233 | ((fx 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().116.0) 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().16.0) 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().KAPPArust-analyzer cargo lab 0.11.0 KAPPA. |
| 234 | }; |
| 235 | let yr = if lab.lrust-analyzer cargo lab 0.11.0 Lab#l. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().EPSILONrust-analyzer cargo lab 0.11.0 EPSILON. 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().KAPPArust-analyzer cargo lab 0.11.0 KAPPA. { |
| 236 | fy.powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(3) |
| 237 | } else { |
| 238 | lab.lrust-analyzer cargo lab 0.11.0 Lab#l. 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().KAPPArust-analyzer cargo lab 0.11.0 KAPPA. |
| 239 | }; |
| 240 | let zr = if fz rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().CBRT_EPSILONrust-analyzer cargo lab 0.11.0 CBRT_EPSILON. { |
| 241 | fz.powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(3) |
| 242 | } else { |
| 243 | ((fz 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().116.0) 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().16.0) 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().KAPPArust-analyzer cargo lab 0.11.0 KAPPA. |
| 244 | }; |
| 245 | |
| 246 | [xr 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().WHITE_Xrust-analyzer cargo lab 0.11.0 WHITE_X., yr, zr 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().WHITE_Zrust-analyzer cargo lab 0.11.0 WHITE_Z.] |
| 247 | } |
| 248 | |
| 249 | fn xyz_to_rgbrust-analyzer cargo lab 0.11.0 xyz_to_rgb().(xyz: [f32; 3]) -> [u8; 3] { |
| 250 | let rgb = xyz_to_rgb_normalizedrust-analyzer cargo lab 0.11.0 xyz_to_rgb_normalized().(xyz); |
| 251 | [ |
| 252 | (rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). 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().255.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() as u8, |
| 253 | (rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). 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().255.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() as u8, |
| 254 | (rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index(). 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().255.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() as u8, |
| 255 | ] |
| 256 | } |
| 257 | |
| 258 | fn xyz_to_rgb_normalizedrust-analyzer cargo lab 0.11.0 xyz_to_rgb_normalized().(xyz: [f32; 3]) -> [f32; 3] { |
| 259 | let x = xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 260 | let y = xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 261 | let z = xyz[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index().; |
| 262 | |
| 263 | // Generated by srgb-matrices.py |
| 264 | let r = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 265 | x, |
| 266 | y, |
| 267 | z, |
| 268 | 3.240812398895283, |
| 269 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().1.5373084456298136, |
| 270 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.4985865229069666, |
| 271 | ); |
| 272 | let g = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 273 | x, |
| 274 | y, |
| 275 | z, |
| 276 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.9692430170086407, |
| 277 | 1.8759663029085742, |
| 278 | 0.04155503085668564, |
| 279 | ); |
| 280 | let b = mul3rust-analyzer cargo lab 0.11.0 mul3().( |
| 281 | x, |
| 282 | y, |
| 283 | z, |
| 284 | 0.055638398436112804, |
| 285 | -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.20400746093241362, |
| 286 | 1.0571295702861434, |
| 287 | ); |
| 288 | |
| 289 | #[inline] |
| 290 | fn xyz_to_rgb_maprust-analyzer cargo lab 0.11.0 xyz_to_rgb_map().(c: f32) -> f32 { |
| 291 | (if c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialOrd<Self>`]gt().S_0rust-analyzer cargo lab 0.11.0 S_0. { |
| 292 | 1.055 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().c.powfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powf().(1.0 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().2.4) 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().0.055 |
| 293 | } else { |
| 294 | 12.92 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().c |
| 295 | }) |
| 296 | .minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]min().(1.0) |
| 297 | .maxrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]max().(0.0) |
| 298 | } |
| 299 | |
| 300 | [xyz_to_rgb_maprust-analyzer cargo lab 0.11.0 xyz_to_rgb_map().(r), xyz_to_rgb_maprust-analyzer cargo lab 0.11.0 xyz_to_rgb_map().(g), xyz_to_rgb_maprust-analyzer cargo lab 0.11.0 xyz_to_rgb_map().(b)] |
| 301 | } |
| 302 | |
| 303 | /// Convenience function to map a slice of RGB values to Lab values in serial |
| 304 | /// |
| 305 | /// # Example |
| 306 | /// ``` |
| 307 | /// # extern crate lab; |
| 308 | /// # use lab::{Lab, rgbs_to_labs}; |
| 309 | /// let rgbs = &[[255u8, 0, 0], [255, 0, 255], [0, 255, 255]]; |
| 310 | /// let labs = lab::rgbs_to_labs(rgbs); |
| 311 | /// assert_eq!(labs, vec![ |
| 312 | /// Lab { l: 53.238235, a: 80.09231, b: 67.202095 }, |
| 313 | /// Lab { l: 60.322693, a: 98.23698, b: -60.827957 }, |
| 314 | /// Lab { l: 91.11428, a: -48.08274, b: -14.12958 } |
| 315 | /// ]); |
| 316 | /// ``` |
| 317 | #[inline] |
| 318 | pub fn rgbs_to_labsrust-analyzer cargo lab 0.11.0 rgbs_to_labs().(rgbs: &[[u8; 3]]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Labrust-analyzer cargo lab 0.11.0 Lab#> { |
| 319 | #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] |
| 320 | let labs = simd::rgbs_to_labs(rgbs); |
| 321 | |
| 322 | #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))] |
| 323 | let labs = __scalarrust-analyzer cargo lab 0.11.0 __scalar/::rgbs_to_labsrust-analyzer cargo lab 0.11.0 __scalar/rgbs_to_labs().(rgbs); |
| 324 | |
| 325 | labs |
| 326 | } |
| 327 | |
| 328 | /// RGB to Lab conversion that operates on a flat `&[u8]` of consecutive RGB triples. |
| 329 | /// |
| 330 | /// # Example |
| 331 | /// ``` |
| 332 | /// # extern crate lab; |
| 333 | /// # use lab::{Lab, rgb_bytes_to_labs}; |
| 334 | /// let rgbs = &[255u8, 0, 0, 255, 0, 255, 0, 255, 255]; |
| 335 | /// let labs = lab::rgb_bytes_to_labs(rgbs); |
| 336 | /// assert_eq!(labs, vec![ |
| 337 | /// Lab { l: 53.238235, a: 80.09231, b: 67.202095 }, |
| 338 | /// Lab { l: 60.322693, a: 98.23698, b: -60.827957 }, |
| 339 | /// Lab { l: 91.11428, a: -48.08274, b: -14.12958 } |
| 340 | /// ]); |
| 341 | /// ``` |
| 342 | pub fn rgb_bytes_to_labsrust-analyzer cargo lab 0.11.0 rgb_bytes_to_labs().(bytes: &[u8]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Labrust-analyzer cargo lab 0.11.0 Lab#> { |
| 343 | #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] |
| 344 | let labs = simd::rgb_bytes_to_labs(bytes); |
| 345 | |
| 346 | #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))] |
| 347 | let labs = __scalarrust-analyzer cargo lab 0.11.0 __scalar/::rgb_bytes_to_labsrust-analyzer cargo lab 0.11.0 __scalar/rgb_bytes_to_labs().(bytes); |
| 348 | |
| 349 | labs |
| 350 | } |
| 351 | |
| 352 | /// Convenience function to map a slice of Lab values to RGB values in serial |
| 353 | /// |
| 354 | /// # Example |
| 355 | /// ``` |
| 356 | /// # extern crate lab; |
| 357 | /// # use lab::{Lab, labs_to_rgbs}; |
| 358 | /// let labs = &[ |
| 359 | /// Lab { l: 91.11321, a: -48.08751, b: -14.131201 }, |
| 360 | /// Lab { l: 60.32421, a: 98.23433, b: -60.824894 }, |
| 361 | /// Lab { l: 97.13926, a: -21.553724, b: 94.47797 }, |
| 362 | /// ]; |
| 363 | /// let rgbs = lab::labs_to_rgbs(labs); |
| 364 | /// assert_eq!(rgbs, vec![[0u8, 255, 255], [255, 0, 255], [255, 255, 0]]); |
| 365 | /// ``` |
| 366 | #[inline] |
| 367 | pub fn labs_to_rgbsrust-analyzer cargo lab 0.11.0 labs_to_rgbs().(labs: &[Labrust-analyzer cargo lab 0.11.0 Lab#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<[u8; 3]> { |
| 368 | #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] |
| 369 | let rgbs = simd::labs_to_rgbs(labs); |
| 370 | |
| 371 | #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))] |
| 372 | let rgbs = __scalarrust-analyzer cargo lab 0.11.0 __scalar/::labs_to_rgbsrust-analyzer cargo lab 0.11.0 __scalar/labs_to_rgbs().(labs); |
| 373 | |
| 374 | rgbs |
| 375 | } |
| 376 | |
| 377 | /// Lab to RGB conversion that returns RGB triples flattened into a `Vec<u8>` |
| 378 | /// |
| 379 | /// # Example |
| 380 | /// ``` |
| 381 | /// # extern crate lab; |
| 382 | /// # use lab::{Lab, labs_to_rgb_bytes}; |
| 383 | /// let labs = &[ |
| 384 | /// Lab { l: 91.11321, a: -48.08751, b: -14.131201 }, |
| 385 | /// Lab { l: 60.32421, a: 98.23433, b: -60.824894 }, |
| 386 | /// Lab { l: 97.13926, a: -21.553724, b: 94.47797 }, |
| 387 | /// ]; |
| 388 | /// let rgb_bytes = lab::labs_to_rgb_bytes(labs); |
| 389 | /// assert_eq!(rgb_bytes, vec![0, 255, 255, 255, 0, 255, 255, 255, 0]); |
| 390 | /// ``` |
| 391 | #[inline] |
| 392 | pub fn labs_to_rgb_bytesrust-analyzer cargo lab 0.11.0 labs_to_rgb_bytes().(labs: &[Labrust-analyzer cargo lab 0.11.0 Lab#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 393 | #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] |
| 394 | let bytes = simd::labs_to_rgb_bytes(labs); |
| 395 | |
| 396 | #[cfg(not(all(target_arch = "x86_64", target_feature = "avx2")))] |
| 397 | let bytes = __scalarrust-analyzer cargo lab 0.11.0 __scalar/::labs_to_rgb_bytesrust-analyzer cargo lab 0.11.0 __scalar/labs_to_rgb_bytes().(labs); |
| 398 | |
| 399 | bytes |
| 400 | } |
| 401 | |
| 402 | #[doc(hidden)] |
| 403 | pub mod __scalarrust-analyzer cargo lab 0.11.0 __scalar/ { |
| 404 | use rgb_to_labrust-analyzer cargo lab 0.11.0 rgb_to_lab().; |
| 405 | use Labrust-analyzer cargo lab 0.11.0 Lab#; |
| 406 | |
| 407 | #[inline] |
| 408 | pub fn labs_to_rgbsrust-analyzer cargo lab 0.11.0 __scalar/labs_to_rgbs().(labs: &[Labrust-analyzer cargo lab 0.11.0 Lab#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<[u8; 3]> { |
| 409 | labs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Labrust-analyzer cargo lab 0.11.0 Lab#::to_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 410 | } |
| 411 | |
| 412 | #[inline] |
| 413 | pub fn labs_to_rgb_bytesrust-analyzer cargo lab 0.11.0 __scalar/labs_to_rgb_bytes().(labs: &[Labrust-analyzer cargo lab 0.11.0 Lab#]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8> { |
| 414 | labs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 415 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Labrust-analyzer cargo lab 0.11.0 Lab#::to_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb().) |
| 416 | .foldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/map/impl#[`Map<I, F>`][Iterator]fold().(Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(labs.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().3), |mut acc, rgb| { |
| 417 | acc.extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(&rgb); |
| 418 | acc |
| 419 | }) |
| 420 | } |
| 421 | |
| 422 | #[inline] |
| 423 | pub fn rgbs_to_labsrust-analyzer cargo lab 0.11.0 __scalar/rgbs_to_labs().(rgbs: &[[u8; 3]]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Labrust-analyzer cargo lab 0.11.0 Lab#> { |
| 424 | rgbs.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 425 | } |
| 426 | |
| 427 | #[inline] |
| 428 | pub fn rgb_bytes_to_labsrust-analyzer cargo lab 0.11.0 __scalar/rgb_bytes_to_labs().(bytes: &[u8]) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Labrust-analyzer cargo lab 0.11.0 Lab#> { |
| 429 | bytes |
| 430 | .chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(3) |
| 431 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|rgb| rgb_to_labrust-analyzer cargo lab 0.11.0 rgb_to_lab().(rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().)) |
| 432 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | impl Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 437 | /// Constructs a new `Lab` from a three-element array of `u8`s |
| 438 | /// |
| 439 | /// # Examples |
| 440 | /// |
| 441 | /// ``` |
| 442 | /// let lab = lab::Lab::from_rgb(&[240, 33, 95]); |
| 443 | /// assert_eq!(lab::Lab { l: 52.334686, a: 75.55157, b: 19.995684 }, lab); |
| 444 | /// ``` |
| 445 | pub fn from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().(rgb: &[u8; 3]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[Lab] { |
| 446 | rgb_to_labrust-analyzer cargo lab 0.11.0 rgb_to_lab().(rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgb[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) |
| 447 | } |
| 448 | |
| 449 | #[doc(hidden)] |
| 450 | pub fn from_rgb_normalizedrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb_normalized().(rgb: &[f32; 3]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[Lab] { |
| 451 | xyz_to_labrust-analyzer cargo lab 0.11.0 xyz_to_lab().(rgb_to_xyz_normalizedrust-analyzer cargo lab 0.11.0 rgb_to_xyz_normalized().(rgb)) |
| 452 | } |
| 453 | |
| 454 | /// Constructs a new `Lab` from a four-element array of `u8`s |
| 455 | /// |
| 456 | /// The `Lab` struct does not store alpha channel information, so the last |
| 457 | /// `u8` representing alpha is discarded. This convenience method exists |
| 458 | /// in order to easily measure colors already stored in an RGBA array. |
| 459 | /// |
| 460 | /// # Examples |
| 461 | /// |
| 462 | /// ``` |
| 463 | /// let lab = lab::Lab::from_rgba(&[240, 33, 95, 255]); |
| 464 | /// assert_eq!(lab::Lab { l: 52.334686, a: 75.55157, b: 19.995684 }, lab); |
| 465 | /// ``` |
| 466 | pub fn from_rgbarust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgba().(rgba: &[u8; 4]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[Lab] { |
| 467 | Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().(&[rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]) |
| 468 | } |
| 469 | |
| 470 | #[doc(hidden)] |
| 471 | pub fn from_rgba_normalizedrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgba_normalized().(rgba: &[f32; 4]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[Lab] { |
| 472 | Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgb_normalizedrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb_normalized().(&[rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., rgba[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().]) |
| 473 | } |
| 474 | |
| 475 | /// Returns the `Lab`'s color in RGB, in a 3-element array. |
| 476 | /// |
| 477 | /// # Examples |
| 478 | /// |
| 479 | /// ``` |
| 480 | /// let lab = lab::Lab { l: 52.330193, a: 75.56704, b: 19.989174 }; |
| 481 | /// let rgb = lab.to_rgb(); |
| 482 | /// assert_eq!([240, 33, 95], rgb); |
| 483 | /// ``` |
| 484 | pub fn to_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb().(&self) -> [u8; 3] { |
| 485 | xyz_to_rgbrust-analyzer cargo lab 0.11.0 xyz_to_rgb().(lab_to_xyzrust-analyzer cargo lab 0.11.0 lab_to_xyz().(self)) |
| 486 | } |
| 487 | |
| 488 | #[doc(hidden)] |
| 489 | pub fn to_rgb_normalizedrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb_normalized().(&self) -> [f32; 3] { |
| 490 | xyz_to_rgb_normalizedrust-analyzer cargo lab 0.11.0 xyz_to_rgb_normalized().(lab_to_xyzrust-analyzer cargo lab 0.11.0 lab_to_xyz().(self)) |
| 491 | } |
| 492 | |
| 493 | /// Measures the perceptual distance between the colors of one `Lab` |
| 494 | /// and an `other`. |
| 495 | /// |
| 496 | /// # Examples |
| 497 | /// |
| 498 | /// ``` |
| 499 | /// # use lab::Lab; |
| 500 | /// let pink = Lab { l: 66.6348, a: 52.260696, b: 14.850557 }; |
| 501 | /// let websafe_pink = Lab { l: 64.2116, a: 62.519463, b: 2.8871894 }; |
| 502 | /// assert_eq!(254.23636, pink.squared_distance(&websafe_pink)); |
| 503 | /// ``` |
| 504 | pub fn squared_distancerust-analyzer cargo lab 0.11.0 impl#[Lab]squared_distance().(&self, other: &Labrust-analyzer cargo lab 0.11.0 Lab#) -> f32 { |
| 505 | (self.lrust-analyzer cargo lab 0.11.0 Lab#l. 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().other.lrust-analyzer cargo lab 0.11.0 Lab#l.).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(2) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().(self.arust-analyzer cargo lab 0.11.0 Lab#a. 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().other.arust-analyzer cargo lab 0.11.0 Lab#a.).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(2) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][`Add<Self>`]add().(self.brust-analyzer cargo lab 0.11.0 Lab#b. 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().other.brust-analyzer cargo lab 0.11.0 Lab#b.).powirust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]powi().(2) |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | impl LChrust-analyzer cargo lab 0.11.0 LCh# { |
| 510 | /// Constructs a new `LCh` from a three-element array of `u8`s |
| 511 | /// |
| 512 | /// # Examples |
| 513 | /// |
| 514 | /// ``` |
| 515 | /// let lch = lab::LCh::from_rgb(&[240, 33, 95]); |
| 516 | /// assert_eq!(lab::LCh { l: 52.334686, c: 78.15284, h: 0.25873056 }, lch); |
| 517 | /// ``` |
| 518 | pub fn from_rgbrust-analyzer cargo lab 0.11.0 impl#[LCh]from_rgb().(rgb: &[u8; 3]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[LCh] { |
| 519 | LChrust-analyzer cargo lab 0.11.0 LCh#::from_labrust-analyzer cargo lab 0.11.0 impl#[LCh]from_lab().(Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().(rgb)) |
| 520 | } |
| 521 | |
| 522 | /// Constructs a new `LCh` from a four-element array of `u8`s |
| 523 | /// |
| 524 | /// The `LCh` struct does not store alpha channel information, so the last |
| 525 | /// `u8` representing alpha is discarded. This convenience method exists |
| 526 | /// in order to easily measure colors already stored in an RGBA array. |
| 527 | /// |
| 528 | /// # Examples |
| 529 | /// |
| 530 | /// ``` |
| 531 | /// let lch = lab::LCh::from_rgba(&[240, 33, 95, 255]); |
| 532 | /// assert_eq!(lab::LCh { l: 52.334686, c: 78.15284, h: 0.25873056 }, lch); |
| 533 | /// ``` |
| 534 | pub fn from_rgbarust-analyzer cargo lab 0.11.0 impl#[LCh]from_rgba().(rgba: &[u8; 4]) -> Selfrust-analyzer cargo lab 0.11.0 impl#[LCh] { |
| 535 | LChrust-analyzer cargo lab 0.11.0 LCh#::from_labrust-analyzer cargo lab 0.11.0 impl#[LCh]from_lab().(Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbarust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgba().(rgba)) |
| 536 | } |
| 537 | |
| 538 | /// Constructs a new `LCh` from a `Lab` |
| 539 | /// |
| 540 | /// # Examples |
| 541 | /// |
| 542 | /// ``` |
| 543 | /// let lab = lab::Lab { l: 52.33686, a: 75.5516, b: 19.998878 }; |
| 544 | /// let lch = lab::LCh::from_lab(lab); |
| 545 | /// assert_eq!(lab::LCh { l: 52.33686, c: 78.15369, h: 0.25877 }, lch); |
| 546 | /// |
| 547 | /// let lab = lab::Lab { l: 52.33686, a: 0.0, b: 0.0 }; |
| 548 | /// let lch = lab::LCh::from_lab(lab); |
| 549 | /// assert_eq!(lab::LCh { l: 52.33686, c: 0.0, h: 0.0 }, lch); |
| 550 | /// ``` |
| 551 | pub fn from_labrust-analyzer cargo lab 0.11.0 impl#[LCh]from_lab().(lab: Labrust-analyzer cargo lab 0.11.0 Lab#) -> Selfrust-analyzer cargo lab 0.11.0 impl#[LCh] { |
| 552 | LChrust-analyzer cargo lab 0.11.0 LCh# { |
| 553 | lrust-analyzer cargo lab 0.11.0 LCh#l.: lab.lrust-analyzer cargo lab 0.11.0 Lab#l., |
| 554 | crust-analyzer cargo lab 0.11.0 LCh#c.: lab.arust-analyzer cargo lab 0.11.0 Lab#a..hypotrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]hypot().(lab.brust-analyzer cargo lab 0.11.0 Lab#b.), |
| 555 | hrust-analyzer cargo lab 0.11.0 LCh#h.: lab.brust-analyzer cargo lab 0.11.0 Lab#b..atan2rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]atan2().(lab.arust-analyzer cargo lab 0.11.0 Lab#a.), |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /// Returns the `LCh`'s color in RGB, in a 3-element array |
| 560 | /// |
| 561 | /// # Examples |
| 562 | /// |
| 563 | /// ``` |
| 564 | /// let mut lch = lab::LCh { l: 52.33686, c: 78.15369, h: 0.25877 }; |
| 565 | /// assert_eq!([240, 33, 95], lch.to_rgb()); |
| 566 | /// |
| 567 | /// lch.h += std::f32::consts::TAU; |
| 568 | /// assert_eq!([240, 33, 95], lch.to_rgb()); |
| 569 | /// ``` |
| 570 | pub fn to_rgbrust-analyzer cargo lab 0.11.0 impl#[LCh]to_rgb().(&self) -> [u8; 3] { |
| 571 | self.to_labrust-analyzer cargo lab 0.11.0 impl#[LCh]to_lab().().to_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb().() |
| 572 | } |
| 573 | |
| 574 | /// Returns the `LCh`'s color in `Lab` |
| 575 | /// |
| 576 | /// Note that due to imprecision of floating point arithmetic, conversions |
| 577 | /// between Lab and LCh are not stable. A chain of Lab→LCh→Lab or |
| 578 | /// LCh→Lab→LCh operations isn’t guaranteed to give back the source colour. |
| 579 | /// |
| 580 | /// # Examples |
| 581 | /// |
| 582 | /// ``` |
| 583 | /// let lch = lab::LCh { l: 52.33686, c: 78.15369, h: 0.25877 }; |
| 584 | /// let lab = lch.to_lab(); |
| 585 | /// assert_eq!(lab::Lab { l: 52.33686, a: 75.5516, b: 19.998878 }, lab); |
| 586 | /// |
| 587 | /// let lch = lab::LCh { l: 52.33686, c: 0.0, h: 0.25877 }; |
| 588 | /// let lab = lch.to_lab(); |
| 589 | /// assert_eq!(lab::Lab { l: 52.33686, a: 0.0, b: 0.0 }, lab); |
| 590 | /// |
| 591 | /// let inp = lab::Lab { l: 29.52658, a: 58.595745, b: -36.281406 }; |
| 592 | /// let lch = lab::LCh { l: 29.52658, c: 68.91881, h: -0.5544043 }; |
| 593 | /// let out = lab::Lab { l: 29.52658, a: 58.59575, b: -36.281406 }; |
| 594 | /// assert_eq!(lch, lab::LCh::from_lab(inp)); |
| 595 | /// assert_eq!(out, lch.to_lab()); |
| 596 | /// ``` |
| 597 | pub fn to_labrust-analyzer cargo lab 0.11.0 impl#[LCh]to_lab().(&self) -> Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 598 | Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 599 | lrust-analyzer cargo lab 0.11.0 Lab#l.: self.lrust-analyzer cargo lab 0.11.0 LCh#l., |
| 600 | arust-analyzer cargo lab 0.11.0 Lab#a.: self.crust-analyzer cargo lab 0.11.0 LCh#c. 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().self.hrust-analyzer cargo lab 0.11.0 LCh#h..cosrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]cos().(), |
| 601 | brust-analyzer cargo lab 0.11.0 Lab#b.: self.crust-analyzer cargo lab 0.11.0 LCh#c. 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().self.hrust-analyzer cargo lab 0.11.0 LCh#h..sinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]sin().(), |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | #[cfg(test)] |
| 607 | mod testsrust-analyzer cargo lab 0.11.0 tests/ { |
| 608 | use superrust-analyzer cargo lab 0.11.0 crate/::{labs_to_rgbsrust-analyzer cargo lab 0.11.0 labs_to_rgbs()., rgbs_to_labsrust-analyzer cargo lab 0.11.0 rgbs_to_labs()., LChrust-analyzer cargo lab 0.11.0 LCh#, Labrust-analyzer cargo lab 0.11.0 Lab#}; |
| 609 | use approxrust-analyzer cargo approx 0.5.1 crate/::assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!; |
| 610 | use randrust-analyzer cargo rand 0.8.7 crate/::Rngrust-analyzer cargo rand 0.8.7 rng/Rng#; |
| 611 | |
| 612 | const PINKrust-analyzer cargo lab 0.11.0 tests/PINK.: Labrust-analyzer cargo lab 0.11.0 Lab# = Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 613 | lrust-analyzer cargo lab 0.11.0 Lab#l.: 66.637695, |
| 614 | arust-analyzer cargo lab 0.11.0 Lab#a.: 52.250145, |
| 615 | brust-analyzer cargo lab 0.11.0 Lab#b.: 14.858591, |
| 616 | }; |
| 617 | |
| 618 | #[rustfmt::skip] |
| 619 | static COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS.: [([u8; 3], Labrust-analyzer cargo lab 0.11.0 Lab#, LChrust-analyzer cargo lab 0.11.0 LCh#); 17] = [ |
| 620 | ([253, 120, 138], |
| 621 | PINKrust-analyzer cargo lab 0.11.0 tests/PINK., |
| 622 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 66.637695, crust-analyzer cargo lab 0.11.0 LCh#c.: 54.321777, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.2770602 }), |
| 623 | |
| 624 | ([127, 0, 0], |
| 625 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 25.299877, arust-analyzer cargo lab 0.11.0 Lab#a.: 47.77421, brust-analyzer cargo lab 0.11.0 Lab#b.: 37.752514 }, |
| 626 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 25.299877, crust-analyzer cargo lab 0.11.0 LCh#c.: 60.890293, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.66875386 }), |
| 627 | ([ 0, 127, 0], |
| 628 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 45.87715, arust-analyzer cargo lab 0.11.0 Lab#a.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().51.405922, brust-analyzer cargo lab 0.11.0 Lab#b.: 49.61748 }, |
| 629 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 45.87715, crust-analyzer cargo lab 0.11.0 LCh#c.: 71.445526, hrust-analyzer cargo lab 0.11.0 LCh#h.: 2.373896 }), |
| 630 | ([ 0, 0, 127], |
| 631 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 12.809523, arust-analyzer cargo lab 0.11.0 Lab#a.: 47.237186, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().64.33636 }, |
| 632 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 12.809523, crust-analyzer cargo lab 0.11.0 LCh#c.: 79.81553, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.93746966 }), |
| 633 | ([ 0, 127, 127], |
| 634 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 47.892532, arust-analyzer cargo lab 0.11.0 Lab#a.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().28.680845, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().8.428156 }, |
| 635 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 47.892532, crust-analyzer cargo lab 0.11.0 LCh#c.: 29.893557, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().2.8557782 }), |
| 636 | ([127, 0, 127], |
| 637 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 29.525677, arust-analyzer cargo lab 0.11.0 Lab#a.: 58.597298, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().36.28323 }, |
| 638 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 29.525677, crust-analyzer cargo lab 0.11.0 LCh#c.: 68.92109, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.554415 }), |
| 639 | ([255, 0, 0], |
| 640 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 53.238235, arust-analyzer cargo lab 0.11.0 Lab#a.: 80.09231, brust-analyzer cargo lab 0.11.0 Lab#b.: 67.202095 }, |
| 641 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 53.238235, crust-analyzer cargo lab 0.11.0 LCh#c.: 104.55094, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.6981073 }), |
| 642 | ([ 0, 255, 0], |
| 643 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 87.73554, arust-analyzer cargo lab 0.11.0 Lab#a.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().86.18078, brust-analyzer cargo lab 0.11.0 Lab#b.: 83.18251 }, |
| 644 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 87.73554, crust-analyzer cargo lab 0.11.0 LCh#c.: 119.776695, hrust-analyzer cargo lab 0.11.0 LCh#h.: 2.373896 }), |
| 645 | ([ 0, 0, 255], |
| 646 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 32.298466, arust-analyzer cargo lab 0.11.0 Lab#a.: 79.192, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().107.858345 }, |
| 647 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 32.298466, crust-analyzer cargo lab 0.11.0 LCh#c.: 133.8088, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.93746966 }), |
| 648 | ([ 0, 255, 255], |
| 649 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 91.11428, arust-analyzer cargo lab 0.11.0 Lab#a.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().48.08274, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().14.12958 }, |
| 650 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 91.11428, crust-analyzer cargo lab 0.11.0 LCh#c.: 50.115814, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().2.8557787 }), |
| 651 | ([255, 0, 255], |
| 652 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 60.322693, arust-analyzer cargo lab 0.11.0 Lab#a.: 98.23698, brust-analyzer cargo lab 0.11.0 Lab#b.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().60.827957 }, |
| 653 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 60.322693, crust-analyzer cargo lab 0.11.0 LCh#c.: 115.544556, hrust-analyzer cargo lab 0.11.0 LCh#h.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().0.55441487 }), |
| 654 | ([255, 255, 0], |
| 655 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 97.139, arust-analyzer cargo lab 0.11.0 Lab#a.: -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f32][Neg]neg().21.556675, brust-analyzer cargo lab 0.11.0 Lab#b.: 94.48001 }, |
| 656 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 97.139, crust-analyzer cargo lab 0.11.0 LCh#c.: 96.90801, hrust-analyzer cargo lab 0.11.0 LCh#h.: 1.7951176 }), |
| 657 | |
| 658 | ([ 0, 0, 0], |
| 659 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 0.0, arust-analyzer cargo lab 0.11.0 Lab#a.: 0.0, brust-analyzer cargo lab 0.11.0 Lab#b.: 0.0 }, |
| 660 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 0.0, crust-analyzer cargo lab 0.11.0 LCh#c.: 0.0, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.0 }), |
| 661 | ([ 64, 64, 64], |
| 662 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 27.09341, arust-analyzer cargo lab 0.11.0 Lab#a.: 0.0, brust-analyzer cargo lab 0.11.0 Lab#b.: 0.0 }, |
| 663 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 27.09341, crust-analyzer cargo lab 0.11.0 LCh#c.: 0.0, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.0 }), |
| 664 | ([127, 127, 127], |
| 665 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 53.192772, arust-analyzer cargo lab 0.11.0 Lab#a.: 0.0, brust-analyzer cargo lab 0.11.0 Lab#b.: 0.0 }, |
| 666 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 53.192772, crust-analyzer cargo lab 0.11.0 LCh#c.: 0.0, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.0 }), |
| 667 | ([196, 196, 196], |
| 668 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 79.15698, arust-analyzer cargo lab 0.11.0 Lab#a.: 0.0, brust-analyzer cargo lab 0.11.0 Lab#b.: 0.0 }, |
| 669 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 79.15698, crust-analyzer cargo lab 0.11.0 LCh#c.: 0.0, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.0 }), |
| 670 | ([255, 255, 255], |
| 671 | Labrust-analyzer cargo lab 0.11.0 Lab# { lrust-analyzer cargo lab 0.11.0 Lab#l.: 100.0, arust-analyzer cargo lab 0.11.0 Lab#a.: 0.0, brust-analyzer cargo lab 0.11.0 Lab#b.: 0.0 }, |
| 672 | LChrust-analyzer cargo lab 0.11.0 LCh# { lrust-analyzer cargo lab 0.11.0 LCh#l.: 100.0, crust-analyzer cargo lab 0.11.0 LCh#c.: 0.0, hrust-analyzer cargo lab 0.11.0 LCh#h.: 0.0 }), |
| 673 | ]; |
| 674 | |
| 675 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 676 | fn test_lab_from_rgbrust-analyzer cargo lab 0.11.0 tests/test_lab_from_rgb().() { |
| 677 | let expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, lab, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().lab).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 678 | let actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS. |
| 679 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 680 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(rgb, _, _)| Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().(rgb)) |
| 681 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 682 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, actual); |
| 683 | } |
| 684 | |
| 685 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 686 | fn test_lab_to_rgbrust-analyzer cargo lab 0.11.0 tests/test_lab_to_rgb().() { |
| 687 | let expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(rgb, _, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().rgb).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 688 | let actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, lab, _)| lab.to_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]to_rgb().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 689 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, actual); |
| 690 | } |
| 691 | |
| 692 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 693 | fn test_lch_from_rgbrust-analyzer cargo lab 0.11.0 tests/test_lch_from_rgb().() { |
| 694 | let expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, _, lch)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().lch).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 695 | let actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS. |
| 696 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 697 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(rgb, _, _)| LChrust-analyzer cargo lab 0.11.0 LCh#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[LCh]from_rgb().(rgb)) |
| 698 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 699 | assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), actual.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 700 | } |
| 701 | |
| 702 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 703 | fn test_lch_to_rgbrust-analyzer cargo lab 0.11.0 tests/test_lch_to_rgb().() { |
| 704 | let expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(rgb, _, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().rgb).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 705 | let actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, _, lch)| lch.to_rgbrust-analyzer cargo lab 0.11.0 impl#[LCh]to_rgb().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 706 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, actual); |
| 707 | } |
| 708 | |
| 709 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 710 | fn test_lch_from_labrust-analyzer cargo lab 0.11.0 tests/test_lch_from_lab().() { |
| 711 | let expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, _, lch)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().lch).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 712 | let actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS. |
| 713 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 714 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, lab, _)| LChrust-analyzer cargo lab 0.11.0 LCh#::from_labrust-analyzer cargo lab 0.11.0 impl#[LCh]from_lab().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().lab)) |
| 715 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 716 | assert_relative_eqrust-analyzer cargo approx 0.5.1 macros/assert_relative_eq!!(expected.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), actual.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().()); |
| 717 | } |
| 718 | |
| 719 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 720 | fn test_lch_to_labrust-analyzer cargo lab 0.11.0 tests/test_lch_to_lab().() { |
| 721 | let mut expected: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, lab, _)| *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().lab).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 722 | let mut actual: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_> = COLOURSrust-analyzer cargo lab 0.11.0 tests/COLOURS..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|(_, _, lch)| lch.to_labrust-analyzer cargo lab 0.11.0 impl#[LCh]to_lab().()).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(); |
| 723 | |
| 724 | // Floating point arithmetic is hard. Due to accumulation of errors (or |
| 725 | // perhaps imprecision of trig functions) the Lab→LCh→Lab conversion |
| 726 | // produces slightly different colour than what the source. Round a* |
| 727 | // and b* to four decimal places to work around this. |
| 728 | fn roundrust-analyzer cargo lab 0.11.0 tests/round().(vec: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Labrust-analyzer cargo lab 0.11.0 Lab#>) { |
| 729 | for lab in vec.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().() { |
| 730 | lab.arust-analyzer cargo lab 0.11.0 Lab#a. = (lab.arust-analyzer cargo lab 0.11.0 Lab#a. 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().100000.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() 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().100000.0; |
| 731 | lab.brust-analyzer cargo lab 0.11.0 Lab#b. = (lab.brust-analyzer cargo lab 0.11.0 Lab#b. 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().100000.0).roundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]round().() 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().100000.0; |
| 732 | } |
| 733 | } |
| 734 | roundrust-analyzer cargo lab 0.11.0 tests/round().(&mut expected); |
| 735 | roundrust-analyzer cargo lab 0.11.0 tests/round().(&mut actual); |
| 736 | |
| 737 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(expected, actual); |
| 738 | } |
| 739 | |
| 740 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 741 | fn test_distancerust-analyzer cargo lab 0.11.0 tests/test_distance().() { |
| 742 | let ugly_websafe_pink = Labrust-analyzer cargo lab 0.11.0 Lab# { |
| 743 | lrust-analyzer cargo lab 0.11.0 Lab#l.: 64.2116, |
| 744 | arust-analyzer cargo lab 0.11.0 Lab#a.: 62.519463, |
| 745 | brust-analyzer cargo lab 0.11.0 Lab#b.: 2.8871894, |
| 746 | }; |
| 747 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(254.65927, PINKrust-analyzer cargo lab 0.11.0 tests/PINK..squared_distancerust-analyzer cargo lab 0.11.0 impl#[Lab]squared_distance().(&ugly_websafe_pink)); |
| 748 | } |
| 749 | |
| 750 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 751 | fn test_sendrust-analyzer cargo lab 0.11.0 tests/test_send().() { |
| 752 | fn assert_sendrust-analyzer cargo lab 0.11.0 tests/assert_send().<T: Sendrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Send#>() {} |
| 753 | assert_sendrust-analyzer cargo lab 0.11.0 tests/assert_send().::<Labrust-analyzer cargo lab 0.11.0 Lab#>(); |
| 754 | } |
| 755 | |
| 756 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 757 | fn test_syncrust-analyzer cargo lab 0.11.0 tests/test_sync().() { |
| 758 | fn assert_syncrust-analyzer cargo lab 0.11.0 tests/assert_sync().<T: Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync#>() {} |
| 759 | assert_syncrust-analyzer cargo lab 0.11.0 tests/assert_sync().::<Labrust-analyzer cargo lab 0.11.0 Lab#>(); |
| 760 | } |
| 761 | |
| 762 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 763 | fn test_rgb_to_lab_to_rgbrust-analyzer cargo lab 0.11.0 tests/test_rgb_to_lab_to_rgb().() { |
| 764 | let rgbs: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<[u8; 3]> = { |
| 765 | let rand_seed = [1u8; 32]; |
| 766 | let rng: randrust-analyzer cargo rand 0.8.7 crate/::rngsrust-analyzer cargo rand 0.8.7 rngs/::StdRngrust-analyzer cargo rand 0.8.7 rngs/std/StdRng# = randrust-analyzer cargo rand 0.8.7 crate/::SeedableRngrust-analyzer cargo rand_core 0.6.4 SeedableRng#::from_seedrust-analyzer cargo rand 0.8.7 rngs/std/impl#[StdRng][SeedableRng]from_seed().(rand_seed); |
| 767 | rng.sample_iterrust-analyzer cargo rand 0.8.7 rng/Rng#sample_iter().(&randrust-analyzer cargo rand 0.8.7 crate/::distributionsrust-analyzer cargo rand 0.8.7 distributions/::Standardrust-analyzer cargo rand 0.8.7 distributions/Standard#) |
| 768 | .takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#take().(2048) |
| 769 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().() |
| 770 | }; |
| 771 | let labs = rgbs_to_labsrust-analyzer cargo lab 0.11.0 rgbs_to_labs().(&rgbs); |
| 772 | let rgbs2 = labs_to_rgbsrust-analyzer cargo lab 0.11.0 labs_to_rgbs().(&labs); |
| 773 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rgbs2, rgbs); |
| 774 | } |
| 775 | |
| 776 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 777 | fn test_grey_errorrust-analyzer cargo lab 0.11.0 tests/test_grey_error().() { |
| 778 | // Grey colours have a* and b* components equal to zero. This test goes |
| 779 | // through all 8-bit greys and calculates squared error. If it goes up, |
| 780 | // a change might have worsen the precision of the calculations. If it |
| 781 | // goes down, calculations got better. |
| 782 | let mut error: f64 = 0.0; |
| 783 | let mut count: usize = 0; |
| 784 | for i in 0..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#255_u32 { |
| 785 | let lab = Labrust-analyzer cargo lab 0.11.0 Lab#::from_rgbrust-analyzer cargo lab 0.11.0 impl#[Lab]from_rgb().(&[i as u8, i as u8, i as u8]); |
| 786 | if lab.arust-analyzer cargo lab 0.11.0 Lab#a. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne().0.0 || lab.brust-analyzer cargo lab 0.11.0 Lab#b. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]ne().0.0 { |
| 787 | error = (lab.arust-analyzer cargo lab 0.11.0 Lab#a. as f64).mul_addrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]mul_add().(lab.arust-analyzer cargo lab 0.11.0 Lab#a. as f64, error); |
| 788 | error = (lab.brust-analyzer cargo lab 0.11.0 Lab#b. as f64).mul_addrust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]mul_add().(lab.brust-analyzer cargo lab 0.11.0 Lab#b. as f64, error); |
| 789 | count rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1; |
| 790 | } |
| 791 | } |
| 792 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!((23, 10.627054791711998), (count, error *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[f64][`Mul<Self>`]mul(). 1e9)); |
| 793 | } |
| 794 | } |