HSB to RGB Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color RGB

Description

HSB to RGB Color

Demo Code


using System.Drawing;
using System.Text;
using System.Collections.Generic;
using System;//from w  w w  .  ja  v a  2s. c  om

public class Main{
        public static Color HSB2RGBColor(int hue, int sat, int bri)
        {
            int r, g, b;
            if (hue > 239)
            {
                hue = 239;
            }
            else
            {
                if (hue < 0)
                {
                    hue = 0;
                }
            }

            if (sat > 240)
            {
                sat = 240;
            }
            else
            {
                if (sat < 0)
                {
                    sat = 0;
                }
            }

            if (bri > 240)
            {
                bri = 240;
            }
            else
            {
                if (bri < 0)
                {
                    bri = 0;
                }
            }

            float H = hue / 239.0f;
            float S = sat / 240.0f;
            float L = bri / 240.0f;

            float red = 0, green = 0, blue = 0;
            float d1, d2;

            if (L == 0)
            {
                red = green = blue = 0;
            }
            else
            {
                if (S == 0)
                {
                    red = green = blue = L;
                }
                else
                {
                    d2 = (L <= 0.5f) ? L * (1.0f + S) : L + S - (L * S);
                    d1 = 2.0f * L - d2;

                    float[] d3 = new float[] { H + 1.0f / 3.0f, H, H - 1.0f / 3.0f };
                    float[] rgb = new float[] { 0, 0, 0 };

                    for (int i = 0; i < 3; i++)
                    {
                        if (d3[i] < 0)
                        {
                            d3[i] += 1.0f;
                        }

                        if (d3[i] > 1.0f)
                        {
                            d3[i] -= 1.0f;
                        }

                        if (6.0f * d3[i] < 1.0f)
                        {
                            rgb[i] = d1 + (d2 - d1) * d3[i] * 6.0f;
                        }
                        else
                        {
                            if (2.0f * d3[i] < 1.0f)
                            {
                                rgb[i] = d2;
                            }
                            else
                            {
                                if (3.0f * d3[i] < 2.0f)
                                {
                                    rgb[i] = (d1 + (d2 - d1) * ((2.0f / 3.0f) - d3[i]) * 6.0f);
                                }
                                else
                                {
                                    rgb[i] = d1;
                                }
                            }
                        }
                    }

                    red = rgb[0];
                    green = rgb[1];
                    blue = rgb[2];
                }
            }

            red = 255.0f * red;
            green = 255.0f * green;
            blue = 255.0f * blue;

            if (red < 1)
            {
                red = 0.0f;
            }
            else
            {
                if (red > 255.0f)
                {
                    red = 255.0f;
                }
            }

            if (green < 1)
            {
                green = 0.0f;
            }
            else
            {
                if (green > 255.0f)
                {
                    green = 255.0f;
                }
            }

            if (blue < 1)
            {
                blue = 0.0f;
            }
            else
            {
                if (blue > 255.0f)
                {
                    blue = 255.0f;
                }
            }

            r = (int)(red + 0.5);
            g = (int)(green + 0.5);
            b = (int)(blue + 0.5);
            return Color.FromArgb(r, g, b);
        }
}

Related Tutorials