2303 views|0 replies

321

Posts

1

Resources
The OP
 

Calculation of mixed ratios of multiple colors for LCD [Copy link]

This post was last edited by shipeng on 2020-5-9 15:24

I suddenly had an idea: Can the LCD font be scaled down according to the required proportion, so that all font sizes share a set of dot matrix data tables? Another advantage of doing this is that when scaling small fonts, the font edges will have a grayscale effect, which will be visually softer. Of course, to achieve this soft effect, it is necessary to use an algorithm to detect the font edge and calculate the grayscale of the color according to the thickness of the edge stroke. At first glance, you may think that this algorithm is very powerful, and even worry that the single chip will not be able to calculate it? In fact, it is not the case. Let me give you an example and you will understand:

For example, I want to share a set of dot matrix data tables for 32*32 fonts and 16*16 fonts, so what should I do? First of all, the dot matrix data table should be 32*32 to make the font edges more harmonious. Then how do we display the 16*16 font? In fact, it is also very simple. After getting the 32*32 dot matrix data of the target character, take two pixels in the X direction and two pixels in the Y direction at a time. Then calculate the color of the pixel you want to light up based on the 4 pixel data. If all 4 pixels are 0, then the background color is displayed. 4 pixels 1 = 1, 3 = 0, according to the formula: mixed red component = (font color red component * 1 + background color red component * 3) / (1 + 3), mixed green component = (font color green component * 1 + background color green component * 3) / (1 + 3), mixed blue component = (font color blue component * 1 + background color blue component * 3) / (1 + 3). In addition, there are 2 pixels that are 1 and 2 that are 0; 3 pixels that are 1 and 1 that is 0; 4 pixels that are all 1; and so on. Finally, mix the calculated mixed red/green/blue components to get the color of the pixel you want to light up.

The following is the source code that implements this function:

static uint16_t CalcMixedColor(uint16_t c1,uint16_t c2,uint16_t i,uint16_t j)//__attribute__((section("RAMCODE"))) 
{
	uint16_t r,g,b;
	b =((c1&0x1F)*i + (c2&0x1F)*j)/(i+j);
	c1 >>= 5;c2>>= 5;
	g =((c1&0x3F)*i + (c2&0x3F)*j)/(i+j);
	c1 >>= 6;c2 >>= 6;
	r =((c1&0x1F)*i + (c2&0x1F)*j)/(i+j);
	return (r<<11|g<<5|b);
}

static void LCD_Show16x16sym(uint16_t x,uint16_t y,uint8_t blk,uint8_t seg,uint16_t f_c,uint16_t b_c)
{
	if (blk<0xB0 || seg<0xA1 || x>LCD_Width-16 || y>LCD_Height-16)return ;
	uint32_t font_buf[32];
	UINT br,chs_ofs=(94*(blk-0xB0)+seg-0xA1)*32*4;
	uint16_t color_pad[5];
	color_pad[0]=b_c;       color_pad[4]=f_c;
	color_pad[1]=CalcMixedColor(f_c,b_c,1,3);
	color_pad[2]=CalcMixedColor(f_c,b_c,2,2);
	color_pad[3]=CalcMixedColor(f_c,b_c,3,1);
	if (FR_OK!=f_lseek(&Font_dat,chs_ofs) || FR_OK!=f_read(&Font_dat,font_buf,32*4,&br))
	{
		for (uint16_t i=0;i<32;i++)font_buf[i]=0xFFFFFFFF;
	}

	if (f_c!=b_c)
	{
		LCD_SetWindows(x,y,x+16-1,y+16-1);

		for(uint16_t i=0;i<32;i++)
		{
			uint32_t temp[2];
			temp[0]=font_buf[i];
			temp[1]=font_buf[++i];
			for(uint16_t j=16;j>0;j--)
			{
				uint8_t pan=(temp[0]&1)+(temp[1]&1);
				temp[0]>>=1;temp[1]>>=1;
				pan += (temp[0]&1)+(temp[1]&1);
				LCD_WR_DATA(color_pad[pan]);
				temp[0]>>=1;temp[1]>>=1;
			}
		}
	}
	else
	{
		for(uint16_t i=0;i<16;i++)
		{
			uint32_t temp[2];
			temp[0]=font_buf[i*2];
			temp[1]=font_buf[i*2+1];
			for(uint16_t j=0;j<16;j++)
			{
				uint8_t pan=(temp[0]&1)+(temp[1]&1);
				temp[0]>>=1;temp[1]>>=1;
				pan += (temp[0]&1)+(temp[1]&1);
				if(pan!=0)LCD_DrawPoint(x+j,y+i,color_pad[pan]);
				temp[0]>>=1;temp[1]>>=1;
			}
		}
	}
}


Now there is a question that I cannot explain: when the font color is black and the background color is light blue, the expected effect is achieved, but after changing the font color to red, I don't know why black appears around the edges of the font?

This post is from MCU
Personal signature模电临时工
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list