chromashift 0.0.21

A library for converting between various color formats and color spaces.
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use crate::{
	A98Rgb, DisplayP3, Hsl, Hwb, Lab, Lch, LinearRgb, Oklab, Oklch, ProphotoRgb, Rec2020, Srgb, XyzD50, XyzD65,
};

/// A direction to interopolate hue values between, when mixing colours.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum HueInterpolation {
	#[default]
	Shorter,
	Longer,
	Increasing,
	Decreasing,
}

/// Trait for calculating mixing two colors together.
///
/// This trait provides a static method which will receive two colours, and can output a Self which should be the result
/// of both colours mixed by the given percentage (the percentage pertains to how much the second colour should apply to
/// the first).
///
/// Per CSS Color (4 12.3 & 5 3.5), interpolation uses premultiplied alpha:
/// 1. Premultiply each component by its alpha
/// 2. Linearly interpolate premultiplied values and alpha independently
/// 3. Un-premultiply by dividing by the interpolated alpha
pub trait ColorMix<T, U>: Sized
where
	T: Into<Self>,
	U: Into<Self>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self;
}

/// Trait for calculating mixing two colors together, with a hue direction for Polar colour spaces.
///
/// This trait provides a static method which will receive two colours, and can output a Self which should be the result
/// of both colours mixed by the given percentage (the percentage pertains to how much the second colour should apply to
/// the first). The Hue direction should be respected. If the colour space is not Polar then consider [ColorMix]
/// instead.
///
/// Per CSS Color 4 12.3, premultiplied alpha is used for non-hue components. The hue component
/// is NOT premultiplied - it is interpolated directly using the specified hue interpolation method.
pub trait ColorMixPolar<T, U>: Sized
where
	T: Into<Self>,
	U: Into<Self>,
{
	fn mix_polar(first: T, second: U, percentage: f64, hue_interpolation: HueInterpolation) -> Self;
}

/// Interpolate a single component using premultiplied alpha.
///
/// CSS Color 4 12.3:
///   premultiplied1 = component1 * alpha1
///   premultiplied2 = component2 * alpha2
///   result_premultiplied = premultiplied1 * (1 - t) + premultiplied2 * t
///   result = result_premultiplied / result_alpha
fn premultiply_lerp(c1: f64, a1: f64, c2: f64, a2: f64, t: f64, result_alpha: f64) -> f64 {
	if result_alpha == 0.0 {
		return c1 * (1.0 - t) + c2 * t;
	}
	let pm1 = c1 * a1;
	let pm2 = c2 * a2;
	(pm1 * (1.0 - t) + pm2 * t) / result_alpha
}

/// Given two hues (`h1`, `h2`), a percentage transform (`t`), and an interpolation direction, return a new Hue rotation
/// transformed by that amount.
pub fn interpolate_hue(h1: f64, h2: f64, t: f64, interpolation: HueInterpolation) -> f64 {
	let (h1, h2) = (h1.rem_euclid(360.0), h2.rem_euclid(360.0));

	let diff = match interpolation {
		HueInterpolation::Shorter => {
			let d = h2 - h1;
			if d.abs() <= 180.0 {
				d
			} else if d > 180.0 {
				d - 360.0
			} else {
				d + 360.0
			}
		}
		HueInterpolation::Longer => {
			let d = h2 - h1;
			if d.abs() > 180.0 {
				d
			} else if d > 0.0 {
				d - 360.0
			} else {
				d + 360.0
			}
		}
		HueInterpolation::Increasing => {
			let mut d = h2 - h1;
			if d < 0.0 {
				d += 360.0;
			}
			d
		}
		HueInterpolation::Decreasing => {
			let mut d = h2 - h1;
			if d > 0.0 {
				d -= 360.0;
			}
			d
		}
	};

	(h1 + diff * t).rem_euclid(360.0)
}

mod sealed {
	pub trait PolarColor {}
}

impl sealed::PolarColor for Hsl {}
impl sealed::PolarColor for Hwb {}
impl sealed::PolarColor for Lch {}
impl sealed::PolarColor for Oklch {}

