Click here to Skip to main content
15,906,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello anybody,
I need to get rgb565 from rgb888,because I want to show pictures on TFT,
I have tried the way like this

C++
#define COLOR_TO_MTK_COLOR_SIMUL(color) ((((color) >> 19) & 0x1f) \

                                            |((((color) >> 10) & 0x3f)  \

                                            |(((color) >> 3) & 0x1f)

but it is very bad method.

Is there anybody meet this problem?
Posted
Updated 10-Mar-12 2:27am
v2

Appears to me that you extracted the color components correctly but forgot to shift the components back into place to form the RGB565 format.

inline unsigned RGB888_to_RGB565 (unsigned rgb)
{
    return 
        (((rgb >> 19) & 0x1f) << 11) |
        (((rgb >> 10) & 0x3f) <<  6) |
        (((rgb >>  3) & 0x1f)      );
}


Is that the reason you were getting bad results? If not, please tell in which way the results you got were too bad or unexpected.
 
Share this answer
 
Hi, i found a error:

C++
unsigned long RGB888_to_RGB565 (unsigned long rgb)
{
    return
        (((rgb >> 19) & 0x1f) << 11) |//19+10+3=32  //000000000000000000011111  //00001F    //1111100000000000
        (((rgb >> 10) & 0x3f) <<  5) |              //000000000011111111111111  //003FFF    //0000011111100000
        (((rgb >>  3) & 0x1f)      );               //000111111111111111111111  //1FFFFF    //
}

int main() {
    long color = RGB888_to_RGB565(0x0000ff);
    pc.printf("%X\n",color);
}


No is 6, is 5. Tank you for you code, it help me to solve a great problem in my job, its my contribution for you.
Bye my friend...
 
Share this answer
 
v2
Hello friends, may be it can help:

C++
Serial pc(USBTX,USBRX);

unsigned int RGB565CONVERT(int r,int g,int b,const char* color) {
    long RGB565;

    RGB565=(((r&0xF8)<<8)|((g&0xFC)<<3)|((b&0xF8)>>3));
    pc.printf("#define  %s      0x%0.4X\n",color,RGB565);
    return RGB565;
}

unsigned int RGB888_to_RGB565 (unsigned long rgb,const char* color) {
    long RGB565;

    RGB565= (((rgb >> 19) & 0x1f) << 11)|(((rgb >> 10) & 0x3f) <<  5)|(((rgb >>  3) & 0x1f));
    pc.printf("#define  %s      0x%0.4X\n",color,RGB565);
    return RGB565;
}

int main() {
    //long color = RGB888_to_RGB565(0x0000ff);
    //pc.printf("%X\n",color);
    RGB888_to_RGB565(0x0000ff,"BRIGHTBLUE");
    RGB565CONVERT(0,0,0,"NEGRO");
    RGB565CONVERT(0,0,255,"BRIGHTBLUE");
    RGB565CONVERT(0,255,0,"BRIGHTGREEN");
    RGB565CONVERT(0,255,255,"BRIGHTCYAN");
    RGB565CONVERT(255,0,0,"BRIGHTRED");
    RGB565CONVERT(255,0,255,"BRIGHTMAGENTA");
    RGB565CONVERT(255,255,0,"BRIGHTYELLOW");
    RGB565CONVERT(0,0,128,"AZUL");
    RGB565CONVERT(0,128,0,"VERDE");
    RGB565CONVERT(0,128,128,"CIAN");
    RGB565CONVERT(128,0,0,"ROJO");
    RGB565CONVERT(128,0,128,"MAGENTA");
    RGB565CONVERT(255,128,0,"BROWN");
    RGB565CONVERT(128,128,128,"gris_claro");
    RGB565CONVERT(64,64,64,"gris_oscuro");
    RGB565CONVERT(128,128,255,"lightblue");
    RGB565CONVERT(128,255,128,"verde_claro");
    RGB565CONVERT(128,255,255,"LIGHTCYAN");
    RGB565CONVERT(255,128,128,"LIGHTRED");
    RGB565CONVERT(255,128,255,"LIGHTMAGENTA");
    RGB565CONVERT(255,255,128,"AMARILLO");
    RGB565CONVERT(255,255,255,"BLANCO");
    RGB565CONVERT(224,224,224,"GRAY0");
    RGB565CONVERT(192,192,192,"GRAY1");
    RGB565CONVERT(160,160,160,"GRAY2");
    RGB565CONVERT(128,128,128,"GRAY3");
    RGB565CONVERT(96,96,96,"GRAY4");
    RGB565CONVERT(64,64,64,"GRAY5");
    RGB565CONVERT(32,32,32,"GRAY6");
    RGB565CONVERT(51,51,51,"GRAY20");
    RGB565CONVERT(102,102,102,"GRAY40");
    RGB565CONVERT(204,204,204,"GRAY80");
    RGB565CONVERT(229,229,229,"GRAY90");
    RGB565CONVERT(242,242,242,"GRAY95");
    RGB565CONVERT(139,0,0,"RED4");
    RGB565CONVERT(255,48,48,"FIREBRICK1");
    RGB565CONVERT(0,100,0,"DARKGREEN");
    RGB565CONVERT(152,251,152,"PALEGREEN");
    RGB565CONVERT(238,221,130,"LIGHTYELLOW");
    RGB565CONVERT(255,215,0,"GOLD");
    RGB565CONVERT(255,140,0,"DARKORANGE");
    
}



OUTPUT:

C++
#define  BRIGHTBLUE                       0x001F
#define  NEGRO      0x0000
#define  BRIGHTBLUE      0x001F
#define  BRIGHTGREEN      0x07E0
#define  BRIGHTCYAN      0x07FF
#define  BRIGHTRED      0xF800
#define  BRIGHTMAGENTA      0xF81F
#define  BRIGHTYELLOW      0xFFE0
#define  AZUL      0x0010
#define  VERDE      0x0400
#define  CIAN      0x0410
#define  ROJO      0x8000
#define  MAGENTA      0x8010
#define  BROWN      0xFC00
#define  gris_claro      0x8410
#define  gris_oscuro      0x4208
#define  lightblue      0x841F
#define  verde_claro      0x87F0
#define  LIGHTCYAN      0x87FF
#define  LIGHTRED      0xFC10
#define  LIGHTMAGENTA      0xFC1F
#define  AMARILLO      0xFFF0
#define  BLANCO      0xFFFF
#define  GRAY0      0xE71C
#define  GRAY1      0xC618
#define  GRAY2      0xA514
#define  GRAY3      0x8410
#define  GRAY4      0x630C
#define  GRAY5      0x4208
#define  GRAY6      0x2104
#define  GRAY20      0x3186
#define  GRAY40      0x632C
#define  GRAY80      0xCE79
#define  GRAY90      0xE73C
#define  GRAY95      0xF79E
#define  RED4      0x8800
#define  FIREBRICK1      0xF986
#define  DARKGREEN      0x0320
#define  PALEGREEN      0x9FD3
#define  LIGHTYELLOW      0xEEF0
#define  GOLD      0xFEA0
#define  DARKORANGE      0xFC60

****************************************Copy into code such as it be***************************
PD: sorry, my english not is good... i am Ecuatorian, i talk spanish.... Copien tal como esta el codigo en su programa...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900