We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Text in ColorEdit cell hard to see

As you can see in the zip file, the ColorEdit cell text can be difficult to read depending on what color the user has selected. Is there a property of the cell that can fix this? Some suggestions are default text colors that better contrast whatever color the user selects. Split the cell into two parts with the first part being the color name with black text on a white background and the second part of the cell being a shaded region of the selected color. coloredit_1877.zip

3 Replies

AD Administrator Syncfusion Team August 22, 2004 05:59 PM

You can use PrepareViewStyleInfo and try to set e.Style.TextColor to a contrasting color. Exactly how you get the contrasting color would be up to you. Below is a code snippet that uses a technique from our Windows Forms FAQ to get a contrasting color. Another simpler option would be to set e.Style.Text to the empty string in PrepareViewStyleInfo. This way all you would see is the colored cell and not the string. If you want to try to draw the text in some white rectangle, you could handle the CellDrawn event and do whatever drawing you want done there.
private void gridControl1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
{
	if(e.Style.CellType == "ColorEdit")
	{
		Color foreColor = Syncfusion.Drawing.ColorConvert.ColorFromString(e.Style.Text);// != null && e.Style.CellValue.ToString() != "") ? (Color) e.Style.CellValue : Color.Black;
		Color adjusted = Color.Black;
		AdjustForeColorBrightnessForBackColor(ref adjusted, foreColor, 1f);
		e.Style.TextColor = adjusted;
	}
}
public static void AdjustForeColorBrightnessForBackColor(ref Color foreColor, Color backColor, float prefContrastLevel)
{
	float fBrightness = foreColor.GetBrightness();
	float bBrightness = backColor.GetBrightness();
	float curContrast = fBrightness - bBrightness;
	float delta = prefContrastLevel - (float)Math.Abs(curContrast);
	if((float)Math.Abs(curContrast) < prefContrastLevel)
	{
		if(bBrightness < 0.5f)
		{
			fBrightness = bBrightness + prefContrastLevel;
			if(fBrightness > 1.0f)
				fBrightness = 1.0f;
		}
		else
		{
			fBrightness = bBrightness - prefContrastLevel;
			if(fBrightness < 0.0f)
				fBrightness = 0.0f;
		}
		float newr, newg, newb;
		ConvertHSBToRGB(foreColor.GetHue(), foreColor.GetSaturation(), fBrightness, out newr, out newg, out newb);
		foreColor = Color.FromArgb(foreColor.A, (int)Math.Floor(newr * 255f),
			(int)Math.Floor(newg * 255f),
			(int)Math.Floor(newb * 255f));
	}
}
public static void ConvertHSBToRGB(float h, float s, float v, out float r, out float g, out float b)
{
	if (s == 0f)
	{
		// if s = 0 then h is undefined
		r = v;
		g = v;
		b = v;
	}
	else
	{
		float hue = (float)h;
		if (h == 360.0f)
		{
			hue = 0.0f;
		}
		hue /= 60.0f;
		int i = (int)Math.Floor((double)hue);
		float f = hue - i;
		float p = v * (1.0f - s);
		float q = v * (1.0f - (s * f));
		float t = v * (1.0f - (s * (1 - f)));
			switch(i)
		{
			case 0: r = v; g = t; b = p; break;
			case 1: r = q; g = v; b = p; break;
			case 2: r = p; g = v; b = t; break;
			case 3: r = p; g = q; b = v; break;
			case 4: r = t; g = p; b = v; break;
			case 5: r = v; g = p; b = q; break;
				default: r = 0.0f; g = 0.0f; b = 0.0f; break; /*Trace.Assert(false);*/ // hue out of range
		}
	}
}


PA Paulo Araujo May 13, 2008 11:55 AM

There is one issue with this...
If the color of the Text is changed, the color of the DropDownButton also changes.
How can I change the color name TextColor but not change the DropDownButton TextColor?

Thanks,
Paulo

