private static Tuple<ColorRGB, string> SafeConvert(ColorRGBFull rgb, ColorRGB result) { if (IsOutside(rgb)) { return Tuple.Create(result, $"Cannot convert colors. Approximated by ({result.R}, {result.G}, {result.B})"); } return Tuple.Create(result, string.Empty); }
public static Tuple<ColorRGB, string> ToRGBFromXYZ(ColorRGBFull rgb) { var result = Colors.ToRGBFromXYZ(rgb); return SafeConvert(rgb, result); }
private static bool IsOutside(ColorRGBFull rgb) { return (rgb.R + Epsilon) < 0 || (rgb.G + Epsilon) < 0 || (rgb.B + Epsilon) < 0 || (rgb.R - Epsilon) > 1 || (rgb.G - Epsilon) > 1 || (rgb.B - Epsilon) > 1; }
public static ColorXYZFull ToXYZ(ColorRGBFull rgb) { var r = InverseGammaCorrection(rgb.R); var g = InverseGammaCorrection(rgb.G); var b = InverseGammaCorrection(rgb.B); var xyz = Multiply(RGBToXYZ, r, g, b); return new ColorXYZFull(xyz.R, xyz.G, xyz.B); }
public static Tuple<ColorHSVFull, string> ToHSVFromXYZ(ColorRGBFull rgb) { var result = Colors.ToHSVFromXYZ(rgb); if (IsOutside(rgb)) { return Tuple.Create(result, $"Cannot convert colors. Approximated by ~({result.Hue:##0.000}, {result.Saturation:0.000}, {result.Value:0.000})"); } return Tuple.Create(result, string.Empty); }
public static ColorRGB ToRGBFromXYZ(ColorRGBFull rgb) { var r = Clamp(rgb.R); var g = Clamp(rgb.G); var b = Clamp(rgb.B); r = GammaCorrect(r); g = GammaCorrect(g); b = GammaCorrect(b); r *= ColorRGB.MaxValue; g *= ColorRGB.MaxValue; b *= ColorRGB.MaxValue; return new ColorRGB((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b)); }
public static ColorRGB ToRGBFromXYZUniform(ColorRGBFull rgb) { var r = Clamp(rgb.R / 100.0); var g = Clamp(rgb.G / 100.0); var b = Clamp(rgb.B / 100.0); var max = Math.Max(r, Math.Max(g, b)); r /= max; g /= max; b /= max; r = GammaCorrect(r); g = GammaCorrect(g); b = GammaCorrect(b); r *= ColorRGB.MaxValue; g *= ColorRGB.MaxValue; b *= ColorRGB.MaxValue; return new ColorRGB((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b)); }
public static ColorRGB ToRGB(ColorRGBFull rgb) { var r = Math.Round(Clamp(rgb.R) * ColorRGB.MaxValue); var g = Math.Round(Clamp(rgb.G) * ColorRGB.MaxValue); var b = Math.Round(Clamp(rgb.B) * ColorRGB.MaxValue); return new ColorRGB((int)r, (int)g, (int)b); }
public static ColorHSVFull ToHSVFromXYZ(ColorRGBFull rgb) { var r = Clamp(rgb.R); var g = Clamp(rgb.G); var b = Clamp(rgb.B); r = GammaCorrect(r); g = GammaCorrect(g); b = GammaCorrect(b); return ToHSV(new ColorRGBFull(r, g, b)); }
public static ColorHSVFull ToHSV(ColorRGBFull rgb) { var min = Math.Min(rgb.R, Math.Min(rgb.G, rgb.B)); var max = Math.Max(rgb.R, Math.Max(rgb.G, rgb.B)); if (min == max) { return new ColorHSVFull(0, 0, min); } else { var f = rgb.R == min ? rgb.G - rgb.B : rgb.G == min ? rgb.B - rgb.R : rgb.R - rgb.G; var i = rgb.R == min ? 3 : rgb.G == min ? 5 : 1; var hue = (i - f / (max - min)) * 60.0 % 360.0; var saturation = (max - min) / max; var value = max; return new ColorHSVFull(hue, saturation, value); } }
public static uint GetBytes(ColorRGBFull rgb) { return GetBytes(ToRGB(rgb)); }
public static ColorRGBFull Clamp(ColorRGBFull rgb) { var r = Clamp(rgb.R); var g = Clamp(rgb.G); var b = Clamp(rgb.B); return new ColorRGBFull(r, g, b); }