Color of a screen pixel: Difference between revisions
(originated with autohotkey) |
|||
Line 3: | Line 3: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<lang AutoHotkey> |
<lang AutoHotkey> |
||
loop, 200 |
|||
{ |
|||
MouseGetPos, MouseX, MouseY |
MouseGetPos, MouseX, MouseY |
||
PixelGetColor, color, %MouseX%, %MouseY% |
PixelGetColor, color, %MouseX%, %MouseY% |
||
TrayTip, color, color at the current cursor is %color%. |
|||
sleep, 500 |
|||
} |
|||
</lang> |
</lang> |
||
Revision as of 14:56, 2 June 2009
You are encouraged to solve this task according to the task description, using any language you may know.
Get color information from an arbitrary pixel on the screen, such as the current location of the mouse cursor. The mouse cursor may or may not have to be active in a gui created by your program.
AutoHotkey
<lang AutoHotkey> MouseGetPos, MouseX, MouseY PixelGetColor, color, %MouseX%, %MouseY% </lang>
BASIC
In a graphics mode (for instance, SCREEN 13 or SCREEN 12) <lang qbasic>color = point(x, y)</lang>
C#
<lang csharp>using System; using System.Drawing; using System.Windows.Forms;
int w = Screen.PrimaryScreen.Bounds.Width; int h = Screen.PrimaryScreen.Bounds.Height;
Bitmap img = new Bitmap(w, h); Graphics g = Graphics.FromImage(img); g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(w, h));
Color color = img.GetPixel(x, y);</lang>
Tcl
Works only on X11 or OSX with Xquartz. <lang tcl>package require Tcl 8.5 package require Tk
- Farm out grabbing the screen to an external program.
- If it was just for a Tk window, we'd use the tkimg library instead
proc grabScreen {image} {
set pipe [open {|xwd -root -silent | convert xwd:- ppm:-} rb] $image put [read $pipe] close $pipe
}
- Get the RGB data for a particular pixel (global coords)
proc getPixelAtPoint {x y} {
set buffer [image create photo] grabScreen $buffer set data [$image get $x $y] image delete $buffer return $data
}
- Demo...
puts [format "pixel at mouse: (%d,%d,%d)" \
{*}[getPixelAtPoint {*}[winfo pointerxy .]]]</lang>