C語言--HSV轉RGB
阿新 • • 發佈:2018-10-31
void HSVtoRGB(uint8_t *r, uint8_t *g, uint8_t *b, uint16_t h, uint16_t s, uint16_t v)
{
// R,G,B from 0-255, H from 0-360, S,V from 0-100
int i;
float RGB_min, RGB_max;
RGB_max = v*2.55f;
RGB_min = RGB_max*(100 - s) / 100.0f;
i = h / 60;
int difs = h % 60; // factorial part of h
// RGB adjustment amount by hue
float RGB_Adj = (RGB_max - RGB_min)*difs / 60.0f;
switch (i) {
case 0:
*r = RGB_max;
*g = RGB_min + RGB_Adj;
*b = RGB_min;
break;
case 1:
*r = RGB_max - RGB_Adj;
*g = RGB_max;
*b = RGB_min;
break;
case 2:
*r = RGB_min;
*g = RGB_max;
*b = RGB_min + RGB_Adj;
break;
case 3:
* r = RGB_min;
*g = RGB_max - RGB_Adj;
*b = RGB_max;
break;
case 4:
*r = RGB_min + RGB_Adj;
*g = RGB_min;
*b = RGB_max;
break;
default: // case 5:
*r = RGB_max;
*g = RGB_min;
*b = RGB_max - RGB_Adj;
break;
}
}