using System;

/// <summary>
/// Summary description for ColorModel
/// </summary>
public class HSVobject
{
    public int h;
    public int s;
    public int v;
    public HSVobject(int h, int s, int v)
    {
        this.h = h;
        this.s = s;
        this.v = v;
    }
    public void validate()
    {
        if (this.h <= 0) { this.h = 0; }
        if (this.h > 360) { this.h = 360; }
        if (this.s <= 0) { this.s = 0; }
        if (this.s > 100) { this.s = 100; }
        if (this.v <= 0) { this.v = 0; }
        if (this.v > 100) { this.v = 100; }
    }
}
public class RGBobject
{
    public int r;
    public int g;
    public int b;
    public RGBobject(int r, int g, int b)
    {
        this.r = r;
        this.g = g;
        this.b = b;
    }
    public void validate()
    {
        if (this.r <= 0) { this.r = 0; }
        if (this.r > 255) { this.r = 255; }
        if (this.g <= 0) { this.g = 0; }
        if (this.g > 255) { this.g = 255; }
        if (this.b <= 0) { this.b = 0; }
        if (this.b > 255) { this.b = 255; }
    }
}
public static class ColorModel
{
    public static string hexify(int number)
    {
        string digits = "0123456789ABCDEF";
        int lsd = number % 16;
        int msd = (number - lsd) / 16;
        char[] c = new char[2];
        c[0] = digits[msd];
        c[1] = digits[lsd];
        string hexified = new string(c);
        return hexified;
    }
    public static int decimalize(string hexNumber)
    {
        hexNumber = hexNumber.ToUpper();
        string digits = "0123456789ABCDEF";
        return (digits.IndexOf(hexNumber[0])) * 16 + digits.IndexOf(hexNumber[1]);
    }
    public static string DEZ2RGB(long number)
    {
        string RGB = "";
        // R
        int iColor = (int)(number / (256 * 256));
        if (iColor > 0)
            RGB += hexify(iColor);
        number -= iColor * 256 * 256;
        // G
        iColor = (int)(number / 256);
        RGB += hexify(iColor);
        number -= iColor * 256;
        // B
        iColor = (int)number;
        RGB += hexify(iColor);

        return RGB;
    }
    public static RGBobject HSV2RGB(HSVobject HSV)
    {
        double h = HSV.h * 1.0 / 360;
        double s = HSV.s * 1.0 / 100;
        double v = HSV.v * 1.0 / 100;
        RGBobject RGB;
        if (s == 0)
        {
            RGB = new RGBobject((int)(v * 255), (int)(v * 255), (int)(v * 255));
        }
        else
        {
            double var_h = h * 6;
            int var_i = (int)Math.Floor(var_h);
            double var_1 = v * (1 - s);
            double var_2 = v * (1 - s * (var_h - var_i));
            double var_3 = v * (1 - s * (1 - (var_h - var_i)));

            double var_r;
            double var_g;
            double var_b;
            if (var_i == 0)
            {
                var_r = v; var_g = var_3; var_b = var_1;
            }
            else if (var_i == 1) { var_r = var_2; var_g = v; var_b = var_1; }
            else if (var_i == 2) { var_r = var_1; var_g = v; var_b = var_3; }
            else if (var_i == 3) { var_r = var_1; var_g = var_2; var_b = v; }
            else if (var_i == 4) { var_r = var_3; var_g = var_1; var_b = v; }
            else { var_r = v; var_g = var_1; var_b = var_2; };

            RGB = new RGBobject((int)(var_r * 255), (int)(var_g * 255), (int)(var_b * 255));
        }
        return RGB;
    }
    private static double Min(double v1, double v2, double v3)
    {
        double temp = Math.Min(v1, v2);
        return Math.Min(temp, v3);
    }
    private static double Max(double v1, double v2, double v3)
    {
        double temp = Math.Max(v1, v2);
        return Math.Max(temp, v3);
    }
    public static HSVobject RGB2HSV(RGBobject RGB)
    {
        System.Drawing.Color color = System.Drawing.Color.FromArgb(RGB.r, RGB.g, RGB.b);
        float hue = color.GetHue();
        float saturation = color.GetSaturation();
        float lightness = color.GetBrightness();
        return new HSVobject((int)hue, (int)(100 * saturation), (int)(100 * lightness));
    }

    public static string RGB2hex(RGBobject RGB)
    {
        return "#" + hexify(RGB.r) + hexify(RGB.g) + hexify(RGB.b);
    }
    public static RGBobject hex2RGB(string strColor)
    {
        int Start = strColor.StartsWith("#") ? 1 : 0;
        return new RGBobject(decimalize(strColor.Substring(Start, 2)), decimalize(strColor.Substring(Start + 2, 2)), decimalize(strColor.Substring(Start + 4, 2)));
    }
}