Live Chat Icon For mobile
Live Chat Icon

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

Platform: WinForms| Category: Colors

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#]
		/// 
		/// Adjusts the specified Fore Color’s brightness based on the specified back color and preferred contrast.
		/// 
		/// The fore Color to adjust.
		/// The back Color for reference.
		/// Preferred contrast level.
		/// 
		/// 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.
		/// 
		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]
’/ 
   ’/ Adjusts the specified Fore Color’s brightness based on the specified back color and preferred contrast.
   ’/ 
   ’/ The fore Color to adjust.
   ’/ The back Color for reference.
   ’/ Preferred contrast level.
   ’/ 
   ’/ 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.
   ’/ 
   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

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.