RGB强度值 RGB16进制转颜色BGR565

RGB16进制转颜色BGR565

适用嵌入式 TFT、LCD屏幕
例如:215 204 13
请输入RGB数值:13 215 213
二进制:0000 1101 1101 0111 1101 0101
┌─B─┬──G──┬─R─┐
11010 110101 00001
BGR:0xD6A1
请输入RGB数值:
RGB强度值 RGB16进制转颜色BGR565_第1张图片
注意!!!转出的是BGR值,不是RGB
转载需标注来源_https://blog.csdn.net/TonyZheng138/article/details/105257299

#include 
#include 
int main(int argc, char *argv[])
{
	int a,b,c; 
	int byte_a[8]={0},byte_b[8]={0},byte_c[8]={0};
	int byte_BGR565[16]={0};
	char BGR565[10];
	printf("RGB16进制转颜色BGR565\n例如:215 204 13 \n请输入RGB数值:");
	while(scanf("%d%d%d",&a,&b,&c)!=EOF)
	{
		for(int i=0;i<8;i++)
		{
			byte_a[i] = a%2;
			a=a/2;
			byte_b[i] = b%2;
			b=b/2;
			byte_c[i] = c%2;
			c=c/2;
		}
		printf("二进制:");
		for(int i=7;i>=0;i--)
		{
			printf("%d",byte_a[i]);
			if(i%4==0)
				printf(" ");
		}
		for(int i=7;i>=0;i--)
		{
			printf("%d",byte_b[i]);
			if(i%4==0)
				printf(" ");
		}
		for(int i=7;i>=0;i--)
		{
			printf("%d",byte_c[i]);
			if(i%4==0)
				printf(" ");
		}
		printf("\n");
		printf("┌─B─┬──G──┬─R─┐\n  ");
		for(int i=0;i<5;i++)
		{
			byte_BGR565[i]=byte_c[7-i];
			printf("%d",byte_BGR565[i]);
		}
		printf("   ");
		for(int i=0;i<6;i++)
		{
			byte_BGR565[i+5]=byte_b[7-i];
			printf("%d",byte_BGR565[i+5]);
		}
		printf("    ");
		for(int i=0;i<5;i++)
		{
			byte_BGR565[i+11]=byte_a[7-i];
			printf("%d",byte_BGR565[11+i]);
		}
		printf("\n");
		/*用8421法
			8 4 2 1
			1 0 1 1
			8+0+2+1=11
			11 转16进制 =B
		*/
		int byte_BGR565_A[4]={0};
		byte_BGR565_A[0]=byte_BGR565[0]*8+byte_BGR565[1]*4+byte_BGR565[2]*2+byte_BGR565[3];
		byte_BGR565_A[1]=byte_BGR565[4]*8+byte_BGR565[5]*4+byte_BGR565[6]*2+byte_BGR565[7];
		byte_BGR565_A[2]=byte_BGR565[8]*8+byte_BGR565[9]*4+byte_BGR565[10]*2+byte_BGR565[11];
		byte_BGR565_A[3]=byte_BGR565[12]*8+byte_BGR565[13]*4+byte_BGR565[14]*2+byte_BGR565[15];
		printf("BGR:0x");
		for(int i=0;i<4;i++) 
		{
			if(byte_BGR565_A[i]<10)
				BGR565[i]=byte_BGR565_A[i]+'0';
			else if(byte_BGR565_A[i] == 10)
				BGR565[i]='A';
			else if(byte_BGR565_A[i] == 11)
				BGR565[i]='B';
			else if(byte_BGR565_A[i] == 12)
				BGR565[i]='C';
			else if(byte_BGR565_A[i] == 13)
				BGR565[i]='D';
			else if(byte_BGR565_A[i] == 14)
				BGR565[i]='E';
			else if(byte_BGR565_A[i] == 15)
				BGR565[i]='F';
			printf("%c",BGR565[i]); 
		}
		printf("\n请输入RGB数值:");
	}
	return 0;
}

你可能感兴趣的:(RGB强度值 RGB16进制转颜色BGR565)