Example #1
0
        private void HandleTransparency()
        {
            //ColorTable = NonIndexedPixels.AsParallel().GroupBy(x => x)
            //    .OrderByDescending(g => g.Count()) //Order by most frequent values
            //    .Select(g => g.FirstOrDefault()) //take the first among the group
            //    .Take(MaximumNumberColor).ToList();

            //Make sure that the transparent color is added to list.
            if (TransparentColor.HasValue && (!IsFirstFrame || UseGlobalColorTable))// && ColorTable.Count == MaximumNumberColor)
            {
                //Only adds if there is MaximumNumberColor colors, so I need to make sure that the color won't be ignored.
                //If there is less than MaximumNumberColor selected colors, it means that the transparent color is already selected.

                //If the color isn't on the list, add or replace.
                if (ColorTable.AsParallel().All(x => x != TransparentColor.Value))
                {
                    //Adds to the last spot, keeping it sorted. (Since all the colors are ordered by descending)
                    ColorTable.Insert(ColorTable.Count - 1, TransparentColor.Value);

                    //Remove the exceding value at the last position.

                    if (ColorTable.Count > MaximumNumberColor)
                    {
                        ColorTable.RemoveAt(MaximumNumberColor);
                    }
                }
            }

            //I need to signal the other method that I won't need transparency.
            WillUseTransparency = !IsFirstFrame && TransparentColor.HasValue && ColorTable.Contains(TransparentColor.Value);
        }
Example #2
0
        private void GeneratePalette()
        {
            //TODO: more ways to decide which color to get
            //Like removing similar colors (with less than 5% similarity) if there is more than 256 colors, etc.
            //I probably can do that, using the groupby method.

            ColorTable = NonIndexedPixels.AsParallel().GroupBy(x => x)
                         .OrderByDescending(g => g.Count()) //Order by most frequent values
                         .Select(g => g.FirstOrDefault())   //take the first among the group
                         .Take(MaximumNumberColor).ToList();

            #region Handle transparency

            //Make sure that the transparent color is added to list.
            if (TransparentColor.HasValue && (!IsFirstFrame || UseGlobalColorTable) && ColorTable.Count == MaximumNumberColor)
            {
                //Only adds if there is MaximumNumberColor colors, so I need to make sure that the color won't be ignored.
                //If there is less than MaximumNumberColor selected colors, it means that the transparent color is already selected.

                //If the color isn't on the list, add or replace.
                if (ColorTable.AsParallel().All(x => x != TransparentColor.Value))
                {
                    //Adds to the last spot, keeping it sorted. (Since all the colors are ordered by descending)
                    ColorTable.Insert(MaximumNumberColor - 1, TransparentColor.Value);

                    //Remove the exceding value at the last position.
                    ColorTable.RemoveAt(MaximumNumberColor);
                }
            }

            //I need to signal the other method that I won't need transparency.
            WillUseTransparency = !IsFirstFrame && TransparentColor.HasValue && ColorTable.Contains(TransparentColor.Value);

            #endregion
        }