1use std::fmt::{self, Debug};
4
5use super::lerp::lerp_double_non_null;
6use super::math::clamp_double;
7
8fn scale_alpha(x: Color, factor: f64) -> Color {
9 x.with_values(
10 Some(clamp_double(x.a * factor, 0.0, 1.0)),
11 None,
12 None,
13 None,
14 None,
15 )
16}
17
18fn wider_color_space(a: ColorSpace, b: ColorSpace) -> ColorSpace {
19 if a == ColorSpace::DisplayP3 || b == ColorSpace::DisplayP3 {
20 ColorSpace::DisplayP3
21 } else {
22 a
23 }
24}
25
26#[derive(Clone, Copy, PartialEq)]
74pub struct Color {
75 pub a: f64,
77 pub r: f64,
79 pub g: f64,
81 pub b: f64,
83 pub color_space: ColorSpace,
85}
86
87impl Color {
88 pub const fn new(value: u32) -> Color {
110 Self::from_argbc(
111 (value >> 24) as i32,
112 (value >> 16) as i32,
113 (value >> 8) as i32,
114 value as i32,
115 ColorSpace::Srgb,
116 )
117 }
118
119 pub const fn from(
139 alpha: f64,
140 red: f64,
141 green: f64,
142 blue: f64,
143 color_space: ColorSpace,
144 ) -> Color {
145 Color {
146 a: alpha,
147 r: red,
148 g: green,
149 b: blue,
150 color_space,
151 }
152 }
153
154 pub const fn from_argb(a: i32, r: i32, g: i32, b: i32) -> Color {
172 Self::from_argbc(a, r, g, b, ColorSpace::Srgb)
173 }
174
175 const fn from_argbc(
176 alpha: i32,
177 red: i32,
178 green: i32,
179 blue: i32,
180 color_space: ColorSpace,
181 ) -> Color {
182 Self::from_rgboc(
183 red,
184 green,
185 blue,
186 ((alpha & 0xff) as f64) / 255.0,
187 color_space,
188 )
189 }
190
191 pub const fn from_rgbo(r: i32, g: i32, b: i32, opacity: f64) -> Color {
210 Self::from_rgboc(r, g, b, opacity, ColorSpace::Srgb)
211 }
212
213 const fn from_rgboc(r: i32, g: i32, b: i32, opacity: f64, color_space: ColorSpace) -> Color {
214 Color {
215 a: opacity,
216 r: ((r & 0xff) as f64) / 255.0,
217 g: ((g & 0xff) as f64) / 255.0,
218 b: ((b & 0xff) as f64) / 255.0,
219 color_space,
220 }
221 }
222
223 fn float_to_int8(x: f64) -> i32 {
224 (x * 255.0).round().clamp(0.0, 255.0) as i32
225 }
226
227 #[deprecated(
232 note = "Use component accessors like .r or .g, or to_argb32 for an explicit conversion"
233 )]
234 pub fn value(&self) -> u32 {
235 self.to_argb32()
236 }
237
238 pub fn to_argb32(&self) -> u32 {
267 #[allow(clippy::identity_op)]
268 {
269 ((Self::float_to_int8(self.a) as u32) << 24)
270 | ((Self::float_to_int8(self.r) as u32) << 16)
271 | ((Self::float_to_int8(self.g) as u32) << 8)
272 | ((Self::float_to_int8(self.b) as u32) << 0)
273 }
274 }
275
276 #[deprecated(note = "Use (*.a * 255.0).round().clamp(0, 255)")]
281 #[allow(deprecated)]
282 pub fn alpha(&self) -> i32 {
283 ((0xff000000 & self.value()) >> 24) as i32
284 }
285
286 #[deprecated(note = "Use .a.")]
291 #[allow(deprecated)]
292 pub fn opacity(&self) -> f64 {
293 (self.alpha() as f64) / (0xFF as f64)
294 }
295
296 #[deprecated(note = "Use (*.r * 255.0).round().clamp(0, 255)")]
298 #[allow(deprecated)]
299 pub fn red(&self) -> i32 {
300 ((0x00ff0000 & self.value()) >> 16) as i32
301 }
302
303 #[deprecated(note = "Use (*.g * 255.0).round().clamp(0, 255)")]
305 #[allow(deprecated)]
306 pub fn green(&self) -> i32 {
307 ((0x0000ff00 & self.value()) >> 8) as i32
308 }
309
310 #[deprecated(note = "Use (*.b * 255.0).round().clamp(0, 255)")]
312 #[allow(deprecated)]
313 pub fn blue(&self) -> i32 {
314 #[allow(clippy::identity_op)]
315 {
316 ((0x000000ff & self.value()) >> 0) as i32
317 }
318 }
319
320 pub fn with_values(
338 &self,
339 alpha: Option<f64>,
340 red: Option<f64>,
341 green: Option<f64>,
342 blue: Option<f64>,
343 color_space: Option<ColorSpace>,
344 ) -> Color {
345 let mut updated_components = None;
346 if alpha.is_some() || red.is_some() || green.is_some() || blue.is_some() {
347 updated_components = Some(Color::from(
348 alpha.unwrap_or(self.a),
349 red.unwrap_or(self.r),
350 green.unwrap_or(self.g),
351 blue.unwrap_or(self.b),
352 self.color_space,
353 ));
354 }
355 if let Some(color_space) = color_space
356 && color_space != self.color_space
357 {
358 let transform = get_color_transform(self.color_space, color_space);
359 transform.transform(updated_components.unwrap_or(*self), color_space)
360 } else {
361 updated_components.unwrap_or(*self)
362 }
363 }
364
365 #[allow(deprecated)]
370 pub fn with_alpha(&self, a: i32) -> Color {
371 Color::from_argb(a, self.red(), self.green(), self.blue())
372 }
373
374 #[deprecated(note = "Use .with_values() to avoid precision loss.")]
379 pub fn with_opacity(&self, opacity: f64) -> Color {
380 debug_assert!((0.0..=1.0).contains(&opacity));
381 self.with_alpha((255.0 * opacity).round() as i32)
382 }
383
384 #[allow(deprecated)]
389 pub fn with_red(&self, r: i32) -> Color {
390 Color::from_argb(self.alpha(), r, self.green(), self.blue())
391 }
392
393 #[allow(deprecated)]
398 pub fn with_green(&self, g: i32) -> Color {
399 Color::from_argb(self.alpha(), self.red(), g, self.blue())
400 }
401
402 #[allow(deprecated)]
407 pub fn with_blue(&self, b: i32) -> Color {
408 Color::from_argb(self.alpha(), self.red(), self.green(), b)
409 }
410
411 fn linearize_color_component(component: f64) -> f64 {
413 if component <= 0.03928 {
414 return component / 12.92;
415 }
416 ((component + 0.055) / 1.055).powf(2.4)
417 }
418
419 pub fn compute_luminance(&self) -> f64 {
426 debug_assert!(self.color_space != ColorSpace::ExtendedSrgb);
427 let r = Self::linearize_color_component(self.r);
429 let g = Self::linearize_color_component(self.g);
430 let b = Self::linearize_color_component(self.b);
431 0.2126 * r + 0.7152 * g + 0.0722 * b
432 }
433
434 pub fn lerp(x: Option<Color>, y: Option<Color>, t: f64) -> Option<Color> {
462 debug_assert!(x.is_none_or(|c| c.color_space != ColorSpace::ExtendedSrgb));
463 debug_assert!(y.is_none_or(|c| c.color_space != ColorSpace::ExtendedSrgb));
464 match (x, y) {
465 (None, None) => None,
466 (Some(x), None) => Some(scale_alpha(x, 1.0 - t)),
467 (None, Some(y)) => Some(scale_alpha(y, t)),
468 (Some(x), Some(y)) => {
469 let a;
470 let b;
471 let result_color_space;
472 if x.color_space == y.color_space {
473 a = x;
474 b = y;
475 result_color_space = x.color_space;
476 } else {
477 result_color_space = wider_color_space(x.color_space, y.color_space);
478 a = x.with_values(None, None, None, None, Some(result_color_space));
479 b = y.with_values(None, None, None, None, Some(result_color_space));
480 }
481 Some(Color::from(
482 clamp_double(lerp_double_non_null(a.a, b.a, t), 0.0, 1.0),
483 clamp_double(lerp_double_non_null(a.r, b.r, t), 0.0, 1.0),
484 clamp_double(lerp_double_non_null(a.g, b.g, t), 0.0, 1.0),
485 clamp_double(lerp_double_non_null(a.b, b.b, t), 0.0, 1.0),
486 result_color_space,
487 ))
488 }
489 }
490 }
491
492 pub fn alpha_blend(foreground: Color, background: Color) -> Color {
501 debug_assert!(foreground.color_space == background.color_space);
502 debug_assert!(foreground.color_space != ColorSpace::ExtendedSrgb);
503 let alpha = foreground.a;
504 if alpha == 0.0 {
505 return background;
507 }
508 let inv_alpha = 1.0 - alpha;
509 let mut back_alpha = background.a;
510 if back_alpha == 1.0 {
511 Color::from(
513 1.0,
514 alpha * foreground.r + inv_alpha * background.r,
515 alpha * foreground.g + inv_alpha * background.g,
516 alpha * foreground.b + inv_alpha * background.b,
517 foreground.color_space,
518 )
519 } else {
520 back_alpha *= inv_alpha;
522 let out_alpha = alpha + back_alpha;
523 debug_assert!(out_alpha != 0.0);
524 Color::from(
525 out_alpha,
526 (foreground.r * alpha + background.r * back_alpha) / out_alpha,
527 (foreground.g * alpha + background.g * back_alpha) / out_alpha,
528 (foreground.b * alpha + background.b * back_alpha) / out_alpha,
529 foreground.color_space,
530 )
531 }
532 }
533
534 pub fn get_alpha_from_opacity(opacity: f64) -> i32 {
538 (clamp_double(opacity, 0.0, 1.0) * 255.0).round() as i32
539 }
540}
541
542impl Debug for Color {
543 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
544 write!(
545 f,
546 "Color(alpha: {:.4}, red: {:.4}, green: {:.4}, blue: {:.4}, colorSpace: {:?})",
547 self.a, self.r, self.g, self.b, self.color_space
548 )
549 }
550}
551
552#[derive(Clone, Copy, PartialEq, Eq)]
563pub enum ColorSpace {
564 Srgb,
571
572 ExtendedSrgb,
577
578 DisplayP3,
586}
587
588impl Debug for ColorSpace {
589 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
590 match self {
591 ColorSpace::Srgb => write!(f, "ColorSpace.sRGB"),
592 ColorSpace::ExtendedSrgb => write!(f, "ColorSpace.extendedSRGB"),
593 ColorSpace::DisplayP3 => write!(f, "ColorSpace.displayP3"),
594 }
595 }
596}
597
598enum ColorTransform {
599 Identity,
600 Clamp,
601 P3ToSrgb,
602 SrgbToP3,
603}
604
605impl ColorTransform {
606 fn transform(self, color: Color, result_color_space: ColorSpace) -> Color {
607 match self {
608 ColorTransform::Identity => color,
609 ColorTransform::Clamp => Color::from(
610 clamp_double(color.a, 0.0, 1.0),
611 clamp_double(color.r, 0.0, 1.0),
612 clamp_double(color.g, 0.0, 1.0),
613 clamp_double(color.b, 0.0, 1.0),
614 result_color_space,
615 ),
616 ColorTransform::P3ToSrgb => {
617 let r_lin = srgb_eotf_extended(color.r);
618 let g_lin = srgb_eotf_extended(color.g);
619 let b_lin = srgb_eotf_extended(color.b);
620
621 let r_out = K_P3_TO_SRGB_LINEAR[0] * r_lin
622 + K_P3_TO_SRGB_LINEAR[1] * g_lin
623 + K_P3_TO_SRGB_LINEAR[2] * b_lin;
624 let g_out = K_P3_TO_SRGB_LINEAR[3] * r_lin
625 + K_P3_TO_SRGB_LINEAR[4] * g_lin
626 + K_P3_TO_SRGB_LINEAR[5] * b_lin;
627 let b_out = K_P3_TO_SRGB_LINEAR[6] * r_lin
628 + K_P3_TO_SRGB_LINEAR[7] * g_lin
629 + K_P3_TO_SRGB_LINEAR[8] * b_lin;
630
631 Color::from(
632 color.a,
633 srgb_oetf_extended(r_out),
634 srgb_oetf_extended(g_out),
635 srgb_oetf_extended(b_out),
636 result_color_space,
637 )
638 }
639 ColorTransform::SrgbToP3 => {
640 let r_lin = srgb_eotf_extended(color.r);
641 let g_lin = srgb_eotf_extended(color.g);
642 let b_lin = srgb_eotf_extended(color.b);
643
644 let r_out = K_SRGB_TO_P3_LINEAR[0] * r_lin
645 + K_SRGB_TO_P3_LINEAR[1] * g_lin
646 + K_SRGB_TO_P3_LINEAR[2] * b_lin;
647 let g_out = K_SRGB_TO_P3_LINEAR[3] * r_lin
648 + K_SRGB_TO_P3_LINEAR[4] * g_lin
649 + K_SRGB_TO_P3_LINEAR[5] * b_lin;
650 let b_out = K_SRGB_TO_P3_LINEAR[6] * r_lin
651 + K_SRGB_TO_P3_LINEAR[7] * g_lin
652 + K_SRGB_TO_P3_LINEAR[8] * b_lin;
653
654 Color::from(
655 color.a,
656 srgb_oetf_extended(r_out),
657 srgb_oetf_extended(g_out),
658 srgb_oetf_extended(b_out),
659 result_color_space,
660 )
661 }
662 }
663 }
664}
665
666const K_SRGB_GAMMA: f64 = 2.4;
669const K_SRGB_LINEAR_THRESHOLD: f64 = 0.04045;
670const K_SRGB_LINEAR_SLOPE: f64 = 12.92;
671const K_SRGB_ENCODED_OFFSET: f64 = 0.055;
672const K_SRGB_ENCODED_DIVISOR: f64 = 1.055;
673const K_SRGB_LINEAR_TO_ENCODED_THRESHOLD: f64 = 0.0031308;
674
675fn srgb_eotf(v: f64) -> f64 {
677 if v <= K_SRGB_LINEAR_THRESHOLD {
678 v / K_SRGB_LINEAR_SLOPE
679 } else {
680 ((v + K_SRGB_ENCODED_OFFSET) / K_SRGB_ENCODED_DIVISOR).powf(K_SRGB_GAMMA)
681 }
682}
683
684fn srgb_oetf(v: f64) -> f64 {
686 if v <= K_SRGB_LINEAR_TO_ENCODED_THRESHOLD {
687 v * K_SRGB_LINEAR_SLOPE
688 } else {
689 K_SRGB_ENCODED_DIVISOR * v.powf(1.0 / K_SRGB_GAMMA) - K_SRGB_ENCODED_OFFSET
690 }
691}
692
693fn srgb_eotf_extended(v: f64) -> f64 {
695 if v < 0.0 {
696 -srgb_eotf(-v)
697 } else {
698 srgb_eotf(v)
699 }
700}
701
702fn srgb_oetf_extended(v: f64) -> f64 {
703 if v < 0.0 {
704 -srgb_oetf(-v)
705 } else {
706 srgb_oetf(v)
707 }
708}
709
710const K_P3_TO_SRGB_LINEAR: [f64; 9] = [
713 1.2249401, -0.2249402, 0.0, -0.0420569, 1.0420571, 0.0, -0.0196376, -0.0786507, 1.0982884,
714];
715
716const K_SRGB_TO_P3_LINEAR: [f64; 9] = [
718 0.8224622, 0.1775380, 0.0, 0.0331942, 0.9668058, 0.0, 0.0170806, 0.0723974, 0.9105220,
719];
720
721fn get_color_transform(source: ColorSpace, destination: ColorSpace) -> ColorTransform {
722 match source {
723 ColorSpace::Srgb => match destination {
724 ColorSpace::Srgb => ColorTransform::Identity,
725 ColorSpace::ExtendedSrgb => ColorTransform::Identity,
726 ColorSpace::DisplayP3 => ColorTransform::SrgbToP3,
727 },
728 ColorSpace::ExtendedSrgb => match destination {
729 ColorSpace::Srgb => ColorTransform::Clamp,
730 ColorSpace::ExtendedSrgb => ColorTransform::Identity,
731 ColorSpace::DisplayP3 => ColorTransform::Clamp,
732 },
733 ColorSpace::DisplayP3 => match destination {
734 ColorSpace::Srgb => ColorTransform::Clamp,
735 ColorSpace::ExtendedSrgb => ColorTransform::P3ToSrgb,
736 ColorSpace::DisplayP3 => ColorTransform::Identity,
737 },
738 }
739}
740
741impl std::fmt::Display for Color {
743 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
744 std::fmt::Debug::fmt(self, f)
745 }
746}
747
748#[cfg(test)]
749mod tests {
750 use super::*;
751
752 fn color_matches(actual: Color, expected: Color, threshold: f64) -> bool {
753 actual.color_space == expected.color_space
754 && (actual.a - expected.a).abs() <= threshold
755 && (actual.r - expected.r).abs() <= threshold
756 && (actual.g - expected.g).abs() <= threshold
757 && (actual.b - expected.b).abs() <= threshold
758 }
759
760 #[test]
762 fn packed_constructor_from_argb_and_from_agree() {
763 let packed = Color::new(0xFF42A5F5);
764 let from_argb = Color::from_argb(0xFF, 0x42, 0xA5, 0xF5);
765 let from = Color::from(
766 1.0,
767 0x42 as f64 / 255.0,
768 0xA5 as f64 / 255.0,
769 0xF5 as f64 / 255.0,
770 ColorSpace::Srgb,
771 );
772 let from_rgbo = Color::from_rgbo(0x42, 0xA5, 0xF5, 1.0);
773
774 assert_eq!(packed, from_argb);
775 assert_eq!(packed, from);
776 assert_eq!(packed, from_rgbo);
777 assert_eq!(packed.to_argb32(), 0xFF42A5F5);
778 assert_eq!(packed.a, 1.0);
779 assert_eq!(packed.color_space, ColorSpace::Srgb);
780 }
781
782 #[test]
783 #[allow(deprecated)]
784 fn color_accessors_should_work() {
785 let foo = Color::new(0x12345678);
786 assert_eq!(foo.alpha(), 0x12);
787 assert_eq!(foo.red(), 0x34);
788 assert_eq!(foo.green(), 0x56);
789 assert_eq!(foo.blue(), 0x78);
790 }
791
792 #[test]
793 fn two_colors_are_equal_when_components_and_space_match() {
794 assert_eq!(Color::new(0x12345678), Color::new(0x12345678));
795 assert_ne!(Color::new(0x12345678), Color::new(0x87654321));
796 let srgb = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::Srgb);
797 let p3 = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::DisplayP3);
798 assert_ne!(srgb, p3);
799 }
800
801 #[test]
802 fn lerp_walks_the_line_and_clamps_extrapolation() {
803 assert_eq!(
804 Color::lerp(
805 Some(Color::new(0x00000000)),
806 Some(Color::new(0xFFFFFFFF)),
807 0.0
808 ),
809 Some(Color::new(0x00000000))
810 );
811 assert!(color_matches(
812 Color::lerp(
813 Some(Color::new(0x00000000)),
814 Some(Color::new(0xFFFFFFFF)),
815 0.5
816 )
817 .unwrap(),
818 Color::new(0x7F7F7F7F),
819 1.0 / 255.0
820 ));
821 assert_eq!(
822 Color::lerp(
823 Some(Color::new(0x00000000)),
824 Some(Color::new(0xFFFFFFFF)),
825 1.0
826 ),
827 Some(Color::new(0xFFFFFFFF))
828 );
829 assert_eq!(
830 Color::lerp(
831 Some(Color::new(0x00000000)),
832 Some(Color::new(0xFFFFFFFF)),
833 -0.1
834 ),
835 Some(Color::new(0x00000000))
836 );
837 assert_eq!(
838 Color::lerp(
839 Some(Color::new(0x00000000)),
840 Some(Color::new(0xFFFFFFFF)),
841 1.1
842 ),
843 Some(Color::new(0xFFFFFFFF))
844 );
845
846 assert_eq!(
848 Color::lerp(
849 Some(Color::new(0xFFFFFFFF)),
850 Some(Color::new(0xFFFFFFFF)),
851 0.04
852 ),
853 Some(Color::new(0xFFFFFFFF))
854 );
855
856 assert_eq!(Color::lerp(None, None, 0.5), None);
857 assert_eq!(
858 Color::lerp(Some(Color::new(0xFFFFFFFF)), None, 0.0),
859 Some(Color::new(0xFFFFFFFF))
860 );
861 }
862
863 #[test]
864 fn lerp_same_color_spaces() {
865 assert_eq!(
866 Color::lerp(
867 Some(Color::from(1.0, 0.0, 0.0, 0.0, ColorSpace::DisplayP3)),
868 Some(Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::DisplayP3)),
869 0.2,
870 ),
871 Some(Color::from(1.0, 0.2, 0.0, 0.0, ColorSpace::DisplayP3))
872 );
873 }
874
875 #[test]
876 fn lerp_mixed_color_spaces_produces_display_p3() {
877 let result = Color::lerp(
878 Some(Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::DisplayP3)),
879 Some(Color::from(1.0, 0.0, 0.0, 1.0, ColorSpace::Srgb)),
880 0.0,
881 )
882 .unwrap();
883 assert_eq!(result.color_space, ColorSpace::DisplayP3);
884 }
885
886 #[test]
887 fn lerp_mixed_color_spaces_srgb_x_and_display_p3_y_at_t_0() {
888 let result = Color::lerp(
889 Some(Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::Srgb)),
890 Some(Color::from(1.0, 0.0, 1.0, 0.0, ColorSpace::DisplayP3)),
891 0.0,
892 )
893 .unwrap();
894 assert_eq!(result.color_space, ColorSpace::DisplayP3);
895 let expected_p3 = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::Srgb).with_values(
896 None,
897 None,
898 None,
899 None,
900 Some(ColorSpace::DisplayP3),
901 );
902 assert!(color_matches(result, expected_p3, 1.0 / 255.0));
903 }
904
905 #[test]
906 fn lerp_mixed_color_spaces_at_midpoint() {
907 let srgb_red = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::Srgb);
908 let p3_green = Color::from(1.0, 0.0, 1.0, 0.0, ColorSpace::DisplayP3);
909 let result = Color::lerp(Some(srgb_red), Some(p3_green), 0.5).unwrap();
910 assert_eq!(result.color_space, ColorSpace::DisplayP3);
911 let srgb_red_as_p3 =
912 srgb_red.with_values(None, None, None, None, Some(ColorSpace::DisplayP3));
913 let expected = Color::from(
914 (srgb_red_as_p3.a + p3_green.a) / 2.0,
915 (srgb_red_as_p3.r + p3_green.r) / 2.0,
916 (srgb_red_as_p3.g + p3_green.g) / 2.0,
917 (srgb_red_as_p3.b + p3_green.b) / 2.0,
918 ColorSpace::DisplayP3,
919 );
920 assert!(color_matches(result, expected, 1e-4));
921 }
922
923 #[test]
924 fn alpha_blend_matches_src_over_dst() {
925 assert_eq!(
926 Color::alpha_blend(Color::new(0x00000000), Color::new(0x00000000)),
927 Color::new(0x00000000)
928 );
929 assert_eq!(
930 Color::alpha_blend(Color::new(0x00000000), Color::new(0xFFFFFFFF)),
931 Color::new(0xFFFFFFFF)
932 );
933 assert_eq!(
934 Color::alpha_blend(Color::new(0xFFFFFFFF), Color::new(0x00000000)),
935 Color::new(0xFFFFFFFF)
936 );
937 assert_eq!(
938 Color::alpha_blend(Color::new(0xFFFFFFFF), Color::new(0xFFFFFFFF)),
939 Color::new(0xFFFFFFFF)
940 );
941 assert_eq!(
942 Color::alpha_blend(Color::new(0x80FFFFFF), Color::new(0xFF000000)),
943 Color::new(0xFF808080)
944 );
945 assert!(color_matches(
946 Color::alpha_blend(Color::new(0x80808080), Color::new(0xFFFFFFFF)),
947 Color::new(0xFFBFBFBF),
948 1.0 / 255.0
949 ));
950 assert!(color_matches(
951 Color::alpha_blend(Color::new(0x80808080), Color::new(0xFF000000)),
952 Color::new(0xFF404040),
953 1.0 / 255.0
954 ));
955 assert_eq!(
956 Color::alpha_blend(
957 Color::from(0.5, 1.0, 1.0, 1.0, ColorSpace::DisplayP3),
958 Color::from(1.0, 0.0, 0.0, 0.0, ColorSpace::DisplayP3),
959 ),
960 Color::from(1.0, 0.5, 0.5, 0.5, ColorSpace::DisplayP3)
961 );
962 }
963
964 #[test]
965 fn compute_gray_luminance() {
966 let light_gray = Color::new(0xFF333333);
968 assert_eq!(light_gray.compute_luminance(), 0.033104766570885055);
971 }
972
973 #[test]
974 fn compute_color_luminance() {
975 let bright_red = Color::new(0xFFFF3B30);
976 assert_eq!(bright_red.compute_luminance(), 0.24601329637099723);
980 }
981
982 #[test]
983 #[allow(deprecated)]
984 fn from_and_accessors() {
985 let color = Color::from(0.1, 0.2, 0.3, 0.4, ColorSpace::Srgb);
986 assert_eq!(color.a, 0.1);
987 assert_eq!(color.r, 0.2);
988 assert_eq!(color.g, 0.3);
989 assert_eq!(color.b, 0.4);
990 assert_eq!(color.color_space, ColorSpace::Srgb);
991
992 assert_eq!(color.alpha(), 26);
993 assert_eq!(color.red(), 51);
994 assert_eq!(color.green(), 77);
995 assert_eq!(color.blue(), 102);
996
997 assert_eq!(color.value(), 0x1a334d66);
998 assert_eq!(color.to_argb32(), 0x1a334d66);
999 }
1000
1001 #[test]
1002 #[allow(deprecated)]
1003 fn from_argb_and_accessors() {
1004 let color = Color::from_argb(10, 20, 35, 47);
1005 assert_eq!(color.alpha(), 10);
1006 assert_eq!(color.red(), 20);
1007 assert_eq!(color.green(), 35);
1008 assert_eq!(color.blue(), 47);
1009 }
1010
1011 #[test]
1012 #[allow(deprecated)]
1013 fn constructor_and_accessors() {
1014 let color = Color::new(0xffeeddcc);
1015 assert_eq!(color.alpha(), 0xff);
1016 assert_eq!(color.red(), 0xee);
1017 assert_eq!(color.green(), 0xdd);
1018 assert_eq!(color.blue(), 0xcc);
1019 }
1020
1021 #[test]
1022 fn p3_to_extended_srgb() {
1023 let p3 = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::DisplayP3);
1024 let srgb = p3.with_values(None, None, None, None, Some(ColorSpace::ExtendedSrgb));
1025 assert_eq!(srgb.a, 1.0);
1026 assert!((srgb.r - 1.0931).abs() < 1e-4);
1027 assert!((srgb.g - -0.22684034705162098).abs() < 1e-4);
1028 assert!((srgb.b - -0.15007957816123998).abs() < 1e-4);
1029 assert_eq!(srgb.color_space, ColorSpace::ExtendedSrgb);
1030 }
1031
1032 #[test]
1033 fn p3_to_srgb() {
1034 let p3 = Color::from(1.0, 1.0, 0.0, 0.0, ColorSpace::DisplayP3);
1035 let srgb = p3.with_values(None, None, None, None, Some(ColorSpace::Srgb));
1036 assert_eq!(srgb.a, 1.0);
1037 assert!((srgb.r - 1.0).abs() < 1e-4);
1038 assert!((srgb.g - 0.0).abs() < 1e-4);
1039 assert!((srgb.b - 0.0).abs() < 1e-4);
1040 assert_eq!(srgb.color_space, ColorSpace::Srgb);
1041 }
1042
1043 #[test]
1044 fn extended_srgb_to_p3() {
1045 let srgb = Color::from(1.0, 1.0931, -0.2268, -0.1501, ColorSpace::ExtendedSrgb);
1046 let p3 = srgb.with_values(None, None, None, None, Some(ColorSpace::DisplayP3));
1047 assert_eq!(p3.a, 1.0);
1048 assert!((p3.r - 1.0).abs() < 1e-4);
1049 assert!((p3.g - 0.0).abs() < 1e-4);
1050 assert!((p3.b - 0.0).abs() < 1e-4);
1051 assert_eq!(p3.color_space, ColorSpace::DisplayP3);
1052 }
1053
1054 #[test]
1055 fn extended_srgb_to_p3_clamped() {
1056 let srgb = Color::from(1.0, 2.0, 0.0, 0.0, ColorSpace::ExtendedSrgb);
1057 let p3 = srgb.with_values(None, None, None, None, Some(ColorSpace::DisplayP3));
1058 assert_eq!(srgb.a, 1.0);
1059 assert!(p3.r <= 1.0);
1060 assert!(p3.g <= 1.0);
1061 assert!(p3.b <= 1.0);
1062 assert!(p3.r >= 0.0);
1063 assert!(p3.g >= 0.0);
1064 assert!(p3.b >= 0.0);
1065 }
1066
1067 #[test]
1068 fn debug_matches_darts_to_string() {
1069 let color = Color::new(0xFF42A5F5);
1070 assert_eq!(
1071 format!("{color:?}"),
1072 "Color(alpha: 1.0000, red: 0.2588, green: 0.6471, blue: 0.9608, colorSpace: ColorSpace.sRGB)"
1073 );
1074 }
1075}