Click here to Skip to main content
15,897,704 members
Articles / Programming Languages / C#

A Simple - Yet Quite Powerful - Palette Quantizer in C#

Rate me:
Please Sign up or sign in to vote.
4.98/5 (104 votes)
28 Jul 2012CPOL21 min read 341.7K   12.8K   168  
A palette quantizer based on human perception
using System;
using System.Drawing;
using SimplePaletteQuantizer.Helpers;

namespace SimplePaletteQuantizer.Quantizers.HSL
{
    /// <summary>
    /// Stores all the informations about single color only once, to be used later.
    /// </summary>
    internal class ColorInfo
    {
        /// <summary>
        /// The original color.
        /// </summary>
        public Color Color { get; private set; }

        /// <summary>
        /// The pixel presence count in the image.
        /// </summary>
        public Int32 Count { get; private set; }

        /// <summary>
        /// A hue component of the color.
        /// </summary>
        public Single Hue { get; private set; }

        /// <summary>
        /// A saturation component of the color.
        /// </summary>
        public Single Saturation { get; private set; }

        /// <summary>
        /// A brightness component of the color.
        /// </summary>
        public Single Brightness { get; private set; }

        /// <summary>
        /// A cached hue hashcode.
        /// </summary>
        public Int32 HueHashCode { get; private set; }

        /// <summary>
        /// A cached saturation hashcode.
        /// </summary>
        public Int32 SaturationHashCode { get; private set; }

        /// <summary>
        /// A cached brightness hashcode.
        /// </summary>
        public Int32 BrightnessHashCode { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorInfo"/> struct.
        /// </summary>
        /// <param name="color">The color.</param>
        public ColorInfo(Color color)
        {
            Color = color;
            Count = 1;

            Hue = color.GetHue();
            Saturation = color.GetSaturation();
            Brightness = color.GetBrightness();

            HueHashCode = Hue.GetHashCode();
            SaturationHashCode = Saturation.GetHashCode();
            BrightnessHashCode = Brightness.GetHashCode();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ColorInfo"/> struct.
        /// </summary>
        /// <param name="hue">The hue.</param>
        /// <param name="saturation">The saturation.</param>
        /// <param name="brightness">The brightness.</param>
        /// <param name="count">The count.</param>
        public ColorInfo(Single hue, Single saturation, Single brightness, Int32 count)
        {
            Color = ColorModelHelper.HSBtoRGB(hue, saturation, brightness);
            Count = count;
        }

        /// <summary>
        /// Increases the count of pixels of this color.
        /// </summary>
        public void IncreaseCount()
        {
            Count++;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Czech Republic Czech Republic
Contacts: EMAIL - smartk8@gmail.com

Comments and Discussions