I'm creating a WindowsForm application that can take my input HSB values and turn them into RGB values. I adapted JDB and Mohsen's answer here: HSL to RGB color conversion
My resulting code is below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace Colour_Picker
{
public class HSBToRGB
{
public static Color HSBToRGBConversion(float hue, float saturation, float brightness)
{
float red, green, blue;
if(saturation == 0){
red = green = blue = brightness; // achromatic
}else{
var q = brightness < 0.5 ? brightness * (1 + saturation) : brightness + saturation - brightness * saturation;
var p = 2 * brightness - q;
red = hue2rgb(p, q, hue + 1f/3);
green = hue2rgb(p, q, hue);
blue = hue2rgb(p, q, hue - 1f/3);
}
return Color.FromArgb((int)Math.Round(red * 255), (int)Math.Round(green * 255), (int)Math.Round(blue * 255));
}
public static float hue2rgb(float p, float q, float t){
if(t < 0) t += 1;
if(t > 1) t -= 1;
if(t < 1f/6) return p + (q - p) * 6 * t;
if(t < 1f/2) return q;
if(t < 2f/3) return p + (q - p) * (2f/3 - t) * 6;
return p;
}
}
}
It mentions in the source that HSB values should be in the set of [0,1] I would ideally like H to be in the set [0,360] and S and B to be in the set [0,100]. I tried messing with hue2rgb but it didn't work. How can I set the limits I would like?
EDIT: Thank you TaW for the help with the errors in my original code. I am actually keeping the limits in the class as [0, 1] but I'm manipulating the HSB values from my wanted limits before the method is called.