Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        /// <summary>
        ///     Use the median cut algorithm to cluster similar colors.
        /// </summary>
        /// <param name="pixelArray">Pixel array.</param>
        /// <param name="colorCount">The color count.</param>
        /// <returns></returns>
        private CMap GetColorMap(byte[][] pixelArray, int colorCount)
        {
            // Send array to quantize function which clusters values using median
            // cut algorithm
            var cmap = Mmcq.Quantize(pixelArray, colorCount);

            return(cmap);
        }
Esempio n. 4
0
        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);
        }