| 37 | #define ToRGBColor(r, g, b) ((b << 16) | (g << 8) | (r)) |
| 38 | |
| 39 | void rgb2hsv(unsigned int rgb, hsv_t* hsv) |
| 40 | { |
| 41 | int r = RGBGetRValue(rgb); |
| 42 | int g = RGBGetGValue(rgb); |
| 43 | int b = RGBGetBValue(rgb); |
| 44 | int m = MIN3(r, g, b); |
| 45 | int M = MAX3(r, g, b); |
| 46 | int delta = M - m; |
| 47 | |
| 48 | if (delta == 0) { |
| 49 | /* Achromatic case (i.e. grayscale) */ |
| 50 | hsv->hue = -1; /* undefined */ |
| 51 | hsv->saturation = 0; |
| 52 | } |
| 53 | else { |
| 54 | int h; |
| 55 | |
| 56 | if (r == M) |
| 57 | h = ((g - b) * 60 * HUE_DEGREE) / delta; |
| 58 | else if (g == M) |
| 59 | h = ((b - r) * 60 * HUE_DEGREE) / delta + 120 * HUE_DEGREE; |
| 60 | else /*if(b == M)*/ |
| 61 | h = ((r - g) * 60 * HUE_DEGREE) / delta + 240 * HUE_DEGREE; |
| 62 | |
| 63 | if (h < 0) |
| 64 | h += 360 * HUE_DEGREE; |
| 65 | |
| 66 | hsv->hue = h; |
| 67 | |
| 68 | /* The constatnt 8 is tuned to statistically cause as little |
| 69 | * tolerated mismatches as possible in RGB -> HSV -> RGB conversion. |
| 70 | * (See the unit test at the bottom of this file.) |
| 71 | */ |
| 72 | hsv->saturation = (256 * delta - 8) / M; |
| 73 | } |
| 74 | hsv->value = M; |
| 75 | } |
| 76 | |
| 77 | unsigned int hsv2rgb(hsv_t* hsv) |
| 78 | { |