I've been making an image editor in my free time and tried to use the HSL to RGB conversion formula here: HSL to RGB color conversion however I have come across an error.
Whenever I got to run my program the conversion does not work as it should, leaving the rgb with an unexpected grey colour. Here's some sample output data:
HSL: 0.0, 0.0, 1.0
RGB: 255.0, 255.0, 255.0
HSL: 214.0, 0.73, 0.5
RGB: 128.0, 128.0, 128.0
HSL: 214.0, 0.74, 0.5
RGB: 128.0, 128.0, 128.0
HSL: 214.0, 0.75, 0.5
RGB: 128.0, 128.0, 128.0
HSL: 214.0, 0.76, 0.5
RGB: 128.0, 128.0, 128.0
HSL: 214.0, 0.76, 0.5
RGB: 128.0, 128.0, 128.0
And below is my code. It's all written in Java.
public double[] hslToRgb(double h, double s, double l){
System.out.println("HSL: " + h + ", " + s + ", " + l);
double r = -1;
double b = -1;
double g = -1;
if(s == 0){
r = l;
b = l;
g = l;
}else{
double q = 1 < 0.5 ? l * (1 + s) : l + s - 1 * s;
double p = 2 * l - q;
r = hueToRgb(p, q, h + (1 / 3));
g = hueToRgb(p, q, h);
b = hueToRgb(p, q, h - (1/ 3));
}
r = Math.round(r * 255);
b = Math.round(b * 255);
g = Math.round(g * 255);
System.out.println("RGB: " + r + ", " + g + ", " + b);
double[] rgb = {r, g, b};
return rgb;
}
private double hueToRgb(double p, double q, double t){
if(t < 0){
t++;
}
if(t > 1){
t--;
}
if(t < 1 / 6){
return p + (q - p) * 6 * t;
}
if(t < 1 / 2){
return q;
}
if(t < 2 / 3){
return p + (q - p) * ((2 / 3) - t) * 6;
}
return p;
}
Can anybody give me some insight as to what might be causing this? I feel like it's a minor error in my code but I can't find it anywhere.