Example #1
0
        public static HSVDATA ColorToHSV(UniColor color)
        {
            HSVDATA hsv = new HSVDATA();

            ColorToHSV(color, ref hsv);
            return(hsv);
        }
Example #2
0
        public static void GetPercentages(UniColor Color, ref double dpRed, ref double dpGreen, ref double dpBlue)
        {
            double vR;
            double vB;
            double vG;
            var    vIn = new byte[3];
            double d;

            GetRGB(Color, out vIn[0], out vIn[1], out vIn[2]);
            vR = vIn[0];
            vG = vIn[1];
            vB = vIn[2];
            if (vR > vG)
            {
                d = vR;
            }
            else
            {
                d = vG;
            }
            if (vB > d)
            {
                d = vB;
            }
            if (d == 0d)
            {
                d = 255.0d;
            }
            dpRed   = vR / d;
            dpGreen = vG / d;
            dpBlue  = vB / d;
        }
Example #3
0
        public static NamedColor GetClosestColor(UniColor value, double maxDeviation = 0.013d, bool ignoreValue = true, bool ignoreSaturation = false, bool ignoreHue = false)
        {
            var hsv1 = ColorMath.ColorToHSV(value);

            NamedColor closest = null;

            double lhue = 360;
            double lsat = 1;
            double lval = 1;

            double dhue, dsat, dval;
            bool   match;

            var mxd = Math.Abs(maxDeviation);

            foreach (var vl in catalog)
            {
                match = true;

                var hsv2 = ColorMath.ColorToHSV(vl.Color);
                if (hsv1.Hue == -1 && hsv2.Hue >= 0)
                {
                    continue;
                }
                if (hsv2.Hue == -1 && hsv1.Hue >= 0)
                {
                    continue;
                }

                dhue = Math.Abs(hsv2.Hue - hsv1.Hue);
                dsat = Math.Abs(hsv2.Saturation - hsv1.Saturation);
                dval = Math.Abs(hsv2.Value - hsv1.Value);

                if (!ignoreHue)
                {
                    match &= dhue <= lhue && (dhue <= (360 * mxd));
                }

                if (!ignoreSaturation)
                {
                    match &= dsat <= lsat && (dsat <= mxd);
                }

                if (!ignoreValue)
                {
                    match &= dval <= lval && (dval <= mxd);
                }

                if (match)
                {
                    lhue = Math.Abs(hsv2.Hue - hsv1.Hue);
                    lsat = Math.Abs(hsv2.Saturation - hsv1.Saturation);
                    lval = Math.Abs(hsv2.Value - hsv1.Value);

                    closest = vl;
                }
            }

            return(closest);
        }
Example #4
0
        public static void GetRGB(UniColor color, out byte red, out byte green, out byte blue)
        {
            int crColor = ((Color)color).ToArgb();

            red   = (byte)(crColor & 0xFF);
            green = (byte)(crColor >> 8 & 0xFF);
            blue  = (byte)(crColor >> 16 & 0xFF);
        }
Example #5
0
        public static void HSVToColor(HSVDATA hsv, ref UniColor dest)
        {
            // preserve alpha
            var a = dest.A;

            dest.SetValue(HSVToColorRaw(hsv));
            dest.A = a;
        }
Example #6
0
        // Single Convert ColorRef to RGB

        public static void ColorToRGB(UniColor color, out RGBDATA bits)
        {
            byte[] b;
            b          = BitConverter.GetBytes(((Color)color).ToArgb());
            bits.Red   = b[2];
            bits.Green = b[1];
            bits.Blue  = b[0];
        }
        public static UniColor GetUniColor(this System.Windows.Media.Color c)
        {
            UniColor clr = new UniColor();

            clr.SetValue(c.A, c.R, c.G, c.B);

            return(clr);
        }
Example #8
0
        public static NamedColor FindColor(UniColor value, out int index)
        {
            NamedColor r;

            index = Search(catalog, value, nameof(Color), out r, true);

            return(r);
        }
Example #9
0
        // Single Convert ColorRef to RGB-reversed

        public static void ColorToBGR(UniColor color, out BGRDATA bits)
        {
            var tibs = new RGBDATA();

            ColorToRGB(color, out tibs);
            bits.Blue  = tibs.Blue;
            bits.Red   = tibs.Red;
            bits.Green = tibs.Green;
        }
Example #10
0
        public static void ColorToBGRA(UniColor Color, out BGRADATA bits)
        {
            var tibs = new ARGBDATA();

            ColorToARGB(Color, out tibs);
            bits.Alpha = tibs.Blue;
            bits.Blue  = tibs.Blue;
            bits.Red   = tibs.Red;
            bits.Green = tibs.Green;
        }
Example #11
0
        public static void ColorToHSV(UniColor color, ref HSVDATA hsv)
        {
            double h, s, v;

            ColorToHSV(color, out h, out s, out v);

            hsv.Hue        = h;
            hsv.Saturation = s;
            hsv.Value      = v;
        }
