Color of a screen pixel: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: Remove bogus second header, Fix typo in first version)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 366:
 
return color;
}</lang>
 
=={{header|C++/CLI}}==
<lang cpp>using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
 
[STAThreadAttribute]
int main()
{
Point^ MousePoint = gcnew Point();
Control^ TempControl = gcnew Control();
MousePoint = TempControl->MousePosition;
Bitmap^ TempBitmap = gcnew Bitmap(1,1);
Graphics^ g = Graphics::FromImage(TempBitmap);
g->CopyFromScreen((Point)MousePoint, Point(0, 0), Size(1, 1));
Color color = TempBitmap->GetPixel(0,0);
Console::WriteLine("R: "+color.R.ToString());
Console::WriteLine("G: "+color.G.ToString());
Console::WriteLine("B: "+color.B.ToString());
}</lang>
 
Line 414 ⟶ 394:
Sample output:
<lang>Color [A=255, R=243, G=242, B=231]</lang>
 
=={{header|C++/CLI}}==
<lang cpp>using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
 
[STAThreadAttribute]
int main()
{
Point^ MousePoint = gcnew Point();
Control^ TempControl = gcnew Control();
MousePoint = TempControl->MousePosition;
Bitmap^ TempBitmap = gcnew Bitmap(1,1);
Graphics^ g = Graphics::FromImage(TempBitmap);
g->CopyFromScreen((Point)MousePoint, Point(0, 0), Size(1, 1));
Color color = TempBitmap->GetPixel(0,0);
Console::WriteLine("R: "+color.R.ToString());
Console::WriteLine("G: "+color.G.ToString());
Console::WriteLine("B: "+color.B.ToString());
}</lang>
 
=={{header|Clojure}}==
Line 442:
Sample output: (values are in RGBA order):
<lang lisp>(#xe0 #x43 #x43 #xff)</lang>
 
=={{header|Delphi}}==
 
Line 647 ⟶ 648:
return Robot().getPixelColor(x, y)
}</lang>
 
 
=={{header|Lingo}}==
Line 663:
SHOW PIXEL
[255 255 255]
 
