diff --git a/rgb2hsv.c b/rgb2hsv.c index 60b175e1c388cedabad25d8766e1d4e36abe2591..49a6d0fcd7bb334ca95a549aba9c1e6fa7537456 100644 --- a/rgb2hsv.c +++ b/rgb2hsv.c @@ -34,11 +34,11 @@ void rgb2hsv(PixelHSV* hsv, PixelRGB8* rgb) { } else if (r >= g && r >= b) { maxComp = r; - if (g < b) { - minComp = g; + if (g >= b) { + minComp = b; } else { - minComp = b; + minComp = g; } tempSubtract = g - b; } @@ -90,5 +90,48 @@ void rgb2hsv(PixelHSV* hsv, PixelRGB8* rgb) { void hsv2rgb(PixelRGB8* rgb, PixelHSV* hsv) { //TODO: Implémenter + int hueMSB = (int)hsv->hue & 0x1C0; + int hueLSB = (int)hsv->hue & 0x03F; + + int tempVS = hsv->value * hsv->sat; + int vMinusVS = hsv->value - tempVS; + int evenMSB = hsv->value - ( (tempVS * hueLSB) / 0x40); + int oddMSB = hsv->value - ( (tempVS * (0x40 - hueLSB)) / 0x40); + + // Formulas for HueMBS shifted down by 1 + // so as to take the hue offset into account + // (equation 6.69 in Bailey does not) + switch (hueMSB >> 6) { + case 0: + rgb->red = hsv->value; + rgb->green = vMinusVS; + rgb->blue = evenMSB; + break; + case 1: + rgb->red = hsv->value; + rgb->green = oddMSB; + rgb->blue = vMinusVS; + break; + case 2: + rgb->red = evenMSB; + rgb->green = hsv->value; + rgb->blue = vMinusVS; + break; + case 3: + rgb->red = vMinusVS; + rgb->green = hsv->value; + rgb->blue = oddMSB; + break; + case 4: + rgb->red = vMinusVS; + rgb->green = evenMSB; + rgb->blue = hsv->value; + break; + case 5: + rgb->red = oddMSB; + rgb->green = vMinusVS; + rgb->blue = hsv->value; + break; + } }