简体   繁体   中英

How do i change the colour of a tabpage and label in sync

I was wondering how do I change the colours of my tabpage and label back colour in sync.

The code im using in a timer is

Random rand = new Random();
int A = rand.Next(0, 255);
int R = rand.Next(0, 255);
int G = rand.Next(0, 255);
int B = rand.Next(0, 255);
tabPage1.BackColor = Color.FromArgb(A, R, G, B)

This is what happens normally: without changing the label back colour. changing the back colour.

The reason is that you are changing both the RBG channels and also the alpha channel.

If you keep alpha at 255 the effect goes away..:

int A = 255;

To understand you must consider that a semi-transparent color is not an independent entity.

Instead it lets the background shine though to some degree so its looks change with the environment.

But that background is not the same for the tabpage and the label: the label's background is the tabpage but the tabpage's background is (probably) the form.

If you add a Panel and another Label in that Panel you can see that the Panel will have the same shade as the 1st Label but the 2nd Label, in the Panel, has an even darker shade as now even more of the rgb colors are being used.

Note that the reason behind the whole problem is a) the fishy way Winforms implements its 'tranparency' and b) the 'ambient property rule.'

Now it copies the background from the parent to supply the child control with info on how to paint itself. So the 'transparent' Label actually the gets semi-transparent color of the TabPage as its parent and then combines it with the same color which it gets from the same Parent as the ambient color.

This means the color is stacked upon itself; this is the same effect as painting with a semi-transparent brush: The more strokes you overlay the more of the color shows and tranparency goes away..

Final note, aside: The way stacking semi-transparency is implemented is not a given, natural choice. If you look at photoshop layer modes you can see a large variety of possible ways and if you imaginge painting onto an opaque color with a semi-transparent brush one could also define the 'right' result should involve adding some transparency, but this is not what gdi+ does; nor the default ('normal') PS layer mode; nor the Winforms control painting..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM