1pub use self::{
355 from_into_color::*, from_into_color_mut::*, from_into_color_unclamped::*,
356 from_into_color_unclamped_mut::*, try_from_into_color::*,
357};
358
359mod from_into_color;
360mod from_into_color_mut;
361mod from_into_color_unclamped;
362mod from_into_color_unclamped_mut;
363mod try_from_into_color;
364
365#[cfg(test)]
366mod tests {
367 use core::marker::PhantomData;
368
369 use super::{FromColor, FromColorUnclamped, IntoColor};
370 use crate::{
371 bool_mask::{BoolMask, HasBoolMask},
372 encoding::linear::Linear,
373 luma::{Luma, LumaStandard},
374 num::{One, Zero},
375 rgb::{Rgb, RgbSpace},
376 Alpha, Clamp, Hsl, Hsluv, Hsv, Hwb, IsWithinBounds, Lab, Lch, Luv, Xyz, Yxy,
377 };
378
379 #[derive(FromColorUnclamped, WithAlpha)]
380 #[palette(
381 skip_derives(Xyz, Luma),
382 component = "f64",
383 rgb_standard = "Linear<S>",
384 palette_internal,
385 palette_internal_not_base_type
386 )]
387 struct WithXyz<S>(PhantomData<S>);
388
389 impl<S> Clone for WithXyz<S> {
390 fn clone(&self) -> Self {
391 *self
392 }
393 }
394
395 impl<S> Copy for WithXyz<S> {}
396
397 impl<S> HasBoolMask for WithXyz<S> {
398 type Mask = bool;
399 }
400
401 impl<S> IsWithinBounds for WithXyz<S> {
402 fn is_within_bounds(&self) -> bool {
403 true
404 }
405 }
406
407 impl<S> Clamp for WithXyz<S> {
408 fn clamp(self) -> Self {
409 self
410 }
411 }
412
413 impl<S1, S2> FromColorUnclamped<WithXyz<S2>> for WithXyz<S1>
414 where
415 S1: RgbSpace,
416 S2: RgbSpace<WhitePoint = S1::WhitePoint>,
417 {
418 fn from_color_unclamped(_color: WithXyz<S2>) -> Self {
419 WithXyz(PhantomData)
420 }
421 }
422
423 impl<S: RgbSpace> FromColorUnclamped<Xyz<S::WhitePoint, f64>> for WithXyz<S> {
424 fn from_color_unclamped(_color: Xyz<S::WhitePoint, f64>) -> Self {
425 WithXyz(PhantomData)
426 }
427 }
428
429 impl<S: RgbSpace> FromColorUnclamped<WithXyz<S>> for Xyz<S::WhitePoint, f64> {
430 fn from_color_unclamped(_color: WithXyz<S>) -> Xyz<S::WhitePoint, f64> {
431 Xyz::new(0.0, 1.0, 0.0)
432 }
433 }
434
435 impl<Rs: RgbSpace, Ls: LumaStandard<WhitePoint = Rs::WhitePoint>>
436 FromColorUnclamped<Luma<Ls, f64>> for WithXyz<Rs>
437 {
438 fn from_color_unclamped(_color: Luma<Ls, f64>) -> Self {
439 WithXyz(PhantomData)
440 }
441 }
442
443 impl<Rs: RgbSpace, Ls: LumaStandard<WhitePoint = Rs::WhitePoint>>
444 FromColorUnclamped<WithXyz<Rs>> for Luma<Ls, f64>
445 {
446 fn from_color_unclamped(_color: WithXyz<Rs>) -> Self {
447 Luma::new(1.0)
448 }
449 }
450
451 #[derive(Copy, Clone, FromColorUnclamped, WithAlpha)]
452 #[palette(
453 skip_derives(Lch, Luma),
454 white_point = "crate::white_point::E",
455 component = "T",
456 rgb_standard = "Linear<(crate::encoding::Srgb, crate::white_point::E)>",
457 palette_internal,
458 palette_internal_not_base_type
459 )]
460 struct WithoutXyz<T>(PhantomData<T>);
461
462 impl<T> HasBoolMask for WithoutXyz<T>
463 where
464 T: HasBoolMask,
465 {
466 type Mask = T::Mask;
467 }
468
469 impl<T> IsWithinBounds for WithoutXyz<T>
470 where
471 T: HasBoolMask,
472 {
473 fn is_within_bounds(&self) -> T::Mask {
474 T::Mask::from_bool(true)
475 }
476 }
477
478 impl<T> Clamp for WithoutXyz<T> {
479 fn clamp(self) -> Self {
480 self
481 }
482 }
483
484 impl<T> FromColorUnclamped<WithoutXyz<T>> for WithoutXyz<T> {
485 fn from_color_unclamped(color: WithoutXyz<T>) -> Self {
486 color
487 }
488 }
489
490 impl<T> FromColorUnclamped<Lch<crate::white_point::E, T>> for WithoutXyz<T> {
491 fn from_color_unclamped(_color: Lch<crate::white_point::E, T>) -> Self {
492 WithoutXyz(PhantomData)
493 }
494 }
495
496 impl<T: One + Zero> FromColorUnclamped<WithoutXyz<T>> for Lch<crate::white_point::E, T> {
497 fn from_color_unclamped(_color: WithoutXyz<T>) -> Lch<crate::white_point::E, T> {
498 Lch::new(T::one(), T::zero(), T::zero())
499 }
500 }
501
502 impl<T> FromColorUnclamped<Luma<Linear<crate::white_point::E>, T>> for WithoutXyz<T> {
503 fn from_color_unclamped(_color: Luma<Linear<crate::white_point::E>, T>) -> Self {
504 WithoutXyz(PhantomData)
505 }
506 }
507
508 impl<T: One> FromColorUnclamped<WithoutXyz<T>> for Luma<Linear<crate::white_point::E>, T> {
509 fn from_color_unclamped(_color: WithoutXyz<T>) -> Luma<Linear<crate::white_point::E>, T> {
510 Luma::new(T::one())
511 }
512 }
513
514 #[test]
515 fn from_with_xyz() {
516 let color: WithXyz<crate::encoding::Srgb> = WithXyz(Default::default());
517 let _ = WithXyz::<crate::encoding::Srgb>::from_color(color);
518
519 let xyz: Xyz<_, f64> = Default::default();
520 let _ = WithXyz::<crate::encoding::Srgb>::from_color(xyz);
521
522 let yxy: Yxy<_, f64> = Default::default();
523 let _ = WithXyz::<crate::encoding::Srgb>::from_color(yxy);
524
525 let lab: Lab<_, f64> = Default::default();
526 let _ = WithXyz::<crate::encoding::Srgb>::from_color(lab);
527
528 let lch: Lch<_, f64> = Default::default();
529 let _ = WithXyz::<crate::encoding::Srgb>::from_color(lch);
530
531 let luv: Hsl<_, f64> = Default::default();
532 let _ = WithXyz::<crate::encoding::Srgb>::from_color(luv);
533
534 let rgb: Rgb<_, f64> = Default::default();
535 let _ = WithXyz::<crate::encoding::Srgb>::from_color(rgb);
536
537 let hsl: Hsl<_, f64> = Default::default();
538 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsl);
539
540 let hsluv: Hsluv<_, f64> = Default::default();
541 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsluv);
542
543 let hsv: Hsv<_, f64> = Default::default();
544 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsv);
545
546 let hwb: Hwb<_, f64> = Default::default();
547 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hwb);
548
549 let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
550 let _ = WithXyz::<crate::encoding::Srgb>::from_color(luma);
551 }
552
553 #[test]
554 fn from_with_xyz_alpha() {
555 let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
556 Alpha::from(WithXyz(Default::default()));
557 let _ = WithXyz::<crate::encoding::Srgb>::from_color(color);
558
559 let xyz: Alpha<Xyz<_, f64>, u8> = Alpha::from(Xyz::default());
560 let _ = WithXyz::<crate::encoding::Srgb>::from_color(xyz);
561
562 let yxy: Alpha<Yxy<_, f64>, u8> = Alpha::from(Yxy::default());
563 let _ = WithXyz::<crate::encoding::Srgb>::from_color(yxy);
564
565 let lab: Alpha<Lab<_, f64>, u8> = Alpha::from(Lab::default());
566 let _ = WithXyz::<crate::encoding::Srgb>::from_color(lab);
567
568 let lch: Alpha<Lch<_, f64>, u8> = Alpha::from(Lch::default());
569 let _ = WithXyz::<crate::encoding::Srgb>::from_color(lch);
570
571 let luv: Alpha<Luv<_, f64>, u8> = Alpha::from(Luv::default());
572 let _ = WithXyz::<crate::encoding::Srgb>::from_color(luv);
573
574 let rgb: Alpha<Rgb<_, f64>, u8> = Alpha::from(Rgb::default());
575 let _ = WithXyz::<crate::encoding::Srgb>::from_color(rgb);
576
577 let hsl: Alpha<Hsl<_, f64>, u8> = Alpha::from(Hsl::default());
578 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsl);
579
580 let hsluv: Alpha<Hsluv<_, f64>, u8> = Alpha::from(Hsluv::default());
581 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsluv);
582
583 let hsv: Alpha<Hsv<_, f64>, u8> = Alpha::from(Hsv::default());
584 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hsv);
585
586 let hwb: Alpha<Hwb<_, f64>, u8> = Alpha::from(Hwb::default());
587 let _ = WithXyz::<crate::encoding::Srgb>::from_color(hwb);
588
589 let luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> =
590 Alpha::from(Luma::<crate::encoding::Srgb, f64>::default());
591 let _ = WithXyz::<crate::encoding::Srgb>::from_color(luma);
592 }
593
594 #[test]
595 fn from_with_xyz_into_alpha() {
596 let color: WithXyz<crate::encoding::Srgb> = WithXyz(Default::default());
597 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(color);
598
599 let xyz: Xyz<_, f64> = Default::default();
600 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(xyz);
601
602 let yxy: Yxy<_, f64> = Default::default();
603 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(yxy);
604
605 let lab: Lab<_, f64> = Default::default();
606 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lab);
607
608 let lch: Lch<_, f64> = Default::default();
609 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lch);
610
611 let luv: Hsl<_, f64> = Default::default();
612 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luv);
613
614 let rgb: Rgb<_, f64> = Default::default();
615 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(rgb);
616
617 let hsl: Hsl<_, f64> = Default::default();
618 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsl);
619
620 let hsluv: Hsluv<_, f64> = Default::default();
621 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsluv);
622
623 let hsv: Hsv<_, f64> = Default::default();
624 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsv);
625
626 let hwb: Hwb<_, f64> = Default::default();
627 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hwb);
628
629 let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
630 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luma);
631 }
632
633 #[test]
634 fn from_with_xyz_alpha_into_alpha() {
635 let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
636 Alpha::from(WithXyz(Default::default()));
637 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(color);
638
639 let xyz: Xyz<_, f64> = Default::default();
640 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(xyz);
641
642 let yxy: Yxy<_, f64> = Default::default();
643 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(yxy);
644
645 let lab: Lab<_, f64> = Default::default();
646 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lab);
647
648 let lch: Lch<_, f64> = Default::default();
649 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lch);
650
651 let luv: Luv<_, f64> = Default::default();
652 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luv);
653
654 let rgb: Rgb<_, f64> = Default::default();
655 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(rgb);
656
657 let hsl: Hsl<_, f64> = Default::default();
658 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsl);
659
660 let hsluv: Hsluv<_, f64> = Default::default();
661 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsluv);
662
663 let hsv: Hsv<_, f64> = Default::default();
664 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsv);
665
666 let hwb: Hwb<_, f64> = Default::default();
667 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hwb);
668
669 let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
670 let _ = Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luma);
671 }
672
673 #[test]
674 fn into_from_with_xyz() {
675 let color = WithXyz::<crate::encoding::Srgb>(PhantomData);
676
677 let _self: WithXyz<crate::encoding::Srgb> = color.into_color();
678 let _xyz: Xyz<_, f64> = color.into_color();
679 let _yxy: Yxy<_, f64> = color.into_color();
680 let _lab: Lab<_, f64> = color.into_color();
681 let _lch: Lch<_, f64> = color.into_color();
682 let _luv: Luv<_, f64> = color.into_color();
683 let _rgb: Rgb<_, f64> = color.into_color();
684 let _hsl: Hsl<_, f64> = color.into_color();
685 let _hsluv: Hsluv<_, f64> = color.into_color();
686 let _hsv: Hsv<_, f64> = color.into_color();
687 let _hwb: Hwb<_, f64> = color.into_color();
688 let _luma: Luma<crate::encoding::Srgb, f64> = color.into_color();
689 }
690
691 #[test]
692 fn into_from_with_xyz_alpha() {
693 let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
694 Alpha::from(WithXyz::<crate::encoding::Srgb>(PhantomData));
695
696 let _self: WithXyz<crate::encoding::Srgb> = color.into_color();
697 let _xyz: Xyz<_, f64> = color.into_color();
698 let _yxy: Yxy<_, f64> = color.into_color();
699 let _lab: Lab<_, f64> = color.into_color();
700 let _lch: Lch<_, f64> = color.into_color();
701 let _luv: Luv<_, f64> = color.into_color();
702 let _rgb: Rgb<_, f64> = color.into_color();
703 let _hsl: Hsl<_, f64> = color.into_color();
704 let _hsluv: Hsluv<_, f64> = color.into_color();
705 let _hsv: Hsv<_, f64> = color.into_color();
706 let _hwb: Hwb<_, f64> = color.into_color();
707 let _luma: Luma<crate::encoding::Srgb, f64> = color.into_color();
708 }
709
710 #[test]
711 fn into_alpha_from_with_xyz() {
712 let color = WithXyz::<crate::encoding::Srgb>(PhantomData);
713
714 let _self: Alpha<WithXyz<crate::encoding::Srgb>, u8> = color.into_color();
715 let _xyz: Alpha<Xyz<_, f64>, u8> = color.into_color();
716 let _yxy: Alpha<Yxy<_, f64>, u8> = color.into_color();
717 let _lab: Alpha<Lab<_, f64>, u8> = color.into_color();
718 let _lch: Alpha<Lch<_, f64>, u8> = color.into_color();
719 let _luv: Alpha<Luv<_, f64>, u8> = color.into_color();
720 let _rgb: Alpha<Rgb<_, f64>, u8> = color.into_color();
721 let _hsl: Alpha<Hsl<_, f64>, u8> = color.into_color();
722 let _hsluv: Alpha<Hsluv<_, f64>, u8> = color.into_color();
723 let _hsv: Alpha<Hsv<_, f64>, u8> = color.into_color();
724 let _hwb: Alpha<Hwb<_, f64>, u8> = color.into_color();
725 let _luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> = color.into_color();
726 }
727
728 #[test]
729 fn into_alpha_from_with_xyz_alpha() {
730 let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
731 Alpha::from(WithXyz::<crate::encoding::Srgb>(PhantomData));
732
733 let _self: Alpha<WithXyz<crate::encoding::Srgb>, u8> = color.into_color();
734 let _xyz: Alpha<Xyz<_, f64>, u8> = color.into_color();
735 let _yxy: Alpha<Yxy<_, f64>, u8> = color.into_color();
736 let _lab: Alpha<Lab<_, f64>, u8> = color.into_color();
737 let _lch: Alpha<Lch<_, f64>, u8> = color.into_color();
738 let _luv: Alpha<Luv<_, f64>, u8> = color.into_color();
739 let _rgb: Alpha<Rgb<_, f64>, u8> = color.into_color();
740 let _hsl: Alpha<Hsl<_, f64>, u8> = color.into_color();
741 let _hsluv: Alpha<Hsluv<_, f64>, u8> = color.into_color();
742 let _hsv: Alpha<Hsv<_, f64>, u8> = color.into_color();
743 let _hwb: Alpha<Hwb<_, f64>, u8> = color.into_color();
744 let _luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> = color.into_color();
745 }
746
747 #[test]
748 fn from_without_xyz() {
749 let color: WithoutXyz<f64> = WithoutXyz(Default::default());
750 let _ = WithoutXyz::<f64>::from_color(color);
751
752 let xyz: Xyz<crate::white_point::E, f64> = Default::default();
753 let _ = WithoutXyz::<f64>::from_color(xyz);
754
755 let yxy: Yxy<crate::white_point::E, f64> = Default::default();
756 let _ = WithoutXyz::<f64>::from_color(yxy);
757
758 let lab: Lab<crate::white_point::E, f64> = Default::default();
759 let _ = WithoutXyz::<f64>::from_color(lab);
760
761 let lch: Lch<crate::white_point::E, f64> = Default::default();
762 let _ = WithoutXyz::<f64>::from_color(lch);
763
764 let luv: Luv<crate::white_point::E, f64> = Default::default();
765 let _ = WithoutXyz::<f64>::from_color(luv);
766
767 let rgb: Rgb<_, f64> = Default::default();
768 let _ = WithoutXyz::<f64>::from_color(rgb);
769
770 let hsl: Hsl<_, f64> = Default::default();
771 let _ = WithoutXyz::<f64>::from_color(hsl);
772
773 let hsluv: Hsluv<_, f64> = Default::default();
774 let _ = WithoutXyz::<f64>::from_color(hsluv);
775
776 let hsv: Hsv<_, f64> = Default::default();
777 let _ = WithoutXyz::<f64>::from_color(hsv);
778
779 let hwb: Hwb<_, f64> = Default::default();
780 let _ = WithoutXyz::<f64>::from_color(hwb);
781
782 let luma: Luma<Linear<crate::white_point::E>, f64> = Default::default();
783 let _ = WithoutXyz::<f64>::from_color(luma);
784 }
785
786 #[test]
787 fn into_without_xyz() {
788 let color = WithoutXyz::<f64>(PhantomData);
789
790 let _self: WithoutXyz<f64> = color.into_color();
791 let _xyz: Xyz<crate::white_point::E, f64> = color.into_color();
792 let _yxy: Yxy<crate::white_point::E, f64> = color.into_color();
793 let _lab: Lab<crate::white_point::E, f64> = color.into_color();
794 let _lch: Lch<crate::white_point::E, f64> = color.into_color();
795 let _luv: Luv<crate::white_point::E, f64> = color.into_color();
796 let _rgb: Rgb<_, f64> = color.into_color();
797 let _hsl: Hsl<_, f64> = color.into_color();
798 let _hsluv: Hsluv<_, f64> = color.into_color();
799 let _hsv: Hsv<_, f64> = color.into_color();
800 let _hwb: Hwb<_, f64> = color.into_color();
801 let _luma: Luma<Linear<crate::white_point::E>, f64> = color.into_color();
802 }
803}