>You can use PrepareViewStyleInfo and try to set e.Style.TextColor to a contrasting color. Exactly how you get the contrasting color would be up to you. Below is a code snippet that uses a technique from our Windows Forms FAQ to get a contrasting color.
>
>Another simpler option would be to set e.Style.Text to the empty string in PrepareViewStyleInfo. This way all you would see is the colored cell and not the string.
>
>If you want to try to draw the text in some white rectangle, you could handle the CellDrawn event and do whatever drawing you want done there.
>
>

>private void gridControl1_PrepareViewStyleInfo(object sender, GridPrepareViewStyleInfoEventArgs e)
>{
> if(e.Style.CellType == "ColorEdit")
> {
> Color foreColor = Syncfusion.Drawing.ColorConvert.ColorFromString(e.Style.Text);// != null && e.Style.CellValue.ToString() != "") ? (Color) e.Style.CellValue : Color.Black;
> Color adjusted = Color.Black;
> AdjustForeColorBrightnessForBackColor(ref adjusted, foreColor, 1f);
> e.Style.TextColor = adjusted;
> }
>}
>public static void AdjustForeColorBrightnessForBackColor(ref Color foreColor, Color backColor, float prefContrastLevel)
>{
> float fBrightness = foreColor.GetBrightness();
> float bBrightness = backColor.GetBrightness();
> float curContrast = fBrightness - bBrightness;
> float delta = prefContrastLevel - (float)Math.Abs(curContrast);
> if((float)Math.Abs(curContrast) < prefContrastLevel)
> {
> if(bBrightness < 0.5f)
> {
> fBrightness = bBrightness + prefContrastLevel;
> if(fBrightness > 1.0f)
> fBrightness = 1.0f;
> }
> else
> {
> fBrightness = bBrightness - prefContrastLevel;
> if(fBrightness < 0.0f)
> fBrightness = 0.0f;
> }
> float newr, newg, newb;
> ConvertHSBToRGB(foreColor.GetHue(), foreColor.GetSaturation(), fBrightness, out newr, out newg, out newb);
> foreColor = Color.FromArgb(foreColor.A, (int)Math.Floor(newr * 255f),
> (int)Math.Floor(newg * 255f),
> (int)Math.Floor(newb * 255f));
> }
>}
>public static void ConvertHSBToRGB(float h, float s, float v, out float r, out float g, out float b)
>{
> if (s == 0f)
> {
> // if s = 0 then h is undefined
> r = v;
> g = v;
> b = v;
> }
> else
> {
> float hue = (float)h;
> if (h == 360.0f)
> {
> hue = 0.0f;
> }
> hue /= 60.0f;
> int i = (int)Math.Floor((double)hue);
> float f = hue - i;
> float p = v * (1.0f - s);
> float q = v * (1.0f - (s * f));
> float t = v * (1.0f - (s * (1 - f)));
> switch(i)
> {
> case 0: r = v; g = t; b = p; break;
> case 1: r = q; g = v; b = p; break;
> case 2: r = p; g = v; b = t; break;
> case 3: r = p; g = q; b = v; break;
> case 4: r = t; g = p; b = v; break;
> case 5: r = v; g = p; b = q; break;
> default: r = 0.0f; g = 0.0f; b = 0.0f; break; /*Trace.Assert(false);*/ // hue out of range
> }
> }
>}
>

>
>
>




JJ Jisha Joy Syncfusion Team May 22, 2008 02:26 AM

Hi Paulo,

One way you can do this by handling the DrawCellButton event handler of the GridControl and call e.Button.Draw method to change the DropDownTextColor in a ColorEdit cell. Below are the codes:

this.gridControl1.DrawCellButton += new GridDrawCellButtonEventHandler(gridControl1_DrawCellButton);

void gridControl1_DrawCellButton(object sender, GridDrawCellButtonEventArgs e)
{
GridStyleInfo style = e.Style;
style.TextColor = Color.Black;
e.Button.Draw(e.Graphics, e.RowIndex, e.ColIndex, e.IsActive, style);
e.Cancel = true;
}

Regards,
Jisha


Loader.
Live Chat Icon For mobile
Up arrow icon