AHK: How to Extract Every Hex Code from an Image

Say for some reason, you really wanted to know the hex code for every color in an image. And you really wanted to use AutoHotkey. This is the program for you.


The Code

Hold up. It’s not entirely done yet. In the last few lines of code, there is a line starting with run, C:\ that needs to be manually adjusted to the document where the color codes should be stored. The rest is ready to go.

CoordMode, Pixel, Screen
CoordMode, Mouse, Screen

FileSelectFile, file,,, Select an Image:, Images (*.gif; *.jpg; *.bmp; *.png; *.tif)

Gui, +AlwaysOnTop -Caption
Gui, Add, Picture, , %file%
Gui, Show

WinGetPos, Xaxis, Yaxis, Width, Height, A
Xaxis := Xaxis + 10
Yaxis := Yaxis + 6
Width:= Width – 20
Height:= Height – 13

clipboard =

loop, %Height%
loop, %Width%
MouseMove, Xaxis+1, Yaxis+1
PixelGetColor, color, Xaxis, Yaxis
clipboard = %clipboard% %color%
Sort, clipboard, D  U
Xaxis:= Xaxis-Width

Run, C:\Users\Desktop\Hex Extraction.txt
Sort, clipboard, D  U
SendInput {Ctrl down}v{Ctrl up} {return}

Gui Destroy

How It Works

Luckily, AutoHotkey has 3 commands that make this sort of thing ridiculously easy.

  1. FileSelectFile, Variable, Options, RootDirectory, Prompt, Filter (*.extension1; *.extension2; *.extension3)


2. PixelGetColor, Variable, X, Y


3. Sort, Variable, Options*
Dx tells the computer to separate entries by the character x
If a space is used, code will just sort entries by words.
U takes out duplicate entries.

The first, FileSelectFile, allows the user to select a file for display in a GUI window. The second, PixelGetColor, does the real heavy lifting by actually getting the HEX value fram a pixel at a specified X and Y coordinate on your screen. A loop can be used to PixelGetColor for each pixel based on the height and width of the active window, appending each new pixel color to a set variable. The third, Sort, will alphabetize and remove unnecessary duplicates, so that the result will not have a gazillion entries of the same code.

Like so.


Tagged , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: