Update: I revised this original post to house the current version of "Get Pixel Color" (v1.1.0) to streamline the content, and redundant info was removed from the thread.
Update 6-2-20: Check out the latest post for v2.0!Update 6-10-20: Uploaded v2.1, which includes fixes that eliminate occasional flicker on the Pixel Previewer (see my most recent post)
I've seen a few questions/requests on the forum about getting pixel information with VoiceAttack.
Gary has explained that this is not possible with VA's native functionality (currently). However, with the magic of C# and VA's support for inline functions we can come up with a command that captures the RGB (and hexadecimal) color values for a particular screen pixel of interest. However,
I'm going to echo Gary's comment that using this method may be considered a Terms of Service/End User License Agreement violation depending on the application in use, so please keep that in mind.
Here are the command actions and associated C# inline function, and the related profile is attached below.
VA Command Actions:// Obtain mouse cursor's X-coordinate
Set integer [~~mouseX] value to the converted value of {MOUSESCREENX}
// Obtain mouse cursor's Y-coordinate
Set integer [~~mouseY] value to the converted value of {MOUSESCREENY}
// Output mouse cursor coordinate
Write [Black] 'Cursor Coordinate = ({INT:~~mouseX} , {INT:~~mouseY})' to log
// C# inline function to obtain RGB and hexadecimal color values for screen pixel at (X, Y) coordinate of interest
Inline C# Function: Retrieve pixel color data at (X, Y) screen coordinate, wait until execution finishes
// Output RGB color values for screen pixel coordinate of interest
Write [Red] 'Red Value = {INT:~~RedValue}' to log
Write [Green] 'Green Value = {INT:~~GreenValue}' to log
Write [Blue] 'Blue Value = {INT:~~BlueValue}' to log
// Output hexadecimal color value for screen pixel coordinate of interest
Write [Gray] 'Hex Value = {TXT:~~HexValue}' to log
C# Inline Function Code:Referenced Assemblies: System.dll; System.Drawing.dll
using System;
using System.Drawing;
using System.Runtime.InteropServices;
public class VAInline
{
public void main()
{
// Retrieve (X,Y) coordinate of interest from VA variables
int Xpos = (int)VA.GetInt("~~mouseX");
int Ypos = (int)VA.GetInt("~~mouseY");
// Retrieve pixel color data at (X, Y) screen coordinate
Color myColor = GetPixelColor(Xpos, Ypos);
// Store RGB color values
int r = myColor.R;
int g = myColor.G;
int b = myColor.B;
// Convert RGB color values to hexadecimal color value and store it
string hex = myColor.R.ToString("X2") + myColor.G.ToString("X2") + myColor.B.ToString("X2");
// Store RGB and hexideimal color data in VA variables
VA.SetInt("~~RedValue", r);
VA.SetInt("~~GreenValue", g);
VA.SetInt("~~BlueValue", b);
VA.SetText("~~HexValue", hex);
}
// Function for retrieving pixel color data at (X, Y) screen coordinate
public System.Drawing.Color GetPixelColor(int x, int y)
{
IntPtr hdc = GetDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
ReleaseDC(IntPtr.Zero, hdc);
Color color = Color.FromArgb((int)(pixel & 0x000000FF), (int)(pixel & 0x0000FF00) >> 8, (int)(pixel & 0x00FF0000) >> 16);
return color;
}
// Imported external functions
[DllImport("user32.dll")]
static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll")]
static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
}
// References:
// https://stackoverflow.com/questions/753132/how-do-i-get-the-colour-of-a-pixel-at-x-y-using-c
// https://stackoverflow.com/questions/13354892/converting-from-rgb-ints-to-hex
If you're keen on visualizing the results you can navigate to
ColorPicker.com and append the hexadecimal result to the end of the web address (like this:
ColorPicker.com/00A8FF). Note that the screen coordinates will have positive or negative values depending on your monitor setup.
For this example the output is simply the coordinate, RGB color, and hexadecimal color data for the pixel below the mouse cursor.
Enjoy!