Skip to content

Add Chromaticity::with_luminance to convert chromaticities to colors - #224

Open
tomcur wants to merge 1 commit into
linebender:mainfrom
tomcur:chromaticity-to-color
Open

tomcur wants to merge 1 commit into
linebender:mainfrom
tomcur:chromaticity-to-color

Conversation

@tomcur

@tomcur tomcur commented Jul 17, 2026

Copy link
Copy Markdown
Member

Chromaticity plus a luminance encodes a color. Mathematically the most straightforward coming from xy chromaticity is going to XYZ. As all our color spaces are relative to some reference white, this chooses to represent it as XyzD65, i.e., with a reference white of D65 (as that's the most common reference white).

The docs note to use ColorSpace::convert_absolute to convert this as an absolute color to another color space.

@tomcur tomcur left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

CI is failing due to cargo rdme. #222 should fix that. (And see #chatter > cargo rdme update has CI impacts.)

Chromaticity plus a luminance encodes a color. Mathematically the most
straightforward coming from `xy` chromaticity is going to XYZ. As all
our color spaces are relative to some reference white, we choose to
represent it as `XyzD65`, i.e., with a reference white of D65 (as that's
the most common reference white).

The docs note to use `ColorSpace::convert_absolute` to convert this as
an absolute color to another color space.
@tomcur
tomcur force-pushed the chromaticity-to-color branch from 88d714c to 604d17a Compare July 17, 2026 14:16

@waywardmonkeys waywardmonkeys left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A small matter of asking for 1-2 more tests...

Comment thread color/src/chromaticity.rs
/// assert!(color.iter().all(|component| (component - 1.).abs() < 1e-4));
/// ```
#[inline]
pub const fn with_luminance(self, luminance: f32) -> OpaqueColor<XyzD65> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

+1 to this returning OpaqueColor<XyzD65>!

Comment thread color/src/chromaticity.rs
1e-4,
),
"`Chromaticity::D50.with_luminance(1)` converted without chromatic adaptation to ProPhoto RGB should match `ProphotoRgb::WHITE_COMPONENTS` (which has a D50 reference white)"
);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All of the tests are with a luminance of 1.0, so we should add a test that is different:

  let chromaticity = Chromaticity { x: 0.25, y: 0.4 };
  assert_eq!(
      chromaticity.with_luminance(0.2).components,
      [0.125, 0.2, 0.175],
  );

And maybe one with a luminance of zero.

Comment thread color/src/chromaticity.rs
/// # Example
///
/// ```rust
/// # use color::{Chromaticity, ColorSpace, OpaqueColor, ProphotoRgb, XyzD65};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The import of OpaqueColor here is not needed, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants