internal static LchColor LabToLch(LabColor lab) { var c = Math.Sqrt(lab.A * lab.A + lab.B * lab.B); var h = (Math.Atan2(lab.B, lab.A) * (180 / Math.PI) + 360) % 360; if (Math.Round(c * 10000) == 0) { h = double.NaN; } return(new LchColor(lab.L, c, h)); }
internal static Color LabToRgb(LabColor lab) { Func <double, double> xyz_rgb = (t) => { return(255 * (t <= 0.00304 ? 12.92 * t : 1.055 * Math.Pow(t, 1 / 2.4) - 0.055)); }; Func <double, double> lab_xyz = (t) => { if (t > LabColor.LabConstants.T1) { return(t * t * t); } else { return(LabColor.LabConstants.T2 * (t - LabColor.LabConstants.T0)); } }; double a, b, g, l, r, x, y, z; l = lab.L; a = lab.A; b = lab.B; y = (l + 16) / 116; x = double.IsNaN(a) ? y : y + a / 500; z = double.IsNaN(b) ? y : y - b / 200; y = LabColor.LabConstants.Yn * lab_xyz(y); x = LabColor.LabConstants.Xn * lab_xyz(x); z = LabColor.LabConstants.Zn * lab_xyz(z); r = xyz_rgb(3.2404542 * x - 1.5371385 * y - 0.4985314 * z); g = xyz_rgb(-0.9692660 * x + 1.8760108 * y + 0.0415560 * z); b = xyz_rgb(0.0556434 * x - 0.2040259 * y + 1.0572252 * z); return(new Color(Convert.ToInt32(r), Convert.ToInt32(g), Convert.ToInt32(b), lab.Alpha)); }