← Back to Pull requests

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

Open
tomcur opened this pull request with 92 lines diff across 2 files
View on GitHub
tomcur tomcur commented on Jul 17, 2026 13:44 UTC
Author

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.

Diff: 92 lines (+83 / -9) across 2 files Reviewer: waywardmonkeys Review duration: 118470m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.39) × (1 − Review Confidence (0.83)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.39 · 92 lines diff across 2 files
0.39
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.79 · 3 substantive comment(s)
0.79
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
83%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.271
Real PR Conversation Timeline
7 items
tomcur tomcur commented on Jul 17, 2026 13:48 UTC
Reviewer

CI is failing due to cargo rdme. https://github.com/linebender/color/pull/222 should fix that. (And see #chatter > cargo rdme update has CI impacts.)

waywardmonkeys waywardmonkeys commented on code on Jul 17, 2026 14:18 UTC
Code Review
color/src/chromaticity.rs:1
@@ -54,19 +54,47 @@ impl Chromaticity {
         y: 0.33767,
     };
 
-    /// Convert the `xy` chromaticities to XYZ, assuming `xyY` with `Y=1`.
-    pub(crate) const fn to_xyz(self) -> [f32; 3] {
-        let y_recip = 1. / self.y;
-        [self.x * y_recip, 1., (1. - self.x - self.y) * y_recip]
+    /// Get the color at this chromaticity with the given `luminance`.
+    ///
+    /// If you convert the color returned by this method to another color space, think carefully
+    /// about whether you want to chromatically adapt the color or not. This method returns the
+    /// absolute color in [XYZ-D65][`XyzD65`], i.e., encoded with a reference white of [`Chromaticity::D65`].
+    /// To get the same absolute color in another color space, use
+    /// [`ColorSpace::convert_absolute`][crate::ColorSpace::convert_absolute].
+    ///
+    /// See the [XYZ-D65](`XyzD65`) color space documentation for some background information on the
+    /// meaning of "reference white."
+    ///
+    /// Note, if [`Self::y`] is zero, the resulting components are non-finite.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// # use color::{Chromaticity, ColorSpace, OpaqueColor, ProphotoRgb, XyzD65};
+    /// // This will be within conversion error of `ProphotoRgb::WHITE_COMPONENTS`,
+    /// // i.e., `[1., 1., 1.]`.
+    /// let color = XyzD65::convert_absolute::<ProphotoRgb>(
+    ///     Chromaticity::D50.with_luminance(1.).components
+    /// );
+    /// assert!(color.iter().all(|component| (component - 1.).abs() < 1e-4));
+    /// ```
+    #[inline]
+    pub const fn with_luminance(self, luminance: f32) -> OpaqueColor<XyzD65> {

+1 to this returning OpaqueColor<XyzD65>!

waywardmonkeys waywardmonkeys commented on code on Jul 17, 2026 14:20 UTC
Code Review
color/src/chromaticity.rs:1
@@ -125,3 +153,47 @@ impl Chromaticity {
         [0.0389, -0.0685, 1.0296],
     ];
 }
+
+#[cfg(test)]
+mod tests {
+    use crate::{ColorSpace, OpaqueColor, ProphotoRgb, XyzD50, XyzD65};
+
+    use super::Chromaticity;
+
+    #[must_use]
+    fn almost_equal<CS: ColorSpace>(col1: [f32; 3], col2: [f32; 3], absolute_epsilon: f32) -> bool {
+        OpaqueColor::<CS>::new(col1).difference(OpaqueColor::new(col2)) <= absolute_epsilon
+    }
+
+    #[test]
+    fn reference_white() {
+        assert!(
+            almost_equal::<XyzD65>(
+                Chromaticity::D65.with_luminance(1.).components,
+                XyzD65::WHITE_COMPONENTS,
+                1e-4,
+            ),
+            "`Chromaticity::D65.with_luminance(1)` should match `XyzD65::WHITE_COMPONENTS`"
+        );
+
+        assert!(
+            almost_equal::<XyzD65>(
+                Chromaticity::D50.with_luminance(1.).components,
+                XyzD50::WHITE_COMPONENTS,
+                1e-4,
+            ),
+            "`Chromaticity::D50.with_luminance(1)` should match `XyzD50::WHITE_COMPONENTS`"
+        );
+
+        assert!(
+            almost_equal::<ProphotoRgb>(
+                XyzD65::convert_absolute::<ProphotoRgb>(
+                    Chromaticity::D50.with_luminance(1.).components
+                ),
+                ProphotoRgb::WHITE_COMPONENTS,
+                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)"
+        );

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.

waywardmonkeys waywardmonkeys commented on Jul 17, 2026 14:22 UTC
Reviewer

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

waywardmonkeys waywardmonkeys approved these changes on Jul 17, 2026 14:22 UTC
Approved Reviewer

Approved these changes.

waywardmonkeys waywardmonkeys commented on code on Jul 17, 2026 19:31 UTC
Code Review
color/src/chromaticity.rs:1
@@ -54,19 +54,47 @@ impl Chromaticity {
         y: 0.33767,
     };
 
-    /// Convert the `xy` chromaticities to XYZ, assuming `xyY` with `Y=1`.
-    pub(crate) const fn to_xyz(self) -> [f32; 3] {
-        let y_recip = 1. / self.y;
-        [self.x * y_recip, 1., (1. - self.x - self.y) * y_recip]
+    /// Get the color at this chromaticity with the given `luminance`.
+    ///
+    /// If you convert the color returned by this method to another color space, think carefully
+    /// about whether you want to chromatically adapt the color or not. This method returns the
+    /// absolute color in [XYZ-D65][`XyzD65`], i.e., encoded with a reference white of [`Chromaticity::D65`].
+    /// To get the same absolute color in another color space, use
+    /// [`ColorSpace::convert_absolute`][crate::ColorSpace::convert_absolute].
+    ///
+    /// See the [XYZ-D65](`XyzD65`) color space documentation for some background information on the
+    /// meaning of "reference white."
+    ///
+    /// Note, if [`Self::y`] is zero, the resulting components are non-finite.
+    ///
+    /// # Example
+    ///
+    /// ```rust
+    /// # use color::{Chromaticity, ColorSpace, OpaqueColor, ProphotoRgb, XyzD65};

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

Assignees
tomcur tomcur
Labels
risk: low confidence: 83.3% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 38.6% (MEDIUM) · Higher = larger blast radius
38.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 83.3% · Lower = rushed or shallow review
83.3%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.4% · Safe to merge
6.4%