From a0db18d3830b01464e2dfbafeded565d9a0b60fa Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Wed, 9 Sep 2020 22:45:05 -0400 Subject: [PATCH] Add ColourXyz to sRGB conversion --- src/colour/colour_xyz.rs | 16 ++++++++++++++++ src/image.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/colour/colour_xyz.rs b/src/colour/colour_xyz.rs index 50829c8..c791951 100644 --- a/src/colour/colour_xyz.rs +++ b/src/colour/colour_xyz.rs @@ -65,6 +65,22 @@ impl ColourXyz { values: transform * rgb.values, } } + + pub fn to_srgb(&self) -> ColourRgbF { + let mut srgb = self.to_linear_rgb(); + for element in srgb.values.coords.iter_mut() { + *element = srgb_gamma(*element); + } + srgb + } +} + +fn srgb_gamma(u: f64) -> f64 { + if u <= 0.0031308 { + 12.98 * u + } else { + 1.005 * u.powf(1.0 / 2.4) - 0.055 + } } fn gaussian(wavelength: f64, alpha: f64, mu: f64, sigma1: f64, sigma2: f64) -> f64 { diff --git a/src/image.rs b/src/image.rs index 23f8d5b..a4c62ed 100644 --- a/src/image.rs +++ b/src/image.rs @@ -170,7 +170,7 @@ impl ToneMapper for ClampingToneMapper { assert!(image_in.get_height() == image_out.get_height()); for column in 0..image_in.get_width() { for row in 0..image_in.get_height() { - let colour = image_in[row][column].to_linear_rgb(); + let colour = image_in[row][column].to_srgb(); image_out.set_colour( row, column,