Example #12
0
        public static NamedColor FindColor(UniColor value, bool closest = false)
        {
            var r = FindColor(value, out _);

            if (r == null && closest)
            {
                r = GetClosestColor(value);
            }

            return(r);
        }
Example #13
0
        public static string Cex(UniColor color)
        {
            string s = "";

            byte[] b = (byte[])color;
            for (int i = 0; i <= 3; i++)
            {
                s += b[i].ToString("X2");
            }
            return("#" + s.ToLower());
        }
Example #14
0
        public static UniColor GrayTone(UniColor Color)
        {
            UniColor GrayToneRet = default;
            ARGBDATA rgbData;
            int      a;
            int      b;
            int      c;
            byte     tone;

            ColorToARGB(Color, out rgbData);
            a           = rgbData.Red;
            b           = rgbData.Green;
            c           = rgbData.Blue;
            tone        = (byte)((a + b + c) / 3d);
            GrayToneRet = new UniColor(rgbData.Alpha, tone, tone, tone);
            return(GrayToneRet);
        }
Example #15
0
        public static UniColor AbsTone(UniColor Color)
        {
            UniColor AbsToneRet = default;
            var      pR         = default(double);
            var      pB         = default(double);
            var      pG         = default(double);
            double   sR;
            double   sB;
            double   sG;

            GetPercentages(Color, ref pR, ref pG, ref pB);
            sR         = pR * 255d;
            sG         = pG * 255d;
            sB         = pB * 255d;
            AbsToneRet = new UniColor(Color.A, (byte)sR, (byte)sG, (byte)sB);
            return(AbsToneRet);
        }
Example #16
0
        public static void LoadColors()
        {
            if (catalog != null)
            {
                return;
            }

            var cl   = new List <NamedColor>();
            var craw = AppResources.ColorList.Replace("\r\n", "\n").Split("\n");

            foreach (var cen in craw)
            {
                if (string.IsNullOrEmpty(cen.Trim()))
                {
                    continue;
                }
                var      et = cen.Split("|");
                UniColor cr = uint.Parse("ff" + et[0], System.Globalization.NumberStyles.HexNumber);

                string text  = TextTools.TitleCase(et[1], false).Replace("'S", "'s");
                string extra = null;

                int x = text.IndexOf("(");
                if (x != -1)
                {
                    et    = text.Split("(");
                    text  = et[0].Trim();
                    extra = et[1].Trim().Trim(')');
                }
                var cc = new NamedColor(text, cr, extra);

                cc.nidxstr = TextTools.NoSpace(text?.ToLower() ?? "");
                cc.eidxstr = TextTools.NoSpace(extra?.ToLower() ?? "");

                cc.idxstr = cc.nidxstr + cc.eidxstr;

                var test = cl.Where((a) => a.Color == cc.Color).FirstOrDefault();
                if (test == null)
                {
                    cl.Add(cc);
                }
            }

            catalog = cl.ToArray();
            Sort(ref catalog, (a, b) => a.Color.CompareTo(b.Color));
        }
Example #17
0
        public static UniColor GetAverageColor(UniColor Color1, UniColor Color2)
        {
            var   Bits = new RGBDATA[3];
            float df;
            int   e;
            float clr2 = Color2.Value;
            byte  al   = Color1.A;
            byte  af   = Color2.A;

            ColorToRGB(Color1, out Bits[0]);
            ColorToRGB(Color2, out Bits[1]);
            e = 0;
            if (Math.Round(clr2) != clr2)
            {
                df            = Bits[0].Red * clr2;
                e             = (int)Math.Round(df, 0);
                Bits[2].Red   = (byte)(e & 0xFF);
                df            = Bits[0].Green * clr2;
                e             = (int)Math.Round(df, 0);
                Bits[2].Green = (byte)(e & 0xFF);
                df            = Bits[0].Blue * clr2;
                e             = (int)Math.Round(df, 0);
                Bits[2].Blue  = (byte)(e & 0xFF);
            }
            else
            {
                df            = (Bits[0].Red + (float)Bits[1].Red) / 2f;
                e             = (int)Math.Round(df, 0);
                Bits[2].Red   = (byte)(e & 0xFF);
                df            = (Bits[0].Green + (float)Bits[1].Green) / 2f;
                e             = (int)Math.Round(df, 0);
                Bits[2].Green = (byte)(e & 0xFF);
                df            = (Bits[0].Blue + (float)Bits[1].Blue) / 2f;
                e             = (int)Math.Round(df, 0);
                Bits[2].Blue  = (byte)(e & 0xFF);
            }

            // Get the average alpha
            al = (byte)((al + af) / 2d);
            return(new UniColor(al, Bits[2].Red, Bits[2].Green, Bits[2].Blue));
        }
