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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
//! Traits for converting between color spaces.
//!
//! # Deriving
//!
//! `FromColorUnclamped` can be derived in a mostly automatic way.
//! The default minimum requirement is to implement `FromColorUnclamped<Xyz>`, but it can
//! also be customized to make use of generics and have other manual implementations.
//!
//! It is also recommended to derive or implement [`WithAlpha`](crate::WithAlpha),
//! to be able to convert between all `Alpha` wrapped color types.
//!
//! ## Configuration Attributes
//!
//! The derives can be configured using one or more `#[palette(...)]` attributes.
//! They can be attached to either the item itself, or to the fields.
//!
//! ```
//! # use palette::rgb::{RgbStandard, RgbSpace};
//! # use palette::convert::FromColorUnclamped;
//! # use palette::{Xyz, Component, FloatComponent};
//! #
//! #[palette(
//!     component = "T",
//!     rgb_standard = "S",
//!     white_point = "<S::Space as RgbSpace>::WhitePoint",
//! )]
//! #[derive(FromColorUnclamped)]
//! #[repr(C)]
//! struct ExampleType<S: RgbStandard, T: Component> {
//!     // ...
//!     #[palette(alpha)]
//!     alpha: T,
//!     standard: std::marker::PhantomData<S>,
//! }
//!
//! # impl<S, T> FromColorUnclamped<Xyz<<S::Space as RgbSpace>::WhitePoint, T>> for ExampleType<S, T>
//! # where
//! #   S: RgbStandard,
//! #   T: FloatComponent
//! # {
//! #   fn from_color_unclamped(color: Xyz<<S::Space as RgbSpace>::WhitePoint, T>) -> Self {
//! #       ExampleType {alpha: T::max_intensity(), standard: std::marker::PhantomData}
//! #   }
//! # }
//! #
//! # impl<S, T> FromColorUnclamped<ExampleType<S, T>> for Xyz<<S::Space as RgbSpace>::WhitePoint, T>
//! # where
//! #   S: RgbStandard,
//! #   T: FloatComponent
//! # {
//! #   fn from_color_unclamped(color: ExampleType<S, T>) -> Self {
//! #       Xyz::default()
//! #   }
//! # }
//! ```
//!
//! ### Item Attributes
//!
//! * `skip_derives(Luma, Rgb)`: No conversion derives will be implemented for these colors.
//! They are instead to be implemented manually, and serve as the basis for the automatic
//! implementations.
//!
//! * `white_point = "some::white_point::Type"`: Sets the white
//! point type that should be used when deriving. The default is `D65`, but it
//! may be any other type, including type parameters.
//!
//! * `component = "some::component::Type"`: Sets the color
//! component type that should be used when deriving. The default is `f32`, but
//! it may be any other type, including type parameters.
//!
//! * `rgb_standard = "some::rgb_standard::Type"`: Sets the RGB standard
//! type that should be used when deriving. The default is to either use `Srgb`
//! or a best effort to convert between standards, but sometimes it has to be set
//! to a specific type. This also accepts type parameters.
//!
//! ## Field Attributes
//!
//! * `alpha`: Specifies field as the color's transparency value.
//!
//! ## Examples
//!
//! Minimum requirements implementation:
//!
//! ```rust
//! use palette::convert::FromColorUnclamped;
//! use palette::{Srgb, Xyz, IntoColor};
//!
//! /// A custom version of Xyz that stores integer values from 0 to 100.
//! #[derive(PartialEq, Debug, FromColorUnclamped)]
//! struct Xyz100 {
//!     x: u8,
//!     y: u8,
//!     z: u8,
//! }
//!
//! // We have to implement at least one "manual" conversion. The default
//! // is to and from `Xyz`, but it can be customized with `skip_derives(...)`.
//! impl FromColorUnclamped<Xyz> for Xyz100 {
//!     fn from_color_unclamped(color: Xyz) -> Xyz100 {
//!         Xyz100 {
//!             x: (color.x * 100.0) as u8,
//!             y: (color.y * 100.0) as u8,
//!             z: (color.z * 100.0) as u8,
//!         }
//!     }
//! }
//!
//! impl FromColorUnclamped<Xyz100> for Xyz {
//!     fn from_color_unclamped(color: Xyz100) -> Xyz {
//!         Xyz::new(
//!             color.x as f32 / 100.0,
//!             color.y as f32 / 100.0,
//!             color.z as f32 / 100.0,
//!         )
//!     }
//! }
//!
//! fn main() {
//!     // Start with an Xyz100 color.
//!     let xyz = Xyz100 {
//!         x: 59,
//!         y: 75,
//!         z: 42,
//!     };
//!
//!     // Convert the color to sRGB.
//!     let rgb: Srgb = xyz.into_color();
//!
//!     assert_eq!(rgb.into_format(), Srgb::new(196u8, 238, 154));
//! }
//! ```
//!
//! With generic components:
//!
//! ```rust
//! #[macro_use]
//! extern crate approx;
//!
//! use palette::rgb::{Rgb, RgbSpace, RgbStandard};
//! use palette::encoding::Linear;
//! use palette::white_point::D65;
//! use palette::convert::{FromColorUnclamped, IntoColorUnclamped};
//! use palette::{FloatComponent, Hsv, Pixel, Srgb, IntoColor};
//!
//! /// sRGB, but with a reversed memory layout.
//! #[palette(
//!     skip_derives(Rgb),
//!     component = "T",
//!     rgb_standard = "palette::encoding::Srgb"
//! )]
//! #[derive(Copy, Clone, Pixel, FromColorUnclamped)]
//! #[repr(C)] // Makes sure the memory layout is as we want it.
//! struct Bgr<T> {
//!     blue: T,
//!     green: T,
//!     red: T,
//! }
//!
//! // It converts from and into any linear Rgb type that has the
//! // D65 white point, which is the default if we don't specify
//! // anything else with the `white_point` attribute argument.
//! impl<S, T> FromColorUnclamped<Bgr<T>> for Rgb<S, T>
//! where
//!     T: FloatComponent,
//!     S: RgbStandard,
//!     S::Space: RgbSpace<WhitePoint = D65>
//! {
//!     fn from_color_unclamped(color: Bgr<T>) -> Rgb<S, T> {
//!         Srgb::new(color.red, color.green, color.blue)
//!             .into_color_unclamped()
//!     }
//! }
//!
//! impl<S, T> FromColorUnclamped<Rgb<S, T>> for Bgr<T>
//! where
//!     T: FloatComponent,
//!     S: RgbStandard,
//!     S::Space: RgbSpace<WhitePoint = D65>
//! {
//!     fn from_color_unclamped(color: Rgb<S, T>) -> Bgr<T> {
//!         let color = Srgb::from_color_unclamped(color);
//!         Bgr {
//!             blue: color.blue,
//!             green: color.green,
//!             red: color.red,
//!         }
//!     }
//! }
//!
//! fn main() {
//!     let buffer = vec![
//!         0.0f64,
//!         0.0,
//!         0.0,
//!         0.0,
//!         0.5,
//!         0.25,
//!     ];
//!     let hsv: Hsv<_, f64> = Bgr::from_raw_slice(&buffer)[1].into_color();
//!
//!     assert_relative_eq!(hsv, Hsv::new(90.0, 1.0, 0.5));
//! }
//! ```
//!
//! With alpha component:
//!
//! ```rust
//! #[macro_use]
//! extern crate approx;
//!
//! use palette::{LinSrgba, Srgb, IntoColor, WithAlpha};
//! use palette::rgb::{Rgb, RgbSpace, RgbStandard};
//! use palette::encoding::Linear;
//! use palette::white_point::D65;
//! use palette::convert::{FromColorUnclamped, IntoColorUnclamped};
//!
//! /// CSS style sRGB.
//! #[palette(
//!     skip_derives(Rgb),
//!     rgb_standard = "palette::encoding::Srgb"
//! )]
//! #[derive(PartialEq, Debug, FromColorUnclamped, WithAlpha)]
//! struct CssRgb {
//!     red: u8,
//!     green: u8,
//!     blue: u8,
//!     #[palette(alpha)]
//!     alpha: f32,
//! }
//!
//! // We will write a conversion function for opaque RGB and
//! // `impl_default_conversions` will take care of preserving
//! // the transparency for us.
//! impl<S> FromColorUnclamped<Rgb<S, f32>> for CssRgb
//! where
//!     S: RgbStandard,
//!     S::Space: RgbSpace<WhitePoint = D65>,
//! {
//!     fn from_color_unclamped(color: Rgb<S, f32>) -> CssRgb{
//!         let srgb = Srgb::from_color_unclamped(color)
//!             .into_format();
//!
//!         CssRgb {
//!             red: srgb.red,
//!             green: srgb.green,
//!             blue: srgb.blue,
//!             alpha: 1.0
//!         }
//!     }
//! }
//!
//! impl<S> FromColorUnclamped<CssRgb> for Rgb<S, f32>
//! where
//!     S: RgbStandard,
//!     S::Space: RgbSpace<WhitePoint = D65>,
//! {
//!     fn from_color_unclamped(color: CssRgb) -> Rgb<S, f32>{
//!         Srgb::new(color.red, color.green, color.blue)
//!             .into_format()
//!             .into_color_unclamped()
//!     }
//! }
//!
//! fn main() {
//!     let css_color = CssRgb {
//!         red: 187,
//!         green: 0,
//!         blue: 255,
//!         alpha: 0.3,
//!     };
//!     let color: LinSrgba = css_color.into_color();
//!
//!     assert_relative_eq!(color, LinSrgba::new(0.496933, 0.0, 1.0, 0.3));
//! }
//! ```

