Jump to content
Sign in to follow this  
CptSpike

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

Recommended Posts

CptSpike

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

Share this post


Link to post
Share on other sites
water

How many entries do you have in your INI file?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
FireFox

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.


 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
CptSpike

Exactly 907 entries, only one section name Firefox.

Share this post


Link to post
Share on other sites
FireFox

Exactly 907 entries, only one section name Firefox.

I haven't asked how many section do you have :huh:

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
CptSpike

No I mean there are no section names to compare - only one section name. [Decimal] to be precise.

Share this post


Link to post
Share on other sites
FireFox

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

 

OS : Win XP SP2 (32 bits) / Win 7 SP1 (64 bits) / Win 8 (64 bits) | Autoit version: latest stable / beta.
Hardware : Intel(R) Core(TM) i5-2400 CPU @ 3.10Ghz / 8 GiB RAM DDR3.

My UDFs : Skype UDF | TrayIconEx UDF | GUI Panel UDF | Excel XML UDF | Is_Pressed_UDF

My Projects : YouTube Multi-downloader | FTP Easy-UP | Lock'n | WinKill | AVICapture | Skype TM | Tap Maker | ShellNew | Scriptner | Const Replacer | FT_Pocket | Chrome theme maker

My Examples : Capture toolIP Camera | Crosshair | Draw Captured Region | Picture Screensaver | Jscreenfix | Drivetemp | Picture viewer

My Snippets : Basic TCP | Systray_GetIconIndex | Intercept End task | Winpcap various | Advanced HotKeySet | Transparent Edit control

 

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
CptSpike

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?

Share this post


Link to post
Share on other sites
water

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 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Melba23

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


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 this post


Link to post
Share on other sites
jdelaney

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 )
  • Like 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.

Share this post


Link to post
Share on other sites
CptSpike

(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?

Share this post


Link to post
Share on other sites
kylomas

CptSpike,

Posted code is posted code...do yourself a favor and take time to understand the code.

Good Luck,

kylomas

  • Like 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

Share this post


Link to post
Share on other sites
CptSpike

Thought it would be nice to ask :P anyways thanks everyone for the help.

-CptSpike

Share this post


Link to post
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
Sign in to follow this  

×