Color From HSB - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

Color From HSB

Demo Code


using System.Windows.Media;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;//from   w  w  w  .  j  a  v a 2s . c  om

public class Main{
        public static Color ColorFromHSB(double H, double S, double B)
        {
            double red = 0.0, green = 0.0, blue = 0.0;

            if (S == 0.0)
            {
                red = green = blue = B;
            }
            else
            {
                double h = H * 360;
                while (h >= 360.0)
                    h -= 360.0;

                h = h / 60.0;
                int i = (int)h;

                double f = h - i;
                double r = B * (1.0 - S);
                double s = B * (1.0 - S * f);
                double t = B * (1.0 - S * (1.0 - f));

                switch (i)
                {
                    case 0: red = B; green = t; blue = r; break;
                    case 1: red = s; green = B; blue = r; break;
                    case 2: red = r; green = B; blue = t; break;
                    case 3: red = r; green = s; blue = B; break;
                    case 4: red = t; green = r; blue = B; break;
                    case 5: red = B; green = r; blue = s; break;
                }
            }

            byte iRed = (byte)(red * 255.0), iGreen = (byte)(green * 255.0), iBlue = (byte)(blue * 255.0);
            return Color.FromRgb(iRed, iGreen, iBlue);
        }
}

Related Tutorials