use core::fmt::{self, Display, Formatter};

#[doc(hidden)]
pub use palette_derive::FromColorUnclamped;

use crate::Clamp;

/// The error type for a color conversion that converted a color into a color
/// with invalid values.
#[derive(Debug)]
pub struct OutOfBounds<T> {
    color: T,
}

impl<T> OutOfBounds<T> {
    /// Create a new error wrapping a color
    #[inline]
    fn new(color: T) -> Self {
        OutOfBounds { color }
    }

    /// Consume this error and return the wrapped color
    #[inline]
    pub fn color(self) -> T {
        self.color
    }
}

#[cfg(feature = "std")]
impl<T: ::std::fmt::Debug> ::std::error::Error for OutOfBounds<T> {
    fn description(&self) -> &str {
        "color conversion is out of bounds"
    }
}

impl<T> Display for OutOfBounds<T> {
    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
        write!(fmt, "color conversion is out of bounds")
    }
}

/// A trait for converting a color into another, in a possibly lossy way.
///
/// `U: IntoColor<T>` is implemented for every type `T: FromColor<U>`.
///
/// See [`FromColor`](crate::convert::FromColor) for more details.
pub trait IntoColor<T>: Sized {
    /// Convert into T with values clamped to the color defined bounds
    ///
    /// ```
    /// use palette::{Clamp, IntoColor, Lch, Srgb};
    ///
    /// let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).into_color();
    /// assert!(rgb.is_within_bounds());
    /// ```
    fn into_color(self) -> T;
}

