Esempio n. 1
0
        static public Color[] V3(Color[] llist, int ColorsCount)
        {
            List <RGB> list = new List <RGB>();

            foreach (Color item in llist)
            {
                list.Add(new RGB(item));
            }

            List <List <Color> > Result = new List <List <Color> >();
            List <Color>         Part   = new List <Color>();
            List <RGB>           temp   = new List <RGB>();

            list = ColorPalette.DistrictByHue(list);
            list = ColorPalette.DeleteLowSaturated(list);

            list.Sort(delegate(RGB left, RGB right)
                      { return(right.Hue.CompareTo(left.Hue)); });

            int KeyStep = list.Count / ColorsCount;

            for (int i = 0; i < ColorsCount; i++)
            {
                temp = list.GetRange(KeyStep * i, KeyStep);
                foreach (RGB item in temp)
                {
                    Part.Add(item.Color);
                }
                Result.Add(Part.GetRange(0, Part.Count));
                Part.Clear();
                temp.Clear();
            }

            int cnt = Result.FindIndex(group => group.Count != 0);

            for (int i = 0; i < Result.Count; i++)
            {
                if (Result[i].Count == 0)
                {
                    Result[i] = Result[cnt];
                }
            }

            return(ColorPalette.GetAverageColors(Result).ToArray());
        }
Esempio n. 2
0
        static public Color[] V2(Color[] llist, int ColorsCount)
        {
            List <RGB> list = new List <RGB>();

            foreach (Color item in llist)
            {
                list.Add(new RGB(item));
            }

            List <List <Color> > Result = new List <List <Color> >();
            List <RGB>           Part   = new List <RGB>();
            int maxLength      = 0;
            int emptyListCount = 0;

            int[] groupLengths = new int[ColorsCount];

            list = ColorPalette.DistrictByHue(list);
            list = ColorPalette.DeleteLowSaturated(list);

            list.Sort(delegate(RGB left, RGB right)
                      { return(right.Hue.CompareTo(left.Hue)); });

            float lower = (float)list[list.Count - 1].Hue;
            float upper = (float)list[0].Hue;

            float KeyStep = (upper - lower) / ColorsCount;

            upper = KeyStep + lower;

            for (int i = 0; i < ColorsCount; i++)
            {
                if (i == ColorsCount - 1)
                {
                    upper = (float)list[0].Hue;
                }

                foreach (RGB item in list)
                {
                    if ((float)item.Hue >= (float)lower && (float)item.Hue <= (float)upper)
                    {
                        Part.Add(item);
                    }
                }

                if (Part.Count > 2)
                {
                    Part = ColorPalette.DistrictByHue(Part, true);
                    Part.Sort(delegate(RGB left, RGB right)
                              { return(left.Brightness.CompareTo(right.Brightness)); });
                }

                if (maxLength < Part.Count)
                {
                    maxLength = Part.Count;
                }
                if (Part.Count == 0)
                {
                    emptyListCount += 1;
                }

                groupLengths[i] = Part.Count;

                List <Color> temp = new List <Color>();
                foreach (RGB item in Part)
                {
                    temp.Add(item.Color);
                }

                Result.Add(temp.GetRange(0, temp.Count));
                Part.Clear();

                lower = lower + KeyStep;
                upper = upper + KeyStep;
            }

            if (emptyListCount != 0)
            {
                List <Color>         maxList    = new List <Color>();
                List <List <Color> > emptyLists = new List <List <Color> >();

                Array.Sort(groupLengths);
                maxList    = Result.Find(group => group.Count == maxLength);
                emptyLists = Result.FindAll(x => x.Count == 0);

                int col = maxList.Count / (emptyListCount + 1);

                for (int i = 1; i <= emptyLists.Count; i++)
                {
                    emptyLists[i - 1].AddRange(maxList.GetRange(0, col));
                    maxList.RemoveRange(0, col);
                }
            }

            int cnt = Result.FindIndex(group => group.Count != 0);

            for (int i = 0; i < Result.Count; i++)
            {
                if (Result[i].Count == 0)
                {
                    Result[i] = Result[cnt];
                }
            }

            return(ColorPalette.GetAverageColors(Result).ToArray());
        }