Skip to content
New issue

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

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

Already on GitHub? # to your account

Add BLACK, WHITE, TRANSPARENT constants to color types. #64

Merged
merged 1 commit into from
Nov 24, 2024
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ You can find its changes [documented below](#010-2024-11-20).

This release has an [MSRV][] of 1.82.

### Added

* Add `BLACK`, `WHITE`, and `TRANSPARENT` constants to the color types. ([#64][] by [@waywardmonkeys][])
Copy link
Member

Choose a reason for hiding this comment

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

Is it worth noting that this is a breaking change?

It feels like there's an interesting thing here, that ideally we'd have different definitions of breaking for creators of custom colorspaces and users of the crate - creators of custom colorspaces are so rare that kind of mad.

I guess color would probably be a good use-case for the semver-trick.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There will be some other breaking changes as well. But I guess we could call out that ColorSpace must have a WHITE_COMPONENTS const now?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, that is the breaking change. Our standards for marking breaking changes aren't very developed.


## [0.1.0][] (2024-11-20)

This release has an [MSRV][] of 1.82.

This is the initial release.

[@waywardmonkeys]: https://github.com/waywardmonkeys

[#64]: https://github.com/linebender/color/pull/64

[Unreleased]: https://github.com/linebender/color/compare/v0.1.0...HEAD
[0.1.0]: https://github.com/linebender/color/releases/tag/v0.1.0

Expand Down
58 changes: 58 additions & 0 deletions color/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ pub(crate) fn fixup_hues_for_interpolate(
}

impl<CS: ColorSpace> OpaqueColor<CS> {
/// A black color.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const BLACK: Self = Self::new([0., 0., 0.]);

/// A white color.
///
/// This value is specific to the color space.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const WHITE: Self = Self::new(CS::WHITE_COMPONENTS);

/// Create a new color from the given components.
pub const fn new(components: [f32; 3]) -> Self {
let cs = PhantomData;
Expand Down Expand Up @@ -292,6 +306,28 @@ pub(crate) const fn add_alpha([x, y, z]: [f32; 3], a: f32) -> [f32; 4] {
}

impl<CS: ColorSpace> AlphaColor<CS> {
/// A black color.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const BLACK: Self = Self::new([0., 0., 0., 1.]);

/// A transparent color.
///
/// This is a black color with full alpha.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const TRANSPARENT: Self = Self::new([0., 0., 0., 0.]);

/// A white color.
///
/// This value is specific to the color space.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const WHITE: Self = Self::new(add_alpha(CS::WHITE_COMPONENTS, 1.));

/// Create a new color from the given components.
pub const fn new(components: [f32; 4]) -> Self {
let cs = PhantomData;
Expand Down Expand Up @@ -428,6 +464,28 @@ impl<CS: ColorSpace> AlphaColor<CS> {
}

impl<CS: ColorSpace> PremulColor<CS> {
/// A black color.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const BLACK: Self = Self::new([0., 0., 0., 1.]);

/// A transparent color.
///
/// This is a black color with full alpha.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const TRANSPARENT: Self = Self::new([0., 0., 0., 0.]);

/// A white color.
///
/// This value is specific to the color space.
///
/// More comprehensive pre-defined colors are available
/// in the [`color::palette`](crate::palette) module.
pub const WHITE: Self = Self::new(add_alpha(CS::WHITE_COMPONENTS, 1.));

/// Create a new color from the given components.
pub const fn new(components: [f32; 4]) -> Self {
let cs = PhantomData;
Expand Down
64 changes: 63 additions & 1 deletion color/src/colorspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pub trait ColorSpace: Clone + Copy + 'static {
/// The tag corresponding to this color space, if a matching tag exists.
const TAG: Option<ColorSpaceTag> = None;

/// The component values for the color white within this color space.
const WHITE_COMPONENTS: [f32; 3];

/// Convert an opaque color to linear sRGB.
///
/// Values are likely to exceed [0, 1] for wide-gamut and HDR colors.
Expand Down Expand Up @@ -157,6 +160,8 @@ impl ColorSpace for LinearSrgb {

const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::LinearSrgb);

const WHITE_COMPONENTS: [f32; 3] = [1., 1., 1.];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
src
}
Expand Down Expand Up @@ -213,6 +218,8 @@ fn lin_to_srgb(x: f32) -> f32 {
impl ColorSpace for Srgb {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::Srgb);

const WHITE_COMPONENTS: [f32; 3] = [1., 1., 1.];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
src.map(srgb_to_lin)
}
Expand Down Expand Up @@ -260,6 +267,8 @@ pub struct DisplayP3;
impl ColorSpace for DisplayP3 {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::DisplayP3);

const WHITE_COMPONENTS: [f32; 3] = [0.99999994, 0.99999994, 0.99999994];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
const LINEAR_DISPLAYP3_TO_SRGB: [[f32; 3]; 3] = [
[1.224_940_2, -0.224_940_18, 0.0],
Expand Down Expand Up @@ -303,6 +312,8 @@ pub struct A98Rgb;
impl ColorSpace for A98Rgb {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::A98Rgb);

const WHITE_COMPONENTS: [f32; 3] = [1., 1., 1.];

fn to_linear_srgb([r, g, b]: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_sRGB * lin_A98_to_XYZ
#[expect(
Expand Down Expand Up @@ -378,6 +389,8 @@ pub struct ProphotoRgb;
impl ColorSpace for ProphotoRgb {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::ProphotoRgb);

const WHITE_COMPONENTS: [f32; 3] = [1., 0.99999994, 1.];

fn to_linear_srgb([r, g, b]: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_sRGB * D50_to_D65 * lin_prophoto_to_XYZ
const LINEAR_PROPHOTORGB_TO_SRGB: [[f32; 3]; 3] = [
Expand Down Expand Up @@ -449,6 +462,8 @@ impl Rec2020 {
impl ColorSpace for Rec2020 {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::Rec2020);

const WHITE_COMPONENTS: [f32; 3] = [1., 1., 1.];

fn to_linear_srgb([r, g, b]: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_sRGB * lin_Rec2020_to_XYZ
#[expect(
Expand Down Expand Up @@ -548,6 +563,8 @@ impl ColorSpace for XyzD50 {

const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::XyzD50);

const WHITE_COMPONENTS: [f32; 3] = [0.9642956, 1., 0.8251046];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
// XYZ_to_lin_sRGB * D50_to_D65
const XYZ_TO_LINEAR_SRGB: [[f32; 3]; 3] = [
Expand Down Expand Up @@ -631,6 +648,8 @@ impl ColorSpace for XyzD65 {

const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::XyzD65);

const WHITE_COMPONENTS: [f32; 3] = [0.9504559, 1., 1.0890577];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
const XYZ_TO_LINEAR_SRGB: [[f32; 3]; 3] = [
[3.240_97, -1.537_383_2, -0.498_610_76],
Expand Down Expand Up @@ -706,6 +725,8 @@ const OKLAB_LMS_TO_LAB: [[f32; 3]; 3] = [
impl ColorSpace for Oklab {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::Oklab);

const WHITE_COMPONENTS: [f32; 3] = [1., 0., 0.];

fn to_linear_srgb(src: [f32; 3]) -> [f32; 3] {
let lms = matmul(&OKLAB_LAB_TO_LMS, src).map(|x| x * x * x);
matmul(&OKLAB_LMS_TO_SRGB, lms)
Expand Down Expand Up @@ -769,6 +790,8 @@ impl ColorSpace for Oklch {

const LAYOUT: ColorSpaceLayout = ColorSpaceLayout::HueThird;

const WHITE_COMPONENTS: [f32; 3] = [1., 0., 90.];

fn from_linear_srgb(src: [f32; 3]) -> [f32; 3] {
lab_to_lch(Oklab::from_linear_srgb(src))
}
Expand Down Expand Up @@ -852,6 +875,8 @@ const KAPPA: f32 = 24389. / 27.;
impl ColorSpace for Lab {
const TAG: Option<ColorSpaceTag> = Some(ColorSpaceTag::Lab);

const WHITE_COMPONENTS: [f32; 3] = [100., 0., 0.];

fn to_linear_srgb([l, a, b]: [f32; 3]) -> [f32; 3] {
let f1 = l * (1. / 116.) + (16. / 116.);
let f0 = a * (1. / 500.) + f1;
Expand Down Expand Up @@ -920,6 +945,8 @@ impl ColorSpace for Lch {

const LAYOUT: ColorSpaceLayout = ColorSpaceLayout::HueThird;

const WHITE_COMPONENTS: [f32; 3] = [100., 0., 0.];

fn from_linear_srgb(src: [f32; 3]) -> [f32; 3] {
lab_to_lch(Lab::from_linear_srgb(src))
}
Expand Down Expand Up @@ -1024,6 +1051,8 @@ impl ColorSpace for Hsl {

const LAYOUT: ColorSpaceLayout = ColorSpaceLayout::HueFirst;

const WHITE_COMPONENTS: [f32; 3] = [0., 0., 100.];

fn from_linear_srgb(src: [f32; 3]) -> [f32; 3] {
let rgb = Srgb::from_linear_srgb(src);
rgb_to_hsl(rgb, true)
Expand Down Expand Up @@ -1109,6 +1138,8 @@ impl ColorSpace for Hwb {

const LAYOUT: ColorSpaceLayout = ColorSpaceLayout::HueFirst;

const WHITE_COMPONENTS: [f32; 3] = [0., 100., 0.];

fn from_linear_srgb(src: [f32; 3]) -> [f32; 3] {
let rgb = Srgb::from_linear_srgb(src);
rgb_to_hwb(rgb)
Expand Down Expand Up @@ -1139,12 +1170,43 @@ impl ColorSpace for Hwb {

#[cfg(test)]
mod tests {
use crate::{A98Rgb, ColorSpace, OpaqueColor, ProphotoRgb, Rec2020, Srgb};
use crate::{
A98Rgb, ColorSpace, DisplayP3, Hsl, Hwb, Lab, Lch, LinearSrgb, Oklab, Oklch, OpaqueColor,
ProphotoRgb, Rec2020, Srgb, XyzD50, XyzD65,
};

fn almost_equal<CS: ColorSpace>(col1: [f32; 3], col2: [f32; 3]) -> bool {
OpaqueColor::<CS>::new(col1).difference(OpaqueColor::new(col2)) < 1e-4
}

#[test]
fn white_components() {
fn check_white<CS: ColorSpace>() {
almost_equal::<Srgb>(
Srgb::WHITE_COMPONENTS,
CS::convert::<Srgb>(CS::WHITE_COMPONENTS),
);
almost_equal::<CS>(
CS::WHITE_COMPONENTS,
Srgb::convert::<CS>(Srgb::WHITE_COMPONENTS),
);
}

check_white::<A98Rgb>();
check_white::<DisplayP3>();
check_white::<Hsl>();
check_white::<Hwb>();
check_white::<Lab>();
check_white::<Lch>();
check_white::<LinearSrgb>();
check_white::<Oklab>();
check_white::<Oklch>();
check_white::<ProphotoRgb>();
check_white::<Rec2020>();
check_white::<XyzD50>();
check_white::<XyzD65>();
}

#[test]
fn a98rgb_srgb() {
for (srgb, a98) in [
Expand Down