HSB From Color - CSharp System.Drawing

CSharp examples for System.Drawing:Color

Description

HSB From Color

Demo Code


using System.Windows.Media;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from ww w .j av a 2 s  .  com*/

public class Main{
        public static void HSBFromColor(Color C, ref double H, ref double S, ref double B)
        {     
            byte red = C.R;
            byte green = C.G;
            byte blue = C.B;

            int imax = red, imin = red;

            if (green > imax) imax = green; else if (green < imin) imin = green;
            if (blue > imax) imax = blue; else if (blue < imin) imin = blue;
            double max = imax / 255.0, min = imin / 255.0;

            double value = max;
            double saturation = (max > 0) ? (max - min) / max : 0.0;
            double hue = 0;

            if (imax > imin)
            {
                double f = 1.0 / ((max - min) * 255.0);
                hue = (imax == red) ? 0.0 + f * (green - blue) : (imax == green) ? 2.0 + f * (blue - red) : 4.0 + f * (red - green);
                hue = hue * 60.0;
                if (hue < 0.0)
                    hue += 360.0;
            }

            H = hue / 360;
            S = saturation;
            B = value;
        }
}

Related Tutorials