Jump to content

Selecting nearest keyvalue in an ini if given value doesn't exist


 Share

Recommended Posts

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

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

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

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

  • Moderators

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:

$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)
EndIf

You 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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 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

 

Link to comment
Share on other sites

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 )
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

(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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...