Exemplo n.º 1
0
        public static DoubleColor Alter(
            this DoubleColor c,
            double?r = null,
            double?g = null,
            double?b = null,
            double?a = null
            )
        {
            if (r != null)
            {
                c.R = r.Value;
            }

            if (g != null)
            {
                c.G = g.Value;
            }

            if (b != null)
            {
                c.B = b.Value;
            }

            if (a != null)
            {
                c.A = a.Value;
            }

            return(c);
        }
        ///
        /// <summary>
        /// Return value of color for WritableBitmap</summary>
        ///
        public static int ToARGB32(
            this DoubleColor c
            )
        {
            Color color = c.ToColor();

            return(color.ToARGB32());
        }
Exemplo n.º 3
0
        public static DoubleColor Lighter(
            this DoubleColor c,
            double coff = 0.25
            )
        {
            AHSL hsl = ColorExt.ToAHSL(c);

            hsl.Luminance = MathEx.Lerp(hsl.Luminance, 1, coff);

            return(ColorExt.Double(hsl));
        }
Exemplo n.º 4
0
 public static DoubleColor Invert(
     this DoubleColor c
     )
 {
     return(new DoubleColor()
     {
         A = c.A,
         R = 255 - c.R,
         G = 255 - c.G,
         B = 255 - c.B
     });
 }
 public static Color ToColor(
     this DoubleColor dc
     )
 {
     return(new Color()
     {
         A = (byte)Math.Round(dc.A, 0),
         R = (byte)Math.Round(dc.R, 0),
         G = (byte)Math.Round(dc.G, 0),
         B = (byte)Math.Round(dc.B, 0)
     });
 }
        public static DoubleColor Double(
            this AYUV yuv
            )
        {
            DoubleColor c = DoubleColor.Empty;

            c.R = (yuv.Y + 1.139837398373983740 * yuv.V) * 255.0;
            c.G = (yuv.Y - 0.3946517043589703515 * yuv.U - 0.5805986066674976801 * yuv.V) * 255.0;
            c.B = (yuv.Y + 2.032110091743119266 * yuv.U) * 255.0;
            c.A = yuv.A * 255.0;

            return(c);
        }
        public static DoubleColor Double(
            this AHSB ahsb
            )
        {
            double      a          = ahsb.Alpha;
            double      hue        = ahsb.HueDegree;
            double      saturation = ahsb.Saturation;
            double      value      = ahsb.Brightness;
            DoubleColor ret;

            int    hi = ((int)(hue / 60)) % 6;
            double f  = hue / 60 - (int)(hue / 60);

            value = value * 255;
            double v = value;
            double p = (value * (1 - saturation));
            double q = (value * (1 - f * saturation));
            double t = (value * (1 - (1 - f) * saturation));

            if (hi == 0)
            {
                ret = new DoubleColor(a * 255, v, t, p);
            }
            else if (hi == 1)
            {
                ret = new DoubleColor(a * 255, q, v, p);
            }
            else if (hi == 2)
            {
                ret = new DoubleColor(a * 255, p, v, t);
            }
            else if (hi == 3)
            {
                ret = new DoubleColor(a * 255, p, q, v);
            }
            else if (hi == 4)
            {
                ret = new DoubleColor(a * 255, t, p, v);
            }
            else
            {
                ret = new DoubleColor(a * 255, v, p, q);
            }

            ret.HueDegree  = hue;
            ret.Saturation = saturation;

            return(ret);
        }
