From 679419bb67321536aa8cf40d2a51455762bc0017 Mon Sep 17 00:00:00 2001 From: Fraser Waters Date: Mon, 10 Nov 2014 01:37:37 +0100 Subject: [PATCH] Add To/From XYZ methods to Color4. --- Source/OpenTK/Graphics/Color4.cs | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Source/OpenTK/Graphics/Color4.cs b/Source/OpenTK/Graphics/Color4.cs index 5da242fd..3b136f02 100644 --- a/Source/OpenTK/Graphics/Color4.cs +++ b/Source/OpenTK/Graphics/Color4.cs @@ -1238,6 +1238,46 @@ namespace OpenTK.Graphics #endregion + #region XYZ + + /// + /// Converts XYZ color values to RGB color values. + /// + /// + /// Returns the converted color value. + /// + /// + /// Color value to convert with the trisimulus values of X, Y, and Z in the corresponding element, and the W element with Alpha (which is copied to the output's Alpha value). + /// Each has a range of 0.0 to 1.0. + /// + /// Uses the CIE XYZ colorspace. + public static Color4 FromXyz(Vector4 xyz) + { + var r = 0.41847f * xyz.X + -0.15866f * xyz.Y + -0.082835f * xyz.Z; + var g = -0.091169f * xyz.X + 0.25243f * xyz.Y + 0.015708f * xyz.Z; + var b = 0.00092090f * xyz.X + -0.0025498f * xyz.Y + 0.17860f * xyz.Z; + return new Color4(r, g, b, xyz.W); + } + + /// + /// Converts RGB color values to XYZ color values. + /// + /// + /// Returns the converted color value with the trisimulus values of X, Y, and Z in the corresponding element, and the W element with Alpha (a copy of the input's Alpha value). + /// Each has a range of 0.0 to 1.0. + /// + /// Color value to convert. + /// Uses the CIE XYZ colorspace. + public static Vector4 ToXyz(Color4 rgb) + { + var x = (0.49f * rgb.R + 0.31f * rgb.G + 0.20f * rgb.B) / 0.17697f; + var y = (0.17697f * rgb.R + 0.81240f * rgb.G + 0.01063f * rgb.B) / 0.17697f; + var z = (0.00f * rgb.R + 0.01f * rgb.G + 0.99f * rgb.B) / 0.17697f; + return new Vector4(x, y, z, rgb.A); + } + + #endregion + #endregion #region IEquatable Members