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 }
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); }
private void GeneratePalette(string path) { var colorList = new List <Color>(); NonIndexedPixels = new List <Color>(); #region Get the Pixels var image = path.SourceFrom(); var pixelUtil = new PixelUtil(image); pixelUtil.LockBits(); for (int x = 0; x < image.PixelWidth; x++) { for (int y = 0; y < image.PixelHeight; y++) { colorList.Add(pixelUtil.GetPixel(x, y)); } } pixelUtil.UnlockBits(); #endregion //Save all the pixels in a property. NonIndexedPixels.AddRange(colorList); //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. //if (NonIndexedPixels.Count == 100) //{ // ColorTable = new List<Color> // { // Color.FromRgb(255, 255, 255), // Color.FromRgb(255, 0, 0), // Color.FromRgb(0, 0, 255) // }; // return; //} ColorTable = colorList.GroupBy(x => x) //Grouping based on its value .OrderByDescending(g => g.Count()) //Order by most frequent values .Select(g => g.FirstOrDefault()) //take the first among the group .Take(256).ToList(); //Make sure that the transparent color is added to list. if (!IsFirstFrame && TransparentColor.HasValue && ColorTable.Count == 256) { //Only adds if there is 256 colors, so I need to make sure that the color won't be ignored. //If there is less than 256 selected colors, it means that the transparent color is already selected. //If the color isn't on the list, add or replace. if (ColorTable.All(x => x != TransparentColor.Value)) { //Adds to the last spot, keeping it sorted. (Since all the colors are ordered by descending) ColorTable.Insert(255, TransparentColor.Value); //Remove the exceding value at the last position. ColorTable.RemoveAt(256); } } //I need to signal the other method that I won't need transparency. WillUseTransparency = TransparentColor.HasValue && ColorTable.Contains(TransparentColor.Value); }