| 1 | mod calc; |
| 2 | mod helper; |
| 3 | mod param; |
| 4 | |
| 5 | pub use calc::*; |
| 6 | pub use helper::*; |
| 7 | pub use param::*; |
| 8 | |
| 9 | use core::f32::consts::{PI, TAU}; |
| 10 | |
| 11 | const PI_3: f32 = PI * 3.0; |
| 12 | |
| 13 | #[cfg(not(feature = "std"))] |
| 14 | use num_traits::float::Float; |
| 15 | |
| 16 | #[allow(clippy::excessive_precision)] |
| 17 | pub(crate) fn oklab_to_linear_rgb(l: f32, a: f32, b: f32) -> [f32; 3] { |
| 18 | let l_ = (l + 0.3963377774 * a + 0.2158037573 * b).powi(3); |
| 19 | let m_ = (l - 0.1055613458 * a - 0.0638541728 * b).powi(3); |
| 20 | let s_ = (l - 0.0894841775 * a - 1.2914855480 * b).powi(3); |
| 21 | let r = 4.0767416621 * l_ - 3.3077115913 * m_ + 0.2309699292 * s_; |
| 22 | let g = -1.2684380046 * l_ + 2.6097574011 * m_ - 0.3413193965 * s_; |
| 23 | let b = -0.0041960863 * l_ - 0.7034186147 * m_ + 1.7076147010 * s_; |
| 24 | [r, g, b] |
| 25 | } |
| 26 | |
| 27 | #[allow(clippy::excessive_precision)] |
| 28 | pub(crate) fn linear_rgb_to_oklab(r: f32, g: f32, b: f32) -> [f32; 3] { |
| 29 | let l_ = (0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b).cbrt(); |
| 30 | let m_ = (0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b).cbrt(); |
| 31 | let s_ = (0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b).cbrt(); |
| 32 | let l = 0.2104542553 * l_ + 0.7936177850 * m_ - 0.0040720468 * s_; |
| 33 | let a = 1.9779984951 * l_ - 2.4285922050 * m_ + 0.4505937099 * s_; |
| 34 | let b = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.8086757660 * s_; |
| 35 | [l, a, b] |
| 36 | } |
| 37 | |
| 38 | pub(crate) const fn hue_to_rgb(n1: f32, n2: f32, h: f32) -> f32 { |
| 39 | let h = modulo(h, 6.0); |
| 40 | |
| 41 | if h < 1.0 { |
| 42 | return n1 + ((n2 - n1) * h); |
| 43 | } |
| 44 | |
| 45 | if h < 3.0 { |
| 46 | return n2; |
| 47 | } |
| 48 | |
| 49 | if h < 4.0 { |
| 50 | return n1 + ((n2 - n1) * (4.0 - h)); |
| 51 | } |
| 52 | |
| 53 | n1 |
| 54 | } |
| 55 | |
| 56 | // h = 0..360 |
| 57 | // s, l = 0..1 |
| 58 | // r, g, b = 0..1 |
| 59 | pub(crate) const fn hsl_to_rgb(h: f32, s: f32, l: f32) -> [f32; 3] { |
| 60 | if s == 0.0 { |
| 61 | return [l, l, l]; |
| 62 | } |
| 63 | |
| 64 | let n2 = if l < 0.5 { |
| 65 | l * (1.0 + s) |
| 66 | } else { |
| 67 | l + s - (l * s) |
| 68 | }; |
| 69 | |
| 70 | let n1 = 2.0 * l - n2; |
| 71 | let h = h / 60.0; |
| 72 | let r = hue_to_rgb(n1, n2, h + 2.0); |
| 73 | let g = hue_to_rgb(n1, n2, h); |
| 74 | let b = hue_to_rgb(n1, n2, h - 2.0); |
| 75 | [r, g, b] |
| 76 | } |
| 77 | |
| 78 | pub(crate) const fn hwb_to_rgb(hue: f32, white: f32, black: f32) -> [f32; 3] { |
| 79 | if white + black >= 1.0 { |
| 80 | let l = white / (white + black); |
| 81 | return [l, l, l]; |
| 82 | } |
| 83 | |
| 84 | let [r, g, b] = hsl_to_rgb(hue, 1.0, 0.5); |
| 85 | let r = r * (1.0 - white - black) + white; |
| 86 | let g = g * (1.0 - white - black) + white; |
| 87 | let b = b * (1.0 - white - black) + white; |
| 88 | [r, g, b] |
| 89 | } |
| 90 | |
| 91 | #[allow(clippy::float_cmp)] |
| 92 | pub(crate) const fn hsv_to_hsl(h: f32, s: f32, v: f32) -> [f32; 3] { |
| 93 | let l = (2.0 - s) * v / 2.0; |
| 94 | |
| 95 | let s = if l != 0.0 { |
| 96 | if l == 1.0 { |
| 97 | 0.0 |
| 98 | } else if l < 0.5 { |
| 99 | s * v / (l * 2.0) |
| 100 | } else { |
| 101 | s * v / (2.0 - l * 2.0) |
| 102 | } |
| 103 | } else { |
| 104 | s |
| 105 | }; |
| 106 | |
| 107 | [h, s, l] |
| 108 | } |
| 109 | |
| 110 | pub(crate) const fn hsv_to_rgb(h: f32, s: f32, v: f32) -> [f32; 3] { |
| 111 | let [h, s, l] = hsv_to_hsl(h, s, v); |
| 112 | hsl_to_rgb(h, s, l) |
| 113 | } |
| 114 | |
| 115 | #[allow(clippy::float_cmp)] |
| 116 | pub(crate) const fn rgb_to_hsv(r: f32, g: f32, b: f32) -> [f32; 3] { |
| 117 | let v = r.max(g.max(b)); |
| 118 | let d = v - r.min(g.min(b)); |
| 119 | |
| 120 | if d == 0.0 { |
| 121 | return [0.0, 0.0, v]; |
| 122 | } |
| 123 | |
| 124 | let s = d / v; |
| 125 | let dr = (v - r) / d; |
| 126 | let dg = (v - g) / d; |
| 127 | let db = (v - b) / d; |
| 128 | |
| 129 | let h = if r == v { |
| 130 | db - dg |
| 131 | } else if g == v { |
| 132 | 2.0 + dr - db |
| 133 | } else { |
| 134 | 4.0 + dg - dr |
| 135 | }; |
| 136 | |
| 137 | let h = (h * 60.0) % 360.0; |
| 138 | [normalize_angle(h), s, v] |
| 139 | } |
| 140 | |
| 141 | #[allow(clippy::float_cmp)] |
| 142 | pub(crate) const fn rgb_to_hsl(r: f32, g: f32, b: f32) -> [f32; 3] { |
| 143 | let min = r.min(g.min(b)); |
| 144 | let max = r.max(g.max(b)); |
| 145 | let l = (max + min) / 2.0; |
| 146 | |
| 147 | if min == max { |
| 148 | return [0.0, 0.0, l]; |
| 149 | } |
| 150 | |
| 151 | let d = max - min; |
| 152 | |
| 153 | let s = if l < 0.5 { |
| 154 | d / (max + min) |
| 155 | } else { |
| 156 | d / (2.0 - max - min) |
| 157 | }; |
| 158 | |
| 159 | let dr = (max - r) / d; |
| 160 | let dg = (max - g) / d; |
| 161 | let db = (max - b) / d; |
| 162 | |
| 163 | let h = if r == max { |
| 164 | db - dg |
| 165 | } else if g == max { |
| 166 | 2.0 + dr - db |
| 167 | } else { |
| 168 | 4.0 + dg - dr |
| 169 | }; |
| 170 | |
| 171 | let h = (h * 60.0) % 360.0; |
| 172 | [normalize_angle(h), s, l] |
| 173 | } |
| 174 | |
| 175 | pub(crate) const fn rgb_to_hwb(r: f32, g: f32, b: f32) -> [f32; 3] { |
| 176 | let [hue, _, _] = rgb_to_hsl(r, g, b); |
| 177 | let white = r.min(g.min(b)); |
| 178 | let black = 1.0 - r.max(g.max(b)); |
| 179 | [hue, white, black] |
| 180 | } |
| 181 | |
| 182 | #[inline] |
| 183 | pub(crate) const fn normalize_angle(t: f32) -> f32 { |
| 184 | ((t % 360.0) + 360.0) % 360.0 |
| 185 | } |
| 186 | |
| 187 | #[inline] |
| 188 | pub(crate) const fn interp_angle(a0: f32, a1: f32, t: f32) -> f32 { |
| 189 | let delta = (((a1 - a0) % 360.0) + 540.0) % 360.0 - 180.0; |
| 190 | (a0 + t * delta + 360.0) % 360.0 |
| 191 | } |
| 192 | |
| 193 | #[inline] |
| 194 | pub(crate) const fn interp_angle_rad(a0: f32, a1: f32, t: f32) -> f32 { |
| 195 | let delta = (((a1 - a0) % TAU) + PI_3) % TAU - PI; |
| 196 | (a0 + t * delta + TAU) % TAU |
| 197 | } |
| 198 | |
| 199 | #[inline] |
| 200 | pub(crate) const fn modulo(x: f32, n: f32) -> f32 { |
| 201 | (x % n + n) % n |
| 202 | } |
| 203 | |
| 204 | // Map t from range [a, b] to range [c, d] |
| 205 | #[inline] |
| 206 | pub(crate) const fn remap(t: f32, a: f32, b: f32, c: f32, d: f32) -> f32 { |
| 207 | (t - a) * ((d - c) / (b - a)) + c |
| 208 | } |
| 209 | |
| 210 | #[cfg(test)] |
| 211 | mod t { |
| 212 | use super::*; |
| 213 | |
| 214 | #[test] |
| 215 | fn normalize_angle_() { |
| 216 | let data = [ |
| 217 | (0.0, 0.0), |
| 218 | (360.0, 0.0), |
| 219 | (720.0, 0.0), |
| 220 | (400.0, 40.0), |
| 221 | (1155.0, 75.0), |
| 222 | (-360.0, 0.0), |
| 223 | (-90.0, 270.0), |
| 224 | (-765.0, 315.0), |
| 225 | ]; |
| 226 | for (x, expected) in data { |
| 227 | let c = normalize_angle(x); |
| 228 | assert_eq!(expected, c); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | #[test] |
| 233 | fn interp_angle_() { |
| 234 | let data = [ |
| 235 | ((0.0, 360.0, 0.5), 0.0), |
| 236 | ((360.0, 90.0, 0.0), 0.0), |
| 237 | ((360.0, 90.0, 0.5), 45.0), |
| 238 | ((360.0, 90.0, 1.0), 90.0), |
| 239 | ]; |
| 240 | for ((a, b, t), expected) in data { |
| 241 | let v = interp_angle(a, b, t); |
| 242 | assert_eq!(expected, v); |
| 243 | } |
| 244 | } |
| 245 | } |