Contrast Color

Recommended Posts

So, been looking around the forums and found a few ways to do this, but none of the methods posted actually solved the issue for the vast majority of the colors I tried. So, trolled around the internet instead, and found a nice method. Essentially it computes the Y in CMYK (converting RGB out), and makes a calculation based on that. if too bright, sets to black, if too dark, sets to white. So far, this works great for any color i try as a background.

Note, on the above, the \$newColor variable usually comes from a ColorPicker box, or a hex value relating to a RGB color in HEX format

```#include <color.au3>
;- Get Red, Green, Blue values from the selected new color
\$red = _ColorGetRed(\$newColor)
\$green = _ColorGetGreen(\$newColor)
\$blue = _ColorGetBlue(\$newColor)

;- Calculate the Y in CMYK, so that we can ascertain the Black & White brightness value (Y)
\$y = .299 * \$red + .587 * \$green + .114 * \$blue

;- Ascertain if the Brightness Value is too high, if so, then make it black text, else white text.
If \$y < 125 Then
\$red = 255
\$green = 255
\$blue = 255
Else
\$red = 0
\$green = 0
\$blue = 0
EndIf

;- Combine RGB values
\$tmp = \$red & ";" & \$green & ";" & \$blue

;- Convert RGB to HEX
\$nRGB = _ColorSwitch(\$tmp, "R2H")```

This does require the colorswitch function I wrote. AND I KNOW I WROTE IT WRONG LOL but you can code your own or use mine.

```Func _ColorSwitch(\$ccode, \$type = "H2R", \$Symbol = "0x")
\$ccode = StringReplace(\$ccode, "0x", "")
\$ccode = StringReplace(\$ccode, "#", "")
If \$type = "H2R" Then;- Hex to RGB
If StringLen(\$ccode) < 6 Then
SetError(1)
Return -1
EndIf
\$1 = Int(\$Symbol & StringLeft(\$ccode, 2))
\$2 = Int(\$Symbol & StringTrimLeft(StringTrimRight(\$ccode, 2), 2))
\$3 = Int(\$Symbol & StringRight(\$ccode, 2))
Return \$1 & ";" & \$2 & ";" & \$3
ElseIf \$type = "H2B" Then;- Hex to BGR
If StringLen(\$ccode) < 6 Then
SetError(1)
Return -1
EndIf
\$3 = Int(\$Symbol & StringLeft(\$ccode, 2))
\$2 = Int(\$Symbol & StringTrimLeft(StringTrimRight(\$ccode, 2), 2))
\$1 = Int(\$Symbol & StringRight(\$ccode, 2))
Return \$1 & ";" & \$2 & ";" & \$3
ElseIf \$type = "B2H" Then;- BGR to Hex
\$tCode = StringSplit(\$ccode, ";")
If IsArray(\$tCode) Then
\$1 = Hex(\$tCode[1], 2)

\$2 = Hex(\$tCode[2], 2)
\$3 = Hex(\$tCode[3], 2)
ConsoleWrite(\$1 & @CRLF)
ConsoleWrite(\$2 & @CRLF)
ConsoleWrite(\$3 & @CRLF)
Return \$Symbol & \$1 & \$2 & \$3
Else
SetError(1)
Return -1
EndIf
ElseIf \$type = "R2H" Then;- RGB to Hex
\$tCode = StringSplit(\$ccode, ";")
If IsArray(\$tCode) Then
\$1 = Hex(Int(\$tCode[1]), 2)
\$2 = Hex(Int(\$tCode[2]), 2)
\$3 = Hex(Int(\$tCode[3]), 2)
Return \$Symbol & \$1 & \$2 & \$3
Else
SetError(1)
Return -1
EndIf
EndIf
SetError(2)
Return -1
EndFunc ;==>_ColorSwitch```

But, it works out great in my new app. As embedded rich text boxes, and _guiListviews have BGR instead of RGB, and these calculations make RGB Hex values, i needed a method to convert back and forth the way I needed. And this worked out great.

Usually, I dont assign a color to a control based on HEX values in windows, i use the INT value instead, to prevent the system from passing a string, or a non compliant hex code. So, when coloring controls, I usually do it this way:

```_GUICtrlRichEdit_SetCtrlBkColor(\$hRichEdit, Int(\$nRGB));- as a richedit example
_GUICtrlListView_SetBkColor(\$LISTCONTROL, Int(\$nRGB));- as a list view example```

This method works best for me, It may work for you, dunno, but hope you get some use out of it. Note, most of these things i'm posting are about my new Jabber front end i'm writing in autoit So adding functionality such as XHTML and colors to a jabber conference room that wouldn't otherwise have it I post as i get em lol, hope it's useful to someone.

Edited by zackrspv

-_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×ï§†§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×ï§† ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë ë§§ëñ§ë øƒ !ïƒë.

Share on other sites

Hi. I have dialogs I use in my apps to let the user select the background color. I have a drop down list of values selected that would work with white labels. I saw your routine and tried plugging in the background color and using the \$y calculation to decide if the label text should be black or white. It works well on many shades, but stuff like Gray where the text should really be white, is coming up with black.

I just use RGB hex values passed in. It would be great if I could let the user select from a variety of shades knowing the text would always be legible. Here's the function:

```\$gray = 0x808080
\$textColor = _CalcTextColor(\$gray)

Func _CalcTextColor(\$bColor)
Local \$r = BitAND(\$bColor, 0xFF0000)
Local \$g = BitAND(\$bColor, 0x00FF00)
Local \$b = BitAND(\$bColor, 0x0000FF)
Local \$y = .299 * \$r + .587 * \$g + .114 * \$b
If \$y < 125 Then
Return 0xFFFFFF
Else
Return 0x000000
EndIf
EndFunc ;==>_CalcTextColor```

Share on other sites

Hi,

If we are swapping functions, I have been using this (which I found somewhere on the forums) for a while:

```Func _CalcContrastColor (\$crBg)
; http://www.codeproject.com/KB/tips/JbColorContrast.aspx
Local Const \$TOLERANCE = 30

If (Abs(BitAND(\$crBg  , 0xFF) - 0x80) <= \$TOLERANCE And _
Abs(BitAND(BitShift(\$crBg , 8) , 0xFF) - 0x80) <= \$TOLERANCE And _
Abs(BitAND(BitShift(\$crBg , 16) & 0xFF) - 0x80) <= \$TOLERANCE) _
Then Return BitAND((0x7F7F7F + \$crBg) , 0xFFFFFF);

Return BitXOR(\$crBg ,0xFFFFFF);

EndFunc```

M23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

Share on other sites

What I found works pretty well, at least for my specific needs, is the simple function below. So far, of the colors I have available for the user to choose, the only "special case" seems to be Lime. If I find other exceptions then I'll probably just make a Switch. All I really need is to have black labels for background colors that would wash out a white label. So I used 0.9 .. just from extrapolaton

```;set text either white or black to contrast
;with background color
Func _CalcTextColor(\$bColor)
If \$bColor = 0x00FF00 Then  Return 0x000000
If \$bColor > (0xFFFFFF * 0.9) Then
Return 0x000000
Else
Return 0xFFFFFF
EndIf
EndFunc  ;==>_CalcTextColor```

Create an account

Register a new account