using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Windows.Graphics.Imaging; namespace ColorThiefDotNet { public partial class ColorThief { /// /// Use the median cut algorithm to cluster similar colors and return the base color from the largest cluster. /// /// The source image. /// /// 1 is the highest quality settings. 10 is the default. There is /// a trade-off between quality and speed. The bigger the number, /// the faster a color will be returned but the greater the /// likelihood that it will not be the visually most dominant color. /// /// if set to true [ignore white]. /// public async Task GetColor(BitmapDecoder sourceImage, int quality = DefaultQuality, bool ignoreWhite = DefaultIgnoreWhite) { var palette = await GetPalette(sourceImage, 3, quality, ignoreWhite); var dominantColor = new QuantizedColor(new Color { A = Convert.ToByte(palette.Average(a => a.Color.A)), R = Convert.ToByte(palette.Average(a => a.Color.R)), G = Convert.ToByte(palette.Average(a => a.Color.G)), B = Convert.ToByte(palette.Average(a => a.Color.B)) }, Convert.ToInt32(palette.Average(a => a.Population))); return dominantColor; } /// /// Use the median cut algorithm to cluster similar colors. /// /// The source image. /// The color count. /// /// 1 is the highest quality settings. 10 is the default. There is /// a trade-off between quality and speed. The bigger the number, /// the faster a color will be returned but the greater the /// likelihood that it will not be the visually most dominant color. /// /// if set to true [ignore white]. /// /// true public async Task> GetPalette(BitmapDecoder sourceImage, int colorCount = DefaultColorCount, int quality = DefaultQuality, bool ignoreWhite = DefaultIgnoreWhite) { var pixelArray = await GetPixelsFast(sourceImage, quality, ignoreWhite); var cmap = GetColorMap(pixelArray, colorCount); if (cmap != null) { var colors = cmap.GeneratePalette(); return colors; } return new List(); } private async Task GetIntFromPixel(BitmapDecoder decoder) { var pixelsData = await decoder.GetPixelDataAsync(); var pixels = pixelsData.DetachPixelData(); return pixels; } private async Task GetPixelsFast(BitmapDecoder sourceImage, int quality, bool ignoreWhite) { if (quality < 1) { quality = DefaultQuality; } var pixels = await GetIntFromPixel(sourceImage); var pixelCount = sourceImage.PixelWidth * sourceImage.PixelHeight; return ConvertPixels(pixels, Convert.ToInt32(pixelCount), quality, ignoreWhite); } } }