Author Topic: Get Pixel Color  (Read 11080 times)

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Get Pixel Color
« on: August 16, 2017, 12:08:13 PM »
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:
Code: [Select]
// 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
Code: [Select]
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! :)
« Last Edit: June 10, 2020, 03:35:35 PM by Exergist »

Exergist

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 405
  • Ride the lightning
Re: Get Pixel Color
« Reply #1 on: June 02, 2020, 09:49:05 PM »
Update 6-2-20: Uploaded v2.0

Update 6-10-20: Uploaded v2.1, which includes fixes that eliminate occasional flicker on the Pixel Previewer



Summary
Attached is a new version of the 'Get Pixel Color' profile. Here are the changes:
  • Added 'Pixel Previewer' functionality (significantly modified from code by Richard Blythe)
  • Added hue, saturation, and lightness (HSL) color data as an output option
  • Added HTML color name data (if available for selected pixel) as an output option
  • Minor bug fixes and improved organization
The biggest change is the addition of the 'Pixel Previewer,' which presents a GUI for previewing screen pixel color information at a zoomed level. Here is an example of what this interface looks like:



Here is an example of the new event log output (with all output options enabled):


(note that the negative coordinates are due to my multi-monitor setup)

VoiceAttack Version Required: v1.8.5

Disclaimer: I am not responsible for anything you do to your PC. If that doesn't sit well with you, then it's time to bail out now ;)

Controls
See the 'Shortcuts' column within the profile for the default key combinations to activate the included commands. Here are the controls for the Pixel Previewer (once activated):
  • 'Left Mouse Button' as well as 'Enter' key = retrieve data for highlighted pixel (and exit Pixel Previewer)
  • 'CTRL' key + 'Left Mouse Button' = consecutively retrieve data for highlighted pixels (if enabled)
  • 'Left,' 'Right,' 'Up,' 'Down' arrow keys = shift highlighted pixel by one in corresponding direction
  • 'Escape' key = exit Pixel Previewer (no data retrieval)
Important Notes
  • The 'Get Pixel Color' command works with any application regardless of window state (i.e., it works with windowed or windowless apps and games). The 'Activate Pixel Previewer' command, however, only works with windowed applications. For instance, the Pixel Previewer works fine with Skyrim when it is run in 'windowed mode,' but it will not work properly when run with 'windowed mode' disabled. Keep in mind that there are 3rd party software out there to force any application to run in 'windowed mode.'
  • While the Pixel Previewer is active you will have limited access to your Windows desktop, since the Pixel Previewer overlays snapshots of the desktop above everything else. Just remember that both a 'Left Mouse Button' click and the 'Escape' key terminate the Pixel Previewer and return you to your native desktop experience.
  • Please contact me if you want to use this profile or its functionality in any commercial applications.
Additional Notes
  • This profile was NOT built with professional color work in mind. If you're really trying to use VoiceAttack for important color-related activities you may want to rethink your options. Having said that, this profile's output matches that of professional color pickers based on my testing.
  • All command actions and inline function code have been thoroughly commented, so you should be able to follow along with the descriptions and make any changes you desire.
  • Feel free to change the profile name, change the command triggers, disable unwanted output, etc. There are additional options for you to tweak within the 'User Input' section of the Pixel Previewer command. Only modify the areas marked as 'Pre-Processing' or 'Processing' if you know what you are doing.
  • There is an option to consecutively select multiple pixels and retrieve all their color information. As mentioned above, simply hold down the 'CTRL' key while clicking the 'Left Mouse Button.' Keep in mind that you won't be able to see the event log output until the Pixel Previewer is terminated (since the overlaid desktop snapshots cover up the VoiceAttack UI). This option is disabled by default, but you can enable it within the settings of the 'Activate Pixel Previewer' command.
  • There is a quirk with Windows that causes pixels with RGB[13,11,12] color, which is close to Black, to not be shown properly when drawn as overlaid screenshots. If this color is present on your screen, it will be instead shown as RGB[13,10,12] color on the screenshots.
  • I've done my best to test everything out, however I'm not perfect so please let me know if you find any issues. Folks are also welcome to provide feedback and ask questions.
Special Thanks
  • Pfeil for helping find sneaky bugs
  • Gary, Pfeil, randolf, and mircon for their input about the Pixel Previewer user interface
Support This and Future Efforts
If you find this profile useful, please consider buying me a cup of coffee. Thank you for your support!
 


Cheers! :)
« Last Edit: June 10, 2020, 03:35:19 PM by Exergist »