0

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;
   }
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Danish Kp
  • 49
  • 1
  • 11

2 Answers2

0

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;
huse.ckr
  • 530
  • 12
  • 39
0
lable.ForeColor = form.BackColor
andy
  • 5,979
  • 2
  • 27
  • 49