如何根据背景改变标签的前景色
即。 如果背景为白色,则标签textcolour应为白色,反之亦然
即时通讯使用以下代码bgDelta始终为0,颜色会有所不同,但在白色背景下,标签颜色也是白色。
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;
}
当我调试时,我知道它是黑色的;
//bg.R=255, bg.G=255, bg.B=255
bgDelta=76.245 + 149.685 + 29.07= 255
105 - bgDelta=-150
result=Color.Black
码;
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;}
更好的方法;
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/87047.html
上一篇: How to change label forecolour based on the background
下一篇: Algorithm to create very distinct, eye catching color from the background