HSV to RGB model

08.10.2017

HSL (Hue, Saturation, Lightness) and HSV (Hue, Saturation, Value) are two alternative representations of the RGB color model, designed in the 1970s by computer graphics researchers to more closely align with the way human vision perceives color-making attributes. In these models, colors of each hue are arranged in a radial slice, around a central axis of neutral colors which ranges from black at the bottom to white at the top. The HSV representation models the way paints of different colors mix together, with the saturation dimension resembling various shades of brightly-colored paint, and the value dimension resembling the mixture of those paints with varying amounts of black or white paint

HSV to RGB conversion formula

When 0 ≤ H < 360, 0 ≤ S ≤ 1 and 0 ≤ V ≤ 1: 

Code:

parameter h (hue)   0 ≤ H < 36000   (36000 = 360°)

parameter s (saturation)  0 ≤ S ≤ 255   (255= 100%)

parameter v (value)   0 ≤ V ≤ 255    (255= 100%) 

uint16_t LCD::hsv_to_rgb(uint16_t h, uint32_t s, uint32_t v)
{
    
    if (h>=36000) h-=36000;
    int16_t ph = h/6;  
    ph=ph % 2000;
    ph-=1000;
    ph=1000-abs(ph);
    if (h==0)
        ph=0;
    uint32_t c= v*s;
    uint32_t x2 = c*ph;
    uint8_t x=x2/255000;
    uint8_t r,g,b;
    int32_t m=v*(255-s)/255;
    x+=m;
    if ( 0<=h && h<6000 )       {  r=v;g=x;b=m;}        //rgb = [c, x, 0];
    if ( 6000<=h && h<12000 )   {  r=x;g=v;b=m;}        //rgb = [x, c, 0];
    if ( 12000<=h && h<18000 )  {  r=m;g=v;b=x;}        //rgb = [0, c, x];
    if ( 18000<=h && h<24000 )  {  r=m;g=x;b=v;}        //rgb = [0, x, c];
    if ( 24000<=h && h<30000 )  {  r=x;g=m;b=v;}        //rgb = [x, 0, c];
    if ( 30000<=h && h<36000 )  {  r=v;g=m;b=x;}        //rgb = [c, 0, x];
    uint16_t rgb = (((r&0b11111000)<<8)|((g&0b11111100)<<3)|(b>>3));
    return rgb;
}

main code:

int main(void)
{
    //initUART();
    myLCD.init();   
    myLCD.clrScr();
    myLCD.setRectangle(2,2,257,480);
    myLCD.LCD_Write_COM(RAMWR);
    for (uint16_t t1=0;t1<450;t1++)
    {
        for (uint16_t o=0;o<256;o++)
        {
            uint16_t color=myLCD.hsv_to_rgb(t1*100,o,255);  
            myLCD.setPixel(color);
        }
    }

    while (1)
    {
    }
}

Download Atmel Studio 7.0 Project  

© 2017 Ketonk
Vytvorené službou Webnode
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky