find nearest colour in list (nearest RGB/BGR value)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 2684
Joined: 19 Dec 2016, 01:58
Location: UK

find nearest colour in list (nearest RGB/BGR value)

17 Jul 2017, 15:31

I've created a script to find, for a given RGB (or BGR) value, the closest colour (color) in a list.
- I'd be interested as to whether there are any other common algorithms for this, that use different approaches, or perhaps any Gdip methods that 'simplify' colours/images by replacing colours with nearby colours.
- Also, I'd be interested if anybody uses something like this regularly, or might find this script or a similar script useful. Thanks.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus



[EDIT:] Some ideas for 'simplifying' or 'rounding' colours in pixels other than the repeated use of Gdip_GetPixel and Gdip_SetPixel:
Gdip: image binary data to hex string for OCR - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=35339
GDI_GrayscaleBitmap() - Converts GDI bitmap to Greyscale - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/82794-gdi-grayscalebitmap-converts-gdi-bitmap-to-greyscale/
Last edited by jeeswg on 10 Aug 2017, 19:42, edited 2 times in total.
scriptor2016
Posts: 274
Joined: 21 Dec 2015, 02:34

Re: find nearest colour in list (nearest RGB/BGR value)

17 Jul 2017, 23:46

I'm always super-interested in any sort of script which has anything to do with color identification, color matching, etc. I tried running your script but nothing was happening - it just showed a message box saying "Done" - am I doing something wrong?

I've been trying to come up with a script that will:

search a defined area on the screen, then identify the color within that area, and then search a different area on the screen and try to find the nearest color match in that area. So for example:

Step 1: x200 y200 (for example) will be a certain color on the screen - the color will always be changing, but at any given time it will be a solid color. The script will get that color upon pressing a hotkey.

Step 2: Next, it will search a different area on the screen, which will have many colors. (This particular area will be a "swatches" panel in Photoshop). So, it will now search the swatches panel and will click on the nearest color match to the first color that was found in step 1. The color will never be the exact same, but it should find the nearest match in terms of R, G and B.

I have some rough work that I tried out in the past, and will have to dig it up.

Is this something that your script is close to doing?
User avatar
jeeswg
Posts: 2684
Joined: 19 Dec 2016, 01:58
Location: UK

Re: find nearest colour in list (nearest RGB/BGR value)

18 Jul 2017, 00:16

Btw the script puts information onto the clipboard.

A possible solution (although possibly slow), is to use the Gdip_All library. Use the Gdip_BitmapFromScreen function to store the image in memory, and use Gdip_GetPixel to get the ARGB values for each pixel, you may need to use vNum & 0xFFFFFF, to discard the 'A' transparency byte, and the Format function, to get the RGB value in the right form (hex with no 0x prefix) for use with my code above. You would then modify my code a bit.

There may be other methods, especially faster methods, maybe Gdip_ImageSearch could do this, or some other existent function, maybe a function could be written in C++, and then converted for use as a machine code function.

MCode Tutorial (Compiled Code in AHK) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=32

[EDIT:] An interesting link, with details on how the PixelSearch command relaxes the RGB threshold (the 'Variation' parameter).
Script to determine how close colors are together - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=20521

Return to “Ask For Help”

Who is online

Users browsing this forum: gregster, Odlanir and 47 guests