Color of a screen pixel: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 4 users not shown)
Line 13:
===easy6502/6502js===
Video memory is mapped into $0200-$05ff, and the bottom 4 bits of each byte represent the pixel's color.
<langsyntaxhighlight lang="6502asm">LDA $0200 ;get the color of the top-left pixel of the screen
LDA $05FF ;get the color of the bottom-right pixel of the screen.</langsyntaxhighlight>
 
===Nintendo Entertainment System===
The NES can't determine the color of a single pixel. The closest you can get is a 16 pixel by 16 pixel section of graphics. And that only tells you the palette in use at that location, not the actual color. Color cells are 2 bits per 16x16 section, in the order of top-left, top-right, bottom-left, bottom-right.
 
<langsyntaxhighlight lang="6502asm">;this code will fail to produce the desired result unless it executes during vblank or forced blank.
;address $23C0 = first color cell of nametable $2000
LDA #$23
Line 27:
 
LDA $2007
AND #%11000000 ;00------ = top-left corner uses palette 0, 01------ = top-left corner uses palette 1, etc.</langsyntaxhighlight>
 
This code assumes no scrolling has taken place, as it only reads the color cells from the top-left nametable. Generally speaking, reading VRAM in this way is not a good practice, as it's very slow and can only be done during vBlank when you have better things to do. For an actual game, it's much easier to keep a shadow of this data in normal RAM and read that instead, and pretend that VRAM is write-only.
Line 34:
===With Interrupts===
MS-DOS has a built-in way to calculate the color of a pixel on-screen.
<langsyntaxhighlight lang="asm">;input: cx = x coordinate of pixel, dx = y coordinate of pixel, bh = page number
mov ah,0Dh
int 10h</langsyntaxhighlight>
 
