public static RGBColor XYZtoRGB(XYZColor xyz) { float x, y, z; x = xyz.X / 100; //X from 0 to 95.047 (Observer = 2°, Illuminant = D65) y = xyz.Y / 100; //Y from 0 to 100.000 z = xyz.Z / 100; //Z from 0 to 108.883 float r, g, b; r = x * 3.2406f + y * -1.5372f + z * -0.4986f; g = x * -0.9689f + y * 1.8758f + z * 0.0415f; b = x * 0.0557f + y * -0.2040f + z * 1.0570f; if (r > 0.0031308f) { r = 1.055f * (float)Math.Pow(r, (1 / 2.4f)) - 0.055f; } else { r = 12.92f * r; } if (g > 0.0031308f) { g = 1.055f * (float)Math.Pow(g, (1 / 2.4f)) - 0.055f; } else { g = 12.92f * g; } if (b > 0.0031308f) { b = 1.055f * (float)Math.Pow(b, (1 / 2.4f)) - 0.055f; } else { b = 12.92f * b; } //clamp values before making it a RGBColor r = MathHelper.Clamp(r, 0, 1); g = MathHelper.Clamp(g, 0, 1); b = MathHelper.Clamp(b, 0, 1); return(new RGBColor(r, g, b, xyz.Alpha)); }
public static XYZColor RGBtoXYZ(RGBColor rgb) { XYZColor xyz = new XYZColor(); float r, g, b; r = rgb.R; g = rgb.G; b = rgb.B; if (r > 0.04045f) { r = (float)Math.Pow((r + 0.055) / 1.055, 2.4); } else { r = r / 12.92f; } if (g > 0.04045) { g = (float)Math.Pow((g + 0.055f) / 1.055f, 2.4f); } else { g = g / 12.92f; } if (b > 0.04045f) { b = (float)Math.Pow((b + 0.055f) / 1.055f, 2.4f); } else { b = b / 12.92f; } r *= 100; g *= 100; b *= 100; xyz.X = r * 0.4124f + g * 0.3576f + b * 0.1805f; xyz.Y = r * 0.2126f + g * 0.7152f + b * 0.0722f; xyz.Z = r * 0.0193f + g * 0.1192f + b * 0.9505f; xyz.Alpha = rgb.Alpha; return(xyz); }
//TODO: Test XYZtoLAB public static CieLabColor XYZtoLAB(XYZColor xyz) { float var_x = xyz.X / XYZColor.XLIMIT; float var_y = xyz.Y / XYZColor.YLIMIT; float var_z = xyz.Z / XYZColor.ZLIMIT; if (var_x > 0.008856) { var_x = (float)Math.Pow(var_x, (1 / 3f)); } else { var_x = (7.787f * var_x) + (16f / 116f); } if (var_y > 0.008856) { var_y = (float)Math.Pow(var_y, (1 / 3f)); } else { var_y = (7.787f * var_y) + (16f / 116f); } if (var_z > 0.008856) { var_z = (float)Math.Pow(var_z, (1 / 3f)); } else { var_z = (7.787f * var_z) + (16f / 116f); } float var_l = (116f * var_y) - 16f; float var_a = 500 * (var_x - var_y); float var_b = 200 * (var_y - var_z); return(new CieLabColor(var_l, var_a, var_b) { Alpha = xyz.Alpha }); }
public static RGBColor XYZtoRGB(XYZColor xyz) { float x, y, z; x = xyz.X/100; //X from 0 to 95.047 (Observer = 2°, Illuminant = D65) y = xyz.Y/100; //Y from 0 to 100.000 z = xyz.Z/100; //Z from 0 to 108.883 float r, g, b; r = x*3.2406f + y*-1.5372f + z*-0.4986f; g = x*-0.9689f + y*1.8758f + z*0.0415f; b = x*0.0557f + y*-0.2040f + z*1.0570f; if (r > 0.0031308f) r = 1.055f*(float)Math.Pow(r, (1/2.4f)) - 0.055f; else r = 12.92f*r; if (g > 0.0031308f) g = 1.055f*(float)Math.Pow(g ,(1/2.4f)) - 0.055f; else g = 12.92f*g; if (b > 0.0031308f) b = 1.055f*(float)Math.Pow(b, (1/2.4f)) - 0.055f; else b = 12.92f*b; //clamp values before making it a RGBColor r = MathHelper.Clamp(r, 0, 1); g = MathHelper.Clamp(g, 0, 1); b = MathHelper.Clamp(b, 0, 1); return new RGBColor(r,g,b,xyz.Alpha); }
//TODO: Test XYZtoLAB public static CieLabColor XYZtoLAB(XYZColor xyz) { float var_x = xyz.X/XYZColor.XLIMIT; float var_y = xyz.Y/XYZColor.YLIMIT; float var_z = xyz.Z/XYZColor.ZLIMIT; if (var_x > 0.008856) var_x = (float) Math.Pow(var_x, (1/3f)); else var_x = (7.787f*var_x) + (16f/116f); if (var_y > 0.008856) var_y = (float)Math.Pow(var_y, (1 / 3f)); else var_y = (7.787f * var_y) + (16f / 116f); if (var_z > 0.008856) var_z = (float)Math.Pow(var_z, (1 / 3f)); else var_z = (7.787f * var_z) + (16f / 116f); float var_l = (116f * var_y) - 16f; float var_a = 500 * (var_x - var_y); float var_b = 200 * (var_y - var_z); return new CieLabColor(var_l, var_a, var_b) { Alpha = xyz.Alpha}; }
public static XYZColor RGBtoXYZ(RGBColor rgb) { XYZColor xyz = new XYZColor(); float r, g, b; r = rgb.R; g = rgb.G; b = rgb.B; if (r > 0.04045f) r = (float)Math.Pow((r + 0.055)/1.055, 2.4); else r = r / 12.92f; if (g > 0.04045) g =(float)Math.Pow((g + 0.055f)/1.055f , 2.4f); else g = g/12.92f; if (b > 0.04045f) b = (float)Math.Pow((b + 0.055f)/1.055f , 2.4f); else b = b/12.92f; r *= 100; g *= 100; b *= 100; xyz.X = r*0.4124f + g*0.3576f + b*0.1805f; xyz.Y = r*0.2126f + g*0.7152f + b*0.0722f; xyz.Z = r*0.0193f + g*0.1192f + b*0.9505f; xyz.Alpha = rgb.Alpha; return xyz; }