Example #18
0
        public static void ColorToCMY(UniColor Color, ref CMYDATA cmy)
        {
            //
            byte r;
            byte g;
            byte b;

            int x;

            GetRGB(Color, out r, out g, out b);

            x = Max(r, g, b);

            r = (byte)Math.Abs(1 - r);
            g = (byte)Math.Abs(1 - g);
            b = (byte)Math.Abs(1 - b);

            cmy.Magenta = r;
            cmy.Yellow  = g;
            cmy.Cyan    = b;
        }
Example #19
0
        public static UniColor SetTone(ref RGBDATA rgbData, float pPercent, UniColor Color = default)
        {
            float x;

            if (Color != default)
            {
                ColorToRGB(Color, out rgbData);
            }
            x = Max(rgbData.Red, rgbData.Green, rgbData.Blue);
            if (x == 0f)
            {
                rgbData.Red   = 0;
                rgbData.Green = 0;
                rgbData.Blue  = 0;
                return(UniColor.Empty);
            }

            rgbData.Red   = (byte)(rgbData.Red / x * (255f * pPercent));
            rgbData.Green = (byte)(rgbData.Green / x * (255f * pPercent));
            rgbData.Blue  = (byte)(rgbData.Blue / x * (255f * pPercent));
            return(RGBToColor(rgbData));
        }
Example #20
0
        public static UniColor SetTone(ref ARGBDATA argbData, float pPercent, UniColor Color = default)
        {
            float x;

            if (Color != UniColor.Empty)
            {
                ColorToARGB(Color, out argbData);
            }
            x = Max(argbData.Red, argbData.Green, argbData.Blue);
            if (x == 0f)
            {
                argbData.Red   = 0;
                argbData.Green = 0;
                argbData.Blue  = 0;
                return(UniColor.Empty);
            }

            argbData.Red   = (byte)(argbData.Red / x * (255f * pPercent));
            argbData.Green = (byte)(argbData.Green / x * (255f * pPercent));
            argbData.Blue  = (byte)(argbData.Blue / x * (255f * pPercent));
            return(ARGBToColor(argbData));
        }
Example #21
0
        public static UniColor CMYToColor(CMYDATA cmy)
        {
            UniColor CMYToColorRet = default;
            //
            int c;
            int m;
            int y;

            byte r;
            byte g;
            byte b;

            c = cmy.Cyan;
            m = cmy.Magenta;
            y = cmy.Yellow;

            r = (byte)Math.Abs(1 - m);
            g = (byte)Math.Abs(1 - y);
            b = (byte)Math.Abs(1 - c);

            return(new UniColor(255, r, g, b));
        }
Example #22
0
 public ColorHitEventArgs(int rawColor)
 {
     Color = rawColor;
 }
Example #23
0
        public static void ColorToHSV(UniColor color, out double hue, out double saturation, out double value)
        {
            hue = default(double);

            double sat;
            double val;
            double Mn;
            double Mx;
            double r;
            double g;
            double b;
            double chroma;


            r = color.R / 255d;
            g = color.G / 255d;
            b = color.B / 255d;

            Mn = Min(r, g, b);
            Mx = Max(r, g, b);

            chroma = Mx - Mn;
            val    = Mx;

            if (chroma == 0)
            {
                hue = -1;

                switch (val)
                {
                case var @case when @case <= 0.5d:
                    saturation = 1d;
                    value      = 510d * val / 360d;
                    break;

                case var case1 when case1 <= 1d:
                default:
                    val        = 1d - val;
                    value      = 1d;
                    saturation = 720d * val / 360d;
                    break;
                }

                return;
            }

            if (Mx == r)
            {
                hue = (g - b) / chroma % 6d;
            }
            else if (Mx == g)
            {
                hue = (b - r) / chroma + 2d;
            }
            else if (Mx == b)
            {
                hue = (r - g) / chroma + 4d;
            }

            hue *= 60d;
            if (hue < 0d)
            {
                hue = 360d + hue;
            }

            sat = val != 0 ? chroma / val : 0;

            value      = val;
            saturation = sat;
        }
 /// <summary>
 /// Gets the WPF <see cref="System.Windows.Media.Color"/> structure for this <see cref="UniColor"/> structure.
 /// </summary>
 /// <param name="c"></param>
 /// <returns>A WPF color structure.</returns>
 public static System.Windows.Media.Color GetWPFColor(this UniColor c)
 {
     return(System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B));
 }
Example #25
0
 public ColorHitEventArgs(Color color)
 {
     Color = new UniColor(color.ToArgb());
 }
 /// <summary>
 /// Sets the value of this <see cref="UniColor"/> structure to the color value provided by the WPF <see cref="System.Windows.Media.Color"/> structure.
 /// </summary>
 /// <param name="color">The color to set.</param>
 public static void SetValue(this UniColor unicolor, System.Windows.Media.Color color)
 {
     unicolor.SetValue(color.A, color.R, color.G, color.B);
 }