RGB to HSL Saturation Close but Incorrect

Currently I am trying to convert RGB to HSL. Everything is working but the saturation value. It is always close to the correct value (usually less than 10 off). For example:
RGB:(196,72,84)
HSL:(354,-46,52)
Correct HSL(354,51,53)

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
double s=0;
double l=0;
chroma=max-min; //works correctly

//LIGHTNESS
l=(max+min)/2;

//SATURATION
if(chroma==0) {s=0;}
else {s=chroma/(1-fabs(2*l-1));}

s=s*100.000000;
l=(l/255.000000)*100.000000;
closed account (3hM2Nwbp)
What is the data-type of 'chroma', 's', and 'l'?

There isn't enough posted source code to answer your question.
chroma, s and l are doubles, but what more do you need?
BUMP
what more do you need?

How about max and min? Are they doubles, too? If not, then l will probably be calculated incorrectly in line 6 (integer division rather than floating point division).

Line 10 looks wrong. Unless -1/2 < l < +1/2, s will be a negative number. And if l == 1 or 0, then s will be infinite. What equation are you trying to use? We can't read your mind.


And just out of curiosity, why do you have so many decimal places in the constant values in lines 12 and 13? Adding extra '0's at the end of a double doesn't make it any more accurate.

@doug4 http://en.wikipedia.org/wiki/HSL_and_HSV
the Lightness is in range [0;1]
for L=0 (black) or L=1 (white) the saturation is 0 because the chroma is 0.


> what more do you need?
http://www.eelis.net/iso-c++/testcase.xhtml
(take especial consideration at points 2, 3, 6)
Topic archived. No new replies allowed.