C语言程序
使用方法:输入十六进制值,程序自动输出转换后的RGB565值。
很简单的代码。
/*rgbto565.c -- Convert 16-bit RGB value into RGB565 format
Follow this example to convert it manually by yourself:
--------
example value:0xF0DF32
let us split it into 3 segments firstly:
F0 DF 32
the first segment usually represents red color value,second for green,and the third for blue.
convert every piece of them into binary format:
11110000 11011111 00110010
cut these segments from head into 3 smaller segments:
11110 110111 00110
1st segment reserved head 5 bits,2nd for 6 bits,last for 5 bits.
and put them together:
1111011011100110
convert this into hexdecimal format:
0xF6E6
got it!
--------
Written by CNflysky @ 2021.03.12
*/
#include <stdio.h>
#include <stdlib.h>
int * getrgb(void){
int * rgb=(int *)malloc(sizeof (int)*3);
printf("Input a hexdecimal color code:\nexample:f0df32 ");
scanf("%2x%2x%2x",&rgb[0],&rgb[1],&rgb[2]);
return rgb;
}
int main(){
int * rgb=getrgb();
printf("Converted RGB565 value:0x%x\n",(rgb[0] >> 3) << 11 | (rgb[1] >> 2) << 5 | (rgb[2] >> 3));
free(rgb);
return 0;
}