/// <summary> /// Transforms this UnityEngine.Color into CIEColor.CIE_XYZColor. /// </summary> /// <returns>The CIE XYZ value of the Color.</returns> public static CIE_XYZColor ToCIE_XYZ(this Color color) { //Ecuations: http://www.brucelindbloom.com/ CIE_XYZColor cieXyz = new CIE_XYZColor(); //Inverse companding float auxR = color.r <= 0.04045f ? color.r / 12.92f : Mathf.Pow((color.r + 0.055f) / 1.055f, 2.4f); float auxG = color.g <= 0.04045f ? color.g / 12.92f : Mathf.Pow((color.g + 0.055f) / 1.055f, 2.4f); float auxB = color.b <= 0.04045f ? color.b / 12.92f : Mathf.Pow((color.b + 0.055f) / 1.055f, 2.4f); //Linear RGB to XYZ float[][] transformationMatrix = { new float[] { 0.4124564f, 0.3575761f, 0.1804375f }, new float[] { 0.2126729f, 0.7151522f, 0.0721750f }, new float[] { 0.0193339f, 0.1191920f, 0.9503041f } }; float[][] rgbMatrix = { new float[] { auxR }, new float[] { auxG }, new float[] { auxB } }; float[][] cieXyzMatrix = MathMatrix.MatrixMultiplication(transformationMatrix, rgbMatrix); cieXyz._x = cieXyzMatrix[0][0]; cieXyz._y = cieXyzMatrix[1][0]; cieXyz._z = cieXyzMatrix[2][0]; return(cieXyz); }
/// <summary> /// Transforms this UnityEngine.Color into CIEColor.CIELabColor. /// </summary> /// <returns>The CIE Lab value of the Color.</returns> public static CIELabColor ToCIELab(this Color color) { //Ecuations: http://www.brucelindbloom.com/ CIE_XYZColor cieXyz = color.ToCIE_XYZ(); CIE_XYZColor referenceWhite = Color.white.ToCIE_XYZ(); //xr = X / Xr float auxX = cieXyz._x / referenceWhite._x; //yr = Y / Yr float auxY = cieXyz._y / referenceWhite._y; //zr = Z / Zr float auxZ = cieXyz._z / referenceWhite._z; //if xr > 0.008856 // fx = xr ^ 1/3 //else // fx = (903.3 * xr + 16) / 116 auxX = (auxX > 0.008856f) ? Mathf.Pow(auxX, 1 / 3f) : ((903.3f * auxX + 16) / 116f); //if yr > 0.008856 // fy = yr ^ 1/3 //else // fy = (903.3 * yr + 16) / 116 auxY = (auxY > 0.008856f) ? Mathf.Pow(auxY, 1 / 3f) : ((903.3f * auxY + 16) / 116f); //if zr > 0.008856 // fz = zr ^ 1/3 //else // fz = (903.3 * zr + 16) / 116 auxZ = (auxZ > 0.008856f) ? Mathf.Pow(auxZ, 1 / 3f) : ((903.3f * auxZ + 16) / 116f); CIELabColor cieLab = new CIELabColor(); //L = 116 * fy -16 cieLab._l = 116 * auxY - 16; //a = 500 * (fx - fy) cieLab._a = 500 * (auxX - auxY); //b = 200 * (fy - fz) cieLab._b = 200 * (auxY - auxZ); return(cieLab); }