Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rec2020 color space #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 123 additions & 1 deletion color/src/colorspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,110 @@ impl ColorSpace for ProphotoRgb {
}
}

/// 🌌 The Rec. 2020 color space.
///
/// Rec. 2020 is similar to [sRGB](`Srgb`) but has higher red, green and blue chromaticities,
/// thereby extending its gamut over sRGB on all components.
///
/// Its components are `[r, g, b]` (red, green, and blue channels respectively), with `[0, 0, 0]`
/// pure black and `[1, 1, 1]` white. The natural bounds of the channels are `[0, 1]`.
///
/// This corresponds to the color space in [CSS Color Module Level 4 § 10.7][css-sec] and is
/// [characterized by the ICC][icc]. The color space is defined by the International
/// Telecommunication Union [here][itu].
///
/// [css-sec]: https://www.w3.org/TR/css-color-4/#predefined-rec2020
/// [icc]: https://www.color.org/chardata/rgb/BT2020.xalter
/// [itu]: https://www.itu.int/rec/R-REC-BT.2020/en
#[derive(Clone, Copy, Debug)]
pub struct Rec2020;

impl Rec2020 {
// These are the parameters of the transfer function defined in the Rec. 2020 specification.
// They are truncated here to f32 precision.
const A: f32 = 1.099_296_8;
const B: f32 = 0.018_053_97;
}

impl ColorSpace for Rec2020 {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::Rec2020);

fn to_linear_srgb([r, g, b]: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_sRGB * lin_Rec2020_to_XYZ
#[expect(
clippy::cast_possible_truncation,
reason = "exact rational, truncate at compile-time"
)]
const LINEAR_REC2020_TO_SRGB: [[f32; 3]; 3] = [
[
(2_785_571_537. / 1_677_558_947.) as f32,
(-985_802_650. / 1_677_558_947.) as f32,
(-122_209_940. / 1_677_558_947.) as f32,
],
[
(-4_638_020_506. / 37_238_079_773.) as f32,
(42_187_016_744. / 37_238_079_773.) as f32,
(-310_916_465. / 37_238_079_773.) as f32,
],
[
(-97_469_024. / 5_369_968_309.) as f32,
(-3_780_738_464. / 37_589_778_163.) as f32,
(42_052_799_795. / 37_589_778_163.) as f32,
Comment on lines +460 to +472
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more lily-gilding here. Debatable usefulness, as the transfer function parameters are reals not representable as rationals.

],
];

fn transfer(x: f32) -> f32 {
if x.abs() < Rec2020::B * 4.5 {
x / 4.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be x * (1.0 / 4.5) for performance.

} else {
((x.abs() + Rec2020::A - 1.) / Rec2020::A)
.powf(1. / 0.45)
.copysign(x)
}
}

matmul(&LINEAR_REC2020_TO_SRGB, [r, g, b].map(transfer))
}

fn from_linear_srgb([r, g, b]: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_Rec2020 * lin_sRGB_to_XYZ
#[expect(
clippy::cast_possible_truncation,
reason = "exact rational, truncate at compile-time"
)]
const LINEAR_SRGB_TO_REC2020: [[f32; 3]; 3] = [
[
(2_939_026_994. / 4_684_425_795.) as f32,
(9_255_011_753. / 28_106_554_770.) as f32,
(173_911_579. / 4_015_222_110.) as f32,
],
[
(76_515_593. / 1_107_360_270.) as f32,
(6_109_575_001. / 6_644_161_620.) as f32,
(75_493_061. / 6_644_161_620.) as f32,
],
[
(12_225_392. / 745_840_075.) as f32,
(1_772_384_008. / 20_137_682_025.) as f32,
(18_035_212_433. / 20_137_682_025.) as f32,
],
];

fn transfer(x: f32) -> f32 {
if x.abs() < Rec2020::B {
x * 4.5
} else {
(Rec2020::A * x.abs().powf(0.45) - Rec2020::A + 1.).copysign(x)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you group it so it's (Rec2020::A - 1.) that's one fewer addition.

}
}
matmul(&LINEAR_SRGB_TO_REC2020, [r, g, b]).map(transfer)
}

fn clip([r, g, b]: [f32; 3]) -> [f32; 3] {
[r.clamp(0., 1.), g.clamp(0., 1.), b.clamp(0., 1.)]
}
}

/// 🌌 The CIE XYZ color space with a 2° observer and a reference white of D50.
///
/// Its components are `[X, Y, Z]`. The components are unbounded, but are usually positive.
Expand Down Expand Up @@ -1035,7 +1139,7 @@ impl ColorSpace for Hwb {

#[cfg(test)]
mod tests {
use crate::{A98Rgb, ColorSpace, OpaqueColor, ProphotoRgb, Srgb};
use crate::{A98Rgb, ColorSpace, OpaqueColor, ProphotoRgb, Rec2020, Srgb};

fn almost_equal<CS: ColorSpace>(col1: [f32; 3], col2: [f32; 3]) -> bool {
OpaqueColor::<CS>::new(col1).difference(OpaqueColor::new(col2)) < 1e-4
Expand Down Expand Up @@ -1068,4 +1172,22 @@ mod tests {
));
}
}

