From: Fraser Waters Date: Mon, 10 Nov 2014 00:59:53 +0000 (+0100) Subject: Add To/From sRGB methods to Color4. X-Git-Tag: 2.0-0~76^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=af924afef435cabd406c94abe270f1c4ab74a2a6;p=platform%2Fcore%2Fcsapi%2Fopentk.git Add To/From sRGB methods to Color4. --- diff --git a/Source/OpenTK/Graphics/Color4.cs b/Source/OpenTK/Graphics/Color4.cs index 972747a..7ccef16 100644 --- a/Source/OpenTK/Graphics/Color4.cs +++ b/Source/OpenTK/Graphics/Color4.cs @@ -914,6 +914,98 @@ namespace OpenTK.Graphics #endregion + #region Color conversions + + #region sRGB + + /// + /// Converts sRGB color values to RGB color values. + /// + /// + /// Returns the converted color value. + /// + /// + /// Color value to convert in sRGB. + /// + public static Color4 FromSrgb(Color4 srgb) + { + float r, g, b; + + if (srgb.R <= 0.04045f) + { + r = srgb.R / 12.92f; + } + else + { + r = (float)Math.Pow((srgb.R + 0.055f) / (1.0f + 0.055f), 2.4f); + } + + if (srgb.G <= 0.04045f) + { + g = srgb.G / 12.92f; + } + else + { + g = (float)Math.Pow((srgb.G + 0.055f) / (1.0f + 0.055f), 2.4f); + } + + if (srgb.B <= 0.04045f) + { + b = srgb.B / 12.92f; + } + else + { + b = (float)Math.Pow((srgb.B + 0.055f) / (1.0f + 0.055f), 2.4f); + } + + return new Color4(r, g, b, srgb.A); + } + + /// + /// Converts RGB color values to sRGB color values. + /// + /// + /// Returns the converted color value. + /// + /// Color value to convert. + public static Color4 ToSrgb(Color4 rgb) + { + float r, g, b; + + if (rgb.R <= 0.0031308) + { + r = 12.92f * rgb.R; + } + else + { + r = (1.0f + 0.055f) * (float)Math.Pow(rgb.R, 1.0f / 2.4f) - 0.055f; + } + + if (rgb.G <= 0.0031308) + { + g = 12.92f * rgb.G; + } + else + { + g = (1.0f + 0.055f) * (float)Math.Pow(rgb.G, 1.0f / 2.4f) - 0.055f; + } + + if (rgb.B <= 0.0031308) + { + b = 12.92f * rgb.B; + } + else + { + b = (1.0f + 0.055f) * (float)Math.Pow(rgb.B, 1.0f / 2.4f) - 0.055f; + } + + return new Color4(r, g, b, rgb.A); + } + + #endregion + + #endregion + #region IEquatable Members ///