Find answers for the most frequently asked questions
What is alpha blending
Alpha-blending refers to allowing a background color to show through a particular color. You use the static Color.FromArgb method to create a alpha-blended color. For example,
SolidBrush redBrushSolid = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
SolidBrush redBrushMedium = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
SolidBrush redBrushLight = new SolidBrush(Color.FromArgb(60, 255, 0, 0));
creates three red brushes. The first argument is the alpha-blending value, from 0 to 255. The last three arguments are the RGB values, denoting in this case, red. In the picture below, all three circles use the color red, but each circle has a different alpha blending setting, allowing the white background to show through.
I need to save a color as a string and be able to retrieve it. How can I do this
Here are a couple of routines that might do what you want. ColorToString
takes a color and represents it as a string that then can be passed into its
companion StringToColor routine that will take the string back into a color.
I think it works with all types of colors. You can download a test project.
publicstringColorToString(Color c)
{
string s = c.ToString();
s = s.Split(newchar[]{’[’,’]’})[1];
string[] strings = s.Split(newchar[]{’=’,’,’});
if(strings.GetLength(0) > 7)
{
s = strings[1] + '','' + strings[3] + '','' + strings[5] + '','' + strings[7];
}
return s;
}
public Color StringToColor(string s)
{
return (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(s);
}
[C#]
///<summary>/// Adjusts the specified Fore Color’s brightness based on the specified back color and preferred contrast.///</summary>///<param name="foreColor">The fore Color to adjust.///<param name="backColor">The back Color for reference.///<param name="prefContrastLevel">Preferred contrast level.///<remarks>/// This method checks if the current contrast in brightness between the 2 colors is /// less than the specified contrast level. If so, it brigtens or darkens the fore color appropriately.///</remarks>publicstaticvoidAdjustForeColorBrightnessForBackColor(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));
}
}
[VB.Net]
’/ <summary>’/ Adjusts the specified Fore Color’s brightness based on the specified back color and preferred contrast.’/ </summary>’/ <param name="foreColor">The fore Color to adjust.’/ <param name="backColor">The back Color for reference.’/ <param name="prefContrastLevel">Preferred contrast level.’/ <remarks>’/ This method checks if the current contrast in brightness between the 2 colors is ’/ less than the specified contrast level. If so, it brigtens or darkens the fore color appropriately.’/ </remarks>PublicSharedSub AdjustForeColorBrightnessForBackColor(ByRef foreColor As Color, backColor As Color, prefContrastLevel AsSingle)
Dim fBrightness AsSingle = foreColor.GetBrightness()
Dim bBrightness AsSingle = backColor.GetBrightness()
Dim curContrast AsSingle = fBrightness - bBrightness
Dim delta AsSingle = prefContrastLevel - System.Convert.ToSingle(Math.Abs(curContrast))
If System.Convert.ToSingle(Math.Abs(curContrast)) < prefContrastLevel ThenIf bBrightness < 0.5FThen
fBrightness = bBrightness + prefContrastLevel
If fBrightness > 1F Then
fBrightness = 1F
EndIfElse
fBrightness = bBrightness - prefContrastLevel
If fBrightness < 0F Then
fBrightness = 0F
EndIfEndIfDim newr, newg, newb AsSingle
ConvertHSBToRGB(foreColor.GetHue(), foreColor.GetSaturation(), fBrightness, newr, newg, newb)
foreColor = Color.FromArgb(foreColor.A, Fix(Math.Floor((newr * 255F))), Fix(Math.Floor((newg * 255F))), Fix(Math.Floor((newb * 255F))))
EndIfEndSub’AdjustForeColorBrightnessForBackColor
Here is a routine that does this. Note that the conversion is not precise but very close. (Please do post any better algorithm in our forums).
[C#]
// This does not seem to yield accurate results, but very close.publicstaticvoidConvertHSBToRGB(float h, float s, float v, outfloat r, outfloat g, outfloat 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)
{
case0: r = v; g = t; b = p; break;
case1: r = q; g = v; b = p; break;
case2: r = p; g = v; b = t; break;
case3: r = p; g = q; b = v; break;
case4: r = t; g = p; b = v; break;
case5: 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
}
}
}
[VB.Net]
PublicSharedSub ConvertHSBToRGB(h AsSingle, s AsSingle, v AsSingle, ByRef r AsSingle, ByRef g AsSingle, ByRef b AsSingle)
If s = 0F Then’ if s = 0 then h is undefined
r = v
g = v
b = v
ElseDim hue AsSingle = System.Convert.ToSingle(h)
If h = 360F Then
hue = 0F
EndIf
hue /= 60F
Dim i AsInteger = Fix(Math.Floor(System.Convert.ToDouble(hue)))
Dim f AsSingle = hue - i
Dim p AsSingle = v *(1F - s)
Dim q AsSingle = v *(1F - s * f)
Dim t AsSingle = v *(1F - s *(1 - f))
SelectCase i
Case0
r = v
g = t
b = p
Case1
r = q
g = v
b = p
Case2
r = p
g = v
b = t
Case3
r = p
g = q
b = v
Case4
r = t
g = p
b = v
Case5
r = v
g = p
b = q
CaseElse
r = 0F
g = 0F
b = 0F ’Trace.Assert(false);’ hue out of rangeEndSelectEndIfEndSub’ConvertHSBToRGB
Is there a way to find out the brightness of a Color?
There is a very convenient Color.GetBrightness method that will tell you how close a Color is to black or white. This is useful when you want to use a bright or a dark color to draw based on whether the background Color is dark or bright.
Share with