=={{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$.
Line 713 ⟶ 714:
{{out}}
<pre>RGB: 20, 2, 124</pre>
 
=={{header|Perl 6}}==
This example works with MacOS, customize with the appropriate <tt>screencapture</tt> utility for other OSes.
<lang perl6>use GD::Raw;
 
my $file = '/tmp/one-pixel-screen-capture.png';
 
qqx/screencapture -R 123,456,1,1 $file/;
 
my $fh = fopen($file, "rb") or die;
my $image = gdImageCreateFromPng($fh);
my $pixel = gdImageGetPixel($image, 0, 0);
my ($red,$green,$blue) =
gdImageRed( $image, $pixel),
gdImageGreen($image, $pixel),
gdImageBlue( $image, $pixel);
 
say "RGB: $red, $green, $blue";
 
fclose($fh);
unlink $file;</lang>
{{out}}
<pre>RGB: 20, 2, 124</pre>
 
Alternately, a version that should work in any X11 environment. Needs X11::xdo and MagickWand installed.
 
<lang perl6>#!/usr/bin/env perl6
 
signal(SIGINT).tap: { sleep .1; cleanup(); print "\n" xx 50, "\e[H\e[J"; exit(0) }
 
multi MAIN () {
use X11::libxdo;
my $xdo = Xdo.new;
my ($lx, $ly) = 0, 0;
loop {
sleep .1;
my ($x, $y, $) = $xdo.get-mouse-location;
next if $lx == $x and $ly == $y;
($lx, $ly) = $x, $y;
display $x, $y, |get-pixel($x, $y);
}
}
 
my %*SUB-MAIN-OPTS = :named-anywhere;
 
multi MAIN (
Int $x, #= Integer x coordinate to pick
Int $y, #= Integer y coordinate to pick
$q = False #= Boolean "quiet" mode, set truthy for decimal values, set to h for hex values
) {
my ($red, $green, $blue) = get-pixel($x, $y);
 
if $q {
$q.lc eq 'h' ??
( printf "%02X:%02X:%02X\n", $red, $green, $blue ) !!
( printf "%03d:%03d:%03d\n", $red, $green, $blue );
} else {
display($x, $y, $red, $green, $blue);
cleanup();
}
exit(0);
}
 
sub get-pixel ($x, $y) {
my $xcolor =
qqx/import -window root -crop 1x1+{$x-1 max 0}+{$y-2 max 0} -depth 8 txt:-/
.comb(/<?after '#'><xdigit> ** 6/);
 
|$xcolor.comb(2)».parse-base(16);
}
 
sub display ($x, $y, $r, $g, $b) {
print "\e[?25l\e[48;2;0;0;0m\e[38;2;255;255;255m\e[H\e[J";
printf " x: %4d y: $y\n", $x;
printf " RGB: %03d:%03d:%03d \n HEX: %02X:%02X:%02X\n",
$r, $g, $b, $r, $g, $b;
print "\e[38;2;{$r};{$g};{$b}m ",
('█' x 18 xx 6).join("\n "), "\n\n";
}
 
sub cleanup { print "\e[0m\e[?25h" }</lang>
 
=={{header|Phix}}==
Line 888 ⟶ 808:
 
See [https://github.com/zdhickman/rosetta-code/blob/master/get-pixel-color.rkt get-pixel-color.rkt].
 
=={{header|Raku}}==
(formerly Perl 6)
This example works with MacOS, customize with the appropriate <tt>screencapture</tt> utility for other OSes.
<lang perl6>use GD::Raw;
 
my $file = '/tmp/one-pixel-screen-capture.png';
 
qqx/screencapture -R 123,456,1,1 $file/;
 
my $fh = fopen($file, "rb") or die;
my $image = gdImageCreateFromPng($fh);
my $pixel = gdImageGetPixel($image, 0, 0);
my ($red,$green,$blue) =
gdImageRed( $image, $pixel),
gdImageGreen($image, $pixel),
gdImageBlue( $image, $pixel);
 
say "RGB: $red, $green, $blue";
 
fclose($fh);
unlink $file;</lang>
{{out}}
<pre>RGB: 20, 2, 124</pre>
 
Alternately, a version that should work in any X11 environment. Needs X11::xdo and MagickWand installed.
 
<lang perl6>#!/usr/bin/env perl6
 
signal(SIGINT).tap: { sleep .1; cleanup(); print "\n" xx 50, "\e[H\e[J"; exit(0) }
 
multi MAIN () {
use X11::libxdo;
my $xdo = Xdo.new;
my ($lx, $ly) = 0, 0;
loop {
sleep .1;
my ($x, $y, $) = $xdo.get-mouse-location;
next if $lx == $x and $ly == $y;
($lx, $ly) = $x, $y;
display $x, $y, |get-pixel($x, $y);
}
}
 
my %*SUB-MAIN-OPTS = :named-anywhere;
 
multi MAIN (
Int $x, #= Integer x coordinate to pick
Int $y, #= Integer y coordinate to pick
$q = False #= Boolean "quiet" mode, set truthy for decimal values, set to h for hex values
) {
my ($red, $green, $blue) = get-pixel($x, $y);
 
if $q {
$q.lc eq 'h' ??
( printf "%02X:%02X:%02X\n", $red, $green, $blue ) !!
( printf "%03d:%03d:%03d\n", $red, $green, $blue );
} else {
display($x, $y, $red, $green, $blue);
cleanup();
}
exit(0);
}
 
sub get-pixel ($x, $y) {
my $xcolor =
qqx/import -window root -crop 1x1+{$x-1 max 0}+{$y-2 max 0} -depth 8 txt:-/
.comb(/<?after '#'><xdigit> ** 6/);
 
|$xcolor.comb(2)».parse-base(16);
}
 
sub display ($x, $y, $r, $g, $b) {
print "\e[?25l\e[48;2;0;0;0m\e[38;2;255;255;255m\e[H\e[J";
printf " x: %4d y: $y\n", $x;
printf " RGB: %03d:%03d:%03d \n HEX: %02X:%02X:%02X\n",
$r, $g, $b, $r, $g, $b;
print "\e[38;2;{$r};{$g};{$b}m ",
('█' x 18 xx 6).join("\n "), "\n\n";
}
 
sub cleanup { print "\e[0m\e[?25h" }</lang>
 
=={{header|REXX}}==
Line 922 ⟶ 924:
screen location (33,1) color is: yellow
</pre>
 
 
 
=={{header|Ring}}==
Line 1,015:
puts [format "pixel at mouse: (%d,%d,%d)" \
{*}[getPixelAtPoint {*}[winfo pointerxy .]]]</lang>
 
 
 
=={{header|XPL0}}==