public VBox FindColor(double targetLuma, double minLuma, double maxLuma, double targetSaturation, double minSaturation, double maxSaturation) { VBox max = null; double maxValue = 0; var highestPopulation = vboxes.Select(p => p.Count(false)).Max(); foreach (var swatch in vboxes) { var avg = swatch.Avg(false); var hsl = FromRgb(avg[0], avg[1], avg[2]).ToHsl(); var sat = hsl.S; var luma = hsl.L; if (sat >= minSaturation && sat <= maxSaturation && luma >= minLuma && luma <= maxLuma) { var thisValue = Mmcq.CreateComparisonValue(sat, targetSaturation, luma, targetLuma, swatch.Count(false), highestPopulation); if (max == null || thisValue > maxValue) { max = swatch; maxValue = thisValue; } } } return(max); }
public int Count(bool force) { if (count == null || force) { var npix = 0; int i; for (i = R1; i <= R2; i++) { int j; for (j = G1; j <= G2; j++) { int k; for (k = B1; k <= B2; k++) { var index = Mmcq.GetColorIndex(i, j, k); npix += histo[index]; } } } count = npix; } return(count.Value); }
/// <summary> /// Use the median cut algorithm to cluster similar colors. /// </summary> /// <param name="sourceImage">The source image.</param> /// <param name="colorCount">The color count.</param> /// <param name="quality"> /// 0 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. /// </param> /// <param name="ignoreWhite">if set to <c>true</c> [ignore white].</param> /// <returns></returns> private CMap GetColorMap(Bitmap sourceImage, int colorCount, int quality = DefaultQuality, bool ignoreWhite = DefaultIgnoreWhite) { var pixelArray = GetPixelsFast(sourceImage, quality, ignoreWhite); // Send array to quantize function which clusters values using median // cut algorithm var cmap = Mmcq.Quantize(pixelArray, colorCount); return(cmap); }
public int[] Avg(bool force) { if (avg == null || force) { var ntot = 0; var rsum = 0; var gsum = 0; var bsum = 0; int i; for (i = R1; i <= R2; i++) { int j; for (j = G1; j <= G2; j++) { int k; for (k = B1; k <= B2; k++) { var histoindex = Mmcq.GetColorIndex(i, j, k); var hval = histo[histoindex]; ntot += hval; rsum += Convert.ToInt32(hval * (i + 0.5) * Mmcq.Mult); gsum += Convert.ToInt32(hval * (j + 0.5) * Mmcq.Mult); bsum += Convert.ToInt32(hval * (k + 0.5) * Mmcq.Mult); } } } if (ntot > 0) { avg = new[] { Math.Abs(rsum / ntot), Math.Abs(gsum / ntot), Math.Abs(bsum / ntot) } } ; else { avg = new[] { Math.Abs(Mmcq.Mult * (R1 + R2 + 1) / 2), Math.Abs(Mmcq.Mult * (G1 + G2 + 1) / 2), Math.Abs(Mmcq.Mult * (B1 + B2 + 1) / 2) } }; } return(avg); }