/// A trait for unchecked conversion of a color into another.
///
/// `U: IntoColorUnclamped<T>` is implemented for every type `T: FromColorUnclamped<U>`.
///
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for more details.
pub trait IntoColorUnclamped<T>: Sized {
    /// Convert into T. The resulting color might be invalid in its color space
    ///
    /// ```
    /// use palette::convert::IntoColorUnclamped;
    /// use palette::{Clamp, Lch, Srgb};
    ///
    ///let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).into_color_unclamped();
    ///assert!(!rgb.is_within_bounds());
    ///```
    fn into_color_unclamped(self) -> T;
}

/// A trait for fallible conversion of a color into another.
///
/// `U: TryIntoColor<T>` is implemented for every type `T: TryFromColor<U>`.
///
/// See [`TryFromColor`](crate::convert::TryFromColor) for more details.
pub trait TryIntoColor<T>: Sized {
    /// Convert into T, returning ok if the color is inside of its defined
    /// range, otherwise an `OutOfBounds` error is returned which contains
    /// the unclamped color.
    ///
    ///```
    /// use palette::convert::TryIntoColor;
    /// use palette::{Hsl, Srgb};
    ///
    /// let rgb: Srgb = match Hsl::new(150.0, 1.0, 1.1).try_into_color() {
    ///     Ok(color) => color,
    ///     Err(err) => {
    ///         println!("Color is out of bounds");
    ///         err.color()
    ///     }
    /// };
    /// ```
    fn try_into_color(self) -> Result<T, OutOfBounds<T>>;
}

