Color of a screen pixel: Difference between revisions

From Rosetta Code
Content added Content deleted
(→‎Ti-89 BASIC: new example)
No edit summary
Line 62: Line 62:


pxlTest(y, x) © returns boolean
pxlTest(y, x) © returns boolean
{{omit from|R}}

Revision as of 11:30, 2 September 2009

Task
Color of a screen pixel
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

Works with: QuickBasic version 4.5

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>

Java

<lang java>public static Color getColorAt(int x, int y){

  return new Robot().getPixelColor(x, y);

}</lang>

Tcl

Library: Tk

Works only on X11 or OSX with Xquartz. <lang tcl>package require Tcl 8.5 package require Tk

  1. Farm out grabbing the screen to an external program.
  2. 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

}

  1. 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

}

  1. Demo...

puts [format "pixel at mouse: (%d,%d,%d)" \

   {*}[getPixelAtPoint {*}[winfo pointerxy .]]]</lang>

TI-89 BASIC

Only the graph screen can be read.

pxlTest(y, x)              © returns boolean