How to change label forecolour based on the background
ie. if the background is white the label textcolour should be white and vice versa
im using the following code bgDelta is always 0 and the colour varies but in case of white background the label colour is also white.
public Color IdealTextColor(Color bg)
{
int nThreshold = 105;
int bgDelta = Convert.ToInt32((bg.R * 0.299) + (bg.G * 0.587) + (bg.B * 0.114));
Color fColor = (105 - bgDelta < nThreshold) ? Color.Black : Color.White;
return fColor;
}
When I debug I got it black;
//bg.R=255, bg.G=255, bg.B=255
bgDelta=76.245 + 149.685 + 29.07= 255
105 - bgDelta=-150
result=Color.Black
Code;
IdealTextColor(Color.White);
...
public Color IdealTextColor(Color bg){
int nThreshold = 105;
int bgDelta = Convert.ToInt32((bg.R * 0.299) + (bg.G * 0.587) + (bg.B * 0.114));
Color fColor = (105 - bgDelta < nThreshold) ? Color.Black : Color.White;
return fColor;}
Better Approach;
double fcolor = 1 - ( 0.299 * color.R + 0.587 * color.G + 0.114 * color.B)/255;
lable.ForeColor = form.BackColor
链接地址: http://www.djcxy.com/p/87048.html
上一篇: 一个区域的itextsharp颜色选择器
下一篇: 如何根据背景改变标签的前景色