Browse Source

CieXyz color space: add transform CieXyzToColor

Former-commit-id: 443e4165941ac4de303c983a84e99721c533e34f
Former-commit-id: 540d751f3c78e57ba07fc70437680822d77cf73e
Former-commit-id: 2c25af1e056e30c0233878f3b0d8c4c1dd54c9ca
pull/17/head
Damiaan 11 years ago
parent
commit
36d239d49c
  1. 25
      src/ImageProcessor/Colors/ColorTransforms.cs
  2. 16
      tests/ImageProcessor.Tests/Colors/ColorConversionTests.cs

25
src/ImageProcessor/Colors/ColorTransforms.cs

@ -67,6 +67,31 @@ namespace ImageProcessor
return new Color(r, g, b);
}
public static implicit operator Color(CieXyz color)
{
var x = color.X;
var y = color.Y;
var z = color.Z;
// assume sRGB
// http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
float r = (x * 3.240969941904521F) + (y * -1.537383177570093F) + (z * -0.498610760293F);
float g = (x * -0.96924363628087F) + (y * 1.87596750150772F) + (z * 0.041555057407175F);
float b = (x * 0.055630079696993F) + (y * -0.20397695888897F) + (z * 1.056971514242878F);
r = r > 0.0031308 ? (float)((1.055 * Math.Pow(r, 1.0 / 2.4)) - 0.055) : r = (r * 12.92F);
g = g > 0.0031308 ? (float)((1.055 * Math.Pow(g, 1.0 / 2.4)) - 0.055) : g = (g * 12.92F);
b = b > 0.0031308 ? (float)((1.055 * Math.Pow(b, 1.0 / 2.4)) - 0.055) : b = (b * 12.92F);
r = Math.Min(Math.Max(0, r), 1);
g = Math.Min(Math.Max(0, g), 1);
b = Math.Min(Math.Max(0, b), 1);
return new Color(r, g, b);
}
/// <summary>
/// Allows the implicit conversion of an instance of <see cref="Hsv"/> to a
/// <see cref="Color"/>.

16
tests/ImageProcessor.Tests/Colors/ColorConversionTests.cs

@ -103,6 +103,22 @@ namespace ImageProcessor.Tests
}
[Fact]
public void CieXyzToColor()
{
CieXyz cieXyz = new CieXyz(0.25F, 0.40F, 0.10F);
Color color = cieXyz;
Assert.Equal(0.4174F, color.R, 3);
Assert.Equal(0.7434F, color.G, 3);
Assert.Equal(0.2162F, color.B, 2);
//xyz2rgb([0.25 0.40 0.10])
//ans = 0.4174 0.7434 0.2152
}
/// <summary>
/// Tests the implicit conversion from <see cref="Color"/> to <see cref="Hsv"/>.
/// </summary>

Loading…
Cancel
Save