Is there a way to force some contrast between two colors (like background color and foreground color)?

Here is a routine that will let you do this. The code below uses the routine from our previous faq how to translate a HSB color to RGB color. [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> 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)); } } [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> Public Shared Sub AdjustForeColorBrightnessForBackColor(ByRef foreColor As Color, backColor As Color, prefContrastLevel As Single) Dim fBrightness As Single = foreColor.GetBrightness() Dim bBrightness As Single = backColor.GetBrightness() Dim curContrast As Single = fBrightness – bBrightness Dim delta As Single = prefContrastLevel – System.Convert.ToSingle(Math.Abs(curContrast)) If System.Convert.ToSingle(Math.Abs(curContrast)) < prefContrastLevel Then If bBrightness < 0.5F Then fBrightness = bBrightness + prefContrastLevel If fBrightness > 1F Then fBrightness = 1F End If Else fBrightness = bBrightness – prefContrastLevel If fBrightness < 0F Then fBrightness = 0F End If End If Dim newr, newg, newb As Single 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)))) End If End Sub ’AdjustForeColorBrightnessForBackColor

How do you translate a HSB color to RGB?

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. 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 } } } [VB.Net] Public Shared Sub ConvertHSBToRGB(h As Single, s As Single, v As Single, ByRef r As Single, ByRef g As Single, ByRef b As Single) If s = 0F Then ’ if s = 0 then h is undefined r = v g = v b = v Else Dim hue As Single = System.Convert.ToSingle(h) If h = 360F Then hue = 0F End If hue /= 60F Dim i As Integer = Fix(Math.Floor(System.Convert.ToDouble(hue))) Dim f As Single = hue – i Dim p As Single = v *(1F – s) Dim q As Single = v *(1F – s * f) Dim t As Single = v *(1F – s *(1 – f)) Select Case i Case 0 r = v g = t b = p Case 1 r = q g = v b = p Case 2 r = p g = v b = t Case 3 r = p g = q b = v Case 4 r = t g = p b = v Case 5 r = v g = p b = q Case Else r = 0F g = 0F b = 0F ’Trace.Assert(false); ’ hue out of range End Select End If End Sub ’ConvertHSBToRGB

Can I trace the caught Exception without breaking into the debugger?

Yes, there are framework classes that will give you the Call Stack information at a particular point. [C#] public static void TraceExceptionCatched(Exception e) { StackTrace st = new StackTrace(1, true); StackFrame sf = st.GetFrame(StackTrace.METHODS_TO_SKIP); if (sf != null) { MethodBase mb = sf.GetMethod(); Trace.WriteLine(e.ToString()); StringBuilder sb = new StringBuilder(); sb.Append(‘catched in File ‘); sb.Append(sf.GetFileName()); sb.Append(‘, line ‘); sb.Append(sf.GetFileLineNumber()); Trace.WriteLine(sb.ToString()); sb = new StringBuilder(); sb.Append(‘in method ‘); sb.Append(mb.ReflectedType.Name); sb.Append(‘.’); sb.Append(mb.Name); sb.Append(‘(‘); bool first = true; foreach (ParameterInfo pi in mb.GetParameters()) { if (!first) sb.Append(‘, ‘); first = false; sb.Append(pi.ParameterType.Name); sb.Append(‘ ‘); sb.Append(pi.Name); } sb.Append(‘);’); Trace.WriteLine(sb.ToString()); } } You could then call this method from the Catch portion of a try/catch block.

Can I trace the Call Stack at any particular point without breaking into the debugger?

Yes, there are classes in the framework that will provide you call stack information that you can trace to the output: [C#] public static void TraceCallStack() { StackTrace st = new StackTrace(1, true); StackFrame sf = st.GetFrame(StackTrace.METHODS_TO_SKIP); if (sf != null) { MethodBase mb = sf.GetMethod(); StringBuilder sb = new StringBuilder(); sb.Append(mb.ReflectedType.Name); sb.Append(‘.’); sb.Append(mb.Name); sb.Append(‘(‘); bool first = true; foreach (ParameterInfo pi in mb.GetParameters()) { if (!first) sb.Append(‘, ‘); first = false; sb.Append(pi.ParameterType.Name); sb.Append(‘ ‘); sb.Append(pi.Name); } sb.Append(‘);’); int n = StackTrace.METHODS_TO_SKIP+1; sf = st.GetFrame(n); if (sf != null) { sb.Append(‘ called from ‘); do { mb = sf.GetMethod(); sb.Append(mb.ReflectedType.Name); sb.Append(‘.’); sb.Append(mb.Name); sb.Append(‘:’); sf = st.GetFrame(++n); } while (sf != null); } Trace.WriteLine(sb.ToString()); } }