In the conversions from HSV, HSL, and HCY to RGB, wrap the hue
value around to the [0,1) range, instead of producing unspecified
results outside this range. In particular, hue=1.0 may arise when
going through lower precision, such as when decomposing/recomposing
an 8-bit image in GIMP (see gimp#5097).
if(chroma < EPSILON) {
red = green = blue = luma;
} else {
- hue *= 6.;
- H_sec = (int)hue;
+ hue = fmod (hue, 1.0);
+ hue += hue < 0.0;
+ hue *= 6.0;
+
+ H_sec = (int) hue;
switch (H_sec)
{
double p = 2 * lightness - q;
+ hue = fmod (hue, 1.0);
+ hue += hue < 0.0;
+
red = hue2cpn (p, q, hue + 1.0/3.0);
green = hue2cpn (p, q, hue);
blue = hue2cpn (p, q, hue - 1.0/3.0);
double chroma, h_tmp, x, min;
chroma = saturation * value;
- h_tmp = hue * 6.0;
+
+ h_tmp = fmod (hue, 1.0);
+ h_tmp += h_tmp < 0.0;
+ h_tmp *= 6.0;
+
x = chroma * (1.0 - fabs (fmod (h_tmp, 2.0) - 1.0));
if (h_tmp < 1.0)