Quick Fix for Inserting Color Data into Windows Paint (AutoHotkey Tip)

Auto-Fill Windows Program Data Fields Using RegEx—Plus, Alternative for Pop-up Messages

While the Coloretta Viva script copies pixel colors, transferring codes to Windows Paint gets awkward. This AutoHotkey data filling technique for multiple fields works in any Windows program. Plus, we look at another method for popping up user messages.

I recently highlighted the AutoHotkey Coloretta Viva color picking app at ComputorEdge Software Showcase. As a color matching tool, I consider the script an excellent start. However, I offer a couple of observations.

Once loaded, Coloretta Viva pops open a magnifying glass which expands the pixels underneath and immediately surrounding the mouse cursor as shown below.

Colorette1

Expanding the color pixels under the mouse cursor makes picking a color from the screen easy. Depending upon the Hotkey combination, you can save one of three color code formats to the Windows clipboard. You can then paste the code into any other Windows programs—except for the “Human Readable” form which consists of the individual values for red, green, and blue all in one string.

While the script does offer three Hotkeys (one for each color: red ⇒CTRL+1, green⇒CTRL+2, blue⇒CTRL+3) for inserting the individual “Human Readable” values, you can’t remove the nagging magnifying glass without closing the script. This deactivates the Hotkeys.

Awkward Tagalong Window

While you can insert the “Human Readable” code into Windows Paint while the script remains loaded, the expanded view of pixels quickly becomes an annoyance as it follows the cursor everywhere—as shown below. Plus, you must use three separate Hotkeys to get the job done (CTRL+1, CTRL+2, and CTRL+3).

Colorette4
In Widows Paint, add specific colors by entering values into the Red, Green, and Blue fields, then click “Add to Custom Colors.” However, the open Coloretta Viva window at the right gets annoying.

Exiting the script removes the expanded pixel view, but it also deactivates the needed Hotkeys. Fortunately, the settings remain saved in the Windows clipboard. However, the format of the saved “Human Readable” data (e.g. R:58 G:114 B:219) eliminates the possibility of a direct paste. We must first parse the clipboard string.

Cover 200By using a Regular Expression (RegEx) with the RegExReplace() function, we can both parse the data and enter it into all three fields with only one Hotkey combination.

Inserting Data into Multiple Fields

By writing a short AutoHotkey routine, we eliminate the need to keep the Coloretta Viva app open while inserting the data into Windows Paint (or a number of other graphic programs). Plus, the separate script automatically inserts the correct data in the correct field all in one go. We only need to click on the first field (Red), hit the Hotkey combination (CTRL+SHIFT+H) while AutoHotkey does the rest of the work:

^+h::
  NewStr := RegExReplace(Clipboard, "R:(\d+) G:(\d+) B:(\d+)" 
          , "$1{tab}$2{tab}$3")
  SendInput, ^a
  SendInput, %NewStr%
Return

We wrapped the RegExReplace() line of code for display purposes using line continuation.

The beauty of the Regular Expression lies in its flexibility when parsing and/or matching data. The RegEx used in the RegExReplace() function pulls out the numeric color values:

R:(\d+) G:(\d+) B:(\d+)

This RegEx works because the Coloretta Viva script always saves data in the same format (including the letters R:, G:, and B:). The expression \d+ tells AutoHotkey to match the sequential numeric digits. Placing parentheses around any part of the main RegEx (i.e. (\d+)) creates a backreference (i.e. $1, $2, and $3) for including in the replacement string:

$1{tab}$2{tab}$3

This RegExReplace() function extracts the three numeric values from the clipboard string and places the field jumping TAB key ({tab}) between them. Now, after first clicking into the Red field, we use the SendInput command to select the all of the first field data (SendInput, ^a), then send the color values directly to the individual Windows Paint fields as the cursor jumps ({tab}) to each successively. (Tabbing into a field normally selects the entire contents of the field.)

