private static IEnumerable <SColorInfo> SolveRootLevel(IEnumerable <SColorInfo> colors, Int32 colorCount) { // initializes the comparers based on hue, saturation and brightness (HSB color model) ColorHueComparer hueComparer = new ColorHueComparer(); ColorSaturationComparer saturationComparer = new ColorSaturationComparer(); ColorBrightnessComparer brightnessComparer = new ColorBrightnessComparer(); // selects three palettes: 1) hue is unique, 2) saturation is unique, 3) brightness is unique Dictionary <Single, SColorInfo> hueColors, saturationColors, brightnessColors; SelectDistinct(colors, out hueColors, out saturationColors, out brightnessColors); // selects the palette (from those 3) which has the most colors, because an image has some details in that category) if (hueColors.Count > saturationColors.Count && hueColors.Count > brightnessColors.Count) { colors = Solve2ndLevel(colors, hueColors, saturationComparer, brightnessComparer, colorCount); } else if (saturationColors.Count > hueColors.Count && saturationColors.Count > brightnessColors.Count) { colors = Solve2ndLevel(colors, saturationColors, hueComparer, brightnessComparer, colorCount); } else { colors = Solve2ndLevel(colors, brightnessColors, hueComparer, saturationComparer, colorCount); } return(colors); }
private static IEnumerable <Color> SolveRootLevel(int colorCount, IEnumerable <Color> colors) { // initializes the comparers based on hue, saturation and brightness (HSB color model) ColorHueComparer hueComparer = new ColorHueComparer(); ColorSaturationComparer saturationComparer = new ColorSaturationComparer(); ColorBrightnessComparer brightnessComparer = new ColorBrightnessComparer(); // selects three palettes: 1) hue is unique, 2) saturation is unique, 3) brightness is unique IEnumerable <Color> hueColors = colors.Distinct(hueComparer); IEnumerable <Color> saturationColors = colors.Distinct(saturationComparer); IEnumerable <Color> brightnessColors = colors.Distinct(brightnessComparer); // retrieves number of colors, ie. how many we've eliminated Int32 hueColorCount = hueColors.Count(); Int32 saturationColorCount = saturationColors.Count(); Int32 brightnessColorCount = brightnessColors.Count(); // selects the palette (from those 3) which has the most colors, because an image has some details in that category if (hueColorCount > saturationColorCount && hueColorCount > brightnessColorCount) { colors = Solve2ndLevel(hueColors, saturationComparer, brightnessComparer, colorCount); } else if (saturationColorCount < hueColorCount && saturationColorCount < brightnessColorCount) { colors = Solve2ndLevel(saturationColors, hueComparer, brightnessComparer, colorCount); } else { colors = Solve2ndLevel(brightnessColors, hueComparer, saturationComparer, colorCount); } return(colors); }
/// <summary> /// See <see cref="BaseColorCacheQuantizer.OnGetPaletteToCache"/> for more details. /// </summary> protected override List <Color> OnGetPaletteToCache(Int32 colorCount) { // otherwise calculate one palette.Clear(); // lucky seed :) FastRandom random = new FastRandom(13); #if (UseDictionary) List <DistinctColorInfo> colorInfoList = colorMap.Values.ToList(); #else List <DistinctColorInfo> colorInfoList = rootBucket.GetValues().ToList(); #endif foundColorCount = colorInfoList.Count; if (foundColorCount >= colorCount) { // shuffles the colormap colorInfoList = colorInfoList. OrderBy(entry => random.Next(foundColorCount)). ToList(); // workaround for backgrounds, the most prevalent color DistinctColorInfo background = colorInfoList.MaxBy(info => info.Count); colorInfoList.Remove(background); colorCount--; ColorHueComparer hueComparer = new ColorHueComparer(); ColorSaturationComparer saturationComparer = new ColorSaturationComparer(); ColorBrightnessComparer brightnessComparer = new ColorBrightnessComparer(); // generates catalogue List <IEqualityComparer <DistinctColorInfo> > comparers = new List <IEqualityComparer <DistinctColorInfo> > { hueComparer, saturationComparer, brightnessComparer }; // take adequate number from each slot while (ProcessList(colorCount, colorInfoList, comparers, out colorInfoList)) { } Int32 listColorCount = colorInfoList.Count(); if (listColorCount > 0) { Int32 allowedTake = Math.Min(colorCount, listColorCount); colorInfoList = colorInfoList.Take(allowedTake).ToList(); } // adds background color first palette.Add(Color.FromArgb(background.Color)); } // adds the selected colors to a final palette palette.AddRange(colorInfoList.Select(colorInfo => Color.FromArgb(colorInfo.Color))); // returns our new palette return(palette); }