impl<T, U, V> ColorMix<T, U> for V
where
	V: ColorMixPolar<T, U> + sealed::PolarColor + Sized,
	T: Into<V>,
	U: Into<V>,
{
	fn mix(first: T, second: U, percentage: f64) -> V {
		ColorMixPolar::mix_polar(first, second, percentage, HueInterpolation::Shorter)
	}
}

impl<T, U> ColorMix<T, U> for Srgb
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red as f64, a1, second.red as f64, a2, t, a);
		let g = premultiply_lerp(first.green as f64, a1, second.green as f64, a2, t, a);
		let b = premultiply_lerp(first.blue as f64, a1, second.blue as f64, a2, t, a);
		Srgb::new(r.round() as u8, g.round() as u8, b.round() as u8, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for LinearRgb
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red, a1, second.red, a2, t, a);
		let g = premultiply_lerp(first.green, a1, second.green, a2, t, a);
		let b = premultiply_lerp(first.blue, a1, second.blue, a2, t, a);
		LinearRgb::new(r, g, b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMixPolar<T, U> for Hsl
where
	Self: From<T> + From<U>,
{
	fn mix_polar(first: T, second: U, percentage: f64, hue_interpolation: HueInterpolation) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let h = interpolate_hue(first.hue as f64, second.hue as f64, t, hue_interpolation);
		let s = premultiply_lerp(first.saturation as f64, a1, second.saturation as f64, a2, t, a);
		let l = premultiply_lerp(first.lightness as f64, a1, second.lightness as f64, a2, t, a);
		Hsl::new(h as f32, s as f32, l as f32, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMixPolar<T, U> for Hwb
where
	Self: From<T> + From<U>,
{
	fn mix_polar(first: T, second: U, percentage: f64, hue_interpolation: HueInterpolation) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let h = interpolate_hue(first.hue as f64, second.hue as f64, t, hue_interpolation);
		let w = premultiply_lerp(first.whiteness as f64, a1, second.whiteness as f64, a2, t, a);
		let b = premultiply_lerp(first.blackness as f64, a1, second.blackness as f64, a2, t, a);
		Hwb::new(h as f32, w as f32, b as f32, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for A98Rgb
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red, a1, second.red, a2, t, a);
		let g = premultiply_lerp(first.green, a1, second.green, a2, t, a);
		let b = premultiply_lerp(first.blue, a1, second.blue, a2, t, a);
		A98Rgb::new(r, g, b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for DisplayP3
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red, a1, second.red, a2, t, a);
		let g = premultiply_lerp(first.green, a1, second.green, a2, t, a);
		let b = premultiply_lerp(first.blue, a1, second.blue, a2, t, a);
		DisplayP3::new(r, g, b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for ProphotoRgb
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red, a1, second.red, a2, t, a);
		let g = premultiply_lerp(first.green, a1, second.green, a2, t, a);
		let b = premultiply_lerp(first.blue, a1, second.blue, a2, t, a);
		ProphotoRgb::new(r, g, b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for Rec2020
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let r = premultiply_lerp(first.red, a1, second.red, a2, t, a);
		let g = premultiply_lerp(first.green, a1, second.green, a2, t, a);
		let b = premultiply_lerp(first.blue, a1, second.blue, a2, t, a);
		Rec2020::new(r, g, b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for Lab
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let l = premultiply_lerp(first.lightness, a1, second.lightness, a2, t, a);
		let ab_a = premultiply_lerp(first.a, a1, second.a, a2, t, a);
		let ab_b = premultiply_lerp(first.b, a1, second.b, a2, t, a);
		Lab::new(l, ab_a, ab_b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMixPolar<T, U> for Lch
where
	Self: From<T> + From<U>,
{
	fn mix_polar(first: T, second: U, percentage: f64, hue_interpolation: HueInterpolation) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let l = premultiply_lerp(first.lightness, a1, second.lightness, a2, t, a);
		let c = premultiply_lerp(first.chroma, a1, second.chroma, a2, t, a);
		let h = interpolate_hue(first.hue, second.hue, t, hue_interpolation);
		Lch::new(l, c, h, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for Oklab
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let l = premultiply_lerp(first.lightness, a1, second.lightness, a2, t, a);
		let ab_a = premultiply_lerp(first.a, a1, second.a, a2, t, a);
		let ab_b = premultiply_lerp(first.b, a1, second.b, a2, t, a);
		Oklab::new(l, ab_a, ab_b, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMixPolar<T, U> for Oklch
where
	Self: From<T> + From<U>,
{
	fn mix_polar(first: T, second: U, percentage: f64, hue_interpolation: HueInterpolation) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let l = premultiply_lerp(first.lightness, a1, second.lightness, a2, t, a);
		let c = premultiply_lerp(first.chroma, a1, second.chroma, a2, t, a);
		let h = interpolate_hue(first.hue, second.hue, t, hue_interpolation);
		Oklch::new(l, c, h, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for XyzD50
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let x = premultiply_lerp(first.x, a1, second.x, a2, t, a);
		let y = premultiply_lerp(first.y, a1, second.y, a2, t, a);
		let z = premultiply_lerp(first.z, a1, second.z, a2, t, a);
		XyzD50::new(x, y, z, (a * 100.0) as f32)
	}
}

impl<T, U> ColorMix<T, U> for XyzD65
where
	Self: From<T> + From<U>,
{
	fn mix(first: T, second: U, percentage: f64) -> Self {
		let first: Self = first.into();
		let second: Self = second.into();
		let t = percentage / 100.0;
		let a1 = first.alpha as f64 / 100.0;
		let a2 = second.alpha as f64 / 100.0;
		let a = a1 * (1.0 - t) + a2 * t;
		let x = premultiply_lerp(first.x, a1, second.x, a2, t, a);
		let y = premultiply_lerp(first.y, a1, second.y, a2, t, a);
		let z = premultiply_lerp(first.z, a1, second.z, a2, t, a);
		XyzD65::new(x, y, z, (a * 100.0) as f32)
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::*;

	macro_rules! assert_close_to {
		($a: expr, $b: expr) => {
			assert!($a.close_to($b, COLOR_EPSILON), "Expected {:?} to be (closely) equal to {:?}", $a, $b);
		};
	}

	#[test]
	fn test_basic_mix() {
		let red = Srgb::new(255, 0, 0, 100.0);
		let blue = Srgb::new(0, 0, 255, 100.0);
		assert_close_to!(Srgb::mix(red, blue, 50.0), Srgb::new(128, 0, 128, 100.0));
	}

	#[test]
	fn test_mix_named_in_oklab() {
		assert_close_to!(
			Oklch::mix(Named::Rebeccapurple, Named::Hotpink, 50.0),
			Oklch::new(0.5842845967725198, 0.17868573405015944, 327.6838446374328, 100.0)
		);
	}

	#[test]
	fn test_mix_named_in_hsl_polar() {
		assert_close_to!(Hsl::mix(Named::Rebeccapurple, Named::Hotpink, 50.0), Hsl::new(300.0, 75.0, 55.294117, 100.0));
		assert_close_to!(
			Hsl::mix_polar(Named::Rebeccapurple, Named::Hotpink, 50.0, HueInterpolation::Longer),
			Hsl::new(120.0, 75.0, 55.294117, 100.0)
		);
		assert_close_to!(
			Hsl::mix_polar(Named::Rebeccapurple, Named::Hotpink, 50.0, HueInterpolation::Decreasing),
			Hsl::new(120.0, 75.0, 55.294117, 100.0)
		);
		assert_close_to!(
			Hsl::mix_polar(Named::Rebeccapurple, Named::Hotpink, 50.0, HueInterpolation::Increasing),
			Hsl::new(300.0, 75.0, 55.294117, 100.0)
		);
	}

	#[test]
	fn test_alpha_mixing() {
		let color1 = Srgb::new(255, 0, 0, 80.0);
		let color2 = Srgb::new(0, 0, 255, 40.0);

		let mixed = Srgb::mix(color1, color2, 50.0);
		assert_eq!(mixed.red, 170);
		assert_eq!(mixed.green, 0);
		assert_eq!(mixed.blue, 85);
		assert_eq!(mixed.alpha, 60.0);
	}

	#[test]
	fn test_hwb_mix() {
		// Hwb is polar - default mix uses Shorter hue interpolation
		// 0° to 240°: diff=240 > 180, so shorter wraps via 360°, midpoint is 300°
		let red = Hwb::new(0.0, 0.0, 0.0, 100.0);
		let blue = Hwb::new(240.0, 0.0, 0.0, 100.0);
		let mixed = Hwb::mix(red, blue, 50.0);
		assert_close_to!(mixed, Hwb::new(300.0, 0.0, 0.0, 100.0));
	}

	#[test]
	fn test_hwb_mix_polar() {
		// 0° to 240°: longer arc goes through 120°
		let red = Hwb::new(0.0, 0.0, 0.0, 100.0);
		let blue = Hwb::new(240.0, 0.0, 0.0, 100.0);
		assert_close_to!(Hwb::mix_polar(red, blue, 50.0, HueInterpolation::Longer), Hwb::new(120.0, 0.0, 0.0, 100.0));
	}

	#[test]
	fn test_a98_rgb_mix() {
		let c1 = A98Rgb::new(1.0, 0.0, 0.0, 100.0);
		let c2 = A98Rgb::new(0.0, 0.0, 1.0, 100.0);
		let mixed = A98Rgb::mix(c1, c2, 50.0);
		assert_close_to!(mixed, A98Rgb::new(0.5, 0.0, 0.5, 100.0));
	}

	/// WPT: color-mix(in lab, lab(10 20 30 / .4), lab(50 60 70 / .8)) → lab(36.666664 46.666664 56.666664 / 0.6)
	#[test]
	fn test_premultiplied_alpha_lab() {
		let c1 = Lab::new(10.0, 20.0, 30.0, 40.0);
		let c2 = Lab::new(50.0, 60.0, 70.0, 80.0);
		let mixed = Lab::mix(c1, c2, 50.0);
		assert_close_to!(mixed, Lab::new(36.666664, 46.666664, 56.666664, 60.0));
	}

	/// WPT: color-mix(in lab, lab(10 20 30 / .4) 25%, lab(50 60 70 / .8)) → lab(44.285713 54.285717 64.28571 / 0.7)
	#[test]
	fn test_premultiplied_alpha_lab_25_75() {
		let c1 = Lab::new(10.0, 20.0, 30.0, 40.0);
		let c2 = Lab::new(50.0, 60.0, 70.0, 80.0);
		// 25% first, 75% second: mix_percentage = 75
		let mixed = Lab::mix(c1, c2, 75.0);
		assert_close_to!(mixed, Lab::new(44.285713, 54.285717, 64.28571, 70.0));
	}

	/// WPT: color-mix(in oklch, oklch(0.1 0.2 30deg / .4), oklch(0.5 0.6 70deg / .8)) → oklch(0.36666664 0.46666664 50 / 0.6)
	#[test]
	fn test_premultiplied_alpha_oklch() {
		let c1 = Oklch::new(0.1, 0.2, 30.0, 40.0);
		let c2 = Oklch::new(0.5, 0.6, 70.0, 80.0);
		let mixed = Oklch::mix(c1, c2, 50.0);
		assert_close_to!(mixed, Oklch::new(0.36666664, 0.46666664, 50.0, 60.0));
	}

	/// When both alphas are 100%, premultiplied interpolation == simple interpolation.
	#[test]
	fn test_premultiplied_alpha_opaque_same_as_simple() {
		let c1 = Lab::new(10.0, 20.0, 30.0, 100.0);
		let c2 = Lab::new(50.0, 60.0, 70.0, 100.0);
		let mixed = Lab::mix(c1, c2, 50.0);
		assert_close_to!(mixed, Lab::new(30.0, 40.0, 50.0, 100.0));
	}
}