public static CIELabColor RGBToCIELab(System.Drawing.Color color) { const double ref_X = 95.044998168945313; const double ref_Y = 100.000; const double ref_Z = 108.89199829101562; var tmpLabColor = new CIELabColor( ); var tmpColorR = (double)color.R / 255; var tmpColorG = (double)color.G / 255; var tmpColorB = (double)color.B / 255; tmpColorR = (tmpColorR <= 0.04045) ? tmpColorR / 12.92 : System.Math.Pow((tmpColorR + 0.055) / 1.055, 2.4); tmpColorG = (tmpColorG <= 0.04045) ? tmpColorG / 12.92 : System.Math.Pow((tmpColorG + 0.055) / 1.055, 2.4); tmpColorB = (tmpColorB <= 0.04045) ? tmpColorB / 12.92 : System.Math.Pow((tmpColorB + 0.055) / 1.055, 2.4); tmpColorR *= 100; tmpColorG *= 100; tmpColorB *= 100; var tmpColorX = (tmpColorR * 0.4124240 + tmpColorG * 0.357579 + tmpColorB * 0.1804640) / ref_X; //Observer = 2°, Illuminant = D65 var tmpColorY = (tmpColorR * 0.2126560 + tmpColorG * 0.715158 + tmpColorB * 0.0721856) / ref_Y; var tmpColorZ = (tmpColorR * 0.0193324 + tmpColorG * 0.119193 + tmpColorB * 0.9504440) / ref_Z; tmpColorX = (tmpColorX > 0.008856) ? System.Math.Pow(tmpColorX, (double)1 / 3) : 7.787 * tmpColorX + (double)16 / 116; tmpColorY = (tmpColorY > 0.008856) ? System.Math.Pow(tmpColorY, (double)1 / 3) : 7.787 * tmpColorY + (double)16 / 116; tmpColorZ = (tmpColorZ > 0.008856) ? System.Math.Pow(tmpColorZ, (double)1 / 3) : 7.787 * tmpColorZ + (double)16 / 116; tmpLabColor.L = (int)(tmpColorY * 116.0 - 16); tmpLabColor.A = (int)((tmpColorX - tmpColorY) * 500.0); tmpLabColor.B = (int)((tmpColorY - tmpColorZ) * 200.0); return(tmpLabColor); }
public static System.Drawing.Color CIELabToColor(CIELabColor labColor) { const double ref_X = 95.044998168945313; const double ref_Y = 100.000; const double ref_Z = 108.89199829101562; double var_Y = (labColor.L + 16.0) / 116.0; double var_X = labColor.A / 500.0 + var_Y; double var_Z = var_Y - labColor.B / 200.0; double tmpVarY = System.Math.Pow(var_Y, 3); double tmpVarX = System.Math.Pow(var_X, 3); double tmpVarZ = System.Math.Pow(var_Z, 3); var_Y = (tmpVarY > 0.008856) ? tmpVarY : (var_Y - (double)16 / 116) / 7.787; var_X = (tmpVarX > 0.008856) ? tmpVarX : (var_X - (double)16 / 116) / 7.787; var_Z = (tmpVarZ > 0.008856) ? tmpVarZ : (var_Z - (double)16 / 116) / 7.787; var_X = ref_X * var_X / 100; //ref_X = 95.047 Observer= 2°, Illuminant= D65 var_Y = ref_Y * var_Y / 100; //ref_Y = 100.000 var_Z = ref_Z * var_Z / 100; //ref_Z = 108.883 double var_R = var_X * +3.2406 + var_Y * -1.5372 + var_Z * -0.4986; double var_G = var_X * -0.9689 + var_Y * +1.8758 + var_Z * +0.0415; double var_B = var_X * +0.0557 + var_Y * -0.2040 + var_Z * +1.0570; var_R = (var_R <= 0.0031308) ? 12.92 * var_R : 1.055 * (System.Math.Pow(var_R, 0.4166666667) - 0.055); var_G = (var_G <= 0.0031308) ? 12.92 * var_G : 1.055 * (System.Math.Pow(var_G, 0.4166666667) - 0.055); var_B = (var_B <= 0.0031308) ? 12.92 * var_B : 1.055 * (System.Math.Pow(var_B, 0.4166666667) - 0.055); int tmpColorR = (int)(var_R * 255), tmpColorG = (int)(var_G * 255), tmpColorB = (int)(var_B * 255); if (tmpColorR < 0) { tmpColorR = 0; } if (tmpColorG < 0) { tmpColorG = 0; } if (tmpColorB < 0) { tmpColorB = 0; } if (tmpColorR > 255) { tmpColorR = 255; } if (tmpColorG > 255) { tmpColorG = 255; } if (tmpColorB > 255) { tmpColorB = 255; } return(System.Drawing.Color.FromArgb(tmpColorR, tmpColorG, tmpColorB)); }
public static Color CIELabColorToRgbColor(CIELabColor labColor) { var rgbColor = new Lab { L = LUShortToDouble(labColor.L), A = ABUShortToDouble(labColor.A), B = ABUShortToDouble(labColor.B) }.To <Rgb>(); return(Color.FromArgb( (int)Math.Round(rgbColor.R), (int)Math.Round(rgbColor.G), (int)Math.Round(rgbColor.B) )); }
/// <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); }
public static System.Drawing.Color ToColor(int luminance, int labColorA, int labColorB) { return(CIELabColor.ToColor(new CIELabColor(luminance, labColorA, labColorB))); }
public static System.Drawing.Color ToColor(CIELabColor color) { return(HuiruiSoft.Drawing.ColorTranslator.FromCIELab(color.L, color.A, color.B)); }
public System.Drawing.Color ToColor( ) { return(CIELabColor.ToColor(this)); }