If in your situation the intervening characters vary, you can use the catchall RegEx (.+?) matching code for more flexibility:

.+?(\d+).+?(\d+).+?(\d+)

Depending upon the dataset parsed by a RegEx and the format of the form in the Windows program, the RegExReplace() function can create a replacement string which includes any number of AutoHotkey commands for navigation, as well as, the appropriate data backreferences for inserting data into the program form.

Tip: If you plan to always use the same graphics program, then you can add code for automatically opening the software (if not open) and possibly add the data directly to the fields without the TABs. For example, the fields in Windows Paint correspond to the control names, EDIT4 (Red), EDIT5, (Green), and EDIT6 (Blue). Using the ControlSend command, you can insert text directly into the field by name.

Using the TrayTip Command for Display User Information

Every once in a while I get surprised by a feature in an AutoHotkey script. It reconfirms for me that, although I’ve been writing about how to use AutoHotkey for years, there is always more to learn. This time I noted the System Tray message (pictured at the bottom of the first image above) which, whenever I copied a pixel color, popped up, then quickly disappeared. While I’ve seen many such messages in Windows, this was the first time I’ve seen one activated by AutoHotkey. A perusal of the code revealed the TrayTip command:

TrayTip [, Title, Text, Seconds, Options]

The TrayTip command “creates a balloon message window near the tray icon. On Windows 10, a toast notification may be shown instead.” (The Seconds “parameter has no effect on Windows Vista and later”—meaning you have no control over how long the message displays without implementing special AutoHotkey gymnastics.)

Now, I can add this TrayTip notification trick to my AutoHotkey toolbag—along with MsgBox, ToolTip, SplashText, SplashImage, and Menu, Tray, Tip. However, you’ll find the TrayTip command’s usefulness generally limited to conditions where you only need a quick peek at an informational window—especially in Windows 10. This pop-up differs from the System Tray icon ToolTip which appears whenever the mouse cursor hovers over a System Tray icon:

Menu, Tray, Tip, [text]

I find that TrayTip acts as another useful way to pass information through AutoHotkey scripts. I can save some code and use it as a replacement method for the ToolTip command which indicates the true or false condition in the Always-on-Top.ahk script.

The current form of the Aways-on-Top.ahk uses three lines of code to post and delete a temporary on-screen message:

^#F8::
  WinSet, AlwaysOnTop, toggle, A
  WinGet, ExStyle, ExStyle, A
  Tooltip, % (ExStyle & 0x8 = 0) ? "Always-On-Top OFF" : "Always-On-Top ON"
  Sleep, 1500
  ToolTip
Return

The TrayTip command only requires one line to flash the message for about five seconds:

^#F8::
  WinSet, AlwaysOnTop, toggle, A
  WinGet, ExStyle, ExStyle, A
  TrayTip, Always-on-Top, % ExStyle & 0x8 ? "Always-On-Top ON" : "Always-On-Top OFF"
Return

Since the TrayTip displays for only five to ten seconds, it would have less value as a replacement notice for the permanent ToolTip in the MousePrecise.ahk script which indicates the actively locked-down position of the left mouse button. In fact, TrayTip can disappear so fast that you might miss it.

TrayTip
The TrayTip command temporarily pops open a System Tray notice, then closes it after five to ten seconds. 

*          *          *

Like anybody else, I have expenses and a need to make ends meet. As “Jack’s AutoHotkey Blog” increases in popularity, coding the test scripts and writing the blogs takes up more of my time. That means I’ve less time to pursue other income earning opportunities. I don’t plan to ever move “Jack’s AutoHotkey Blog” behind a paywall, but if you think my efforts are worth a bit of your hard-earned cash, then you can offer a token of your appreciation by purchasing one or two of my AutoHotkey books. You may not need the references yourself, but you might know someone who can benefit from them.

Thank you,

jack

 

Advertisements

One thought on “Quick Fix for Inserting Color Data into Windows Paint (AutoHotkey Tip)

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