Emmote Posted April 21, 2009 Share Posted April 21, 2009 Heya, I'm having some trouble with a script I'm working on. I basically need to grab a specific pixel color and convert what comes out into a word. For example: Red: 28 Green: 191 Blue: 42 Should come out as "Green" I have no problem getting the red, green and blue components, and getting their decimal value using _ColorGetRed etc. It's just I don't really know how to go about converting those values into a word without using exhaustive if / switch statements. So I was basically wondering if there's a simple way of doing this? --Emm Link to comment Share on other sites More sharing options...
Mat Posted April 21, 2009 Share Posted April 21, 2009 I would consider looking at a website that deals in displaying colours... and seeing if you can simply read that website. that way you would get the full database, and could just check rgb values. Else make a database that can be read by your script, and split it into sections so the conditional is not as long. AutoIt Project Listing Link to comment Share on other sites More sharing options...
Marlo Posted April 21, 2009 Share Posted April 21, 2009 http://www.htmlgoodies.com/tutorials/color...cle.php/3478961 Click here for the best AutoIt help possible.Currently Working on: Autoit RAT Link to comment Share on other sites More sharing options...
GEOSoft Posted April 21, 2009 Share Posted April 21, 2009 What about something like this? ; $Red = 28 $Green = 191 $Blue = 42 MsgBox(0, "Results", _ColorPredominate($Red, $Green, $Blue)) Func _ColorPredominate($iRed, $iGrn, $iBlu) If $iRed > $iGrn AND $iRed > $iBlu Then Return "Red" If $iBlu > $iGrn AND $iBlu > $iRed Then Return "Blue" If $iGrn > $iBlu AND $iGrn > $iRed Then Return "Green" EndFunc ; George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Emmote Posted April 21, 2009 Author Share Posted April 21, 2009 (edited) What about something like this? ; $Red = 28 $Green = 191 $Blue = 42 MsgBox(0, "Results", _ColorPredominate($Red, $Green, $Blue)) Func _ColorPredominate($iRed, $iGrn, $iBlu) If $iRed > $iGrn AND $iRed > $iBlu Then Return "Red" If $iBlu > $iGrn AND $iBlu > $iRed Then Return "Blue" If $iGrn > $iBlu AND $iGrn > $iRed Then Return "Green" EndFunc ; Yeah, that is basically what I'm looking for, thank you. I need to be able to convert to say "Yellow", "Orange" and "Purple" too though, but I think with your source I can manage. Edited April 21, 2009 by Emmote Link to comment Share on other sites More sharing options...
GEOSoft Posted April 21, 2009 Share Posted April 21, 2009 Yeah, that is basically what I'm looking for, thank you.I need to be able to convert to say "Yellow", "Orange" and "Purple" too though, but I think with your source I can manage. Those are fairly easy to add. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
weaponx Posted April 21, 2009 Share Posted April 21, 2009 (edited) Those are fairly easy to add.This isn't trivial. You would actually have to define a set of colors to match against and then use some kind of metric to find the closest match. You could use this list here:http://www.w3schools.com/HTML/html_colornames.aspor herehttp://en.wikipedia.org/wiki/Web_colors Edited April 21, 2009 by weaponx Link to comment Share on other sites More sharing options...
Mat Posted April 21, 2009 Share Posted April 21, 2009 list of about 140 in an ini I made: (OK then mr forum, it'll be a txt instead...) based on marlos site (or the one he linked too anyway...) looking at it, I think it has a very similar list to w3s... AutoIt Project Listing Link to comment Share on other sites More sharing options...
GEOSoft Posted April 21, 2009 Share Posted April 21, 2009 I'm not sure that he wants to play with broad ranges here. If he does, even my example isn't of much use. If $iRed > $iBlu AND $iGrn > $iBlu Then Return "Yellow" George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
Emmote Posted April 21, 2009 Author Share Posted April 21, 2009 (edited) I'm not sure that he wants to play with broad ranges here. If he does, even my example isn't of much use.If $iRed > $iBlu AND $iGrn > $iBlu Then Return "Yellow"What I need to do is simplify a colour into one of these 7 words."Red", "Green", "Blue", "Orange", "Yellow", "Purple" or "White"So I think I really need a way of hashing the dec or hex colour into a number range wherea->b = colourXb->c = colourYetc. Edited April 21, 2009 by Emmote Link to comment Share on other sites More sharing options...
Mat Posted April 21, 2009 Share Posted April 21, 2009 Geosofts method also doesn't react if blue = green... for those 7 an if test will be fine... it wont be worth the effort to make a more complex method. AutoIt Project Listing Link to comment Share on other sites More sharing options...
weaponx Posted April 21, 2009 Share Posted April 21, 2009 (edited) This uses the ini file posted my mdiesel (140 colors).Merry Christmas:expandcollapse popupDim $TestColor = 0xFFFF80 ;Should be closest to "Khaki" F0E68C $sPath = "colors.ini" $aWebColors = IniReadSection($sPath, "colours") ConsoleWrite("Palette Size: " & $aWebColors[0][0] & @CRLF) ;140 Total ;Convert text strings to hex For $X = 1 to $aWebColors[0][0] ;ConsoleWrite($aWebColors[$X][0] & ": " & $aWebColors[$X][1] & @CRLF) $aWebColors[$X][1] = Dec($aWebColors[$X][1]) Next $Result = ClosestWebColor($TestColor) MsgBox(0,"","Original: " & Hex($TestColor) & @CRLF & "Match: " & $Result[0] & " - " & Hex($Result[1])) ;----------------------------------- ; ClosestWebColor ;----------------------------------- ;Return 2-Element array ;[0]: Color name ;[1]: Color code Func ClosestWebColor($Color) Local $a[2] Local $Closest = 250000 Local $Distance = 0 For $X = 1 to $aWebColors[0][0] $Distance = Euclidian($Color, $aWebColors[$X][1]) If $Distance < $Closest Then $Closest = $Distance $a[0] = $aWebColors[$X][0] ;Name $a[1] = $aWebColors[$X][1] ;Code EndIf Next Return $a EndFunc ;----------------------------------- ; Euclidian ;----------------------------------- ;Measure Euclidian distance between 2 colors in hex format Func Euclidian($Color1, $Color2) Local $a1 = Hex2RGB($Color1) Local $a2 = Hex2RGB($Color2) Return Sqrt(($a1[0]-$a2[0])^2 + ($a1[1]-$a2[1])^2 + ($a1[2]-$a2[2])^2) EndFunc ;----------------------------------- ; Hex2RGB ;----------------------------------- ;Convert hex color to 3 element array ;[0]: R ;[1]: G ;[2]: B Func Hex2RGB($nColor) Local $a[3] $a[0] = BitAND(BitShift($nColor, 16), 0xFF) ;R $a[1] = BitAND(BitShift($nColor, 8), 0xFF) ;G $a[2] = BitAND($nColor, 0xFF) ;B Return $a EndFuncEDIT: No includes required Edited April 21, 2009 by weaponx Link to comment Share on other sites More sharing options...
Emmote Posted April 21, 2009 Author Share Posted April 21, 2009 (edited) Amazing, weaponx, thankyou very much =D Managed to port that code to do EXACTLY what I needed ^^ Edited April 21, 2009 by Emmote Link to comment Share on other sites More sharing options...
weaponx Posted April 21, 2009 Share Posted April 21, 2009 If speed is an issue, this version converts all hex values to RGB only once instead of every time the Euclidian function is called so it should be faster: expandcollapse popupDim $TestColor = 0xFFFF80 ;Should be closest to "Khaki" F0E68C $sPath = "colors.ini" $aWebColors = IniReadSection($sPath, "colours") Redim $aWebColors[Ubound($aWebColors)][5] ;Resize array to accomodate RGB ConsoleWrite("Palette Size: " & $aWebColors[0][0] & @CRLF) ;140 Total ;Convert text strings to hex For $X = 1 to $aWebColors[0][0] $aWebColors[$X][1] = Dec($aWebColors[$X][1]) $aRGB = Hex2RGB($aWebColors[$X][1]) $aWebColors[$X][2] = $aRGB[0] ;R $aWebColors[$X][3] = $aRGB[1] ;G $aWebColors[$X][4] = $aRGB[2] ;B Next $Result = ClosestWebColor($TestColor) MsgBox(0,"","Original: " & Hex($TestColor) & @CRLF & "Match: " & $Result[0] & " - " & Hex($Result[1])) ;----------------------------------- ; ClosestWebColor ;----------------------------------- ;Return 2-Element array ;[0]: Color name ;[1]: Color code Func ClosestWebColor($hColor) Local $aRGB = Hex2RGB($hColor) Local $R = $aRGB[0] Local $G = $aRGB[1] Local $B = $aRGB[2] Local $a[2] Local $Closest = 250000 Local $Distance = 0 For $X = 1 to $aWebColors[0][0] $Distance = Euclidian($R,$G,$B, $aWebColors[$X][2],$aWebColors[$X][3],$aWebColors[$X][4]) If $Distance < $Closest Then $Closest = $Distance $a[0] = $aWebColors[$X][0] ;Name $a[1] = $aWebColors[$X][1] ;Code EndIf Next Return $a EndFunc ;----------------------------------- ; Euclidian ;----------------------------------- ;Measure Euclidian distance between 2 colors in hex format Func Euclidian($R1,$G1,$B1,$R2,$G2,$B2) Return Sqrt(($R1-$R2)^2 + ($G1-$G2)^2 + ($B1-$B2)^2) EndFunc ;----------------------------------- ; Hex2RGB ;----------------------------------- ;Convert hex color to 3 element array ;[0]: R ;[1]: G ;[2]: B Func Hex2RGB($nColor) Local $a[3] $a[0] = BitAND(BitShift($nColor, 16), 0xFF) ;R $a[1] = BitAND(BitShift($nColor, 8), 0xFF) ;G $a[2] = BitAND($nColor, 0xFF) ;B Return $a EndFunc Link to comment Share on other sites More sharing options...
Mat Posted April 21, 2009 Share Posted April 21, 2009 very nice code weaponx!! good to see my stuff getting used. AutoIt Project Listing 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