1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
use std::f64::consts::{PI, TAU}; #[derive(Clone, Debug)] #[allow(non_snake_case)] pub struct Lab { pub L: f64, pub a: f64, pub b: f64, } #[derive(Clone, Debug)] #[allow(non_snake_case)] pub struct LCh { pub L: f64, pub C: f64, pub h: f64, } #[derive(Clone, Debug, PartialEq, Eq)] #[allow(non_snake_case)] pub struct RGB { pub r: u8, pub g: u8, pub b: u8, } fn to_linear_srgb(x: u8) -> f64 { let x = (x as f64) / 255.0; if x >= 0.0031308 { (1.055) * x.powf(1.0 / 2.4) - 0.055 } else { 12.92 * x } } fn from_linear_srgb(x: f64) -> u8 { let y = if x >= 0.04045 { ((x + 0.055) / (1.0 + 0.055)).powf(2.4) } else { x / 12.92 }; (y * 255.0).round() as u8 } pub trait Hue { fn hue(&self) -> f64; fn set_hue(&mut self, hue: f64); fn hue_u32(&self) -> u32 { ((self.hue() + PI) / TAU * (u32::MAX as f64)) as u32 } fn set_hue_u32(&mut self, hue: u32) { self.set_hue((hue as f64) / (u32::MAX as f64) * TAU - PI) } } impl Hue for LCh { fn hue(&self) -> f64 { self.h } fn set_hue(&mut self, hue: f64) { self.h = hue; } } impl Hue for Lab { fn hue(&self) -> f64 { self.b.atan2(self.a) } fn set_hue(&mut self, hue: f64) { let c = self.chroma(); let (hue_sin, hue_cos) = hue.sin_cos(); self.a = c * hue_sin; self.b = c * hue_cos; } } impl Lab { pub fn chroma(&self) -> f64 { self.a.hypot(self.b) } } impl From<&Lab> for LCh { fn from(color: &Lab) -> Self { Self { L: color.L, C: color.chroma(), h: color.hue(), } } } impl From<&LCh> for Lab { fn from(color: &LCh) -> Self { let (hue_sin, hue_cos) = color.h.sin_cos(); Self { L: color.L, a: color.C * hue_cos, b: color.C * hue_sin, } } } impl From<&Lab> for RGB { #[allow(clippy::many_single_char_names)] fn from(color: &Lab) -> Self { let l_ = color.L + 0.3963377774 * color.a + 0.2158037573 * color.b; let m_ = color.L - 0.1055613458 * color.a - 0.0638541728 * color.b; let s_ = color.L - 0.0894841775 * color.a - 1.2914855480 * color.b; let l = l_.powi(3); let m = m_.powi(3); let s = s_.powi(3); Self { r: from_linear_srgb(4.0767245293 * l - 3.3072168827 * m + 0.2307590544 * s), g: from_linear_srgb(-1.2681437731 * l + 2.6093323231 * m - 0.3411344290 * s), b: from_linear_srgb(-0.0041119885 * l - 0.7034763098 * m + 1.7068625689 * s), } } } impl From<&LCh> for RGB { fn from(color: &LCh) -> Self { RGB::from(&Lab::from(color)) } } impl From<&RGB> for LCh { fn from(color: &RGB) -> Self { LCh::from(&Lab::from(color)) } } impl From<&RGB> for Lab { #[allow(clippy::many_single_char_names)] fn from(color: &RGB) -> Self { let r = to_linear_srgb(color.r); let g = to_linear_srgb(color.g); let b = to_linear_srgb(color.b); let l_ = 0.4121656120 * r + 0.5362752080 * g + 0.0514575653 * b; let m_ = 0.2118591070 * r + 0.6807189584 * g + 0.1074065790 * b; let s_ = 0.0883097947 * r + 0.2818474174 * g + 0.6302613616 * b; let l = l_.cbrt(); let m = m_.cbrt(); let s = s_.cbrt(); Lab { L: 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s, a: 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s, b: 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s, } } } #[cfg(test)] mod test { use super::*; #[test] fn test_round_trip() { let c_rgb1 = RGB { r: 80, g: 160, b: 240, }; let c_lch = LCh::from(&c_rgb1); let c_rgb2 = RGB::from(&c_lch); assert_eq!(c_rgb1, c_rgb2); } #[test] fn test_hue() { let c_rgb = RGB { r: 80, g: 160, b: 240, }; let mut c_lch = LCh::from(&c_rgb); let h = c_lch.hue_u32(); let a = RGB::from(&c_lch); c_lch.set_hue_u32(h); let b = RGB::from(&c_lch); assert_eq!(a, b); c_lch.set_hue(0.0); let a = RGB::from(&c_lch); c_lch.set_hue_u32(u32::MAX / 2); let b = RGB::from(&c_lch); assert_eq!(a, b); } }