1. 程式人生 > >HSV轉RGB C51 code

HSV轉RGB C51 code

#define RGB_LIMIT 255
void LED_HSV_Control(uint16_t h, uint16_t s, uint16_t v)
{
// R,G,B from 0-255, H from 0-360, S,V from 0-1000
int i,difs;
unsigned char r,g,b;
long RGB_min, RGB_max,RGB_Adj;

RGB_max = v*RGB_LIMIT/1000;    //(v*RGB_LIMIT)/1000  //此處有1000倍
RGB_min = RGB_max *(1000 - s)/1000;

i = h / 60;
difs = h % 60; // factorial part of h

				   // RGB adjustment amount by hue 
RGB_Adj = (RGB_max - RGB_min)*difs / 60;

switch (i) {
case 0:
	r = RGB_max;    //(v*255)/1000
	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/1000;
	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;
}
DEBUG_INT("\r\n H %u",h);
DEBUG_INT("\r\n S %u",s);
DEBUG_INT("\r\n V %u",v);
DEBUG_INT("\r\nLED_R %bu",r);
DEBUG_INT("\r\nLED_G %bu",g);
DEBUG_INT("\r\nLED_B %bu\r\n",b);
LED_RGB_Control(r,g,b);

}