Color of a screen pixel: Difference between revisions

Content added Content deleted
(Add Processing)
(Regrouped BASIC examples.)
Line 15: Line 15:
[https://lh4.googleusercontent.com/-Gw0xm7RIEck/Uute2nQSGsI/AAAAAAAAJ90/rq3UuWYE9Yw/s1600/Capture.PNG <VIEW THE BLOCKS AND ANDROID APP DISPLAY>]
[https://lh4.googleusercontent.com/-Gw0xm7RIEck/Uute2nQSGsI/AAAAAAAAJ90/rq3UuWYE9Yw/s1600/Capture.PNG <VIEW THE BLOCKS AND ANDROID APP DISPLAY>]


=={{header|Applesoft BASIC}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>PixelGetColor, color, %X%, %Y%</lang>

=={{header|AutoIt}}==
<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)</lang>
{{Out}}
<pre>
Color at x=3,y=733 ==> 3829413 = 0x003A6EA5
</pre>

=={{header|Axe}}==
<lang axe>Disp pxl-Test(50,50)▶Dec,i</lang>

=={{header|BaCon}}==
BaCon can make use of the High Performance Canvas include. Outside this canvas it needs to access XLib API functions.
<lang qbasic>INCLUDE canvas
FULLSCREEN
color = GETINK(100, 100, 4)
WAITKEY</lang>

=={{header|BASIC}}==

==={{header|Applesoft BASIC}}===
[http://en.wikipedia.org/wiki/Apple_II_graphics#Low-Resolution_.28Lo-Res.29_graphics Low-Resolution (Lo-Res) graphics] 40x48, 16 colors, page 1
[http://en.wikipedia.org/wiki/Apple_II_graphics#Low-Resolution_.28Lo-Res.29_graphics Low-Resolution (Lo-Res) graphics] 40x48, 16 colors, page 1

<lang Applesoft BASIC>X = PDL (0) * 5 / 32
<lang Applesoft BASIC>X = PDL (0) * 5 / 32
Y = PDL (1) * 3 / 16
Y = PDL (1) * 3 / 16
Line 68: Line 95:
</lang>
</lang>


=={{header|AutoHotkey}}==
==={{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).
<lang AutoHotkey>PixelGetColor, color, %X%, %Y%</lang>
<lang bbcbasic> palette_index% = POINT(x%, y%)
RGB24b_colour% = TINT(x%, y%)</lang>


=={{header|AutoIt}}==
==={{header|FreeBASIC}}===
This is a very simple example from the FreeBASIC documentation. To obtain the color of an arbitrary screen pixel (i.e. outside
<lang autoit>Opt('MouseCoordMode',1) ; 1 = (default) absolute screen coordinates
the graphics screen controlled by FB) one would need to use API functions.
$pos = MouseGetPos()
<lang freebasic>FB 1.05.0 Win64
$c = PixelGetColor($pos[0], $pos[1])
ConsoleWrite("Color at x=" & $pos[0] & ",y=" & $pos[1] & _
" ==> " & $c & " = 0x" & Hex($c) & @CRLF)</lang>
{{Out}}
<pre>
Color at x=3,y=733 ==> 3829413 = 0x003A6EA5
</pre>


' Set an appropriate screen mode - 320 x 240 x 8bpp indexed color
=={{header|Axe}}==
ScreenRes 320, 240, 8
<lang axe>Disp pxl-Test(50,50)▶Dec,i</lang>


' Draw a line using color 12 (light red)
=={{header|BaCon}}==
Line (20,20)-(100,100), 12
BaCon can make use of the High Performance Canvas include. Outside this canvas it needs to access XLib API functions.
<lang qbasic>INCLUDE canvas
FULLSCREEN
color = GETINK(100, 100, 4)
WAITKEY</lang>


' Print the color of a point on the line
=={{header|BASIC}}==
Print Point(20,20) '' prints 12

' Sleep before the program closes
Sleep</lang>

==={{header|QuickBASIC}}===
{{works with|QuickBasic|4.5}}
{{works with|QuickBasic|4.5}}


Line 98: Line 123:
<lang qbasic>color = point(x, y)</lang>
<lang qbasic>color = point(x, y)</lang>


=={{header|BBC BASIC}}==
==={{header|Integer BASIC}}===

In [[BBC BASIC for Windows]] you can read either the 'logical colour' (palette index) or the 'true colour' (24-bit RGB value).
See [[#Applesoft BASIC|Applesoft BASIC]].
<lang bbcbasic> palette_index% = POINT(x%, y%)

RGB24b_colour% = TINT(x%, y%)</lang>
==={{header|Liberty BASIC}}===
<lang lb>'This example requires the Windows API
Struct point, x As long, y As long

hDC = GetDC(0)
result = GetCursorPos()
Print GetPixel(hDC, point.x.struct, point.y.struct)
Call ReleaseDC 0, hDC
End


Sub ReleaseDC hWnd, hDC
CallDLL #user32,"ReleaseDC", hWnd As uLong, hDC As uLong, ret As Long
End Sub

Function GetDC(hWnd)
CallDLL #user32, "GetDC", hWnd As uLong, GetDC As uLong
End Function

Function GetCursorPos()
CallDLL #user32, "GetCursorPos", point As struct, GetCursorPos As uLong
End Function

Function GetPixel(hDC, x, y)
CallDLL #gdi32, "GetPixel", hDC As uLong, x As long, y As long, GetPixel As long
End Function</lang>

==={{header|Locomotive Basic}}===

<lang locobasic>10 x=320:y=200
20 color=TEST(x,y)
30 PRINT "Pen color at"; x; y; "is"; color</lang>

==={{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.

<lang PureBasic>Color = Point(x, y)</lang>

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.

<lang PureBasic>
hDC = GetDC_(0)
Color = GetPixel_(hDC, x, y)
ReleaseDC_(0, hDC)</lang>

This work fine!!

<lang 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: ")
TextGadget(1,0,15,50,12,"Green: ")
TextGadget(2,0,30,50,12,"Blue: ")
TextGadget(3,50,0,50,12,"")
TextGadget(4,50,15,50,12,"")
TextGadget(5,50,30,50,12,"")
hDC = GetDC_(0)
Repeat
oldx=poz\x
oldy=poz\y
GetCursorPos_(@poz)
Color = GetPixel_(hDC, poz\x, poz\y)
If poz\x<>oldx Or poz\y<>oldy
SetGadgetText(3,Str(Red(color)))
SetGadgetText(4,Str(Green(color)))
SetGadgetText(5,Str(Blue(color)))
EndIf
event=WaitWindowEvent(200)
Until event=#PB_Event_CloseWindow
ReleaseDC_(0, hDC)
EndIf</lang>

==={{header|SmileBASIC}}===
<lang smilebasic>DEF GETPX X,Y OUT R,G,B
PCOL=GSPOIT(X,Y)
RGBREAD PCOL OUT R,G,B
END</lang>

==={{header|TI-89 BASIC}}===
Only the graph screen can be read.

<lang ti89b>pxlTest(y, x) © returns boolean</lang>

==={{header|Visual Basic .NET}}===
<lang vbnet> Private Function GetPixelColor(ByVal Location As Point) As Color

Dim b As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(b)

g.CopyFromScreen(Location, Point.Empty, New Size(1, 1))

Return b.GetPixel(0, 0)

End Function</lang>

==={{header|VBA}}===
In "pure" Visual Basic for Application, there is no way to find the color of a screen pixel.
We have to use api's functions.
This code should be adapted for 64 bits versions...

<lang vb>
Option Explicit

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "USER32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As Long

Sub Color_of_a_screen_pixel()
Dim myColor As Long
myColor = Get_Color_Under_Cursor
End Sub

Function Get_Color_Under_Cursor() As Long
Dim Pos As POINTAPI, lngDc As Long

lngDc = GetWindowDC(0)
GetCursorPos Pos
Get_Color_Under_Cursor = GetPixel(lngDc, Pos.x, Pos.y)
End Function
</lang>

==={{header|Yabasic}}===
<lang Yabasic>open window 100, 100
backcolor 255, 0, 0
clear window
color 0, 255, 0
fill rectangle 50, 50, 75, 75

x = 60 : y = 60

s$ = right$(getbit$(x, y, x, y), 6)
blue = dec(right$(s$, 2))
green = dec(mid$(s$, 3, 2))
red = dec(left$(s$, 2))

print red, " ", green, " ", blue</lang>

==={{header|ZX Spectrum Basic}}===
The built-in function <code>POINT (x,y)</code> returns 0 if the pixel at <tt>x,y</tt> is set to the relevant <code>PAPER</code> colour, or 1 if it is set to the <code>INK</code> colour. (Note that there can only be a maximum of two colours in each 8x8-pixel section of the screen.)



=={{header|C}}==
=={{header|C}}==
Line 321: Line 490:
Colour at cursor position X:2756 Y:59 = R(250) G(196) B(182)
Colour at cursor position X:2756 Y:59 = R(250) G(196) B(182)
</pre>
</pre>

=={{header|FreeBASIC}}==
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.
<lang freebasic>FB 1.05.0 Win64

' Set an appropriate screen mode - 320 x 240 x 8bpp indexed color
ScreenRes 320, 240, 8

' Draw a line using color 12 (light red)
Line (20,20)-(100,100), 12

' Print the color of a point on the line
Print Point(20,20) '' prints 12

' Sleep before the program closes
Sleep</lang>


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 413: Line 565:
x=658,y=610 pixel=47802,0,65535
x=658,y=610 pixel=47802,0,65535
x=934,y=487 pixel=0,0,0</pre>
x=934,y=487 pixel=0,0,0</pre>

=={{header|Integer BASIC}}==

See [[#Applesoft BASIC|Applesoft BASIC]].


=={{header|Java}}==
=={{header|Java}}==
Line 453: Line 601:
}</lang>
}</lang>


=={{header|Liberty BASIC}}==
<lang lb>'This example requires the Windows API
Struct point, x As long, y As long


=={{header|Lingo}}==
hDC = GetDC(0)
result = GetCursorPos()
Print GetPixel(hDC, point.x.struct, point.y.struct)
Call ReleaseDC 0, hDC
End



Sub ReleaseDC hWnd, hDC
CallDLL #user32,"ReleaseDC", hWnd As uLong, hDC As uLong, ret As Long
End Sub

Function GetDC(hWnd)
CallDLL #user32, "GetDC", hWnd As uLong, GetDC As uLong
End Function

Function GetCursorPos()
CallDLL #user32, "GetCursorPos", point As struct, GetCursorPos As uLong
End Function

Function GetPixel(hDC, x, y)
CallDLL #gdi32, "GetPixel", hDC As uLong, x As long, y As long, GetPixel As long
End Function</lang>

=={{header|Lingo}}==
{{libheader|ScrnXtra3 Xtra}}
{{libheader|ScrnXtra3 Xtra}}
<lang lingo>on getScreenPixelColor (x, y)
<lang lingo>on getScreenPixelColor (x, y)
Line 487: Line 610:
return img.getPixel(0, 0)
return img.getPixel(0, 0)
end</lang>
end</lang>

=={{header|Locomotive Basic}}==

<lang locobasic>10 x=320:y=200
20 color=TEST(x,y)
30 PRINT "Pen color at"; x; y; "is"; color</lang>


=={{header|Logo}}==
=={{header|Logo}}==
Line 568: Line 685:
println(c, c >> 16 & 0xFF, c >> 8 & 0xFF, c >> 8 & 0xFF);
println(c, c >> 16 & 0xFF, c >> 8 & 0xFF, c >> 8 & 0xFF);
}</lang>
}</lang>

=={{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.

<lang PureBasic>Color = Point(x, y)</lang>

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.
<lang PureBasic>
hDC = GetDC_(0)
Color = GetPixel_(hDC, x, y)
ReleaseDC_(0, hDC)</lang>

This work fine!!

<lang 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: ")
TextGadget(1,0,15,50,12,"Green: ")
TextGadget(2,0,30,50,12,"Blue: ")
TextGadget(3,50,0,50,12,"")
TextGadget(4,50,15,50,12,"")
TextGadget(5,50,30,50,12,"")
hDC = GetDC_(0)
Repeat
oldx=poz\x
oldy=poz\y
GetCursorPos_(@poz)
Color = GetPixel_(hDC, poz\x, poz\y)
If poz\x<>oldx Or poz\y<>oldy
SetGadgetText(3,Str(Red(color)))
SetGadgetText(4,Str(Green(color)))
SetGadgetText(5,Str(Blue(color)))
EndIf
event=WaitWindowEvent(200)
Until event=#PB_Event_CloseWindow
ReleaseDC_(0, hDC)
EndIf</lang>


=={{header|Python}}==
=={{header|Python}}==
Line 762: Line 842:
{{libheader|Scala}}
{{libheader|Scala}}
<lang Scala>def getColorAt(x: Int, y: Int): Color = new Robot().getPixelColor(x, y)</lang>
<lang Scala>def getColorAt(x: Int, y: Int): Color = new Robot().getPixelColor(x, y)</lang>

=={{header|SmileBASIC}}==
<lang smilebasic>DEF GETPX X,Y OUT R,G,B
PCOL=GSPOIT(X,Y)
RGBREAD PCOL OUT R,G,B
END</lang>


=={{header|Tcl}}==
=={{header|Tcl}}==
Line 795: Line 869:
{*}[getPixelAtPoint {*}[winfo pointerxy .]]]</lang>
{*}[getPixelAtPoint {*}[winfo pointerxy .]]]</lang>


=={{header|TI-89 BASIC}}==
Only the graph screen can be read.


<lang ti89b>pxlTest(y, x) © returns boolean</lang>

=={{header|Visual Basic .NET}}==
<lang vbnet> Private Function GetPixelColor(ByVal Location As Point) As Color

Dim b As New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(b)

g.CopyFromScreen(Location, Point.Empty, New Size(1, 1))

Return b.GetPixel(0, 0)

End Function</lang>
=={{header|VBA}}==
In "pure" Visual Basic for Application, there is no way to find the color of a screen pixel.
We have to use api's functions.
This code should be adapted for 64 bits versions...

<lang vb>
Option Explicit

Private Type POINTAPI
x As Long
y As Long
End Type

Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Private Declare Function GetCursorPos Lib "USER32" (lpPoint As POINTAPI) As Long
Private Declare Function GetWindowDC Lib "USER32" (ByVal hWnd As Long) As Long

Sub Color_of_a_screen_pixel()
Dim myColor As Long
myColor = Get_Color_Under_Cursor
End Sub

Function Get_Color_Under_Cursor() As Long
Dim Pos As POINTAPI, lngDc As Long

lngDc = GetWindowDC(0)
GetCursorPos Pos
Get_Color_Under_Cursor = GetPixel(lngDc, Pos.x, Pos.y)
End Function
</lang>


=={{header|XPL0}}==
=={{header|XPL0}}==
Line 850: Line 879:
int Color, X, Y;
int Color, X, Y;
Color:= ReadPix(X, Y);</lang>
Color:= ReadPix(X, Y);</lang>

=={{header|Yabasic}}==
<lang Yabasic>open window 100, 100
backcolor 255, 0, 0
clear window
color 0, 255, 0
fill rectangle 50, 50, 75, 75

x = 60 : y = 60

s$ = right$(getbit$(x, y, x, y), 6)
blue = dec(right$(s$, 2))
green = dec(mid$(s$, 3, 2))
red = dec(left$(s$, 2))

print red, " ", green, " ", blue</lang>

=={{header|ZX Spectrum Basic}}==
The built-in function <code>POINT (x,y)</code> returns 0 if the pixel at <tt>x,y</tt> is set to the relevant <code>PAPER</code> colour, or 1 if it is set to the <code>INK</code> colour. (Note that there can only be a maximum of two colours in each 8x8-pixel section of the screen.)


{{omit from|ACL2}}
{{omit from|ACL2}}