@@ -24,6 +24,51 @@ use crate::floatfuncs::FloatFuncs;
24
24
///
25
25
/// See the [XYZ-D65 color space](`XyzD65`) documentation for some
26
26
/// background information on color spaces.
27
+ ///
28
+ /// # Implementing `ColorSpace`
29
+ ///
30
+ /// When implementing a custom color space, take care to set the associated constants correctly.
31
+ /// The following is an example implementation of the
32
+ /// [Rec. 709](https://www.color.org/chardata/rgb/BT2020.xalter) color space.
33
+ ///
34
+ /// **Note:**
35
+ /// - [`ColorSpace::convert`] can be implemented to specialize specific conversions;
36
+ /// - implement [`ColorSpace::scale_chroma`] if your color space has a natural representation of
37
+ /// chroma.
38
+ ///
39
+ /// ```rust
40
+ /// use color::{ColorSpace, ColorSpaceLayout};
41
+ ///
42
+ /// /// The Rec. 709 color space, using the electro-optical transfer function
43
+ /// /// defined in ITU-R BT.1886.
44
+ /// ///
45
+ /// /// Rec. 709 is very similar to sRGB, having the same natural gamut, but
46
+ /// /// does have a different transfer function.
47
+ /// ///
48
+ /// /// See https://www.color.org/chardata/rgb/BT709.xalter.
49
+ /// #[derive(Clone, Copy, Debug)]
50
+ /// pub struct Rec709;
51
+ ///
52
+ /// impl ColorSpace for Rec709 {
53
+ /// const IS_LINEAR: bool = false;
54
+ ///
55
+ /// const LAYOUT: ColorSpaceLayout = ColorSpaceLayout::Rectangular;
56
+ ///
57
+ /// const WHITE_COMPONENTS: [f32; 3] = [1., 1., 1.];
58
+ ///
59
+ /// fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
60
+ /// src.map(|x| x.powf(2.4))
61
+ /// }
62
+ ///
63
+ /// fn from_linear_srgb(src: [f32; 3]) -> [f32; 3] {
64
+ /// src.map(|x| x.powf(1. / 2.4))
65
+ /// }
66
+ ///
67
+ /// fn clip([r, g, b]: [f32; 3]) -> [f32; 3] {
68
+ /// [r.clamp(0., 1.), g.clamp(0., 1.), b.clamp(0., 1.)]
69
+ /// }
70
+ /// }
71
+ /// ```
27
72
pub trait ColorSpace : Clone + Copy + ' static {
28
73
/// Whether the color space is linear.
29
74
///
0 commit comments