Ejemplo n.º 1
0
        /// <summary>
        /// See <see cref="BaseColorCache.OnGetColorPaletteIndex"/> for more details.
        /// </summary>
        protected override void OnGetColorPaletteIndex(Color color, out Int32 paletteIndex)
        {
            BucketInfo bucket     = GetBucket(color);
            Int32      colorCount = bucket.Colors.Count();

            paletteIndex = 0;

            if (colorCount == 1)
            {
                paletteIndex = bucket.Colors.First().Key;
            }
            else
            {
                Int32 index      = 0;
                Int32 colorIndex = ColorModelHelper.GetEuclideanDistance(color, ColorModel, bucket.Colors.Values.ToList());

                foreach (Int32 colorPaletteIndex in bucket.Colors.Keys)
                {
                    if (index == colorIndex)
                    {
                        paletteIndex = colorPaletteIndex;
                        break;
                    }

                    index++;
                }
            }
        }
        /// <summary>
        /// Splits this cube's color list at median index, and returns two newly created cubes.
        /// </summary>
        /// <param name="componentIndex">Index of the component (red = 0, green = 1, blue = 2).</param>
        /// <param name="firstMedianCutCube">The first created cube.</param>
        /// <param name="secondMedianCutCube">The second created cube.</param>
        public void SplitAtMedian(Byte componentIndex, out MedianCutCube firstMedianCutCube, out MedianCutCube secondMedianCutCube)
        {
            List <Int32> colors;

            switch (componentIndex)
            {
            // red colors
            case 0:
                colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentA(ColorModel, TextureBitmap.Color.FromARGB(argb))).ToList();
                break;

            // green colors
            case 1:
                colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentB(ColorModel, TextureBitmap.Color.FromARGB(argb))).ToList();
                break;

            // blue colors
            case 2:
                colors = colorList.OrderBy(argb => ColorModelHelper.GetComponentC(ColorModel, TextureBitmap.Color.FromARGB(argb))).ToList();
                break;

            default:
                throw new NotSupportedException("Only three color components are supported (R, G and B).");
            }

            // retrieves the median index (a half point)
            Int32 medianIndex = colorList.Count >> 1;

            // creates the two half-cubes
            firstMedianCutCube  = new MedianCutCube(colors.GetRange(0, medianIndex));
            secondMedianCutCube = new MedianCutCube(colors.GetRange(medianIndex, colors.Count - medianIndex));
        }
        /// <summary>
        /// Determines whether the color is in the space of this cube.
        /// </summary>
        /// <param name="color">The color to be checked, if it's contained in this cube.</param>
        /// <returns>if true a color is in the space of this cube, otherwise returns false.</returns>
        public Boolean IsColorIn(TextureBitmap.Color color)
        {
            Int32 red   = ColorModelHelper.GetComponentA(ColorModel, color);
            Int32 green = ColorModelHelper.GetComponentB(ColorModel, color);
            Int32 blue  = ColorModelHelper.GetComponentC(ColorModel, color);

            return((red >= redLowBound && red <= redHighBound) &&
                   (green >= greenLowBound && green <= greenHighBound) &&
                   (blue >= blueLowBound && blue <= blueHighBound));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// See <see cref="BaseColorCache.OnGetColorPaletteIndex"/> for more details.
        /// </summary>
        protected override void OnGetColorPaletteIndex(TextureBitmap.Color color, out Int32 paletteIndex)
        {
            Dictionary <Int32, TextureBitmap.Color> candidates = root.GetPaletteIndex(color, 0);

            paletteIndex = 0;
            Int32 index      = 0;
            Int32 colorIndex = ColorModelHelper.GetEuclideanDistance(color, ColorModel, candidates.Values.ToList());

            foreach (Int32 colorPaletteIndex in candidates.Keys)
            {
                if (index == colorIndex)
                {
                    paletteIndex = colorPaletteIndex;
                    break;
                }

                index++;
            }
        }
Ejemplo n.º 5
0
        private Int64 GetColorBucketIndex(Color color)
        {
            Single normalizedDistance = 0.0f;
            Single componentA, componentB, componentC;

            switch (ColorModel)
            {
            case ColorModel.RedGreenBlue: normalizedDistance = NormalizedDistanceRGB; break;

            case ColorModel.HueSaturationLuminance: normalizedDistance = NormalizedDistanceHSL; break;

            case ColorModel.LabColorSpace: normalizedDistance = NormalizedDistanceLab; break;
            }

            ColorModelHelper.GetColorComponents(ColorModel, color, out componentA, out componentB, out componentC);
            Single distance   = componentA * componentA + componentB * componentB + componentC * componentC;
            Single normalized = distance * normalizedDistance * MaximalDistance;
            Int64  resultHash = (Int64)normalized / bucketSize;

            return(resultHash);
        }
Ejemplo n.º 6
0
        protected override int CalculatePaletteIndex(Color color)
        {
            var candidates = _root.GetPaletteIndex(color, 0);

            var result     = 0;
            int index      = 0;
            int colorIndex =
                ColorModelHelper.GetSmallestEuclideanDistanceIndex(_colorModel, color, candidates.Values.ToList());

            foreach (var colorPaletteIndex in candidates.Keys)
            {
                if (index == colorIndex)
                {
                    result = colorPaletteIndex;
                    break;
                }

                index++;
            }

            return(result);
        }
        /// <summary>
        /// Shrinks this cube to the least dimensions that covers all the colors in the RGB space.
        /// </summary>
        private void Shrink()
        {
            redLowBound  = greenLowBound = blueLowBound = 255;
            redHighBound = greenHighBound = blueHighBound = 0;

            foreach (Int32 argb in colorList)
            {
                TextureBitmap.Color color = TextureBitmap.Color.FromARGB(argb);

                Int32 red   = ColorModelHelper.GetComponentA(ColorModel, color);
                Int32 green = ColorModelHelper.GetComponentB(ColorModel, color);
                Int32 blue  = ColorModelHelper.GetComponentC(ColorModel, color);

                if (red < redLowBound)
                {
                    redLowBound = red;
                }
                if (red > redHighBound)
                {
                    redHighBound = red;
                }
                if (green < greenLowBound)
                {
                    greenLowBound = green;
                }
                if (green > greenHighBound)
                {
                    greenHighBound = green;
                }
                if (blue < blueLowBound)
                {
                    blueLowBound = blue;
                }
                if (blue > blueHighBound)
                {
                    blueHighBound = blue;
                }
            }
        }
 private int CalculatePaletteIndexInternal(Color color)
 {
     return(ColorModelHelper.GetSmallestEuclideanDistanceIndex(ColorModel, color, Palette, AlphaThreshold));
 }
 /// <summary>
 /// See <see cref="BaseColorCache.OnGetColorPaletteIndex"/> for more details.
 /// </summary>
 protected override void OnGetColorPaletteIndex(Color color, out Int32 paletteIndex)
 {
     paletteIndex = ColorModelHelper.GetEuclideanDistance(color, ColorModel, palette);
 }