Color of a screen pixel: Difference between revisions

Content added Content deleted
No edit summary
Line 97: Line 97:
<lang lisp>(defn get-color-at [x y]
<lang lisp>(defn get-color-at [x y]
(.getPixelColor (java.awt.Robot.) x y))</lang>
(.getPixelColor (java.awt.Robot.) x y))</lang>


=={{header|Delphi}}==

<lang Delphi>
program ScreenPixel;

{$APPTYPE CONSOLE}

uses
Windows,
SysUtils,
Graphics;


// Use this function in a GUI application to return the color
function GetPixelColourAsColor(const PixelCoords: TPoint): TColor;
var
dc: HDC;
begin
// Get Device Context of windows desktop
dc := GetDC(0);
// Read the color of the pixel at the given coordinates
Result := GetPixel(dc,PixelCoords.X,PixelCoords.Y);
end;

// Use this function to get a string representation of the current colour
function GetPixelColourAsString(const PixelCoords: TPoint): string;
var
r,g,b: Byte;
col: TColor;
begin
col := GetPixelColourAsColor(PixelCoords);

// Convert the Delphi TColor value to it's RGB components
r := col and $FF;
g := (col shr 8) and $FF;
b := (col shr 16) and $FF;

// Format the result
Result := 'R('+IntToStr(r)+') G('+IntToStr(g)+') G('+IntToStr(b)+')';

{
Alternatively, format the result as follows to get a
string representation of the Delphi TColor value

Result := ColorToString(GetPixel(dc,curP.X,curP.Y));
}

end;

var
s: string;
P: TPoint;
begin
s := '';

Writeln('Move mouse over a pixel. Hit return to get colour of selected pixel.');

repeat
Readln(s);
if s = '' then
begin
GetCursorPos(P);
Writeln('Colour at cursor position X:'+
IntToStr(P.X)+' Y:'+
IntToStr(P.Y) +' = '+
GetPixelColourAsString(P)
);
Writeln('');
Writeln('Move mouse and hit enter again.');
end;
until
SameText(s,'quit');

end.
</lang>

Example output:

<pre>
Move mouse over a pixel. Hit return to get colour of selected pixel.

Colour at cursor position X:429 Y:161 = R(0) G(0) B(0)

Move mouse and hit enter again.

Colour at cursor position X:942 Y:358 = R(182) G(206) B(231)

Move mouse and hit enter again.

Colour at cursor position X:704 Y:103 = R(240) G(240) B(240)

Move mouse and hit enter again.

Colour at cursor position X:2756 Y:59 = R(250) G(196) B(182)
</pre>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==