///A trait for converting one color from another, in a possibly lossy way.
///
/// `U: FromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Clamp`.
///
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version of this trait.
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
/// is out of bounds.
///
/// # The Difference Between FromColor and From
///
/// The conversion traits, including `FromColor`, were added to gain even more flexibility
/// than what `From` and the other standard library traits can give. There are a few subtle,
/// but important, differences in their semantics:
///
/// * `FromColor` and `IntoColor` are allowed to be lossy, meaning converting `A -> B -> A`
/// may result in a different value than the original. This applies to `A -> A` as well.
/// * `From<Self>` and `Into<Self>` are blanket implemented, while `FromColor<Self>` and
/// `IntoColor<Self>` have to be manually implemented. This allows additional flexibility,
/// such as allowing implementing `FromColor<Rgb<S2, T>> for Rgb<S1, T>`.
/// * Implementing `FromColorUnclamped` and `Clamp` is enough to get all the other conversion
/// traits, while `From` and `Into` would not be possible to blanket implement in the same way.
/// This also reduces the work that needs to be done by macros.
///
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait FromColor<T>: Sized {
    /// Convert from T with values clamped to the color defined bounds.
    ///
    /// ```
    /// use palette::{Clamp, FromColor, Lch, Srgb};
    ///
    /// let rgb = Srgb::from_color(Lch::new(50.0, 100.0, -175.0));
    /// assert!(rgb.is_within_bounds());
    /// ```
    fn from_color(t: T) -> Self;
}

/// A trait for unchecked conversion of one color from another.
///
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
/// See [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an error when the result
/// is out of bounds.
///
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait FromColorUnclamped<T>: Sized {
    /// Convert from T. The resulting color might be invalid in its color space.
    ///
    /// ```
    /// use palette::convert::FromColorUnclamped;
    /// use palette::{Clamp, Lch, Srgb};
    ///
    /// let rgb = Srgb::from_color_unclamped(Lch::new(50.0, 100.0, -175.0));
    /// assert!(!rgb.is_within_bounds());
    /// ```
    fn from_color_unclamped(val: T) -> Self;
}

/// A trait for fallible conversion of one color from another.
///
/// `U: TryFromColor<T>` is implemented for every type `U: FromColorUnclamped<T> + Clamp`.
///
/// See [`FromColor`](crate::convert::FromColor) for a lossy version of this trait.
/// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a lossless version.
///
/// See the [`convert`](crate::convert) module for how to implement `FromColorUnclamped` for
/// custom colors.
pub trait TryFromColor<T>: Sized {
    /// Convert from T, returning ok if the color is inside of its defined
    /// range, otherwise an `OutOfBounds` error is returned which contains
    /// the unclamped color.
    ///
    ///```
    /// use palette::convert::TryFromColor;
    /// use palette::{Hsl, Srgb};
    ///
    /// let rgb = match Srgb::try_from_color(Hsl::new(150.0, 1.0, 1.1)) {
    ///     Ok(color) => color,
    ///     Err(err) => {
    ///         println!("Color is out of bounds");
    ///         err.color()
    ///     }
    /// };
    /// ```
    fn try_from_color(t: T) -> Result<Self, OutOfBounds<Self>>;
}

impl<T, U> FromColor<T> for U
where
    U: FromColorUnclamped<T> + Clamp,
{
    #[inline]
    fn from_color(t: T) -> Self {
        let mut this = Self::from_color_unclamped(t);
        if !this.is_within_bounds() {
            this.clamp_self();
        }
        this
    }
}

impl<T, U> TryFromColor<T> for U
where
    U: FromColorUnclamped<T> + Clamp,
{
    #[inline]
    fn try_from_color(t: T) -> Result<Self, OutOfBounds<Self>> {
        let this = Self::from_color_unclamped(t);
        if this.is_within_bounds() {
            Ok(this)
        } else {
            Err(OutOfBounds::new(this))
        }
    }
}

