zackrspv Posted June 25, 2009 Share Posted June 25, 2009 (edited) 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.expandcollapse popupFunc _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 ;==>_ColorSwitchBut, 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 exampleThis 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 June 25, 2009 by zackrspv -_-------__--_-_-____---_-_--_-__-__-_ ^^€ñ†®øÞÿ ë×阮§ wï†høµ† ƒë@®, wï†høµ† †ïmë, @ñd wï†høµ† @ †ïmïdï†ÿ ƒø® !ïƒë. €×阮 ñø†, bµ† ïñ§†ë@d wï†hïñ, ñ@ÿ, †h®øµghøµ† †hë 맧ëñ§ë øƒ !ïƒë. Link to comment Share on other sites More sharing options...
MilesAhead Posted June 28, 2009 Share Posted June 28, 2009 (edited) 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 Edited June 28, 2009 by MilesAhead My Freeware Page Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 28, 2009 Moderators Share Posted June 28, 2009 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 Just adjust the $TOLERANCE to your taste. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
MilesAhead Posted June 28, 2009 Share Posted June 28, 2009 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 My Freeware Page Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now