Skip to content

Commit

Permalink
improvement: temporarily skip XYZ_Normalized in conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
Walther committed Oct 11, 2023
1 parent 22e785f commit 9d5700e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
5 changes: 3 additions & 2 deletions clovers/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// TODO: more flexible colors?

use crate::colors::{sRGB, sRGB_Linear, XYZ_Normalized, XYZ_Tristimulus};
use crate::colors::{sRGB, sRGB_Linear, XYZ_Tristimulus};
use crate::{Float, Vec3};
use core::iter::Sum;
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign};
Expand Down Expand Up @@ -243,7 +243,8 @@ impl From<sRGB_Linear> for Color {

impl From<XYZ_Tristimulus> for Color {
fn from(value: XYZ_Tristimulus) -> Self {
let value: XYZ_Normalized = value.into();
// FIXME:
// let value: XYZ_Normalized = value.into();
let value: sRGB_Linear = value.into();
let value: Color = value.into();
value
Expand Down
33 changes: 24 additions & 9 deletions clovers/src/colors/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{color::Color, Float};

use super::XYZ_Normalized;
use super::XYZ_Tristimulus;

/// Linear `sRGB` color based on three [Floats](crate::Float) values.
#[derive(Copy, Clone, Debug, PartialEq)]
Expand All @@ -18,9 +18,9 @@ pub struct sRGB_Linear {
}

/// Conversion from XYZ (D65) to linear `sRGB` values <https://color.org/chardata/rgb/sRGB.pdf>
impl From<XYZ_Normalized> for sRGB_Linear {
fn from(value: XYZ_Normalized) -> Self {
let XYZ_Normalized { x, y, z } = value;
impl From<XYZ_Tristimulus> for sRGB_Linear {
fn from(value: XYZ_Tristimulus) -> Self {
let XYZ_Tristimulus { x, y, z } = value;
let r = 3.240_625_5 * x - 1.537_208 * y - 0.498_628_6 * z;
let g = -0.968_930_7 * x + 1.875_756_1 * y + 0.041_517_5 * z;
let b = 0.055_710_1 * x - 0.204_021_1 * y + 1.056_995_9 * z;
Expand All @@ -32,6 +32,21 @@ impl From<XYZ_Normalized> for sRGB_Linear {
sRGB_Linear { r, g, b }
}
}
// FIXME: this should be required, but gets worse results visually?
// impl From<XYZ_Normalized> for sRGB_Linear {
// fn from(value: XYZ_Normalized) -> Self {
// let XYZ_Normalized { x, y, z } = value;
// let r = 3.240_625_5 * x - 1.537_208 * y - 0.498_628_6 * z;
// let g = -0.968_930_7 * x + 1.875_756_1 * y + 0.041_517_5 * z;
// let b = 0.055_710_1 * x - 0.204_021_1 * y + 1.056_995_9 * z;

// let r = r.clamp(0.0, 1.0);
// let g = g.clamp(0.0, 1.0);
// let b = b.clamp(0.0, 1.0);

// sRGB_Linear { r, g, b }
// }
// }

/// Color component transfer function.
/// Note: Produces `sRGB` digital values with a range 0 to 1, which must then be multiplied by 2^(bit depth) – 1 and quantized.
Expand Down Expand Up @@ -96,7 +111,7 @@ impl From<Color> for sRGB_Linear {

#[cfg(test)]
mod tests {
use crate::colors::{sRGB, XYZ_Normalized, XYZ_Tristimulus};
use crate::colors::{sRGB, XYZ_Tristimulus};

use super::sRGB_Linear;

Expand All @@ -107,8 +122,8 @@ mod tests {
y: 0.0,
z: 0.0,
};
let converted: XYZ_Normalized = original.into();
let converted: sRGB_Linear = converted.into();
// FIXME: let converted: XYZ_Normalized = original.into();
let converted: sRGB_Linear = original.into();
let expected = sRGB_Linear {
r: 0.0,
g: 0.0,
Expand All @@ -125,8 +140,8 @@ mod tests {
y: 1.0000,
z: 1.0888,
};
let converted: XYZ_Normalized = original.into();
let converted: sRGB_Linear = converted.into();
// FIXME: let converted: XYZ_Normalized = original.into();
let converted: sRGB_Linear = original.into();
let converted: sRGB = converted.into();
// NOTE: if using floats in the expected, precision errors ensue. Womp womp.
// let expected = sRGB {
Expand Down

0 comments on commit 9d5700e

Please sign in to comment.