impl<T, U> IntoColor<U> for T
where
    U: FromColor<T>,
{
    #[inline]
    fn into_color(self) -> U {
        U::from_color(self)
    }
}

impl<T, U> IntoColorUnclamped<U> for T
where
    U: FromColorUnclamped<T>,
{
    #[inline]
    fn into_color_unclamped(self) -> U {
        U::from_color_unclamped(self)
    }
}

impl<T, U> TryIntoColor<U> for T
where
    U: TryFromColor<T>,
{
    #[inline]
    fn try_into_color(self) -> Result<U, OutOfBounds<U>> {
        U::try_from_color(self)
    }
}

#[cfg(test)]
mod tests {
    use core::marker::PhantomData;

    use super::{FromColor, FromColorUnclamped, IntoColor};
    use crate::encoding::linear::Linear;
    use crate::luma::{Luma, LumaStandard};
    use crate::rgb::{Rgb, RgbSpace};
    use crate::{Alpha, Hsl, Hsluv, Hsv, Hwb, Lab, Lch, Luv, Xyz, Yxy};
    use crate::{Clamp, FloatComponent};

    #[derive(FromColorUnclamped, WithAlpha)]
    #[palette(
        skip_derives(Xyz, Luma),
        white_point = "S::WhitePoint",
        component = "f64",
        rgb_standard = "Linear<S>",
        palette_internal,
        palette_internal_not_base_type
    )]
    struct WithXyz<S: RgbSpace>(PhantomData<S>);

    impl<S: RgbSpace> Clone for WithXyz<S> {
        fn clone(&self) -> Self {
            *self
        }
    }

    impl<S: RgbSpace> Copy for WithXyz<S> {}

    impl<S: RgbSpace> Clamp for WithXyz<S> {
        fn is_within_bounds(&self) -> bool {
            true
        }

        fn clamp(&self) -> Self {
            *self
        }

        fn clamp_self(&mut self) {}
    }

    impl<S1, S2> FromColorUnclamped<WithXyz<S2>> for WithXyz<S1>
    where
        S1: RgbSpace,
        S2: RgbSpace<WhitePoint = S1::WhitePoint>,
    {
        fn from_color_unclamped(_color: WithXyz<S2>) -> Self {
            WithXyz(PhantomData)
        }
    }

    impl<S: RgbSpace> FromColorUnclamped<Xyz<S::WhitePoint, f64>> for WithXyz<S> {
        fn from_color_unclamped(_color: Xyz<S::WhitePoint, f64>) -> Self {
            WithXyz(PhantomData)
        }
    }

    impl<S: RgbSpace> FromColorUnclamped<WithXyz<S>> for Xyz<S::WhitePoint, f64> {
        fn from_color_unclamped(_color: WithXyz<S>) -> Xyz<S::WhitePoint, f64> {
            Xyz::with_wp(0.0, 1.0, 0.0)
        }
    }

    impl<Rs: RgbSpace, Ls: LumaStandard<WhitePoint = Rs::WhitePoint>>
        FromColorUnclamped<Luma<Ls, f64>> for WithXyz<Rs>
    {
        fn from_color_unclamped(_color: Luma<Ls, f64>) -> Self {
            WithXyz(PhantomData)
        }
    }

    impl<Rs: RgbSpace, Ls: LumaStandard<WhitePoint = Rs::WhitePoint>>
        FromColorUnclamped<WithXyz<Rs>> for Luma<Ls, f64>
    {
        fn from_color_unclamped(_color: WithXyz<Rs>) -> Self {
            Luma::new(1.0)
        }
    }

    #[derive(Copy, Clone, FromColorUnclamped, WithAlpha)]
    #[palette(
        skip_derives(Lch, Luma),
        white_point = "crate::white_point::E",
        component = "T",
        rgb_standard = "Linear<(crate::encoding::Srgb, crate::white_point::E)>",
        palette_internal,
        palette_internal_not_base_type
    )]
    struct WithoutXyz<T: FloatComponent>(PhantomData<T>);

    impl<T: FloatComponent> Clamp for WithoutXyz<T> {
        fn is_within_bounds(&self) -> bool {
            true
        }

        fn clamp(&self) -> Self {
            *self
        }

        fn clamp_self(&mut self) {}
    }

    impl<T: FloatComponent> FromColorUnclamped<WithoutXyz<T>> for WithoutXyz<T> {
        fn from_color_unclamped(color: WithoutXyz<T>) -> Self {
            color
        }
    }

    impl<T: FloatComponent> FromColorUnclamped<Lch<crate::white_point::E, T>> for WithoutXyz<T> {
        fn from_color_unclamped(_color: Lch<crate::white_point::E, T>) -> Self {
            WithoutXyz(PhantomData)
        }
    }

    impl<T: FloatComponent> FromColorUnclamped<WithoutXyz<T>> for Lch<crate::white_point::E, T> {
        fn from_color_unclamped(_color: WithoutXyz<T>) -> Lch<crate::white_point::E, T> {
            Lch::with_wp(T::one(), T::zero(), T::zero())
        }
    }

    impl<T: FloatComponent> FromColorUnclamped<Luma<Linear<crate::white_point::E>, T>>
        for WithoutXyz<T>
    {
        fn from_color_unclamped(_color: Luma<Linear<crate::white_point::E>, T>) -> Self {
            WithoutXyz(PhantomData)
        }
    }

    impl<T: FloatComponent> FromColorUnclamped<WithoutXyz<T>>
        for Luma<Linear<crate::white_point::E>, T>
    {
        fn from_color_unclamped(_color: WithoutXyz<T>) -> Luma<Linear<crate::white_point::E>, T> {
            Luma::new(T::one())
        }
    }

    #[test]
    fn from_with_xyz() {
        let color: WithXyz<crate::encoding::Srgb> = WithXyz(Default::default());
        WithXyz::<crate::encoding::Srgb>::from_color(color);

        let xyz: Xyz<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(xyz);

        let yxy: Yxy<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(yxy);

        let lab: Lab<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(lab);

        let lch: Lch<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(lch);

        let luv: Hsl<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(luv);

        let rgb: Rgb<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(rgb);

        let hsl: Hsl<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(hsl);

        let hsluv: Hsluv<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(hsluv);

        let hsv: Hsv<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(hsv);

        let hwb: Hwb<_, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(hwb);

        let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
        WithXyz::<crate::encoding::Srgb>::from_color(luma);
    }

    #[test]
    fn from_with_xyz_alpha() {
        let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
            Alpha::from(WithXyz(Default::default()));
        WithXyz::<crate::encoding::Srgb>::from_color(color);

        let xyz: Alpha<Xyz<_, f64>, u8> = Alpha::from(Xyz::default());
        WithXyz::<crate::encoding::Srgb>::from_color(xyz);

        let yxy: Alpha<Yxy<_, f64>, u8> = Alpha::from(Yxy::default());
        WithXyz::<crate::encoding::Srgb>::from_color(yxy);

        let lab: Alpha<Lab<_, f64>, u8> = Alpha::from(Lab::default());
        WithXyz::<crate::encoding::Srgb>::from_color(lab);

        let lch: Alpha<Lch<_, f64>, u8> = Alpha::from(Lch::default());
        WithXyz::<crate::encoding::Srgb>::from_color(lch);

        let luv: Alpha<Luv<_, f64>, u8> = Alpha::from(Luv::default());
        WithXyz::<crate::encoding::Srgb>::from_color(luv);

        let rgb: Alpha<Rgb<_, f64>, u8> = Alpha::from(Rgb::default());
        WithXyz::<crate::encoding::Srgb>::from_color(rgb);

        let hsl: Alpha<Hsl<_, f64>, u8> = Alpha::from(Hsl::default());
        WithXyz::<crate::encoding::Srgb>::from_color(hsl);

        let hsluv: Alpha<Hsluv<_, f64>, u8> = Alpha::from(Hsluv::default());
        WithXyz::<crate::encoding::Srgb>::from_color(hsluv);

        let hsv: Alpha<Hsv<_, f64>, u8> = Alpha::from(Hsv::default());
        WithXyz::<crate::encoding::Srgb>::from_color(hsv);

        let hwb: Alpha<Hwb<_, f64>, u8> = Alpha::from(Hwb::default());
        WithXyz::<crate::encoding::Srgb>::from_color(hwb);

        let luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> =
            Alpha::from(Luma::<crate::encoding::Srgb, f64>::default());
        WithXyz::<crate::encoding::Srgb>::from_color(luma);
    }

    #[test]
    fn from_with_xyz_into_alpha() {
        let color: WithXyz<crate::encoding::Srgb> = WithXyz(Default::default());
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(color);

        let xyz: Xyz<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(xyz);

        let yxy: Yxy<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(yxy);

        let lab: Lab<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lab);

        let lch: Lch<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lch);

        let luv: Hsl<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luv);

        let rgb: Rgb<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(rgb);

        let hsl: Hsl<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsl);

        let hsluv: Hsluv<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsluv);

        let hsv: Hsv<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsv);

        let hwb: Hwb<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hwb);

        let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luma);
    }

    #[test]
    fn from_with_xyz_alpha_into_alpha() {
        let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
            Alpha::from(WithXyz(Default::default()));
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(color);

        let xyz: Xyz<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(xyz);

        let yxy: Yxy<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(yxy);

        let lab: Lab<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lab);

        let lch: Lch<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(lch);

        let luv: Luv<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luv);

        let rgb: Rgb<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(rgb);

        let hsl: Hsl<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsl);

        let hsluv: Hsluv<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsluv);

        let hsv: Hsv<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hsv);

        let hwb: Hwb<_, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(hwb);

        let luma: Luma<crate::encoding::Srgb, f64> = Default::default();
        Alpha::<WithXyz<crate::encoding::Srgb>, u8>::from_color(luma);
    }

    #[test]
    fn into_from_with_xyz() {
        let color = WithXyz::<crate::encoding::Srgb>(PhantomData);

        let _self: WithXyz<crate::encoding::Srgb> = color.into_color();
        let _xyz: Xyz<_, f64> = color.into_color();
        let _yxy: Yxy<_, f64> = color.into_color();
        let _lab: Lab<_, f64> = color.into_color();
        let _lch: Lch<_, f64> = color.into_color();
        let _luv: Luv<_, f64> = color.into_color();
        let _rgb: Rgb<_, f64> = color.into_color();
        let _hsl: Hsl<_, f64> = color.into_color();
        let _hsluv: Hsluv<_, f64> = color.into_color();
        let _hsv: Hsv<_, f64> = color.into_color();
        let _hwb: Hwb<_, f64> = color.into_color();
        let _luma: Luma<crate::encoding::Srgb, f64> = color.into_color();
    }

    #[test]
    fn into_from_with_xyz_alpha() {
        let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
            Alpha::from(WithXyz::<crate::encoding::Srgb>(PhantomData));

        let _self: WithXyz<crate::encoding::Srgb> = color.into_color();
        let _xyz: Xyz<_, f64> = color.into_color();
        let _yxy: Yxy<_, f64> = color.into_color();
        let _lab: Lab<_, f64> = color.into_color();
        let _lch: Lch<_, f64> = color.into_color();
        let _luv: Luv<_, f64> = color.into_color();
        let _rgb: Rgb<_, f64> = color.into_color();
        let _hsl: Hsl<_, f64> = color.into_color();
        let _hsluv: Hsluv<_, f64> = color.into_color();
        let _hsv: Hsv<_, f64> = color.into_color();
        let _hwb: Hwb<_, f64> = color.into_color();
        let _luma: Luma<crate::encoding::Srgb, f64> = color.into_color();
    }

    #[test]
    fn into_alpha_from_with_xyz() {
        let color = WithXyz::<crate::encoding::Srgb>(PhantomData);

        let _self: Alpha<WithXyz<crate::encoding::Srgb>, u8> = color.into_color();
        let _xyz: Alpha<Xyz<_, f64>, u8> = color.into_color();
        let _yxy: Alpha<Yxy<_, f64>, u8> = color.into_color();
        let _lab: Alpha<Lab<_, f64>, u8> = color.into_color();
        let _lch: Alpha<Lch<_, f64>, u8> = color.into_color();
        let _luv: Alpha<Luv<_, f64>, u8> = color.into_color();
        let _rgb: Alpha<Rgb<_, f64>, u8> = color.into_color();
        let _hsl: Alpha<Hsl<_, f64>, u8> = color.into_color();
        let _hsluv: Alpha<Hsluv<_, f64>, u8> = color.into_color();
        let _hsv: Alpha<Hsv<_, f64>, u8> = color.into_color();
        let _hwb: Alpha<Hwb<_, f64>, u8> = color.into_color();
        let _luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> = color.into_color();
    }

    #[test]
    fn into_alpha_from_with_xyz_alpha() {
        let color: Alpha<WithXyz<crate::encoding::Srgb>, u8> =
            Alpha::from(WithXyz::<crate::encoding::Srgb>(PhantomData));

        let _self: Alpha<WithXyz<crate::encoding::Srgb>, u8> = color.into_color();
        let _xyz: Alpha<Xyz<_, f64>, u8> = color.into_color();
        let _yxy: Alpha<Yxy<_, f64>, u8> = color.into_color();
        let _lab: Alpha<Lab<_, f64>, u8> = color.into_color();
        let _lch: Alpha<Lch<_, f64>, u8> = color.into_color();
        let _luv: Alpha<Luv<_, f64>, u8> = color.into_color();
        let _rgb: Alpha<Rgb<_, f64>, u8> = color.into_color();
        let _hsl: Alpha<Hsl<_, f64>, u8> = color.into_color();
        let _hsluv: Alpha<Hsluv<_, f64>, u8> = color.into_color();
        let _hsv: Alpha<Hsv<_, f64>, u8> = color.into_color();
        let _hwb: Alpha<Hwb<_, f64>, u8> = color.into_color();
        let _luma: Alpha<Luma<crate::encoding::Srgb, f64>, u8> = color.into_color();
    }

    #[test]
    fn from_without_xyz() {
        let color: WithoutXyz<f64> = WithoutXyz(Default::default());
        WithoutXyz::<f64>::from_color(color);

        let xyz: Xyz<crate::white_point::E, f64> = Default::default();
        WithoutXyz::<f64>::from_color(xyz);

        let yxy: Yxy<crate::white_point::E, f64> = Default::default();
        WithoutXyz::<f64>::from_color(yxy);

        let lab: Lab<crate::white_point::E, f64> = Default::default();
        WithoutXyz::<f64>::from_color(lab);

        let lch: Lch<crate::white_point::E, f64> = Default::default();
        WithoutXyz::<f64>::from_color(lch);

        let luv: Luv<crate::white_point::E, f64> = Default::default();
        WithoutXyz::<f64>::from_color(luv);

        let rgb: Rgb<_, f64> = Default::default();
        WithoutXyz::<f64>::from_color(rgb);

        let hsl: Hsl<_, f64> = Default::default();
        WithoutXyz::<f64>::from_color(hsl);

        let hsluv: Hsluv<_, f64> = Default::default();
        WithoutXyz::<f64>::from_color(hsluv);

        let hsv: Hsv<_, f64> = Default::default();
        WithoutXyz::<f64>::from_color(hsv);

        let hwb: Hwb<_, f64> = Default::default();
        WithoutXyz::<f64>::from_color(hwb);

        let luma: Luma<Linear<crate::white_point::E>, f64> = Default::default();
        WithoutXyz::<f64>::from_color(luma);
    }

    #[test]
    fn into_without_xyz() {
        let color = WithoutXyz::<f64>(PhantomData);

        let _self: WithoutXyz<f64> = color.into_color();
        let _xyz: Xyz<crate::white_point::E, f64> = color.into_color();
        let _yxy: Yxy<crate::white_point::E, f64> = color.into_color();
        let _lab: Lab<crate::white_point::E, f64> = color.into_color();
        let _lch: Lch<crate::white_point::E, f64> = color.into_color();
        let _luv: Luv<crate::white_point::E, f64> = color.into_color();
        let _rgb: Rgb<_, f64> = color.into_color();
        let _hsl: Hsl<_, f64> = color.into_color();
        let _hsluv: Hsluv<_, f64> = color.into_color();
        let _hsv: Hsv<_, f64> = color.into_color();
        let _hwb: Hwb<_, f64> = color.into_color();
        let _luma: Luma<Linear<crate::white_point::E>, f64> = color.into_color();
    }
}