CptSpike Posted October 16, 2012 Share Posted October 16, 2012 Title may not be particularly clear, sorry I'm struggling to put my problem in to words. But here goes. I'm writing a program that takes pixel colour info from the mouse pointer, and looks it up in an INI file to see the corresponding Pantone colour. Snippet of the INI looks like this: 16702208=108C 16765184=109C 14396928=110C (Pantone colour on the right) This is great if the pixel colour matches exactly, but if not, the user gets nothing. I want them to get the nearest match. Is it possible for AutoIt to select the nearest listed decimal number? I tried adding 1 to the pixel decimal number until it hits a matching a Pantone colour but this is far too slow. Sorry if I'm not articulating this very well. Many thanks. -CptSpike Link to comment Share on other sites More sharing options...
water Posted October 16, 2012 Share Posted October 16, 2012 How many entries do you have in your INI file? My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
FireFox Posted October 16, 2012 Share Posted October 16, 2012 Hi, I think you will need to get the section keys with IniReadSection and then comparing the data to get the nearest pantone color. Br, FireFox. Link to comment Share on other sites More sharing options...
CptSpike Posted October 16, 2012 Author Share Posted October 16, 2012 Exactly 907 entries, only one section name Firefox. Link to comment Share on other sites More sharing options...
FireFox Posted October 16, 2012 Share Posted October 16, 2012 Exactly 907 entries, only one section name Firefox.I haven't asked how many section do you have Link to comment Share on other sites More sharing options...
CptSpike Posted October 16, 2012 Author Share Posted October 16, 2012 No I mean there are no section names to compare - only one section name. [Decimal] to be precise. Link to comment Share on other sites More sharing options...
FireFox Posted October 16, 2012 Share Posted October 16, 2012 No I mean there are no section names to compare - only one section name. [Decimal] to be precise. oh, I meant compare pantone colors in the array (which contains all pantone colors). Link to comment Share on other sites More sharing options...
water Posted October 16, 2012 Share Posted October 16, 2012 The solution Firefox suggests should do what you need. InIReadSections returns a 2D array. If the entries are already sorted then search until you find a value which is higher than the value you want to comapre. Then take this or the previous value. You need to handle the situation where the first or last entry of the array is returned so you don't get out of bounds. My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
CptSpike Posted October 16, 2012 Author Share Posted October 16, 2012 Oh I see. Hadn't thought about that. Still not sure where to go from there - most of the decimal values the program looks up won't be listed in the ini file, how would I get it to pick the closest match instead? Link to comment Share on other sites More sharing options...
water Posted October 16, 2012 Share Posted October 16, 2012 This example will return the next highest value if the search value is not found in the ini file. It returns -1 if the search value is higher then the highest value in the ini file. Example works for this ini file: [section1] 16702208=108C 16765184=109C 14396928=110C #include <array.au3> Global $aValues = IniReadSection("C:temppantone.ini", "Section1") _Arraysort($aValues, 0, 1, 0, 0) _ArrayDisplay($aValues) Global $iReturn, $iSearchFor = 1670000 $iReturn = Search($iSearchFor) MsgBox(0, "", "Value searched: " & $iSearchFor & @CRLF & "Value returned: " & $iReturn) Func Search($iSearch) For $i = 1 To $aValues[0][0] If $aValues[$i][0] > $iSearch Then Return $aValues[$i][1] ConsoleWrite($i & @LF) Next Return -1 ; Value not found in table Endfunc My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted October 16, 2012 Moderators Share Posted October 16, 2012 CptSpike,Ii fear there is a fundamental flaw in your +1 theory. The colour is made up of 3 elements (RGB) and each of them can vary slightly. So you need to add to each element of the colour and not just the final total. This code gives you the idea:expandcollapse popup$iPantone = "FEDB00" ; Hex of 16702208 ; Simulate PixelGetColor converted to Hex $iColor = "FDDA00" $iRed = StringMid($iColor, 1, 2) $iGrn = StringMid($iColor, 3, 2) $iBlu = StringMid($iColor, 5, 2) ; Start by adding to R $iTest_Color = Hex(Dec($iRed) + 1, 2) & $iGrn & $iBlu If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) EndIf ; Now G $iTest_Color = $iRed & Hex(Dec($iGrn) + 1, 2) & $iBlu If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIf ; Now B $iTest_Color = $iRed & $iGrn & Hex(Dec($iBlu) + 1, 2) If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIf ; Now RG $iTest_Color = Hex(Dec($iRed) + 1, 2) & Hex(Dec($iGrn) + 1, 2) & $iBlu If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIf ; Now RB $iTest_Color = Hex(Dec($iRed) + 1, 2) & $iGrn & Hex(Dec($iBlu) + 1, 2) If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIf ; Now GB $iTest_Color = $iRed & Hex(Dec($iGrn) + 1, 2) & Hex(Dec($iBlu) + 1, 2) If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIf ; Now RGB $iTest_Color = Hex(Dec($iRed) + 1, 2) & Hex(Dec($iGrn) + 1, 2) & Hex(Dec($iBlu) + 1, 2) If $iTest_Color = $iPantone Then ConsoleWrite("Found!" & @CRLF) Else ConsoleWrite($iTest_Color & " - " & $iPantone & @CRLF) EndIfYou can see that you need to vary the colour elements separately to match the Pantone value. Of course in the real case you would need to subtract as well as add and the number of combinations you need to test becomes quite large. There should be a way of doing it faster using the Bit* functions rather then the cumbersome proof-of-concept code I used above - I will have a think about it. 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...
jdelaney Posted October 16, 2012 Share Posted October 16, 2012 Using Water's array #include <array.au3> #include <Color.au3> Global $aValues = IniReadSection("C:temppantone.ini", "Section1") _Arraysort($aValues, 0, 1, 0, 0) ;_ArrayDisplay($aValues) $iColor = 16702209 $iGreen = _ColorGetGreen ( $iColor ) $iRed = _ColorGetRed ( $iColor ) $iBlue = _ColorGetBlue ( $iColor ) ConsoleWrite ( $iGreen & " " & $iRed & " " & $iBlue & @CRLF ) Dim $aDiff[1][2] $iCounter = 0 For $i = 1 To UBound ( $aValues ) - 1 $iCurrentDiff = Abs ( _ColorGetGreen ( $aValues[$i][0] ) - $iGreen ) + Abs ( _ColorGetRed ( $aValues[$i][0] ) - $iRed ) + Abs ( _ColorGetBlue ( $aValues[$i][0] ) - $iBlue ) ReDim $aDiff[$iCounter+1][2] $aDiff[$iCounter][0] = $iCurrentDiff $aDiff[$iCounter][1] = $i $iCounter += 1 Next _ArraySort ( $aDiff, 0,0,0,0 ) $iLowestDiff = $aDiff[0][1] ConsoleWrite ( "Value searched: " & $iColor & @CRLF & "Value returned: " & $aValues[$iLowestDiff][0] & @CRLF ) CptSpike 1 IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window. Link to comment Share on other sites More sharing options...
CptSpike Posted October 16, 2012 Author Share Posted October 16, 2012 (OffTopic) I love this forum. So helpful. (OnTopic) Will play around with my code more tomorrow, looks like the array method is the way to go. Thanks for pointing out the mathemathical flaw Melba - I assumed the colours would follow a linear decimal pattern. But life is never simple jdelaney, may I shamelessly steal your code? Link to comment Share on other sites More sharing options...
kylomas Posted October 16, 2012 Share Posted October 16, 2012 CptSpike, Posted code is posted code...do yourself a favor and take time to understand the code. Good Luck, kylomas CptSpike 1 Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
CptSpike Posted October 16, 2012 Author Share Posted October 16, 2012 Thought it would be nice to ask anyways thanks everyone for the help. -CptSpike 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