Beispiel #1
0
 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);
 }
Beispiel #2
0
 public static Tuple<ColorRGB, string> ToRGBFromXYZ(ColorRGBFull rgb)
 {
     var result = Colors.ToRGBFromXYZ(rgb);
     return SafeConvert(rgb, result);
 }
Beispiel #3
0
 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;
 }
Beispiel #4
0
 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);
 }
Beispiel #5
0
 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);
 }
Beispiel #6
0
 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));
 }
Beispiel #7
0
 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));
 }
Beispiel #8
0
 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);
 }
Beispiel #9
0
 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));
 }
Beispiel #10
0
        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);
            }
        }
Beispiel #11
0
 public static uint GetBytes(ColorRGBFull rgb)
 {
     return GetBytes(ToRGB(rgb));
 }
Beispiel #12
0
 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);
 }