The color is returned in AL.
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">PROC Main()
BYTE POINTER ptr
BYTE
Line 86:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Color_of_a_screen_pixel.png Screenshot from Atari 8-bit computer]
Line 97:
[https://lh4.googleusercontent.com/-Gw0xm7RIEck/Uute2nQSGsI/AAAAAAAAJ90/rq3UuWYE9Yw/s1600/Capture.PNG <VIEW THE BLOCKS AND ANDROID APP DISPLAY>]
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">PixelGetColor, color, %X%, %Y%</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<langsyntaxhighlight lang="autoit">Opt('MouseCoordMode',1) ; 1 = (default) absolute screen coordinates
$pos = MouseGetPos()
$c = PixelGetColor($pos[0], $pos[1])
ConsoleWrite("Color at x=" & $pos[0] & ",y=" & $pos[1] & _
" ==> " & $c & " = 0x" & Hex($c) & @CRLF)</langsyntaxhighlight>
{{Out}}
<pre>
Line 111:
 
=={{header|Axe}}==
<langsyntaxhighlight lang="axe">Disp pxl-Test(50,50)▶Dec,i</langsyntaxhighlight>
 
=={{header|BaCon}}==
BaCon can make use of the High Performance Canvas include. Outside this canvas it needs to access XLib API functions.
<langsyntaxhighlight lang="qbasic">INCLUDE canvas
FULLSCREEN
color = GETINK(100, 100, 4)
WAITKEY</langsyntaxhighlight>
 
=={{header|BASIC}}==
Line 124:
==={{header|AmigaBASIC}}===
 
<langsyntaxhighlight lang="amigabasic">MOUSE ON
 
WHILE 1
m=MOUSE(0)
PRINT POINT(MOUSE(1),MOUSE(2))
WEND</langsyntaxhighlight>
(The color index is -1 if the mouse pointer is outside the Basic window.)
 
Line 135:
[http://en.wikipedia.org/wiki/Apple_II_graphics#Low-Resolution_.28Lo-Res.29_graphics Low-Resolution (Lo-Res) graphics] 40x48, 16 colors, page 1
 
<langsyntaxhighlight Applesoftlang="applesoft BASICbasic">X = PDL (0) * 5 / 32
Y = PDL (1) * 3 / 16
COLOR= SCRN( X,Y)</langsyntaxhighlight>
[http://en.wikipedia.org/wiki/Apple_II_graphics#High-Resolution_.28Hi-Res.29_graphics Hi-Resolution (Hi-Res) graphics] 280x192, 6 colors
 
There is no HSCRN( X,Y) function in Applesoft. What follows is an elaborate subroutine that determines the hi-res color at the location given by variables X and Y on the current hi-res page. A color value in the range from 0 to 7 is returned in the variable C. The color is determined by peeking at adjacent pixels and the Most Significant Bit [http://en.wikipedia.org/wiki/Most_significant_bit MSB]. The VTAB routine is used as an aid to calculate the address of pixels. Other colors beyond the 6 hi-res colors can be displayed by positioning pixels at byte boundaries using the MSB. This routine is limited to the eight hi-res colors.
 
<syntaxhighlight lang="applesoft basic">
<lang Applesoft BASIC>
100 REM GET HCOLOR
110 REM PARAMETERS: X Y
Line 184:
X = 267 : Y = 166 : GOSUB 100
HCOLOR= C
</syntaxhighlight>
</lang>
 
==={{header|BBC BASIC}}===
In [[BBC BASIC for Windows]] you can read either the 'logical colour' (palette index) or the 'true colour' (24-bit RGB value).
<langsyntaxhighlight lang="bbcbasic"> palette_index% = POINT(x%, y%)
RGB24b_colour% = TINT(x%, y%)</langsyntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 199:
The Commodore 64 hires bitmap is 320&times;200, subdivided into 8&times;8 cells starting at the top left and moving right. Each cell is addressed top to bottom by 8 bytes. Each byte controls a horizontal row of 8 bits. This requires calculation on the programmer's part to translate X,Y coordinates into a specific memory address/value combination (lines 1210 through 1220).
 
<langsyntaxhighlight lang="gwbasic">5 rem commodore 64 example
10 base=2*4096:x=100:y=50:poke53280,0
20 gosub 1000:print chr$(147);
Line 225:
1220 bit=7-(x and 7):byte=base+ro*320+char*8+li:cb=1024+ro+ch
1230 return
</syntaxhighlight>
</lang>
 
'''Example 2:''' Commodore Plus 4 and 128
Line 231:
On both machines, there is a split graphics-text screen that can be used, and the extended BASIC provides functions for reading pixel and color values from the bitmap.
 
<langsyntaxhighlight lang="gwbasic">10 color 0,1:color 1,3: rem set border to black and pixel color to red
15 graphic 2,1: rem enter split graphics/text mode and clear screen
20 draw 1,100,50 : rem plot pixel at 100,50
30 print "pixel color at";rdot(0);",";rdot(1);"is";rclr(rdot(2))
40 get k$:if k$="" then 40
50 graphic 0,1 : rem return to text mode</langsyntaxhighlight>
 
 
Line 242:
This is a very simple example from the FreeBASIC documentation. To obtain the color of an arbitrary screen pixel (i.e. outside
the graphics screen controlled by FB) one would need to use API functions.
<langsyntaxhighlight lang="freebasic">FB 1.05.0 Win64
 
' Set an appropriate screen mode - 320 x 240 x 8bpp indexed color
Line 254:
 
' Sleep before the program closes
Sleep</langsyntaxhighlight>
 
==={{header|QuickBASIC}}===
Line 260:
 
In a graphics mode (for instance, <tt>SCREEN 13</tt> or <tt>SCREEN 12</tt>)
<langsyntaxhighlight lang="qbasic">color = point(x, y)</langsyntaxhighlight>
 
==={{header|Integer BASIC}}===
Line 267:
 
==={{header|Liberty BASIC}}===
<langsyntaxhighlight lang="lb">'This example requires the Windows API
Struct point, x As long, y As long
 
Line 291:
Function GetPixel(hDC, x, y)
CallDLL #gdi32, "GetPixel", hDC As uLong, x As long, y As long, GetPixel As long
End Function</langsyntaxhighlight>
 
==={{header|Locomotive Basic}}===
 
<langsyntaxhighlight lang="locobasic">10 x=320:y=200
20 color=TEST(x,y)
30 PRINT "Pen color at"; x; y; "is"; color</langsyntaxhighlight>
 
==={{header|PureBasic}}===
Return the color used at the x,y position in the current output. If the current output has an alpha channel then the result will be a 32bit RGBA value, otherwise it will be a 24bit RGB value. The color can be split in their RGB and alpha values by using the Red(), Green(), Blue() and Alpha() functions.
 
<langsyntaxhighlight PureBasiclang="purebasic">Color = Point(x, y)</langsyntaxhighlight>
 
To get the colour of a pixel on the screen when it is not managed by PureBasic (ie. from other programs' windows), it is necessary to use Windows API. This works only under Windows.
 
<syntaxhighlight lang="purebasic">
<lang PureBasic>
hDC = GetDC_(0)
Color = GetPixel_(hDC, x, y)
ReleaseDC_(0, hDC)</langsyntaxhighlight>
 
This work fine!!
 
<langsyntaxhighlight PureBasiclang="purebasic">poz.point
If OpenWindow(0,0,0,100,45,"Get pixel color at cursor position",#PB_Window_MinimizeGadget)
TextGadget(0,0,0,50,12,"Red: ")
Line 335:
Until event=#PB_Event_CloseWindow
ReleaseDC_(0, hDC)
EndIf</langsyntaxhighlight>
 
=={{header|QBasic}}==
<syntaxhighlight lang="qbasic">
<lang QBasic>
'Example: Find color of a screen pixel in QBasic (adapted from QBasic Help file).
' POINT(x%, y%) returns color of pixel at coordinates x,y.
Line 351:
NEXT y%
END
</syntaxhighlight>
</lang>
 
==={{header|BASIC256}}===
<langsyntaxhighlight lang="basic256">color rgb(0, 255, 0)
rect 50, 50, 75, 75
 
Line 367:
azul = (c & 0xff)
 
print rojo; " "; verde; " "; azul</langsyntaxhighlight>
 
==={{header|SmileBASIC}}===
<langsyntaxhighlight lang="smilebasic">DEF GETPX X,Y OUT R,G,B
PCOL=GSPOIT(X,Y)
RGBREAD PCOL OUT R,G,B
END</langsyntaxhighlight>
 
==={{header|TI-89 BASIC}}===
Only the graph screen can be read.
 
<langsyntaxhighlight lang="ti89b">pxlTest(y, x) © returns boolean</langsyntaxhighlight>
 
==={{header|Visual Basic .NET}}===
<langsyntaxhighlight lang="vbnet"> Private Function GetPixelColor(ByVal Location As Point) As Color
 
Dim b As New Bitmap(1, 1)
Line 390:
Return b.GetPixel(0, 0)
 
End Function</langsyntaxhighlight>
 
==={{header|VBA}}===
Line 397:
This code should be adapted for 64 bits versions...
 
<syntaxhighlight lang="vb">
<lang vb>
Option Explicit
 
Line 421:
Get_Color_Under_Cursor = GetPixel(lngDc, Pos.x, Pos.y)
End Function
</syntaxhighlight>
</lang>
 
==={{header|Yabasic}}===
<langsyntaxhighlight Yabasiclang="yabasic">open window 100, 100
backcolor 255, 0, 0
clear window
Line 437:
red = dec(left$(s$, 2))
 
print red, " ", green, " ", blue</langsyntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
Line 444:
=={{header|C}}==
{{libheader|Xlib}}
<syntaxhighlight lang="c">
<lang c>
#include <X11/Xlib.h>
void
Line 460:
get_pixel_color (display, 30, 40, &c);
printf ("%d %d %d\n", c.red, c.green, c.blue);
</syntaxhighlight>
</lang>
 
{{works with|Windows}}
(Linux users, see [http://www.muquit.com/muquit/software/grabc/grabc.html grabc].)
<langsyntaxhighlight lang="c">#include <Windows.h>
 
COLORREF getColorAtCursor(void) {
Line 489:
 
return color;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Windows.Forms;
Line 514:
Console.WriteLine(GetPixel(Cursor.Position));
}
}</langsyntaxhighlight>
Sample output:
<syntaxhighlight lang="text">Color [A=255, R=243, G=242, B=231]</langsyntaxhighlight>
 
=={{header|C++/CLI}}==
<langsyntaxhighlight lang="cpp">using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
Line 536:
Console::WriteLine("G: "+color.G.ToString());
Console::WriteLine("B: "+color.B.ToString());
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(defn get-color-at [x y]
(.getPixelColor (java.awt.Robot.) x y))</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
Using Allegro and their Common Graphics package
<langsyntaxhighlight lang="lisp">(in-package :cg-user)
 
(defun print-hex (n)
Line 561:
(get-pixel (position-x pos) (position-y pos))))
 
(print-hex (get-mouse-pixel))</langsyntaxhighlight>
 
Sample output: (values are in RGBA order):
<langsyntaxhighlight lang="lisp">(#xe0 #x43 #x43 #xff)</langsyntaxhighlight>
 
=={{header|Delphi}}==
 
<syntaxhighlight lang="delphi">
<lang Delphi>
program ScreenPixel;
 
Line 640:
 
end.
</syntaxhighlight>
</lang>
 
Example output:
Line 663:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">open System.Drawing
open System.Windows.Forms
 
Line 675:
let GetPixelAtMouse () =
let pt = Cursor.Position
GetPixel pt.X pt.Y</langsyntaxhighlight>
 
 
=={{header|FutureBasic}}==
Tracks color information of the pixel under the current mouse x/y coordinates.
<syntaxhighlight lang="futurebasic">
_window = 1
begin enum 1
_view
_colorWell
_imageView
end enum
 
void local fn BuildWindow
window _window, @"ColorUnderMouse", (0,0,500,400), NSWindowStyleMaskTitled + NSWindowStyleMaskClosable
view subclass _view, (0,0,500,300)
colorwell _colorWell, YES, fn ColorWhite, ( 410, 310, 70, 70 ), NO, _window
end fn
 
void local fn DrawRect
CFArrayRef array = @[fn ColorRed, fn ColorOrange, fn ColorYellow, fn ColorGreen, fn ColorBlue, fn ColorWithRGB(0,0.29,0.51,1), fn ColorWithRGB(0.58,0.0,0.83,1)]
GradientRef grad = fn GradientWithColors( array )
GradientDrawInRect( grad, fn ViewFrame(_view), 0 )
end fn
 
 
void local fn DoMouse( tag as NSInteger )
CGPoint pt = fn EventLocationInView( tag )
ColorRef color = fn ViewColorAtPoint( tag, pt )
ColorWellSetColor( _colorWell, color )
cls : printf @"%.0fx, %.0fy, %@", pt.x, pt.y, color
end fn
 
void local fn DoDialog( ev as long, tag as long )
select ( tag )
case _view
select ( ev )
case _viewDrawRect : fn DrawRect
case _viewMouseDown : fn DoMouse( tag )
case _viewMouseDragged : fn DoMouse( tag )
end select
end select
select ( ev )
case _windowWillClose : end
end select
end fn
 
fn BuildWindow
 
on dialog fn DoDialog
 
HandleEvents
</syntaxhighlight>
{{output}}
[[File:Color Under Mouse.png]]
 
=={{header|Go}}==
{{libheader|RobotGo}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 693 ⟶ 750:
color := robotgo.GetPixelColor(x, y)
fmt.Printf("Color of pixel at (%d, %d) is 0x%s\n", x, y, color)
}</langsyntaxhighlight>
 
{{out}}
Line 702 ⟶ 759:
 
=={{header|Groovy}}==
<langsyntaxhighlight Groovylang="groovy">import java.awt.Robot
 
class GetPixelColor {
Line 713 ⟶ 770:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Icon and Unicon don't have direct access to the screen; however, we can read the colour of of a maximal sized window instead. The graphics procedure generates all pixels from a rectangular selection as a comma separated string with RGB values.
<langsyntaxhighlight Iconlang="icon">link graphics,printf
procedure main()
Line 741 ⟶ 798:
WDone(W) # q to exit
end
</syntaxhighlight>
</lang>
 
{{libheader|Icon Programming Library}}
Line 757 ⟶ 814:
Assuming the OS is Windows (Windows being an operating system with fairly wide adoption, which also guarantees a specific concept of "screen"):
 
<langsyntaxhighlight Jlang="j">GetDC=: 'user32.dll GetDC >i i'&cd NB. hdx: GetDC hwnd
GetPixel=: 'gdi32.dll GetPixel >l i i i'&cd NB. rgb: GetPixel hdc x y
GetCursorPos=: 'user32.dll GetCursorPos i *i'&cd NB. success: point</langsyntaxhighlight>
 
Task example -- reading the color of the pixel at the mouse cursor:
 
<langsyntaxhighlight Jlang="j"> |.(3#256) #: GetPixel (GetDC<0),1{::GetCursorPos<0 0
121 91 213</langsyntaxhighlight>
 
Breaking this down:
 
<langsyntaxhighlight Jlang="j"> GetDC<0
1325469620
GetCursorPos<0 0
Line 779 ⟶ 836:
13982585
|.(3#256)#:13982585
121 91 213</langsyntaxhighlight>
 
(The windows color result is packed as 0x00bbggrr. So, after splitting the integer result into bytes we need to reverse their order to get red,green,blue.)
Line 785 ⟶ 842:
=={{header|Java}}==
{{uses from|AWT|Robot}}
<langsyntaxhighlight lang="java">public static Color getColorAt(int x, int y){
return new Robot().getPixelColor(x, y);
}</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">
# Windows GDI version
function getpixelcolors(x, y)
Line 802 ⟶ 859:
cols = getpixelcolors(x, y)
println("At screen point (x=$x, y=$y) the color RGB components are red: $(cols[1]), green: $(cols[2]), and blue: $(cols[3])")
</langsyntaxhighlight> {{output}} <pre>
At screen point (x=120, y=100) the color RGB components are red: 1, green: 36, and blue: 86
</pre>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">import java.awt.*
 
fun getMouseColor(): Color {
Line 816 ⟶ 873:
fun getColorAt(x: Int, y: Int): Color {
return Robot().getPixelColor(x, y)
}</langsyntaxhighlight>
 
=={{header|Lingo}}==
 
{{libheader|ScrnXtra3 Xtra}}
<langsyntaxhighlight lang="lingo">on getScreenPixelColor (x, y)
sx = xtra("ScrnXtra3").new()
img = sx.ScreenToImage(rect(x, y, x+1, y+1))
return img.getPixel(0, 0)
end</langsyntaxhighlight>
 
=={{header|Logo}}==
Line 835 ⟶ 892:
=={{header|M2000 Interpreter}}==
Colors is M2000 have a negative value for RGB, or positive for default colors (0 to 15 are the default colors). Also numbers above 0x80000000 (is a positive number), are Windows colors too. Point return a negative value so we have to make it positive to get the RGB value where Red is the least significant byte. Html color has R as the most significant byte (of three), so to display properly we have to use a mix of Right$(),Mid$() and Left$() functions on string representation on color$.
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckColor {
\\ Print hex code for color, and html code for color
Line 847 ⟶ 904:
}
CheckColor
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">getPixel[{x_?NumberQ, y_?NumberQ}, screenNumber_Integer: 1] := ImageValue[CurrentScreenImage[n], {x, y}]</langsyntaxhighlight>
 
=={{header|Nim}}==
===Using GTK2===
{{libheader|GTK2}}
<langsyntaxhighlight Nimlang="nim">import gtk2, gdk2, gdk2pixbuf
 
type Color = tuple[r, g, b: byte]
Line 867 ⟶ 924:
result = cast[ptr Color](p.getPixels)[]
 
echo getPixelColor(0, 0)</langsyntaxhighlight>
 
===Using GTK3===
{{libheader|gintro}}
<langsyntaxhighlight Nimlang="nim">import gintro/[gtk, gobject, gio, gdk, gdkpixbuf]
 
type Color = tuple[r, g, b: byte]
Line 887 ⟶ 944:
discard run(app)
 
echo getPixelColor(1500, 800)</langsyntaxhighlight>
 
=={{header|Perl}}==
This example works with MacOS, customize with the appropriate <tt>screencapture</tt> utility for other OSes.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use GD;
Line 904 ⟶ 961:
print "RGB: $red, $green, $blue\n";
 
unlink $file;</langsyntaxhighlight>
{{out}}
<pre>RGB: 20, 2, 124</pre>
Line 910 ⟶ 967:
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<langsyntaxhighlight Phixlang="phix">integer {r,g,b} = im_pixel(image, x, y)</langsyntaxhighlight>
An example of this in use can be found in demo/pGUI/simple_paint.exw
 
Line 917 ⟶ 974:
{{works with|Windows}}
{{libheader|GD}}
<langsyntaxhighlight lang="php">$img = imagegrabscreen();
$color = imagecolorat($im, 10, 50);
imagedestroy($im);</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
Using '[http://www.muquit.com/muquit/software/grabc/grabc.html grabc]'
as recommended in the C solution
<langsyntaxhighlight PicoLisplang="picolisp">(in '(grabc)
(mapcar hex (cdr (line NIL 1 2 2 2))) )</langsyntaxhighlight>
Output:
<pre>73,61,205
Line 933 ⟶ 990:
Access any pixel value on the sketch canvas. A color in Processing is a 32-bit int, organized in four components, alpha red green blue, as AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB. Each component is 8 bits (a number between 0 and 255), and can be accessed with alpha(), red(), green(), blue().
 
<langsyntaxhighlight lang="processing">void draw(){
color c = get(mouseX,mouseY);
println(c, red(c), green(c), blue(c));
}</langsyntaxhighlight>
 
For greater speed, pixels may be looked up by index in the pixels[] array, and color components may be retrieved by bit-shifting.
 
<langsyntaxhighlight lang="processing">void draw(){
loadPixels();
color c = pixels[mouseY * width + mouseX];
println(c, c >> 16 & 0xFF, c >> 8 & 0xFF, c >> 8 & 0xFF);
}</langsyntaxhighlight>
 
=={{header|Python}}==
{{libheader|PyWin32}}
{{works_with|Windows}}
<langsyntaxhighlight lang="python">def get_pixel_colour(i_x, i_y):
import win32gui
i_desktop_window_id = win32gui.GetDesktopWindow()
Line 958 ⟶ 1,015:
return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
 
print (get_pixel_colour(0, 0))</langsyntaxhighlight>
 
{{libheader|PIL}}
 
{{works_with|Windows}}
<langsyntaxhighlight lang="python">def get_pixel_colour(i_x, i_y):
import PIL.ImageGrab
return PIL.ImageGrab.grab().load()[i_x, i_y]
 
print (get_pixel_colour(0, 0))</langsyntaxhighlight>
{{libheader|PIL}}
{{libheader|python-xlib}}
<langsyntaxhighlight lang="python">def get_pixel_colour(i_x, i_y):
import PIL.Image # python-imaging
import PIL.ImageStat # python-imaging
Line 980 ⟶ 1,037:
return tuple(map(int, lf_colour))
 
print (get_pixel_colour(0, 0))</langsyntaxhighlight>
{{libheader|PyGTK}}
<langsyntaxhighlight lang="python">def get_pixel_colour(i_x, i_y):
import gtk # python-gtk2
o_gdk_pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
Line 988 ⟶ 1,045:
return tuple(o_gdk_pixbuf.get_pixels_array().tolist()[0][0])
 
print (get_pixel_colour(0, 0))</langsyntaxhighlight>
{{libheader|PyQt}}
<langsyntaxhighlight lang="python">def get_pixel_colour(i_x, i_y):
import PyQt4.QtGui # python-qt4
app = PyQt4.QtGui.QApplication([])
Line 998 ⟶ 1,055:
return ((i_colour >> 16) & 0xff), ((i_colour >> 8) & 0xff), (i_colour & 0xff)
 
print (get_pixel_colour(0, 0))</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 1,008 ⟶ 1,065:
 
This example works with MacOS, customize with the appropriate <tt>screencapture</tt> utility for other OSes.
<syntaxhighlight lang="raku" perl6line>use GD::Raw;
 
my $file = '/tmp/one-pixel-screen-capture.png';
Line 1,025 ⟶ 1,082:
 
fclose($fh);
unlink $file;</langsyntaxhighlight>
{{out}}
<pre>RGB: 20, 2, 124</pre>
Line 1,031 ⟶ 1,088:
Alternately, a version that should work in any X11 environment. Needs X11::xdo and MagickWand installed.
 
<syntaxhighlight lang="raku" perl6line>signal(SIGINT).tap: { sleep .1; cleanup(); print "\n" xx 50, "\e[H\e[J"; exit(0) }
 
multi MAIN () {
Line 1,083 ⟶ 1,140:
}
 
sub cleanup { print "\e[0m\e[?25h" }</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 1,093 ⟶ 1,150:
 
The REXX program converts the hexadecimal attribute of the character at the location of the cursor to a familiar name of a color.
<langsyntaxhighlight lang="rexx">/*REXX program obtains the cursor position (within it's window) and displays it's color.*/
parse value cursor() with r c . /*get cursor's location in DOS screen. */
 
Line 1,113 ⟶ 1,170:
if hue=='0E'x then color= 'yellow' /*or bright yellow. */
if hue=='0F'x then color= 'white' /*or bright, brite white. */
say 'screen location ('r","c') color is:' color /*display color of char at row, column.*/</langsyntaxhighlight>
{{out|output}}
<pre>
Line 1,120 ⟶ 1,177:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Color of a screen pixel
 
Line 1,155 ⟶ 1,212:
b = bytes2float(b)
return [r,g,b]
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,163 ⟶ 1,220:
</pre>
 
=={{header|RPL}}==
RPL was designed for black-and-white LCD screens. The <code>PIX?</code> instruction returns 1 if the designated pixel is black - actually dark gray or blue, depending on the model - and 0 if it's not.
(10,10) PIX?
<code>PIX?</code> was introduced in 1990 with the HP-48. On previous machines (HP-28C/S), the only way to test a pixel was to convert the status of the 131x32 LCD matrix into a 548-character string using the <code>LCD→</code> command, and then test the appropriate bit of the appropriate character.
=={{header|Ruby}}==
 
This example requires ImageMagick >= 6.2.10 (works on X11, unsure about other platforms).
 
<langsyntaxhighlight lang="ruby">module Screen
IMPORT_COMMAND = '/usr/bin/import'
 
Line 1,178 ⟶ 1,238:
end
end
end</langsyntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
<langsyntaxhighlight Scalalang="scala">def getColorAt(x: Int, y: Int): Color = new Robot().getPixelColor(x, y)</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|Smalltalk/X}}
<langsyntaxhighlight lang="smalltalk">Display rootView colorAt:(10@10).
Display rootView colorAt:(Display pointerPosition)</langsyntaxhighlight>
 
=={{header|Standard ML}}==
Works with PolyML
<langsyntaxhighlight Standardlang="standard MLml">open XWindows ;
val disp = XOpenDisplay "" ;
 
Line 1,201 ⟶ 1,261:
end;
 
XGetPixel disp im (XPoint {x=0,y=0}) ;</langsyntaxhighlight>
result
val it = 6371827: int
Line 1,207 ⟶ 1,267:
{{libheader|Tk}}
Works only on X11 or OSX with Xquartz.
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require Tk
 
Line 1,228 ⟶ 1,288:
# Demo...
puts [format "pixel at mouse: (%d,%d,%d)" \
{*}[getPixelAtPoint {*}[winfo pointerxy .]]]</langsyntaxhighlight>
 
=={{header|Wren}}==
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "dome" for Window, Process
import "graphics" for Canvas, Color
import "input" for Mouse
Line 1,257 ⟶ 1,317:
 
static getRGB(col) { "{%(col.r), %(col.g), %(col.b)}" }
}</langsyntaxhighlight>
 
{{out}}
Line 1,271 ⟶ 1,331:
graphics.
 
<langsyntaxhighlight XPL0lang="xpl0">code ReadPix=44;
int Color, X, Y;
Color:= ReadPix(X, Y);</langsyntaxhighlight>
 
{{omit from|ACL2}}
Line 1,280 ⟶ 1,340:
{{omit from|Blast}}
{{omit from|Brainf***}}
{{omit from|EasyLang}}
{{omit from|GUISS}}
{{omit from|Lilypond}}
9,476

edits