Skip to content

Commit 858da7d

Browse files
authored
Add impl ColorSpace example (#130)
Closes #47.
1 parent fac2dbe commit 858da7d

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This release has an [MSRV][] of 1.82.
1818
### Added
1919

2020
* Support for the ACES2065-1 color space. ([#124][] by [@tomcur][])
21+
* A documentation example implementing `ColorSpace`. ([#130][] by [@tomcur][])
2122

2223
### Fixed
2324

@@ -117,6 +118,7 @@ This is the initial release.
117118
[#124]: https://github.com/linebender/color/pull/124
118119
[#128]: https://github.com/linebender/color/pull/128
119120
[#129]: https://github.com/linebender/color/pull/129
121+
[#130]: https://github.com/linebender/color/pull/130
120122

121123
[Unreleased]: https://github.com/linebender/color/compare/v0.2.2...HEAD
122124
[0.2.2]: https://github.com/linebender/color/releases/tag/v0.2.2

color/src/colorspace.rs

+45
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,51 @@ use crate::floatfuncs::FloatFuncs;
2424
///
2525
/// See the [XYZ-D65 color space](`XyzD65`) documentation for some
2626
/// 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+
/// ```
2772
pub trait ColorSpace: Clone + Copy + 'static {
2873
/// Whether the color space is linear.
2974
///

0 commit comments

Comments
 (0)