#[test]
fn rec2020_srgb() {
for (srgb, rec2020) in [
([0.1, 0.2, 0.3], [0.091284, 0.134169, 0.230056]),
([0.05, 0.1, 0.15], [0.029785, 0.043700, 0.083264]),
([0., 1., 0.], [0.567542, 0.959279, 0.268969]),
] {
assert!(almost_equal::<Srgb>(
srgb,
Rec2020::convert::<Srgb>(rec2020)
));
assert!(almost_equal::<Rec2020>(
rec2020,
Srgb::convert::<Rec2020>(srgb)
));
}
}
}
2 changes: 1 addition & 1 deletion color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod floatfuncs;
pub use color::{AlphaColor, HueDirection, OpaqueColor, PremulColor};
pub use colorspace::{
A98Rgb, ColorSpace, ColorSpaceLayout, DisplayP3, Hsl, Hwb, Lab, Lch, LinearSrgb, Oklab, Oklch,
ProphotoRgb, Srgb, XyzD50, XyzD65,
ProphotoRgb, Rec2020, Srgb, XyzD50, XyzD65,
};
pub use dynamic::{DynamicColor, Interpolator};
pub use gradient::{gradient, GradientIter};
Expand Down
1 change: 1 addition & 0 deletions color/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,7 @@ impl<'a> Parser<'a> {
"display-p3" => ColorSpaceTag::DisplayP3,
"a98-rgb" => ColorSpaceTag::A98Rgb,
"prophoto-rgb" => ColorSpaceTag::ProphotoRgb,
"rec2020" => ColorSpaceTag::Rec2020,
"xyz-d50" => ColorSpaceTag::XyzD50,
"xyz" | "xyz-d65" => ColorSpaceTag::XyzD65,
_ => return Err(ParseError::UnknownColorSpace),
Expand Down
1 change: 1 addition & 0 deletions color/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl core::fmt::Display for DynamicColor {
ColorSpaceTag::DisplayP3 => write_color_function(self, "display-p3", f),
ColorSpaceTag::A98Rgb => write_color_function(self, "a98-rgb", f),
ColorSpaceTag::ProphotoRgb => write_color_function(self, "prophoto-rgb", f),
ColorSpaceTag::Rec2020 => write_color_function(self, "rec2020", f),
ColorSpaceTag::Hsl => write_legacy_function(self, "hsl", 1.0, f),
ColorSpaceTag::Hwb => write_modern_function(self, "hwb", f),
ColorSpaceTag::XyzD50 => write_color_function(self, "xyz-d50", f),
Expand Down
11 changes: 8 additions & 3 deletions color/src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use crate::{
A98Rgb, ColorSpace, ColorSpaceLayout, DisplayP3, Hsl, Hwb, Lab, Lch, LinearSrgb, Missing,
Oklab, Oklch, ProphotoRgb, Srgb, XyzD50, XyzD65,
Oklab, Oklch, ProphotoRgb, Rec2020, Srgb, XyzD50, XyzD65,
};

/// The color space tag for dynamic colors.
Expand Down Expand Up @@ -43,6 +43,8 @@ pub enum ColorSpaceTag {
A98Rgb,
/// The [`ProphotoRgb`] color space.
ProphotoRgb,
/// The [`Rec2020`] color space.
Rec2020,
/// The [`XyzD50`] color space.
XyzD50,
/// The [`XyzD65`] color space.
Expand All @@ -66,8 +68,8 @@ impl ColorSpaceTag {
matches!(
(self, other),
(
Srgb | LinearSrgb | DisplayP3 | A98Rgb | ProphotoRgb | XyzD50 | XyzD65,
Srgb | LinearSrgb | DisplayP3 | A98Rgb | ProphotoRgb | XyzD50 | XyzD65
Srgb | LinearSrgb | DisplayP3 | A98Rgb | ProphotoRgb | Rec2020 | XyzD50 | XyzD65,
Srgb | LinearSrgb | DisplayP3 | A98Rgb | ProphotoRgb | Rec2020 | XyzD50 | XyzD65
) | (Lab | Oklab, Lab | Oklab)
| (Lch | Oklch, Lch | Oklch)
)
Expand Down Expand Up @@ -143,6 +145,7 @@ impl ColorSpaceTag {
Self::DisplayP3 => DisplayP3::from_linear_srgb(rgb),
Self::A98Rgb => A98Rgb::from_linear_srgb(rgb),
Self::ProphotoRgb => ProphotoRgb::from_linear_srgb(rgb),
Self::Rec2020 => Rec2020::from_linear_srgb(rgb),
Self::XyzD50 => XyzD50::from_linear_srgb(rgb),
Self::XyzD65 => XyzD65::from_linear_srgb(rgb),
Self::Hsl => Hsl::from_linear_srgb(rgb),
Expand All @@ -164,6 +167,7 @@ impl ColorSpaceTag {
Self::DisplayP3 => DisplayP3::to_linear_srgb(src),
Self::A98Rgb => A98Rgb::to_linear_srgb(src),
Self::ProphotoRgb => ProphotoRgb::to_linear_srgb(src),
Self::Rec2020 => Rec2020::to_linear_srgb(src),
Self::XyzD50 => XyzD50::to_linear_srgb(src),
Self::XyzD65 => XyzD65::to_linear_srgb(src),
Self::Hsl => Hsl::to_linear_srgb(src),
Expand Down Expand Up @@ -219,6 +223,7 @@ impl ColorSpaceTag {
Self::DisplayP3 => DisplayP3::clip(src),
Self::A98Rgb => A98Rgb::clip(src),
Self::ProphotoRgb => ProphotoRgb::clip(src),
Self::Rec2020 => Rec2020::clip(src),
Self::XyzD50 => XyzD50::clip(src),
Self::XyzD65 => XyzD65::clip(src),
Self::Hsl => Hsl::clip(src),
Expand Down