Exemplo n.º 8
0
        public static DoubleColor AlterHue(
            this DoubleColor c,
            ColorWheelBase wheel,
            double hue
            )
        {
            AHSB src  = c.ToAHSB();
            AHSB dest = wheel.GetColor(hue).ToAHSB();

            dest.Saturation = src.Saturation;
            dest.Brightness = src.Brightness;
            dest.Alpha      = src.Alpha;

            return(dest.Double());
        }
        public static DoubleColor Lerp(
            this DoubleColor c1,
            DoubleColor c2,
            double amount                                      // value between 0.0 and 1.0
            )
        {
            var c = new DoubleColor()
            {
                A = MathEx.Lerp(c1.A, c2.A, amount),
                R = MathEx.Lerp(c1.R, c2.R, amount),
                G = MathEx.Lerp(c1.G, c2.G, amount),
                B = MathEx.Lerp(c1.B, c2.B, amount)
            };

            return(c);
        }
        public static AHSB ToAHSB(
            this DoubleColor c
            )
        {
            double a = c.A / 255.0;
            double r = c.R / 255.0;
            double g = c.G / 255.0;
            double b = c.B / 255.0;

            AHSB ret;

            double max = Math.Max(r, Math.Max(g, b));
            double min = Math.Min(r, Math.Min(g, b));

            //
            // Black - gray - white
            //
            if (min == max)
            {
                ret = new AHSB(a, 0, 0, (double)min);
            }
            else
            {
                var d = (r == min) ? g - b : ((b == min) ? r - g : b - r);
                var h = (r == min) ? 3 : ((b == min) ? 1 : 5);

                double hue   = 60 * (h - d / (max - min));
                double sat   = (max - min) / max;
                double value = max;

                ret = new AHSB(a, hue, sat, value);
            }

            if (ret.Saturation == 0)
            {
                ret.HueDegree = c.HueDegree;
            }

            if (ret.Brightness == 0)
            {
                ret.Saturation = c.Saturation;
            }

            return(ret);
        }
        public static AYUV ToAYUV(
            this DoubleColor c
            )
        {
            AYUV yuv = new AYUV();

            //
            // normalizes red, green, blue values
            //
            double r = (double)c.R / 255.0;
            double g = (double)c.G / 255.0;
            double b = (double)c.B / 255.0;

            yuv.Y = 0.299 * r + 0.587 * g + 0.114 * b;
            yuv.U = -0.14713 * r - 0.28886 * g + 0.436 * b;
            yuv.V = 0.615 * r - 0.51499 * g - 0.10001 * b;
            yuv.A = c.A / 255.0;

            return(yuv);
        }
        protected virtual void UpdateRgbComponents(
            )
        {
            DoubleColor p = m_previous;
            DoubleColor c = m_color;

            if (p.R != c.R)
            {
                FirePropertyChanged("R");
            }

            if (p.G != c.G)
            {
                FirePropertyChanged("G");
            }

            if (p.B != c.B)
            {
                FirePropertyChanged("B");
            }
        }
        public static CIEAXYZ ToCIEAXYZ(
            this DoubleColor c
            )
        {
            // normalize
            double rl    = c.R / 255.0;
            double gl    = c.G / 255.0;
            double bl    = c.B / 255.0;
            double alpha = c.A / 255.0;

            // convert to a sRGB form
            double r = (rl > 0.04045) ? Math.Pow((rl + 0.055) / (1.055), 2.4) : (rl / 12.92);
            double g = (gl > 0.04045) ? Math.Pow((gl + 0.055) / (1.055), 2.4) : (gl / 12.92);
            double b = (bl > 0.04045) ? Math.Pow((bl + 0.055) / (1.055), 2.4) : (bl / 12.92);

            // converts
            return(new CIEAXYZ(
                       alpha,
                       (r * 0.4124 + g * 0.3576 + b * 0.1805),
                       (r * 0.2126 + g * 0.7152 + b * 0.0722),
                       (r * 0.0193 + g * 0.1192 + b * 0.9505)));
        }
 public static CIELab ToCIELab(
     this DoubleColor c
     )
 {
     return(c.ToCIEAXYZ().ToCIELab());
 }
        public static AHSL ToAHSL(
            this DoubleColor c
            )
        {
            double h = 0;
            double s = 0;
            double l = 0;
            double a = 0;

            double r = (double)c.R / 255.0;
            double g = (double)c.G / 255.0;
            double b = (double)c.B / 255.0;

            a = (double)c.A / 255.0;

            double max = Math.Max(r, Math.Max(g, b));
            double min = Math.Min(r, Math.Min(g, b));

            // hue
            if (max == min)
            {
                h = 0; // undefined
            }
            else if (max == r && g >= b)
            {
                h = 60.0 * (g - b) / (max - min);
            }
            else if (max == r && g < b)
            {
                h = 60.0 * (g - b) / (max - min) + 360.0;
            }
            else if (max == g)
            {
                h = 60.0 * (b - r) / (max - min) + 120.0;
            }
            else if (max == b)
            {
                h = 60.0 * (r - g) / (max - min) + 240.0;
            }

            // luminance
            l = (max + min) / 2.0;

            // saturation
            if (l == 0 || max == min)
            {
                s = 0;
            }
            else if (0 < l && l <= 0.5)
            {
                s = (max - min) / (max + min);
            }
            else if (l > 0.5)
            {
                s = (max - min) / (2 - (max + min));
            }

            if (s == 0) // gray
            {
                h = c.HueDegree;
            }

            if (l == 0)
            {
                s = c.Saturation;
            }

            return(new AHSL(h, s, l, a));
        }
 public abstract double GetAngle(
     DoubleColor c
     );
 public override double GetAngle(
     DoubleColor c
     )
 {
     return(ToWheelAngle(c.ToAHSB().HueDegree));
 }
 public override double GetAngle(
     DoubleColor c
     )
 {
     return(c.HueDegree);
 }
        public static DoubleColor Double(
            this AHSL hsl
            )
        {
            double      h = hsl.HueDegree;
            double      s = hsl.Saturation;
            double      l = hsl.Luminance;
            double      a = hsl.Alpha;
            DoubleColor ret;

            if (s == 0)
            {
                // achromatic color (gray scale)
                ret = new DoubleColor(a * 255.0, l * 255.0, l * 255.0, l * 255.0);
            }
            else
            {
                double q = (l < 0.5) ? (l * (1.0 + s)) : (l + s - (l * s));
                double p = (2.0 * l) - q;

                double   Hk = h / 360.0;
                double[] T  = new double[3];

                T[0] = Hk + (1.0 / 3.0);    // Tr
                T[1] = Hk;                  // Tb
                T[2] = Hk - (1.0 / 3.0);    // Tg

                for (int i = 0; i < 3; i++)
                {
                    if (T[i] < 0)
                    {
                        T[i] += 1.0;
                    }
                    if (T[i] > 1)
                    {
                        T[i] -= 1.0;
                    }

                    if ((T[i] * 6) < 1)
                    {
                        T[i] = p + ((q - p) * 6.0 * T[i]);
                    }
                    else if ((T[i] * 2.0) < 1) // (1.0 / 6.0) <= T[i] && T[i] < 0.5
                    {
                        T[i] = q;
                    }
                    else if ((T[i] * 3.0) < 2) // 0.5 <= T[i] && T[i] < (2.0/3.0)
                    {
                        T[i] = p + (q - p) * ((2.0 / 3.0) - T[i]) * 6.0;
                    }
                    else
                    {
                        T[i] = p;
                    }
                }

                ret = new DoubleColor(a * 255.0, T[0] * 255.0, T[1] * 255.0, T[2] * 255.0);
            }

            //
            // this will preserve hue for gray colors, and saturation for black
            //
            ret.HueDegree  = h;
            ret.Saturation = s;

            return(ret);
        }