Color of a Pixel
up vote
5
down vote
favorite
Write a program that prints the RGB color of a given pixel on the screen.
The program should read a line from stdin in the format x,y
, where x and y are unsigned integers. The pixel (0,0) is the top left pixel in the screen.
The program should then output a line to stdout in the format RRR,GGG,BBB
, which represents the color of the pixel at (x
,y
).
RRR
, GGG
, and BBB
should be floating-point numbers between 0 and 1.
Undefined Behavior
It is okay for the program to exhibid undefined behavior (crash, give incorrect output, etc) in the following conditions:
- The user inputs a string, signed integer, float, or any other invalid data type
- The user input is not in the format
x,y
- The specified pixel is off-screen
The answer with the fewest bytes wins.
code-golf color
|
show 2 more comments
up vote
5
down vote
favorite
Write a program that prints the RGB color of a given pixel on the screen.
The program should read a line from stdin in the format x,y
, where x and y are unsigned integers. The pixel (0,0) is the top left pixel in the screen.
The program should then output a line to stdout in the format RRR,GGG,BBB
, which represents the color of the pixel at (x
,y
).
RRR
, GGG
, and BBB
should be floating-point numbers between 0 and 1.
Undefined Behavior
It is okay for the program to exhibid undefined behavior (crash, give incorrect output, etc) in the following conditions:
- The user inputs a string, signed integer, float, or any other invalid data type
- The user input is not in the format
x,y
- The specified pixel is off-screen
The answer with the fewest bytes wins.
code-golf color
17
Why shouldRRR
,GGG
, andBBB
be floating points between 0 and 1? Usually they're integers in the range[0,255]
. I would suggest to allow both.
– Kevin Cruijssen
yesterday
3
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
6
If you allow only the 0 — 1 format you should not useRRR
,GGG
, andBBB
in the spec, butR
,G
,B
– sergiol
yesterday
4
What if we have multiple monitors?
– tsh
yesterday
3
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return0, 0, 0
, defeating the purpose of the challenge.
– maxb
16 hours ago
|
show 2 more comments
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Write a program that prints the RGB color of a given pixel on the screen.
The program should read a line from stdin in the format x,y
, where x and y are unsigned integers. The pixel (0,0) is the top left pixel in the screen.
The program should then output a line to stdout in the format RRR,GGG,BBB
, which represents the color of the pixel at (x
,y
).
RRR
, GGG
, and BBB
should be floating-point numbers between 0 and 1.
Undefined Behavior
It is okay for the program to exhibid undefined behavior (crash, give incorrect output, etc) in the following conditions:
- The user inputs a string, signed integer, float, or any other invalid data type
- The user input is not in the format
x,y
- The specified pixel is off-screen
The answer with the fewest bytes wins.
code-golf color
Write a program that prints the RGB color of a given pixel on the screen.
The program should read a line from stdin in the format x,y
, where x and y are unsigned integers. The pixel (0,0) is the top left pixel in the screen.
The program should then output a line to stdout in the format RRR,GGG,BBB
, which represents the color of the pixel at (x
,y
).
RRR
, GGG
, and BBB
should be floating-point numbers between 0 and 1.
Undefined Behavior
It is okay for the program to exhibid undefined behavior (crash, give incorrect output, etc) in the following conditions:
- The user inputs a string, signed integer, float, or any other invalid data type
- The user input is not in the format
x,y
- The specified pixel is off-screen
The answer with the fewest bytes wins.
code-golf color
code-golf color
asked yesterday
Cee
434
434
17
Why shouldRRR
,GGG
, andBBB
be floating points between 0 and 1? Usually they're integers in the range[0,255]
. I would suggest to allow both.
– Kevin Cruijssen
yesterday
3
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
6
If you allow only the 0 — 1 format you should not useRRR
,GGG
, andBBB
in the spec, butR
,G
,B
– sergiol
yesterday
4
What if we have multiple monitors?
– tsh
yesterday
3
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return0, 0, 0
, defeating the purpose of the challenge.
– maxb
16 hours ago
|
show 2 more comments
17
Why shouldRRR
,GGG
, andBBB
be floating points between 0 and 1? Usually they're integers in the range[0,255]
. I would suggest to allow both.
– Kevin Cruijssen
yesterday
3
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
6
If you allow only the 0 — 1 format you should not useRRR
,GGG
, andBBB
in the spec, butR
,G
,B
– sergiol
yesterday
4
What if we have multiple monitors?
– tsh
yesterday
3
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return0, 0, 0
, defeating the purpose of the challenge.
– maxb
16 hours ago
17
17
Why should
RRR
, GGG
, and BBB
be floating points between 0 and 1? Usually they're integers in the range [0,255]
. I would suggest to allow both.– Kevin Cruijssen
yesterday
Why should
RRR
, GGG
, and BBB
be floating points between 0 and 1? Usually they're integers in the range [0,255]
. I would suggest to allow both.– Kevin Cruijssen
yesterday
3
3
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
6
6
If you allow only the 0 — 1 format you should not use
RRR
, GGG
, and BBB
in the spec, but R
, G
, B
– sergiol
yesterday
If you allow only the 0 — 1 format you should not use
RRR
, GGG
, and BBB
in the spec, but R
, G
, B
– sergiol
yesterday
4
4
What if we have multiple monitors?
– tsh
yesterday
What if we have multiple monitors?
– tsh
yesterday
3
3
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return
0, 0, 0
, defeating the purpose of the challenge.– maxb
16 hours ago
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return
0, 0, 0
, defeating the purpose of the challenge.– maxb
16 hours ago
|
show 2 more comments
9 Answers
9
active
oldest
votes
up vote
7
down vote
Java 10 (lambda function), 105 75 bytes
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
A function taking two integer parameters and returning a String RRR,GGG,BBB
, where the colors are integers in the range [0, 255]
.
-30 bytes thanks to @LukeStevens by using java.awt.Color
's default toString()
output and modifying it so only the digits and commas remain.
Explanation:
x->y-> // Method with two integer parameters and String return-type
(new java.awt.Robot() // Create a AWT-Robot instance
.getPixelColor(x,y) // Get the pixel AWT-Color at position x,y
+"") // Call the default toString() of the AWT-Color
// i.e. "java.awt.Color[r=213,g=230,b=245]"
.replaceAll("[^\d,]","") // Remove everything except for digits and commas
NOTE: The default toString()
implementation of the java.awt.Color
has always been the same across JVM versions as far as I know, but can potentially change in the future. I tested it in both Java 8 and Java 10 and it returned "java.awt.Color[r=#,g=#,b=#]"
.
But, since the challenge states:
- Full program
- Taking input in the format
x,y
from STDIN - Output in the format
R,G,B
to STDOUT - Have the
R
,G
,B
as floating points in the range[0.0, 1.0]
The code becomes A LOT more verbose:
Java 10 (full program), 266 bytes
interface M{static void main(Stringa)throws Exception{var s=new java.util.Scanner(System.in).next().split(",");var c=new java.awt.Robot().getPixelColor(new Short(s[0]),new Short(s[1]));System.out.print(c.getRed()/255d+","+c.getGreen()/255d+","+c.getBlue()/255d);}}
Explanation:
interface M{ // Class
static void main(Stringa)
// Mandatory main method
throws Exception{ // Mandatory throws clause for `new Robot()`
var s=new java.util.Scanner(System.in)
// Create a Scanner for STDIN
.next() // Get the String user input
.split(","); // Split it on "," and save it as String-array in `s`
var c=new java.awt.Robot()
// Create a AWT-Robot instance
.getPixelColor(
// And get the pixel AWT-Color at position:
new Short( // Convert String to Short (and implicitly to int):
s[0]), // x-coordinate input by user from String-array `s`
new Short( // Convert String to Short (and implicitly to int):
s[1])); // y-coordinate input by user from String-array `s`
System.out.print( // Print to STDOUT:
c.getRed() // The red part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getGreen() // Appended with the green part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getBlue() // Appended with the blue part of RGB as integer in the range [0,255]
/255d);}} // Converted to a floating point in the range [0.0, 1.0]
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply havex->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes
– Luke Stevens
yesterday
@LukeStevens Smart use of the defaulttoString()
ofjava.awt.Color
. Thanks!
– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing thetoString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(
– Olivier Grégoire
yesterday
2
Note that the Javadoc is very clear thatColor::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).
– Olivier Grégoire
yesterday
1
Finally, you should replace" "
with","
to comply with the strictRRR,GGG,BBB
format (notRRR GGG BBB
).
– Olivier Grégoire
yesterday
|
show 1 more comment
up vote
7
down vote
6502 machine code (C64), 280 260 bytes
00 C0 A2 00 20 CF FF C9 0D 9D 02 C1 F0 03 E8 D0 F3 20 D2 FF A5 7A 85 FB A5 7B
85 FC A9 01 85 7A A9 C1 85 7B 20 73 00 20 6B A9 A5 14 85 FD A5 15 85 FE 20 73
00 20 6B A9 A5 FB 85 7A A5 FC 85 7B A5 14 4A 4A 4A AA 20 F0 E9 A5 FD 46 FE 6A
4A 4A A8 A5 14 29 07 AA A9 00 38 6A CA 10 FC 85 FE B1 D1 0A 26 FC 0A 26 FC 0A
26 FC 85 FB A5 D2 29 03 09 D8 85 D2 B1 D1 85 02 A9 02 18 2D 18 D0 0A 0A 65 FC
29 0F 09 D0 85 FC A9 33 85 01 A5 FD 29 07 A8 B1 FB A2 37 86 01 25 FE D0 05 AD
21 D0 85 02 A6 D6 20 F0 E9 A5 02 29 0F AA BC D2 C0 20 BC C0 BC E2 C0 20 B7 C0
BC F2 C0 A9 2C 20 D2 FF 98 0A A9 30 90 02 A9 31 20 D2 FF A9 2E 20 D2 FF 98 29
7F 4C D2 FF 30 B0 35 35 36 33 32 39 36 33 38 33 35 37 34 37 30 B0 32 38 32 37
32 39 33 32 34 33 35 B0 34 37 30 B0 32 38 36 33 36 34 32 30 34 33 35 36 39 37
I expected this to be possible in a lot fewer bytes, but unfortunately... well, since I finished it, posting it now anyways. At least, the restrictive format helped with one thing: Something similar to stdin
(input from a current device) only exists on the C64 in text mode, because the OS only supports this mode -- so no need to consider other modes of the graphics chip.
Note on the output of the color values: the C64 graphics chip doesn't use RGB colors but directly generates a video signal with YUV colors, with a fixed 16 colors palette. I used rounded values of the colodore conversion to RGB with "default" monitor settings here.
-20 bytes: better output routine, encoding the 3 character output per color channel in a single byte.
Regarding the comment: It's theoretically possible to use even multicolor character mode of the VIC with the stock C64 OS, but it requires a custom font that's actually legible with only 4 horizontal pixels of doubled width. Not entirely impossible, but very unlikely. Likewise, extended color mode (or extended background mode, which is the same) could be used with the C64 OS, but requires to reconfigure the graphics chip directly. I opt to ignore all these possibilities in the sense of code golfing here: It's not the standard environment you find on a Commodore 64 with running stock OS. What's possible with stock OS is switching between two builtin fonts (shift + commodore key), the program does account for that.
Online demo
Usage: SYS49152
to start.
Commented disassembly:
00 C0 .WORD $C000 ; load address
.C:c000 A2 00 LDX #$00 ; loop index for input
.C:c002 .input:
.C:c002 20 CF FF JSR $FFCF ; character from input device
.C:c005 C9 0D CMP #$0D ; compare with enter
.C:c007 9D 16 C1 STA .buf,X ; store to buffer
.C:c00a F0 03 BEQ .parse ; was enter -> start parsing
.C:c00c E8 INX ; next character
.C:c00d D0 F3 BNE .input ; and repeat input loop
.C:c00f .parse:
.C:c00f 20 D2 FF JSR $FFD2 ; output the enter character
.C:c012 A5 7A LDA $7A ; save pointer of BASIC parser
.C:c014 85 FB STA $FB
.C:c016 A5 7B LDA $7B
.C:c018 85 FC STA $FC
.C:c01a A9 15 LDA #$15 ; set pointer of BASIC parser to
.C:c01c 85 7A STA $7A ; buffer-1
.C:c01e A9 C1 LDA #$C1
.C:c020 85 7B STA $7B
.C:c022 20 73 00 JSR $0073 ; get next character
.C:c025 20 6B A9 JSR $A96B ; BASIC routine to parse number
.C:c028 A5 14 LDA $14 ; lowbyte of parsed number to $fd
.C:c02a 85 FD STA $FD
.C:c02c A5 15 LDA $15 ; highbyte to $fe
.C:c02e 85 FE STA $FE
.C:c030 20 73 00 JSR $0073 ; get next character
.C:c033 20 6B A9 JSR $A96B ; parse as number ...
.C:c036 A5 FB LDA $FB ; restore pointer of BASIC parser
.C:c038 85 7A STA $7A
.C:c03a A5 FC LDA $FC
.C:c03c 85 7B STA $7B
.C:c03e A5 14 LDA $14 ; load y coordinate
.C:c040 4A LSR A ; divide by 8 for character row
.C:c041 4A LSR A
.C:c042 4A LSR A
.C:c043 AA TAX ; -> to X
.C:c044 20 F0 E9 JSR $E9F0 ; set pointer to character row
.C:c047 A5 FD LDA $FD ; divide x coordinate by 8
.C:c049 46 FE LSR $FE
.C:c04b 6A ROR A
.C:c04c 4A LSR A
.C:c04d 4A LSR A
.C:c04e A8 TAY ; -> to Y
.C:c04f A5 14 LDA $14 ; load y coordinate
.C:c051 29 07 AND #$07 ; mask pixel position in character
.C:c053 AA TAX ; -> to X
.C:c054 A9 00 LDA #$00 ; initialize pixel mask to 0
.C:c056 38 SEC ; set carry for bit to shift in
.C:c057 .bitnum:
.C:c057 6A ROR A ; shift bit in mask
.C:c058 CA DEX ; and repeat until
.C:c059 10 FC BPL .bitnum ; in correct position
.C:c05b 85 FE STA $FE ; store pixel mask to $fe
.C:c05d B1 D1 LDA ($D1),Y ; load character code
.C:c05f 0A ASL A ; multiply by 8
.C:c060 26 FC ROL $FC
.C:c062 0A ASL A
.C:c063 26 FC ROL $FC
.C:c065 0A ASL A
.C:c066 26 FC ROL $FC
.C:c068 85 FB STA $FB ; and store to $fb/$fc
.C:c06a A5 D2 LDA $D2 ; move pointer to position in color RAM
.C:c06c 29 03 AND #$03
.C:c06e 09 D8 ORA #$D8
.C:c070 85 D2 STA $D2
.C:c072 B1 D1 LDA ($D1),Y ; load color of character
.C:c074 85 02 STA $02 ; and store to $2
.C:c076 A9 02 LDA #$02 ; check which charset is active
.C:c078 18 CLC
.C:c079 2D 18 D0 AND $D018
.C:c07c 0A ASL A ; and calculate offset
.C:c07d 0A ASL A
.C:c07e 65 FC ADC $FC ; add to (character code * 8)
.C:c080 29 0F AND #$0F
.C:c082 09 D0 ORA #$D0 ; and add offset to character ROM
.C:c084 85 FC STA $FC
.C:c086 A9 33 LDA #$33 ; bank in character ROM
.C:c088 85 01 STA $01
.C:c08a A5 FD LDA $FD ; load y coordinate
.C:c08c 29 07 AND #$07 ; mask pixel-row number
.C:c08e A8 TAY
.C:c08f B1 FB LDA ($FB),Y ; load pixel row from character ROM
.C:c091 A2 37 LDX #$37 ; bank out character ROM
.C:c093 86 01 STX $01
.C:c095 25 FE AND $FE ; apply pixel mask
.C:c097 D0 05 BNE .pixelcol ; not 0 -> pixel is set
.C:c099 AD 21 D0 LDA $D021 ; otherwise load background color
.C:c09c 85 02 STA $02 ; and store to $2
.C:c09e .pixelcol:
.C:c09e A6 D6 LDX $D6 ; restore screen row pointer for
.C:c0a0 20 F0 E9 JSR $E9F0 ; current cursor position
.C:c0a3 A5 02 LDA $02 ; load color
.C:c0a5 29 0F AND #$0F ; mask low nibble (only 16 colors)
.C:c0a7 AA TAX ; -> to X
.C:c0a8 BC D2 C0 LDY .red,X ; load encoded output for red
.C:c0ab 20 BC C0 JSR .out2 ; call output without comma
.C:c0ae BC E2 C0 LDY .green,X ; load encoded output for green
.C:c0b1 20 B7 C0 JSR .out1 ; call output with comma
.C:c0b4 BC F2 C0 LDY .blue,X ; load encoded output for blue
.C:c0b7 .out1:
.C:c0b7 A9 2C LDA #$2C ; load ","
.C:c0b9 20 D2 FF JSR $FFD2 ; and output
.C:c0bc .out2:
.C:c0bc 98 TYA ; encoded output to A
.C:c0bd 0A ASL A ; shift top bit to carry
.C:c0be A9 30 LDA #$30 ; load "0"
.C:c0c0 90 02 BCC .firstdig ; carry clear -> to output
.C:c0c2 A9 31 LDA #$31 ; load "1"
.C:c0c4 .firstdig:
.C:c0c4 20 D2 FF JSR $FFD2 ; and output
.C:c0c7 A9 2E LDA #$2E ; load "."
.C:c0c9 20 D2 FF JSR $FFD2 ; and output
.C:c0cc 98 TYA ; encoded output to A
.C:c0cd 29 7F AND #$7F ; mask out top bit
.C:c0cf 4C D2 FF JMP $FFD2 ; to output and exit
.C:c0d2 .red: ; encoded values for red
.C:c0d2 30 B0 35 35 .BYTE $30,$B0,$35,$35 ; ASCII digit ($30-$39) after
.C:c0d6 36 33 32 39 .BYTE $36,$33,$32,$39 ; decimal point, with bit 7
.C:c0da 36 33 38 33 .BYTE $36,$33,$38,$33 ; indicating 0 or 1 before
.C:c0de 35 37 34 37 .BYTE $35,$37,$34,$37 ; decimal point
.C:c0e2 .green: ; encoded values for green
.C:c0e2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0e6 32 37 32 39 .BYTE $32,$37,$32,$39
.C:c0ea 33 32 34 33 .BYTE $33,$32,$34,$33
.C:c0ee 35 B0 34 37 .BYTE $35,$B0,$34,$37
.C:c0f2 .blue: ; encoded values for blue
.C:c0f2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0f6 36 33 36 34 .BYTE $36,$33,$36,$34
.C:c0fa 32 30 34 33 .BYTE $32,$30,$34,$33
.C:c0fe 35 36 39 37 .BYTE $35,$36,$39,$37
.C:c102 .buf: ; buffer for input ("stdin")
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that withcc65
.. quite possible :) but at least, the compiled code will be larger :)
– Felix Palmen
4 hours ago
add a comment |
up vote
4
down vote
TI-BASIC (TI-83/84+), 20 bytes
Since the screen is black and white, we just need to test if the specified pixel is on or off and map that to black or white RGB. Also, the pixels are only accessible through row and column in that order, so that's why the coordinates are reversed.
Prompt X,Y
255
Ans-{Ans,Ans,Ans}pxl-Test(Y,X
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
add a comment |
up vote
1
down vote
Mathematica, 69 Bytes
Just the function is 34 bytes.
CurrentScreenImage~PixelValue~#&
Takes input in the form {x,y}.
The image is the merging of images on all monitors. If you want a particular screen, use the integer index - e.g. CurrentScreenImage[1]
Full program exactly as specified is 69 Bytes
CurrentScreenImage~PixelValue~ToExpression["{"<>InputString<>"}"]
add a comment |
up vote
1
down vote
bash, 103 / 86 bytes
With a strict interpretation of the spec (input from STDIN and output on STDOUT are both comma-separated):
read x
import -window root -crop 1x1+${x/,/+} ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n","$3/n","$4/n}'
With a looser input format (plus-separated input as a command line argument, space-separated output:
import -window root -crop 1x1+$1 ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n,$3/n,$4/n}'
Depends on imagemagick, awk, and coreutils.
add a comment |
up vote
1
down vote
AutoHotKey, 113 bytes
CoordMode,Pixel
InputBox,x
InputBox,y
PixelGetColor,c,x,y
MsgBox % c&255 . "," . c>>8&255 . "," . c>>16&255
Use dialog boxes instead of stdin/stdout.
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
add a comment |
up vote
0
down vote
Python 2 + PIL library, 96 91 bytes
import PIL.ImageGrab as i
print','.join('%.1f'%(x/255.)for x in i.grab().getpixel(input()))
Implements the specification literally as requested. Windows only though - doesn't work on Linux, and produces extra output (alpha value) on Mac.
from PIL import ImageGrab as i
→from PIL import*
,i.grab
→ImageGrab.grab
.
– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the__import__('...')
way work.
– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
add a comment |
up vote
0
down vote
Bash + coreutils + scrot + netpbm, 90 bytes
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$2+$1)))>f;od -t u1 -N3 -An;)
Loose I/O version
Takes x
and y
as separate command-line arguments.
Prints r
, g
, b
as ints from 0-255 on separate lines
Bash + coreutils + scrot + netpbm + bc + sed, 172 bytes
IFS=, read x y
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$y+$x)))>f;od -vw1 -tu1 -N3 -An|while read p;do bc<<<"scale=2;$p/$m"|tr 'n' ,;done;)|sed s/,$//
Strict I/O version
Input on stdin as x,y
Output on stdout as r.rr,g.gg,b.bb
(no newline).
Note that the output must consist of floats in the range0-1
.
– Shaggy
3 hours ago
add a comment |
up vote
0
down vote
TI-Nspire assembly - 112 bytes
50 52 47 00 30 40 2D E9 FF FF FF FA 00 F0 17 F8
04 1C 00 F0 14 F8 85 00 2D 18 AD 01 2D 19 6C 00
C0 21 09 06 09 69 08 5B 3F 25 42 09 2A 40 1F 25
03 1C 2B 40 C1 0A 29 40 0A A0 0A DF 30 BD 00 20
0A 23 07 49 10 25 8A 69 2A 42 FC D1 0A 68 FF 25
2A 40 30 3A 0B DB 85 00 2D 18 6D 00 A8 18 F1 E7
00 00 02 90 25 64 2C 25 64 2C 25 64 0A 00 70 47
This program outputs integers in the range 0-31 for R and B and 0-63 for G, because the device natively uses a RGB565 framebuffer. It uses serial for input and output.
Source:
.string "PRG"
push {r4, r5, lr}
blx main
.thumb
main:
@ read x and y from serial into r4 and r0
bl read_int
mov r4, r0
bl read_int
@ turn x and y into framebuffer offset
@ r4 = ((r0 * 320) + r4) * 2
lsl r5, r0, #2
add r5, r0
lsl r5, #6
add r5, r4
lsl r4, r5, #1
@ load pixel from framebuffer
@ r0 = ((uint16_t **)0xc0000000)[0x10][r4 / 2]
mov r1, #0xC0
lsl r1, #24
ldr r1, [r1, #0x10]
ldrh r0, [r1, r4]
@ unpack RGB565 value into r1, r2, r3
mov r5, #0x3f
lsr r2, r0, #5
and r2, r5
mov r5, #0x1f
mov r3, r0
and r3, r5
lsr r1, r0, #11
and r1, r5
@ call printf
adr r0, fmt
swi #10
@ return
pop {r4, r5, pc}
@ subroutine to read an integer from serial
read_int:
mov r0, #0
mov r3, #10
ldr r1, serial_base
@ loop until characters come in on serial
2:
mov r5, #(1<<4)
1:
ldr r2, [r1, #0x18]
tst r2, r5
bne 1b
@ read character from serial and mask out status bits
ldr r2, [r1]
mov r5, #0xff
and r2, r5
@ subtract 48 ('0') from byte; if result is negative, return
sub r2, #48
blt 1f
@ multiply existing numbers by 10 and add new number to them
lsl r5, r0, #2
add r5, r0
lsl r5, #1
add r0, r5, r2
b 2b
serial_base:.word 0x90020000
fmt:.string "%d,%d,%dn"
@ this instruction is over here because serial_base and fmt need to be word-aligned
1:bx lr
add a comment |
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
Java 10 (lambda function), 105 75 bytes
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
A function taking two integer parameters and returning a String RRR,GGG,BBB
, where the colors are integers in the range [0, 255]
.
-30 bytes thanks to @LukeStevens by using java.awt.Color
's default toString()
output and modifying it so only the digits and commas remain.
Explanation:
x->y-> // Method with two integer parameters and String return-type
(new java.awt.Robot() // Create a AWT-Robot instance
.getPixelColor(x,y) // Get the pixel AWT-Color at position x,y
+"") // Call the default toString() of the AWT-Color
// i.e. "java.awt.Color[r=213,g=230,b=245]"
.replaceAll("[^\d,]","") // Remove everything except for digits and commas
NOTE: The default toString()
implementation of the java.awt.Color
has always been the same across JVM versions as far as I know, but can potentially change in the future. I tested it in both Java 8 and Java 10 and it returned "java.awt.Color[r=#,g=#,b=#]"
.
But, since the challenge states:
- Full program
- Taking input in the format
x,y
from STDIN - Output in the format
R,G,B
to STDOUT - Have the
R
,G
,B
as floating points in the range[0.0, 1.0]
The code becomes A LOT more verbose:
Java 10 (full program), 266 bytes
interface M{static void main(Stringa)throws Exception{var s=new java.util.Scanner(System.in).next().split(",");var c=new java.awt.Robot().getPixelColor(new Short(s[0]),new Short(s[1]));System.out.print(c.getRed()/255d+","+c.getGreen()/255d+","+c.getBlue()/255d);}}
Explanation:
interface M{ // Class
static void main(Stringa)
// Mandatory main method
throws Exception{ // Mandatory throws clause for `new Robot()`
var s=new java.util.Scanner(System.in)
// Create a Scanner for STDIN
.next() // Get the String user input
.split(","); // Split it on "," and save it as String-array in `s`
var c=new java.awt.Robot()
// Create a AWT-Robot instance
.getPixelColor(
// And get the pixel AWT-Color at position:
new Short( // Convert String to Short (and implicitly to int):
s[0]), // x-coordinate input by user from String-array `s`
new Short( // Convert String to Short (and implicitly to int):
s[1])); // y-coordinate input by user from String-array `s`
System.out.print( // Print to STDOUT:
c.getRed() // The red part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getGreen() // Appended with the green part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getBlue() // Appended with the blue part of RGB as integer in the range [0,255]
/255d);}} // Converted to a floating point in the range [0.0, 1.0]
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply havex->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes
– Luke Stevens
yesterday
@LukeStevens Smart use of the defaulttoString()
ofjava.awt.Color
. Thanks!
– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing thetoString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(
– Olivier Grégoire
yesterday
2
Note that the Javadoc is very clear thatColor::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).
– Olivier Grégoire
yesterday
1
Finally, you should replace" "
with","
to comply with the strictRRR,GGG,BBB
format (notRRR GGG BBB
).
– Olivier Grégoire
yesterday
|
show 1 more comment
up vote
7
down vote
Java 10 (lambda function), 105 75 bytes
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
A function taking two integer parameters and returning a String RRR,GGG,BBB
, where the colors are integers in the range [0, 255]
.
-30 bytes thanks to @LukeStevens by using java.awt.Color
's default toString()
output and modifying it so only the digits and commas remain.
Explanation:
x->y-> // Method with two integer parameters and String return-type
(new java.awt.Robot() // Create a AWT-Robot instance
.getPixelColor(x,y) // Get the pixel AWT-Color at position x,y
+"") // Call the default toString() of the AWT-Color
// i.e. "java.awt.Color[r=213,g=230,b=245]"
.replaceAll("[^\d,]","") // Remove everything except for digits and commas
NOTE: The default toString()
implementation of the java.awt.Color
has always been the same across JVM versions as far as I know, but can potentially change in the future. I tested it in both Java 8 and Java 10 and it returned "java.awt.Color[r=#,g=#,b=#]"
.
But, since the challenge states:
- Full program
- Taking input in the format
x,y
from STDIN - Output in the format
R,G,B
to STDOUT - Have the
R
,G
,B
as floating points in the range[0.0, 1.0]
The code becomes A LOT more verbose:
Java 10 (full program), 266 bytes
interface M{static void main(Stringa)throws Exception{var s=new java.util.Scanner(System.in).next().split(",");var c=new java.awt.Robot().getPixelColor(new Short(s[0]),new Short(s[1]));System.out.print(c.getRed()/255d+","+c.getGreen()/255d+","+c.getBlue()/255d);}}
Explanation:
interface M{ // Class
static void main(Stringa)
// Mandatory main method
throws Exception{ // Mandatory throws clause for `new Robot()`
var s=new java.util.Scanner(System.in)
// Create a Scanner for STDIN
.next() // Get the String user input
.split(","); // Split it on "," and save it as String-array in `s`
var c=new java.awt.Robot()
// Create a AWT-Robot instance
.getPixelColor(
// And get the pixel AWT-Color at position:
new Short( // Convert String to Short (and implicitly to int):
s[0]), // x-coordinate input by user from String-array `s`
new Short( // Convert String to Short (and implicitly to int):
s[1])); // y-coordinate input by user from String-array `s`
System.out.print( // Print to STDOUT:
c.getRed() // The red part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getGreen() // Appended with the green part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getBlue() // Appended with the blue part of RGB as integer in the range [0,255]
/255d);}} // Converted to a floating point in the range [0.0, 1.0]
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply havex->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes
– Luke Stevens
yesterday
@LukeStevens Smart use of the defaulttoString()
ofjava.awt.Color
. Thanks!
– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing thetoString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(
– Olivier Grégoire
yesterday
2
Note that the Javadoc is very clear thatColor::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).
– Olivier Grégoire
yesterday
1
Finally, you should replace" "
with","
to comply with the strictRRR,GGG,BBB
format (notRRR GGG BBB
).
– Olivier Grégoire
yesterday
|
show 1 more comment
up vote
7
down vote
up vote
7
down vote
Java 10 (lambda function), 105 75 bytes
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
A function taking two integer parameters and returning a String RRR,GGG,BBB
, where the colors are integers in the range [0, 255]
.
-30 bytes thanks to @LukeStevens by using java.awt.Color
's default toString()
output and modifying it so only the digits and commas remain.
Explanation:
x->y-> // Method with two integer parameters and String return-type
(new java.awt.Robot() // Create a AWT-Robot instance
.getPixelColor(x,y) // Get the pixel AWT-Color at position x,y
+"") // Call the default toString() of the AWT-Color
// i.e. "java.awt.Color[r=213,g=230,b=245]"
.replaceAll("[^\d,]","") // Remove everything except for digits and commas
NOTE: The default toString()
implementation of the java.awt.Color
has always been the same across JVM versions as far as I know, but can potentially change in the future. I tested it in both Java 8 and Java 10 and it returned "java.awt.Color[r=#,g=#,b=#]"
.
But, since the challenge states:
- Full program
- Taking input in the format
x,y
from STDIN - Output in the format
R,G,B
to STDOUT - Have the
R
,G
,B
as floating points in the range[0.0, 1.0]
The code becomes A LOT more verbose:
Java 10 (full program), 266 bytes
interface M{static void main(Stringa)throws Exception{var s=new java.util.Scanner(System.in).next().split(",");var c=new java.awt.Robot().getPixelColor(new Short(s[0]),new Short(s[1]));System.out.print(c.getRed()/255d+","+c.getGreen()/255d+","+c.getBlue()/255d);}}
Explanation:
interface M{ // Class
static void main(Stringa)
// Mandatory main method
throws Exception{ // Mandatory throws clause for `new Robot()`
var s=new java.util.Scanner(System.in)
// Create a Scanner for STDIN
.next() // Get the String user input
.split(","); // Split it on "," and save it as String-array in `s`
var c=new java.awt.Robot()
// Create a AWT-Robot instance
.getPixelColor(
// And get the pixel AWT-Color at position:
new Short( // Convert String to Short (and implicitly to int):
s[0]), // x-coordinate input by user from String-array `s`
new Short( // Convert String to Short (and implicitly to int):
s[1])); // y-coordinate input by user from String-array `s`
System.out.print( // Print to STDOUT:
c.getRed() // The red part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getGreen() // Appended with the green part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getBlue() // Appended with the blue part of RGB as integer in the range [0,255]
/255d);}} // Converted to a floating point in the range [0.0, 1.0]
Java 10 (lambda function), 105 75 bytes
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
A function taking two integer parameters and returning a String RRR,GGG,BBB
, where the colors are integers in the range [0, 255]
.
-30 bytes thanks to @LukeStevens by using java.awt.Color
's default toString()
output and modifying it so only the digits and commas remain.
Explanation:
x->y-> // Method with two integer parameters and String return-type
(new java.awt.Robot() // Create a AWT-Robot instance
.getPixelColor(x,y) // Get the pixel AWT-Color at position x,y
+"") // Call the default toString() of the AWT-Color
// i.e. "java.awt.Color[r=213,g=230,b=245]"
.replaceAll("[^\d,]","") // Remove everything except for digits and commas
NOTE: The default toString()
implementation of the java.awt.Color
has always been the same across JVM versions as far as I know, but can potentially change in the future. I tested it in both Java 8 and Java 10 and it returned "java.awt.Color[r=#,g=#,b=#]"
.
But, since the challenge states:
- Full program
- Taking input in the format
x,y
from STDIN - Output in the format
R,G,B
to STDOUT - Have the
R
,G
,B
as floating points in the range[0.0, 1.0]
The code becomes A LOT more verbose:
Java 10 (full program), 266 bytes
interface M{static void main(Stringa)throws Exception{var s=new java.util.Scanner(System.in).next().split(",");var c=new java.awt.Robot().getPixelColor(new Short(s[0]),new Short(s[1]));System.out.print(c.getRed()/255d+","+c.getGreen()/255d+","+c.getBlue()/255d);}}
Explanation:
interface M{ // Class
static void main(Stringa)
// Mandatory main method
throws Exception{ // Mandatory throws clause for `new Robot()`
var s=new java.util.Scanner(System.in)
// Create a Scanner for STDIN
.next() // Get the String user input
.split(","); // Split it on "," and save it as String-array in `s`
var c=new java.awt.Robot()
// Create a AWT-Robot instance
.getPixelColor(
// And get the pixel AWT-Color at position:
new Short( // Convert String to Short (and implicitly to int):
s[0]), // x-coordinate input by user from String-array `s`
new Short( // Convert String to Short (and implicitly to int):
s[1])); // y-coordinate input by user from String-array `s`
System.out.print( // Print to STDOUT:
c.getRed() // The red part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getGreen() // Appended with the green part of RGB as integer in the range [0,255]
/255d // Converted to a floating point in the range [0.0, 1.0]
+"," // Appended with a comma delimiter
+c.getBlue() // Appended with the blue part of RGB as integer in the range [0,255]
/255d);}} // Converted to a floating point in the range [0.0, 1.0]
edited yesterday
answered yesterday
Kevin Cruijssen
34.9k554184
34.9k554184
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply havex->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes
– Luke Stevens
yesterday
@LukeStevens Smart use of the defaulttoString()
ofjava.awt.Color
. Thanks!
– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing thetoString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(
– Olivier Grégoire
yesterday
2
Note that the Javadoc is very clear thatColor::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).
– Olivier Grégoire
yesterday
1
Finally, you should replace" "
with","
to comply with the strictRRR,GGG,BBB
format (notRRR GGG BBB
).
– Olivier Grégoire
yesterday
|
show 1 more comment
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply havex->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes
– Luke Stevens
yesterday
@LukeStevens Smart use of the defaulttoString()
ofjava.awt.Color
. Thanks!
– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing thetoString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(
– Olivier Grégoire
yesterday
2
Note that the Javadoc is very clear thatColor::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).
– Olivier Grégoire
yesterday
1
Finally, you should replace" "
with","
to comply with the strictRRR,GGG,BBB
format (notRRR GGG BBB
).
– Olivier Grégoire
yesterday
2
2
For the first, loose-rules answer, you can make use of Colors String conversion and simply have
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes– Luke Stevens
yesterday
For the first, loose-rules answer, you can make use of Colors String conversion and simply have
x->y->(new java.awt.Robot().getPixelColor(x,y)+"").replaceAll("[^\d,]","")
for 75 bytes– Luke Stevens
yesterday
@LukeStevens Smart use of the default
toString()
of java.awt.Color
. Thanks!– Kevin Cruijssen
yesterday
@LukeStevens Smart use of the default
toString()
of java.awt.Color
. Thanks!– Kevin Cruijssen
yesterday
Good job guys, you beat me to it: I was busy implementing the
toString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(– Olivier Grégoire
yesterday
Good job guys, you beat me to it: I was busy implementing the
toString()
way as well... ;-) Except I had to download an IDE to do all that: I only use TIO now :(– Olivier Grégoire
yesterday
2
2
Note that the Javadoc is very clear that
Color::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).– Olivier Grégoire
yesterday
Note that the Javadoc is very clear that
Color::toString
is implementation-discretionary. So it might be good to specify which JVM was used for the tests (it works as is with OpenJDK and Oracle JDK).– Olivier Grégoire
yesterday
1
1
Finally, you should replace
" "
with ","
to comply with the strict RRR,GGG,BBB
format (not RRR GGG BBB
).– Olivier Grégoire
yesterday
Finally, you should replace
" "
with ","
to comply with the strict RRR,GGG,BBB
format (not RRR GGG BBB
).– Olivier Grégoire
yesterday
|
show 1 more comment
up vote
7
down vote
6502 machine code (C64), 280 260 bytes
00 C0 A2 00 20 CF FF C9 0D 9D 02 C1 F0 03 E8 D0 F3 20 D2 FF A5 7A 85 FB A5 7B
85 FC A9 01 85 7A A9 C1 85 7B 20 73 00 20 6B A9 A5 14 85 FD A5 15 85 FE 20 73
00 20 6B A9 A5 FB 85 7A A5 FC 85 7B A5 14 4A 4A 4A AA 20 F0 E9 A5 FD 46 FE 6A
4A 4A A8 A5 14 29 07 AA A9 00 38 6A CA 10 FC 85 FE B1 D1 0A 26 FC 0A 26 FC 0A
26 FC 85 FB A5 D2 29 03 09 D8 85 D2 B1 D1 85 02 A9 02 18 2D 18 D0 0A 0A 65 FC
29 0F 09 D0 85 FC A9 33 85 01 A5 FD 29 07 A8 B1 FB A2 37 86 01 25 FE D0 05 AD
21 D0 85 02 A6 D6 20 F0 E9 A5 02 29 0F AA BC D2 C0 20 BC C0 BC E2 C0 20 B7 C0
BC F2 C0 A9 2C 20 D2 FF 98 0A A9 30 90 02 A9 31 20 D2 FF A9 2E 20 D2 FF 98 29
7F 4C D2 FF 30 B0 35 35 36 33 32 39 36 33 38 33 35 37 34 37 30 B0 32 38 32 37
32 39 33 32 34 33 35 B0 34 37 30 B0 32 38 36 33 36 34 32 30 34 33 35 36 39 37
I expected this to be possible in a lot fewer bytes, but unfortunately... well, since I finished it, posting it now anyways. At least, the restrictive format helped with one thing: Something similar to stdin
(input from a current device) only exists on the C64 in text mode, because the OS only supports this mode -- so no need to consider other modes of the graphics chip.
Note on the output of the color values: the C64 graphics chip doesn't use RGB colors but directly generates a video signal with YUV colors, with a fixed 16 colors palette. I used rounded values of the colodore conversion to RGB with "default" monitor settings here.
-20 bytes: better output routine, encoding the 3 character output per color channel in a single byte.
Regarding the comment: It's theoretically possible to use even multicolor character mode of the VIC with the stock C64 OS, but it requires a custom font that's actually legible with only 4 horizontal pixels of doubled width. Not entirely impossible, but very unlikely. Likewise, extended color mode (or extended background mode, which is the same) could be used with the C64 OS, but requires to reconfigure the graphics chip directly. I opt to ignore all these possibilities in the sense of code golfing here: It's not the standard environment you find on a Commodore 64 with running stock OS. What's possible with stock OS is switching between two builtin fonts (shift + commodore key), the program does account for that.
Online demo
Usage: SYS49152
to start.
Commented disassembly:
00 C0 .WORD $C000 ; load address
.C:c000 A2 00 LDX #$00 ; loop index for input
.C:c002 .input:
.C:c002 20 CF FF JSR $FFCF ; character from input device
.C:c005 C9 0D CMP #$0D ; compare with enter
.C:c007 9D 16 C1 STA .buf,X ; store to buffer
.C:c00a F0 03 BEQ .parse ; was enter -> start parsing
.C:c00c E8 INX ; next character
.C:c00d D0 F3 BNE .input ; and repeat input loop
.C:c00f .parse:
.C:c00f 20 D2 FF JSR $FFD2 ; output the enter character
.C:c012 A5 7A LDA $7A ; save pointer of BASIC parser
.C:c014 85 FB STA $FB
.C:c016 A5 7B LDA $7B
.C:c018 85 FC STA $FC
.C:c01a A9 15 LDA #$15 ; set pointer of BASIC parser to
.C:c01c 85 7A STA $7A ; buffer-1
.C:c01e A9 C1 LDA #$C1
.C:c020 85 7B STA $7B
.C:c022 20 73 00 JSR $0073 ; get next character
.C:c025 20 6B A9 JSR $A96B ; BASIC routine to parse number
.C:c028 A5 14 LDA $14 ; lowbyte of parsed number to $fd
.C:c02a 85 FD STA $FD
.C:c02c A5 15 LDA $15 ; highbyte to $fe
.C:c02e 85 FE STA $FE
.C:c030 20 73 00 JSR $0073 ; get next character
.C:c033 20 6B A9 JSR $A96B ; parse as number ...
.C:c036 A5 FB LDA $FB ; restore pointer of BASIC parser
.C:c038 85 7A STA $7A
.C:c03a A5 FC LDA $FC
.C:c03c 85 7B STA $7B
.C:c03e A5 14 LDA $14 ; load y coordinate
.C:c040 4A LSR A ; divide by 8 for character row
.C:c041 4A LSR A
.C:c042 4A LSR A
.C:c043 AA TAX ; -> to X
.C:c044 20 F0 E9 JSR $E9F0 ; set pointer to character row
.C:c047 A5 FD LDA $FD ; divide x coordinate by 8
.C:c049 46 FE LSR $FE
.C:c04b 6A ROR A
.C:c04c 4A LSR A
.C:c04d 4A LSR A
.C:c04e A8 TAY ; -> to Y
.C:c04f A5 14 LDA $14 ; load y coordinate
.C:c051 29 07 AND #$07 ; mask pixel position in character
.C:c053 AA TAX ; -> to X
.C:c054 A9 00 LDA #$00 ; initialize pixel mask to 0
.C:c056 38 SEC ; set carry for bit to shift in
.C:c057 .bitnum:
.C:c057 6A ROR A ; shift bit in mask
.C:c058 CA DEX ; and repeat until
.C:c059 10 FC BPL .bitnum ; in correct position
.C:c05b 85 FE STA $FE ; store pixel mask to $fe
.C:c05d B1 D1 LDA ($D1),Y ; load character code
.C:c05f 0A ASL A ; multiply by 8
.C:c060 26 FC ROL $FC
.C:c062 0A ASL A
.C:c063 26 FC ROL $FC
.C:c065 0A ASL A
.C:c066 26 FC ROL $FC
.C:c068 85 FB STA $FB ; and store to $fb/$fc
.C:c06a A5 D2 LDA $D2 ; move pointer to position in color RAM
.C:c06c 29 03 AND #$03
.C:c06e 09 D8 ORA #$D8
.C:c070 85 D2 STA $D2
.C:c072 B1 D1 LDA ($D1),Y ; load color of character
.C:c074 85 02 STA $02 ; and store to $2
.C:c076 A9 02 LDA #$02 ; check which charset is active
.C:c078 18 CLC
.C:c079 2D 18 D0 AND $D018
.C:c07c 0A ASL A ; and calculate offset
.C:c07d 0A ASL A
.C:c07e 65 FC ADC $FC ; add to (character code * 8)
.C:c080 29 0F AND #$0F
.C:c082 09 D0 ORA #$D0 ; and add offset to character ROM
.C:c084 85 FC STA $FC
.C:c086 A9 33 LDA #$33 ; bank in character ROM
.C:c088 85 01 STA $01
.C:c08a A5 FD LDA $FD ; load y coordinate
.C:c08c 29 07 AND #$07 ; mask pixel-row number
.C:c08e A8 TAY
.C:c08f B1 FB LDA ($FB),Y ; load pixel row from character ROM
.C:c091 A2 37 LDX #$37 ; bank out character ROM
.C:c093 86 01 STX $01
.C:c095 25 FE AND $FE ; apply pixel mask
.C:c097 D0 05 BNE .pixelcol ; not 0 -> pixel is set
.C:c099 AD 21 D0 LDA $D021 ; otherwise load background color
.C:c09c 85 02 STA $02 ; and store to $2
.C:c09e .pixelcol:
.C:c09e A6 D6 LDX $D6 ; restore screen row pointer for
.C:c0a0 20 F0 E9 JSR $E9F0 ; current cursor position
.C:c0a3 A5 02 LDA $02 ; load color
.C:c0a5 29 0F AND #$0F ; mask low nibble (only 16 colors)
.C:c0a7 AA TAX ; -> to X
.C:c0a8 BC D2 C0 LDY .red,X ; load encoded output for red
.C:c0ab 20 BC C0 JSR .out2 ; call output without comma
.C:c0ae BC E2 C0 LDY .green,X ; load encoded output for green
.C:c0b1 20 B7 C0 JSR .out1 ; call output with comma
.C:c0b4 BC F2 C0 LDY .blue,X ; load encoded output for blue
.C:c0b7 .out1:
.C:c0b7 A9 2C LDA #$2C ; load ","
.C:c0b9 20 D2 FF JSR $FFD2 ; and output
.C:c0bc .out2:
.C:c0bc 98 TYA ; encoded output to A
.C:c0bd 0A ASL A ; shift top bit to carry
.C:c0be A9 30 LDA #$30 ; load "0"
.C:c0c0 90 02 BCC .firstdig ; carry clear -> to output
.C:c0c2 A9 31 LDA #$31 ; load "1"
.C:c0c4 .firstdig:
.C:c0c4 20 D2 FF JSR $FFD2 ; and output
.C:c0c7 A9 2E LDA #$2E ; load "."
.C:c0c9 20 D2 FF JSR $FFD2 ; and output
.C:c0cc 98 TYA ; encoded output to A
.C:c0cd 29 7F AND #$7F ; mask out top bit
.C:c0cf 4C D2 FF JMP $FFD2 ; to output and exit
.C:c0d2 .red: ; encoded values for red
.C:c0d2 30 B0 35 35 .BYTE $30,$B0,$35,$35 ; ASCII digit ($30-$39) after
.C:c0d6 36 33 32 39 .BYTE $36,$33,$32,$39 ; decimal point, with bit 7
.C:c0da 36 33 38 33 .BYTE $36,$33,$38,$33 ; indicating 0 or 1 before
.C:c0de 35 37 34 37 .BYTE $35,$37,$34,$37 ; decimal point
.C:c0e2 .green: ; encoded values for green
.C:c0e2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0e6 32 37 32 39 .BYTE $32,$37,$32,$39
.C:c0ea 33 32 34 33 .BYTE $33,$32,$34,$33
.C:c0ee 35 B0 34 37 .BYTE $35,$B0,$34,$37
.C:c0f2 .blue: ; encoded values for blue
.C:c0f2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0f6 36 33 36 34 .BYTE $36,$33,$36,$34
.C:c0fa 32 30 34 33 .BYTE $32,$30,$34,$33
.C:c0fe 35 36 39 37 .BYTE $35,$36,$39,$37
.C:c102 .buf: ; buffer for input ("stdin")
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that withcc65
.. quite possible :) but at least, the compiled code will be larger :)
– Felix Palmen
4 hours ago
add a comment |
up vote
7
down vote
6502 machine code (C64), 280 260 bytes
00 C0 A2 00 20 CF FF C9 0D 9D 02 C1 F0 03 E8 D0 F3 20 D2 FF A5 7A 85 FB A5 7B
85 FC A9 01 85 7A A9 C1 85 7B 20 73 00 20 6B A9 A5 14 85 FD A5 15 85 FE 20 73
00 20 6B A9 A5 FB 85 7A A5 FC 85 7B A5 14 4A 4A 4A AA 20 F0 E9 A5 FD 46 FE 6A
4A 4A A8 A5 14 29 07 AA A9 00 38 6A CA 10 FC 85 FE B1 D1 0A 26 FC 0A 26 FC 0A
26 FC 85 FB A5 D2 29 03 09 D8 85 D2 B1 D1 85 02 A9 02 18 2D 18 D0 0A 0A 65 FC
29 0F 09 D0 85 FC A9 33 85 01 A5 FD 29 07 A8 B1 FB A2 37 86 01 25 FE D0 05 AD
21 D0 85 02 A6 D6 20 F0 E9 A5 02 29 0F AA BC D2 C0 20 BC C0 BC E2 C0 20 B7 C0
BC F2 C0 A9 2C 20 D2 FF 98 0A A9 30 90 02 A9 31 20 D2 FF A9 2E 20 D2 FF 98 29
7F 4C D2 FF 30 B0 35 35 36 33 32 39 36 33 38 33 35 37 34 37 30 B0 32 38 32 37
32 39 33 32 34 33 35 B0 34 37 30 B0 32 38 36 33 36 34 32 30 34 33 35 36 39 37
I expected this to be possible in a lot fewer bytes, but unfortunately... well, since I finished it, posting it now anyways. At least, the restrictive format helped with one thing: Something similar to stdin
(input from a current device) only exists on the C64 in text mode, because the OS only supports this mode -- so no need to consider other modes of the graphics chip.
Note on the output of the color values: the C64 graphics chip doesn't use RGB colors but directly generates a video signal with YUV colors, with a fixed 16 colors palette. I used rounded values of the colodore conversion to RGB with "default" monitor settings here.
-20 bytes: better output routine, encoding the 3 character output per color channel in a single byte.
Regarding the comment: It's theoretically possible to use even multicolor character mode of the VIC with the stock C64 OS, but it requires a custom font that's actually legible with only 4 horizontal pixels of doubled width. Not entirely impossible, but very unlikely. Likewise, extended color mode (or extended background mode, which is the same) could be used with the C64 OS, but requires to reconfigure the graphics chip directly. I opt to ignore all these possibilities in the sense of code golfing here: It's not the standard environment you find on a Commodore 64 with running stock OS. What's possible with stock OS is switching between two builtin fonts (shift + commodore key), the program does account for that.
Online demo
Usage: SYS49152
to start.
Commented disassembly:
00 C0 .WORD $C000 ; load address
.C:c000 A2 00 LDX #$00 ; loop index for input
.C:c002 .input:
.C:c002 20 CF FF JSR $FFCF ; character from input device
.C:c005 C9 0D CMP #$0D ; compare with enter
.C:c007 9D 16 C1 STA .buf,X ; store to buffer
.C:c00a F0 03 BEQ .parse ; was enter -> start parsing
.C:c00c E8 INX ; next character
.C:c00d D0 F3 BNE .input ; and repeat input loop
.C:c00f .parse:
.C:c00f 20 D2 FF JSR $FFD2 ; output the enter character
.C:c012 A5 7A LDA $7A ; save pointer of BASIC parser
.C:c014 85 FB STA $FB
.C:c016 A5 7B LDA $7B
.C:c018 85 FC STA $FC
.C:c01a A9 15 LDA #$15 ; set pointer of BASIC parser to
.C:c01c 85 7A STA $7A ; buffer-1
.C:c01e A9 C1 LDA #$C1
.C:c020 85 7B STA $7B
.C:c022 20 73 00 JSR $0073 ; get next character
.C:c025 20 6B A9 JSR $A96B ; BASIC routine to parse number
.C:c028 A5 14 LDA $14 ; lowbyte of parsed number to $fd
.C:c02a 85 FD STA $FD
.C:c02c A5 15 LDA $15 ; highbyte to $fe
.C:c02e 85 FE STA $FE
.C:c030 20 73 00 JSR $0073 ; get next character
.C:c033 20 6B A9 JSR $A96B ; parse as number ...
.C:c036 A5 FB LDA $FB ; restore pointer of BASIC parser
.C:c038 85 7A STA $7A
.C:c03a A5 FC LDA $FC
.C:c03c 85 7B STA $7B
.C:c03e A5 14 LDA $14 ; load y coordinate
.C:c040 4A LSR A ; divide by 8 for character row
.C:c041 4A LSR A
.C:c042 4A LSR A
.C:c043 AA TAX ; -> to X
.C:c044 20 F0 E9 JSR $E9F0 ; set pointer to character row
.C:c047 A5 FD LDA $FD ; divide x coordinate by 8
.C:c049 46 FE LSR $FE
.C:c04b 6A ROR A
.C:c04c 4A LSR A
.C:c04d 4A LSR A
.C:c04e A8 TAY ; -> to Y
.C:c04f A5 14 LDA $14 ; load y coordinate
.C:c051 29 07 AND #$07 ; mask pixel position in character
.C:c053 AA TAX ; -> to X
.C:c054 A9 00 LDA #$00 ; initialize pixel mask to 0
.C:c056 38 SEC ; set carry for bit to shift in
.C:c057 .bitnum:
.C:c057 6A ROR A ; shift bit in mask
.C:c058 CA DEX ; and repeat until
.C:c059 10 FC BPL .bitnum ; in correct position
.C:c05b 85 FE STA $FE ; store pixel mask to $fe
.C:c05d B1 D1 LDA ($D1),Y ; load character code
.C:c05f 0A ASL A ; multiply by 8
.C:c060 26 FC ROL $FC
.C:c062 0A ASL A
.C:c063 26 FC ROL $FC
.C:c065 0A ASL A
.C:c066 26 FC ROL $FC
.C:c068 85 FB STA $FB ; and store to $fb/$fc
.C:c06a A5 D2 LDA $D2 ; move pointer to position in color RAM
.C:c06c 29 03 AND #$03
.C:c06e 09 D8 ORA #$D8
.C:c070 85 D2 STA $D2
.C:c072 B1 D1 LDA ($D1),Y ; load color of character
.C:c074 85 02 STA $02 ; and store to $2
.C:c076 A9 02 LDA #$02 ; check which charset is active
.C:c078 18 CLC
.C:c079 2D 18 D0 AND $D018
.C:c07c 0A ASL A ; and calculate offset
.C:c07d 0A ASL A
.C:c07e 65 FC ADC $FC ; add to (character code * 8)
.C:c080 29 0F AND #$0F
.C:c082 09 D0 ORA #$D0 ; and add offset to character ROM
.C:c084 85 FC STA $FC
.C:c086 A9 33 LDA #$33 ; bank in character ROM
.C:c088 85 01 STA $01
.C:c08a A5 FD LDA $FD ; load y coordinate
.C:c08c 29 07 AND #$07 ; mask pixel-row number
.C:c08e A8 TAY
.C:c08f B1 FB LDA ($FB),Y ; load pixel row from character ROM
.C:c091 A2 37 LDX #$37 ; bank out character ROM
.C:c093 86 01 STX $01
.C:c095 25 FE AND $FE ; apply pixel mask
.C:c097 D0 05 BNE .pixelcol ; not 0 -> pixel is set
.C:c099 AD 21 D0 LDA $D021 ; otherwise load background color
.C:c09c 85 02 STA $02 ; and store to $2
.C:c09e .pixelcol:
.C:c09e A6 D6 LDX $D6 ; restore screen row pointer for
.C:c0a0 20 F0 E9 JSR $E9F0 ; current cursor position
.C:c0a3 A5 02 LDA $02 ; load color
.C:c0a5 29 0F AND #$0F ; mask low nibble (only 16 colors)
.C:c0a7 AA TAX ; -> to X
.C:c0a8 BC D2 C0 LDY .red,X ; load encoded output for red
.C:c0ab 20 BC C0 JSR .out2 ; call output without comma
.C:c0ae BC E2 C0 LDY .green,X ; load encoded output for green
.C:c0b1 20 B7 C0 JSR .out1 ; call output with comma
.C:c0b4 BC F2 C0 LDY .blue,X ; load encoded output for blue
.C:c0b7 .out1:
.C:c0b7 A9 2C LDA #$2C ; load ","
.C:c0b9 20 D2 FF JSR $FFD2 ; and output
.C:c0bc .out2:
.C:c0bc 98 TYA ; encoded output to A
.C:c0bd 0A ASL A ; shift top bit to carry
.C:c0be A9 30 LDA #$30 ; load "0"
.C:c0c0 90 02 BCC .firstdig ; carry clear -> to output
.C:c0c2 A9 31 LDA #$31 ; load "1"
.C:c0c4 .firstdig:
.C:c0c4 20 D2 FF JSR $FFD2 ; and output
.C:c0c7 A9 2E LDA #$2E ; load "."
.C:c0c9 20 D2 FF JSR $FFD2 ; and output
.C:c0cc 98 TYA ; encoded output to A
.C:c0cd 29 7F AND #$7F ; mask out top bit
.C:c0cf 4C D2 FF JMP $FFD2 ; to output and exit
.C:c0d2 .red: ; encoded values for red
.C:c0d2 30 B0 35 35 .BYTE $30,$B0,$35,$35 ; ASCII digit ($30-$39) after
.C:c0d6 36 33 32 39 .BYTE $36,$33,$32,$39 ; decimal point, with bit 7
.C:c0da 36 33 38 33 .BYTE $36,$33,$38,$33 ; indicating 0 or 1 before
.C:c0de 35 37 34 37 .BYTE $35,$37,$34,$37 ; decimal point
.C:c0e2 .green: ; encoded values for green
.C:c0e2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0e6 32 37 32 39 .BYTE $32,$37,$32,$39
.C:c0ea 33 32 34 33 .BYTE $33,$32,$34,$33
.C:c0ee 35 B0 34 37 .BYTE $35,$B0,$34,$37
.C:c0f2 .blue: ; encoded values for blue
.C:c0f2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0f6 36 33 36 34 .BYTE $36,$33,$36,$34
.C:c0fa 32 30 34 33 .BYTE $32,$30,$34,$33
.C:c0fe 35 36 39 37 .BYTE $35,$36,$39,$37
.C:c102 .buf: ; buffer for input ("stdin")
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that withcc65
.. quite possible :) but at least, the compiled code will be larger :)
– Felix Palmen
4 hours ago
add a comment |
up vote
7
down vote
up vote
7
down vote
6502 machine code (C64), 280 260 bytes
00 C0 A2 00 20 CF FF C9 0D 9D 02 C1 F0 03 E8 D0 F3 20 D2 FF A5 7A 85 FB A5 7B
85 FC A9 01 85 7A A9 C1 85 7B 20 73 00 20 6B A9 A5 14 85 FD A5 15 85 FE 20 73
00 20 6B A9 A5 FB 85 7A A5 FC 85 7B A5 14 4A 4A 4A AA 20 F0 E9 A5 FD 46 FE 6A
4A 4A A8 A5 14 29 07 AA A9 00 38 6A CA 10 FC 85 FE B1 D1 0A 26 FC 0A 26 FC 0A
26 FC 85 FB A5 D2 29 03 09 D8 85 D2 B1 D1 85 02 A9 02 18 2D 18 D0 0A 0A 65 FC
29 0F 09 D0 85 FC A9 33 85 01 A5 FD 29 07 A8 B1 FB A2 37 86 01 25 FE D0 05 AD
21 D0 85 02 A6 D6 20 F0 E9 A5 02 29 0F AA BC D2 C0 20 BC C0 BC E2 C0 20 B7 C0
BC F2 C0 A9 2C 20 D2 FF 98 0A A9 30 90 02 A9 31 20 D2 FF A9 2E 20 D2 FF 98 29
7F 4C D2 FF 30 B0 35 35 36 33 32 39 36 33 38 33 35 37 34 37 30 B0 32 38 32 37
32 39 33 32 34 33 35 B0 34 37 30 B0 32 38 36 33 36 34 32 30 34 33 35 36 39 37
I expected this to be possible in a lot fewer bytes, but unfortunately... well, since I finished it, posting it now anyways. At least, the restrictive format helped with one thing: Something similar to stdin
(input from a current device) only exists on the C64 in text mode, because the OS only supports this mode -- so no need to consider other modes of the graphics chip.
Note on the output of the color values: the C64 graphics chip doesn't use RGB colors but directly generates a video signal with YUV colors, with a fixed 16 colors palette. I used rounded values of the colodore conversion to RGB with "default" monitor settings here.
-20 bytes: better output routine, encoding the 3 character output per color channel in a single byte.
Regarding the comment: It's theoretically possible to use even multicolor character mode of the VIC with the stock C64 OS, but it requires a custom font that's actually legible with only 4 horizontal pixels of doubled width. Not entirely impossible, but very unlikely. Likewise, extended color mode (or extended background mode, which is the same) could be used with the C64 OS, but requires to reconfigure the graphics chip directly. I opt to ignore all these possibilities in the sense of code golfing here: It's not the standard environment you find on a Commodore 64 with running stock OS. What's possible with stock OS is switching between two builtin fonts (shift + commodore key), the program does account for that.
Online demo
Usage: SYS49152
to start.
Commented disassembly:
00 C0 .WORD $C000 ; load address
.C:c000 A2 00 LDX #$00 ; loop index for input
.C:c002 .input:
.C:c002 20 CF FF JSR $FFCF ; character from input device
.C:c005 C9 0D CMP #$0D ; compare with enter
.C:c007 9D 16 C1 STA .buf,X ; store to buffer
.C:c00a F0 03 BEQ .parse ; was enter -> start parsing
.C:c00c E8 INX ; next character
.C:c00d D0 F3 BNE .input ; and repeat input loop
.C:c00f .parse:
.C:c00f 20 D2 FF JSR $FFD2 ; output the enter character
.C:c012 A5 7A LDA $7A ; save pointer of BASIC parser
.C:c014 85 FB STA $FB
.C:c016 A5 7B LDA $7B
.C:c018 85 FC STA $FC
.C:c01a A9 15 LDA #$15 ; set pointer of BASIC parser to
.C:c01c 85 7A STA $7A ; buffer-1
.C:c01e A9 C1 LDA #$C1
.C:c020 85 7B STA $7B
.C:c022 20 73 00 JSR $0073 ; get next character
.C:c025 20 6B A9 JSR $A96B ; BASIC routine to parse number
.C:c028 A5 14 LDA $14 ; lowbyte of parsed number to $fd
.C:c02a 85 FD STA $FD
.C:c02c A5 15 LDA $15 ; highbyte to $fe
.C:c02e 85 FE STA $FE
.C:c030 20 73 00 JSR $0073 ; get next character
.C:c033 20 6B A9 JSR $A96B ; parse as number ...
.C:c036 A5 FB LDA $FB ; restore pointer of BASIC parser
.C:c038 85 7A STA $7A
.C:c03a A5 FC LDA $FC
.C:c03c 85 7B STA $7B
.C:c03e A5 14 LDA $14 ; load y coordinate
.C:c040 4A LSR A ; divide by 8 for character row
.C:c041 4A LSR A
.C:c042 4A LSR A
.C:c043 AA TAX ; -> to X
.C:c044 20 F0 E9 JSR $E9F0 ; set pointer to character row
.C:c047 A5 FD LDA $FD ; divide x coordinate by 8
.C:c049 46 FE LSR $FE
.C:c04b 6A ROR A
.C:c04c 4A LSR A
.C:c04d 4A LSR A
.C:c04e A8 TAY ; -> to Y
.C:c04f A5 14 LDA $14 ; load y coordinate
.C:c051 29 07 AND #$07 ; mask pixel position in character
.C:c053 AA TAX ; -> to X
.C:c054 A9 00 LDA #$00 ; initialize pixel mask to 0
.C:c056 38 SEC ; set carry for bit to shift in
.C:c057 .bitnum:
.C:c057 6A ROR A ; shift bit in mask
.C:c058 CA DEX ; and repeat until
.C:c059 10 FC BPL .bitnum ; in correct position
.C:c05b 85 FE STA $FE ; store pixel mask to $fe
.C:c05d B1 D1 LDA ($D1),Y ; load character code
.C:c05f 0A ASL A ; multiply by 8
.C:c060 26 FC ROL $FC
.C:c062 0A ASL A
.C:c063 26 FC ROL $FC
.C:c065 0A ASL A
.C:c066 26 FC ROL $FC
.C:c068 85 FB STA $FB ; and store to $fb/$fc
.C:c06a A5 D2 LDA $D2 ; move pointer to position in color RAM
.C:c06c 29 03 AND #$03
.C:c06e 09 D8 ORA #$D8
.C:c070 85 D2 STA $D2
.C:c072 B1 D1 LDA ($D1),Y ; load color of character
.C:c074 85 02 STA $02 ; and store to $2
.C:c076 A9 02 LDA #$02 ; check which charset is active
.C:c078 18 CLC
.C:c079 2D 18 D0 AND $D018
.C:c07c 0A ASL A ; and calculate offset
.C:c07d 0A ASL A
.C:c07e 65 FC ADC $FC ; add to (character code * 8)
.C:c080 29 0F AND #$0F
.C:c082 09 D0 ORA #$D0 ; and add offset to character ROM
.C:c084 85 FC STA $FC
.C:c086 A9 33 LDA #$33 ; bank in character ROM
.C:c088 85 01 STA $01
.C:c08a A5 FD LDA $FD ; load y coordinate
.C:c08c 29 07 AND #$07 ; mask pixel-row number
.C:c08e A8 TAY
.C:c08f B1 FB LDA ($FB),Y ; load pixel row from character ROM
.C:c091 A2 37 LDX #$37 ; bank out character ROM
.C:c093 86 01 STX $01
.C:c095 25 FE AND $FE ; apply pixel mask
.C:c097 D0 05 BNE .pixelcol ; not 0 -> pixel is set
.C:c099 AD 21 D0 LDA $D021 ; otherwise load background color
.C:c09c 85 02 STA $02 ; and store to $2
.C:c09e .pixelcol:
.C:c09e A6 D6 LDX $D6 ; restore screen row pointer for
.C:c0a0 20 F0 E9 JSR $E9F0 ; current cursor position
.C:c0a3 A5 02 LDA $02 ; load color
.C:c0a5 29 0F AND #$0F ; mask low nibble (only 16 colors)
.C:c0a7 AA TAX ; -> to X
.C:c0a8 BC D2 C0 LDY .red,X ; load encoded output for red
.C:c0ab 20 BC C0 JSR .out2 ; call output without comma
.C:c0ae BC E2 C0 LDY .green,X ; load encoded output for green
.C:c0b1 20 B7 C0 JSR .out1 ; call output with comma
.C:c0b4 BC F2 C0 LDY .blue,X ; load encoded output for blue
.C:c0b7 .out1:
.C:c0b7 A9 2C LDA #$2C ; load ","
.C:c0b9 20 D2 FF JSR $FFD2 ; and output
.C:c0bc .out2:
.C:c0bc 98 TYA ; encoded output to A
.C:c0bd 0A ASL A ; shift top bit to carry
.C:c0be A9 30 LDA #$30 ; load "0"
.C:c0c0 90 02 BCC .firstdig ; carry clear -> to output
.C:c0c2 A9 31 LDA #$31 ; load "1"
.C:c0c4 .firstdig:
.C:c0c4 20 D2 FF JSR $FFD2 ; and output
.C:c0c7 A9 2E LDA #$2E ; load "."
.C:c0c9 20 D2 FF JSR $FFD2 ; and output
.C:c0cc 98 TYA ; encoded output to A
.C:c0cd 29 7F AND #$7F ; mask out top bit
.C:c0cf 4C D2 FF JMP $FFD2 ; to output and exit
.C:c0d2 .red: ; encoded values for red
.C:c0d2 30 B0 35 35 .BYTE $30,$B0,$35,$35 ; ASCII digit ($30-$39) after
.C:c0d6 36 33 32 39 .BYTE $36,$33,$32,$39 ; decimal point, with bit 7
.C:c0da 36 33 38 33 .BYTE $36,$33,$38,$33 ; indicating 0 or 1 before
.C:c0de 35 37 34 37 .BYTE $35,$37,$34,$37 ; decimal point
.C:c0e2 .green: ; encoded values for green
.C:c0e2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0e6 32 37 32 39 .BYTE $32,$37,$32,$39
.C:c0ea 33 32 34 33 .BYTE $33,$32,$34,$33
.C:c0ee 35 B0 34 37 .BYTE $35,$B0,$34,$37
.C:c0f2 .blue: ; encoded values for blue
.C:c0f2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0f6 36 33 36 34 .BYTE $36,$33,$36,$34
.C:c0fa 32 30 34 33 .BYTE $32,$30,$34,$33
.C:c0fe 35 36 39 37 .BYTE $35,$36,$39,$37
.C:c102 .buf: ; buffer for input ("stdin")
6502 machine code (C64), 280 260 bytes
00 C0 A2 00 20 CF FF C9 0D 9D 02 C1 F0 03 E8 D0 F3 20 D2 FF A5 7A 85 FB A5 7B
85 FC A9 01 85 7A A9 C1 85 7B 20 73 00 20 6B A9 A5 14 85 FD A5 15 85 FE 20 73
00 20 6B A9 A5 FB 85 7A A5 FC 85 7B A5 14 4A 4A 4A AA 20 F0 E9 A5 FD 46 FE 6A
4A 4A A8 A5 14 29 07 AA A9 00 38 6A CA 10 FC 85 FE B1 D1 0A 26 FC 0A 26 FC 0A
26 FC 85 FB A5 D2 29 03 09 D8 85 D2 B1 D1 85 02 A9 02 18 2D 18 D0 0A 0A 65 FC
29 0F 09 D0 85 FC A9 33 85 01 A5 FD 29 07 A8 B1 FB A2 37 86 01 25 FE D0 05 AD
21 D0 85 02 A6 D6 20 F0 E9 A5 02 29 0F AA BC D2 C0 20 BC C0 BC E2 C0 20 B7 C0
BC F2 C0 A9 2C 20 D2 FF 98 0A A9 30 90 02 A9 31 20 D2 FF A9 2E 20 D2 FF 98 29
7F 4C D2 FF 30 B0 35 35 36 33 32 39 36 33 38 33 35 37 34 37 30 B0 32 38 32 37
32 39 33 32 34 33 35 B0 34 37 30 B0 32 38 36 33 36 34 32 30 34 33 35 36 39 37
I expected this to be possible in a lot fewer bytes, but unfortunately... well, since I finished it, posting it now anyways. At least, the restrictive format helped with one thing: Something similar to stdin
(input from a current device) only exists on the C64 in text mode, because the OS only supports this mode -- so no need to consider other modes of the graphics chip.
Note on the output of the color values: the C64 graphics chip doesn't use RGB colors but directly generates a video signal with YUV colors, with a fixed 16 colors palette. I used rounded values of the colodore conversion to RGB with "default" monitor settings here.
-20 bytes: better output routine, encoding the 3 character output per color channel in a single byte.
Regarding the comment: It's theoretically possible to use even multicolor character mode of the VIC with the stock C64 OS, but it requires a custom font that's actually legible with only 4 horizontal pixels of doubled width. Not entirely impossible, but very unlikely. Likewise, extended color mode (or extended background mode, which is the same) could be used with the C64 OS, but requires to reconfigure the graphics chip directly. I opt to ignore all these possibilities in the sense of code golfing here: It's not the standard environment you find on a Commodore 64 with running stock OS. What's possible with stock OS is switching between two builtin fonts (shift + commodore key), the program does account for that.
Online demo
Usage: SYS49152
to start.
Commented disassembly:
00 C0 .WORD $C000 ; load address
.C:c000 A2 00 LDX #$00 ; loop index for input
.C:c002 .input:
.C:c002 20 CF FF JSR $FFCF ; character from input device
.C:c005 C9 0D CMP #$0D ; compare with enter
.C:c007 9D 16 C1 STA .buf,X ; store to buffer
.C:c00a F0 03 BEQ .parse ; was enter -> start parsing
.C:c00c E8 INX ; next character
.C:c00d D0 F3 BNE .input ; and repeat input loop
.C:c00f .parse:
.C:c00f 20 D2 FF JSR $FFD2 ; output the enter character
.C:c012 A5 7A LDA $7A ; save pointer of BASIC parser
.C:c014 85 FB STA $FB
.C:c016 A5 7B LDA $7B
.C:c018 85 FC STA $FC
.C:c01a A9 15 LDA #$15 ; set pointer of BASIC parser to
.C:c01c 85 7A STA $7A ; buffer-1
.C:c01e A9 C1 LDA #$C1
.C:c020 85 7B STA $7B
.C:c022 20 73 00 JSR $0073 ; get next character
.C:c025 20 6B A9 JSR $A96B ; BASIC routine to parse number
.C:c028 A5 14 LDA $14 ; lowbyte of parsed number to $fd
.C:c02a 85 FD STA $FD
.C:c02c A5 15 LDA $15 ; highbyte to $fe
.C:c02e 85 FE STA $FE
.C:c030 20 73 00 JSR $0073 ; get next character
.C:c033 20 6B A9 JSR $A96B ; parse as number ...
.C:c036 A5 FB LDA $FB ; restore pointer of BASIC parser
.C:c038 85 7A STA $7A
.C:c03a A5 FC LDA $FC
.C:c03c 85 7B STA $7B
.C:c03e A5 14 LDA $14 ; load y coordinate
.C:c040 4A LSR A ; divide by 8 for character row
.C:c041 4A LSR A
.C:c042 4A LSR A
.C:c043 AA TAX ; -> to X
.C:c044 20 F0 E9 JSR $E9F0 ; set pointer to character row
.C:c047 A5 FD LDA $FD ; divide x coordinate by 8
.C:c049 46 FE LSR $FE
.C:c04b 6A ROR A
.C:c04c 4A LSR A
.C:c04d 4A LSR A
.C:c04e A8 TAY ; -> to Y
.C:c04f A5 14 LDA $14 ; load y coordinate
.C:c051 29 07 AND #$07 ; mask pixel position in character
.C:c053 AA TAX ; -> to X
.C:c054 A9 00 LDA #$00 ; initialize pixel mask to 0
.C:c056 38 SEC ; set carry for bit to shift in
.C:c057 .bitnum:
.C:c057 6A ROR A ; shift bit in mask
.C:c058 CA DEX ; and repeat until
.C:c059 10 FC BPL .bitnum ; in correct position
.C:c05b 85 FE STA $FE ; store pixel mask to $fe
.C:c05d B1 D1 LDA ($D1),Y ; load character code
.C:c05f 0A ASL A ; multiply by 8
.C:c060 26 FC ROL $FC
.C:c062 0A ASL A
.C:c063 26 FC ROL $FC
.C:c065 0A ASL A
.C:c066 26 FC ROL $FC
.C:c068 85 FB STA $FB ; and store to $fb/$fc
.C:c06a A5 D2 LDA $D2 ; move pointer to position in color RAM
.C:c06c 29 03 AND #$03
.C:c06e 09 D8 ORA #$D8
.C:c070 85 D2 STA $D2
.C:c072 B1 D1 LDA ($D1),Y ; load color of character
.C:c074 85 02 STA $02 ; and store to $2
.C:c076 A9 02 LDA #$02 ; check which charset is active
.C:c078 18 CLC
.C:c079 2D 18 D0 AND $D018
.C:c07c 0A ASL A ; and calculate offset
.C:c07d 0A ASL A
.C:c07e 65 FC ADC $FC ; add to (character code * 8)
.C:c080 29 0F AND #$0F
.C:c082 09 D0 ORA #$D0 ; and add offset to character ROM
.C:c084 85 FC STA $FC
.C:c086 A9 33 LDA #$33 ; bank in character ROM
.C:c088 85 01 STA $01
.C:c08a A5 FD LDA $FD ; load y coordinate
.C:c08c 29 07 AND #$07 ; mask pixel-row number
.C:c08e A8 TAY
.C:c08f B1 FB LDA ($FB),Y ; load pixel row from character ROM
.C:c091 A2 37 LDX #$37 ; bank out character ROM
.C:c093 86 01 STX $01
.C:c095 25 FE AND $FE ; apply pixel mask
.C:c097 D0 05 BNE .pixelcol ; not 0 -> pixel is set
.C:c099 AD 21 D0 LDA $D021 ; otherwise load background color
.C:c09c 85 02 STA $02 ; and store to $2
.C:c09e .pixelcol:
.C:c09e A6 D6 LDX $D6 ; restore screen row pointer for
.C:c0a0 20 F0 E9 JSR $E9F0 ; current cursor position
.C:c0a3 A5 02 LDA $02 ; load color
.C:c0a5 29 0F AND #$0F ; mask low nibble (only 16 colors)
.C:c0a7 AA TAX ; -> to X
.C:c0a8 BC D2 C0 LDY .red,X ; load encoded output for red
.C:c0ab 20 BC C0 JSR .out2 ; call output without comma
.C:c0ae BC E2 C0 LDY .green,X ; load encoded output for green
.C:c0b1 20 B7 C0 JSR .out1 ; call output with comma
.C:c0b4 BC F2 C0 LDY .blue,X ; load encoded output for blue
.C:c0b7 .out1:
.C:c0b7 A9 2C LDA #$2C ; load ","
.C:c0b9 20 D2 FF JSR $FFD2 ; and output
.C:c0bc .out2:
.C:c0bc 98 TYA ; encoded output to A
.C:c0bd 0A ASL A ; shift top bit to carry
.C:c0be A9 30 LDA #$30 ; load "0"
.C:c0c0 90 02 BCC .firstdig ; carry clear -> to output
.C:c0c2 A9 31 LDA #$31 ; load "1"
.C:c0c4 .firstdig:
.C:c0c4 20 D2 FF JSR $FFD2 ; and output
.C:c0c7 A9 2E LDA #$2E ; load "."
.C:c0c9 20 D2 FF JSR $FFD2 ; and output
.C:c0cc 98 TYA ; encoded output to A
.C:c0cd 29 7F AND #$7F ; mask out top bit
.C:c0cf 4C D2 FF JMP $FFD2 ; to output and exit
.C:c0d2 .red: ; encoded values for red
.C:c0d2 30 B0 35 35 .BYTE $30,$B0,$35,$35 ; ASCII digit ($30-$39) after
.C:c0d6 36 33 32 39 .BYTE $36,$33,$32,$39 ; decimal point, with bit 7
.C:c0da 36 33 38 33 .BYTE $36,$33,$38,$33 ; indicating 0 or 1 before
.C:c0de 35 37 34 37 .BYTE $35,$37,$34,$37 ; decimal point
.C:c0e2 .green: ; encoded values for green
.C:c0e2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0e6 32 37 32 39 .BYTE $32,$37,$32,$39
.C:c0ea 33 32 34 33 .BYTE $33,$32,$34,$33
.C:c0ee 35 B0 34 37 .BYTE $35,$B0,$34,$37
.C:c0f2 .blue: ; encoded values for blue
.C:c0f2 30 B0 32 38 .BYTE $30,$B0,$32,$38 ; ...
.C:c0f6 36 33 36 34 .BYTE $36,$33,$36,$34
.C:c0fa 32 30 34 33 .BYTE $32,$30,$34,$33
.C:c0fe 35 36 39 37 .BYTE $35,$36,$39,$37
.C:c102 .buf: ; buffer for input ("stdin")
edited 17 hours ago
answered yesterday
Felix Palmen
3,221525
3,221525
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that withcc65
.. quite possible :) but at least, the compiled code will be larger :)
– Felix Palmen
4 hours ago
add a comment |
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that withcc65
.. quite possible :) but at least, the compiled code will be larger :)
– Felix Palmen
4 hours ago
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
This doesn't seem to support multicolor mode.
– nwellnhof
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
Of course not, it works with the original OS. Technically, this OS works in any VIC mode, but there will be only garbage on the screen, so it isn't usable. But your comment made me remember "extended color mode", which works quite well with stock OS ... dammit
– Felix Palmen
yesterday
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
@nwellnhof added some reasoning about it, I hope this is agreeable.
– Felix Palmen
17 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
Makes one wonder if writing this in C would be shorter?
– Rogem
7 hours ago
@Rogem could try that with
cc65
.. quite possible :) but at least, the compiled code will be larger :)– Felix Palmen
4 hours ago
@Rogem could try that with
cc65
.. quite possible :) but at least, the compiled code will be larger :)– Felix Palmen
4 hours ago
add a comment |
up vote
4
down vote
TI-BASIC (TI-83/84+), 20 bytes
Since the screen is black and white, we just need to test if the specified pixel is on or off and map that to black or white RGB. Also, the pixels are only accessible through row and column in that order, so that's why the coordinates are reversed.
Prompt X,Y
255
Ans-{Ans,Ans,Ans}pxl-Test(Y,X
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
add a comment |
up vote
4
down vote
TI-BASIC (TI-83/84+), 20 bytes
Since the screen is black and white, we just need to test if the specified pixel is on or off and map that to black or white RGB. Also, the pixels are only accessible through row and column in that order, so that's why the coordinates are reversed.
Prompt X,Y
255
Ans-{Ans,Ans,Ans}pxl-Test(Y,X
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
add a comment |
up vote
4
down vote
up vote
4
down vote
TI-BASIC (TI-83/84+), 20 bytes
Since the screen is black and white, we just need to test if the specified pixel is on or off and map that to black or white RGB. Also, the pixels are only accessible through row and column in that order, so that's why the coordinates are reversed.
Prompt X,Y
255
Ans-{Ans,Ans,Ans}pxl-Test(Y,X
TI-BASIC (TI-83/84+), 20 bytes
Since the screen is black and white, we just need to test if the specified pixel is on or off and map that to black or white RGB. Also, the pixels are only accessible through row and column in that order, so that's why the coordinates are reversed.
Prompt X,Y
255
Ans-{Ans,Ans,Ans}pxl-Test(Y,X
edited 7 hours ago
answered yesterday
kamoroso94
69049
69049
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
add a comment |
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
Note that this doesn't work on color TI calculators (Like the TI-84 Plus CE).
– pizzapants184
yesterday
add a comment |
up vote
1
down vote
Mathematica, 69 Bytes
Just the function is 34 bytes.
CurrentScreenImage~PixelValue~#&
Takes input in the form {x,y}.
The image is the merging of images on all monitors. If you want a particular screen, use the integer index - e.g. CurrentScreenImage[1]
Full program exactly as specified is 69 Bytes
CurrentScreenImage~PixelValue~ToExpression["{"<>InputString<>"}"]
add a comment |
up vote
1
down vote
Mathematica, 69 Bytes
Just the function is 34 bytes.
CurrentScreenImage~PixelValue~#&
Takes input in the form {x,y}.
The image is the merging of images on all monitors. If you want a particular screen, use the integer index - e.g. CurrentScreenImage[1]
Full program exactly as specified is 69 Bytes
CurrentScreenImage~PixelValue~ToExpression["{"<>InputString<>"}"]
add a comment |
up vote
1
down vote
up vote
1
down vote
Mathematica, 69 Bytes
Just the function is 34 bytes.
CurrentScreenImage~PixelValue~#&
Takes input in the form {x,y}.
The image is the merging of images on all monitors. If you want a particular screen, use the integer index - e.g. CurrentScreenImage[1]
Full program exactly as specified is 69 Bytes
CurrentScreenImage~PixelValue~ToExpression["{"<>InputString<>"}"]
Mathematica, 69 Bytes
Just the function is 34 bytes.
CurrentScreenImage~PixelValue~#&
Takes input in the form {x,y}.
The image is the merging of images on all monitors. If you want a particular screen, use the integer index - e.g. CurrentScreenImage[1]
Full program exactly as specified is 69 Bytes
CurrentScreenImage~PixelValue~ToExpression["{"<>InputString<>"}"]
edited yesterday
answered yesterday
Kelly Lowder
2,978416
2,978416
add a comment |
add a comment |
up vote
1
down vote
bash, 103 / 86 bytes
With a strict interpretation of the spec (input from STDIN and output on STDOUT are both comma-separated):
read x
import -window root -crop 1x1+${x/,/+} ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n","$3/n","$4/n}'
With a looser input format (plus-separated input as a command line argument, space-separated output:
import -window root -crop 1x1+$1 ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n,$3/n,$4/n}'
Depends on imagemagick, awk, and coreutils.
add a comment |
up vote
1
down vote
bash, 103 / 86 bytes
With a strict interpretation of the spec (input from STDIN and output on STDOUT are both comma-separated):
read x
import -window root -crop 1x1+${x/,/+} ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n","$3/n","$4/n}'
With a looser input format (plus-separated input as a command line argument, space-separated output:
import -window root -crop 1x1+$1 ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n,$3/n,$4/n}'
Depends on imagemagick, awk, and coreutils.
add a comment |
up vote
1
down vote
up vote
1
down vote
bash, 103 / 86 bytes
With a strict interpretation of the spec (input from STDIN and output on STDOUT are both comma-separated):
read x
import -window root -crop 1x1+${x/,/+} ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n","$3/n","$4/n}'
With a looser input format (plus-separated input as a command line argument, space-separated output:
import -window root -crop 1x1+$1 ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n,$3/n,$4/n}'
Depends on imagemagick, awk, and coreutils.
bash, 103 / 86 bytes
With a strict interpretation of the spec (input from STDIN and output on STDOUT are both comma-separated):
read x
import -window root -crop 1x1+${x/,/+} ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n","$3/n","$4/n}'
With a looser input format (plus-separated input as a command line argument, space-separated output:
import -window root -crop 1x1+$1 ppm:-|od -dj13|awk 'NR<2{n=2^16;print$2/n,$3/n,$4/n}'
Depends on imagemagick, awk, and coreutils.
answered yesterday
Doorknob♦
53.9k16112341
53.9k16112341
add a comment |
add a comment |
up vote
1
down vote
AutoHotKey, 113 bytes
CoordMode,Pixel
InputBox,x
InputBox,y
PixelGetColor,c,x,y
MsgBox % c&255 . "," . c>>8&255 . "," . c>>16&255
Use dialog boxes instead of stdin/stdout.
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
add a comment |
up vote
1
down vote
AutoHotKey, 113 bytes
CoordMode,Pixel
InputBox,x
InputBox,y
PixelGetColor,c,x,y
MsgBox % c&255 . "," . c>>8&255 . "," . c>>16&255
Use dialog boxes instead of stdin/stdout.
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
AutoHotKey, 113 bytes
CoordMode,Pixel
InputBox,x
InputBox,y
PixelGetColor,c,x,y
MsgBox % c&255 . "," . c>>8&255 . "," . c>>16&255
Use dialog boxes instead of stdin/stdout.
AutoHotKey, 113 bytes
CoordMode,Pixel
InputBox,x
InputBox,y
PixelGetColor,c,x,y
MsgBox % c&255 . "," . c>>8&255 . "," . c>>16&255
Use dialog boxes instead of stdin/stdout.
answered 20 hours ago
tsh
8,13511446
8,13511446
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
add a comment |
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
TIL that AutoHotKey is Turing complete
– Rogem
7 hours ago
add a comment |
up vote
0
down vote
Python 2 + PIL library, 96 91 bytes
import PIL.ImageGrab as i
print','.join('%.1f'%(x/255.)for x in i.grab().getpixel(input()))
Implements the specification literally as requested. Windows only though - doesn't work on Linux, and produces extra output (alpha value) on Mac.
from PIL import ImageGrab as i
→from PIL import*
,i.grab
→ImageGrab.grab
.
– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the__import__('...')
way work.
– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
add a comment |
up vote
0
down vote
Python 2 + PIL library, 96 91 bytes
import PIL.ImageGrab as i
print','.join('%.1f'%(x/255.)for x in i.grab().getpixel(input()))
Implements the specification literally as requested. Windows only though - doesn't work on Linux, and produces extra output (alpha value) on Mac.
from PIL import ImageGrab as i
→from PIL import*
,i.grab
→ImageGrab.grab
.
– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the__import__('...')
way work.
– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
add a comment |
up vote
0
down vote
up vote
0
down vote
Python 2 + PIL library, 96 91 bytes
import PIL.ImageGrab as i
print','.join('%.1f'%(x/255.)for x in i.grab().getpixel(input()))
Implements the specification literally as requested. Windows only though - doesn't work on Linux, and produces extra output (alpha value) on Mac.
Python 2 + PIL library, 96 91 bytes
import PIL.ImageGrab as i
print','.join('%.1f'%(x/255.)for x in i.grab().getpixel(input()))
Implements the specification literally as requested. Windows only though - doesn't work on Linux, and produces extra output (alpha value) on Mac.
edited yesterday
answered yesterday
Kirill L.
3,3761118
3,3761118
from PIL import ImageGrab as i
→from PIL import*
,i.grab
→ImageGrab.grab
.
– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the__import__('...')
way work.
– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
add a comment |
from PIL import ImageGrab as i
→from PIL import*
,i.grab
→ImageGrab.grab
.
– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the__import__('...')
way work.
– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
from PIL import ImageGrab as i
→ from PIL import*
, i.grab
→ ImageGrab.grab
.– Erik the Outgolfer
yesterday
from PIL import ImageGrab as i
→ from PIL import*
, i.grab
→ ImageGrab.grab
.– Erik the Outgolfer
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@Erik this does not for PIL. PIL only imports the names explicitly listed.
– ovs
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the
__import__('...')
way work.– Kirill L.
yesterday
@ovs, Ah, that's probably the problem why I couldn't also make the
__import__('...')
way work.– Kirill L.
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
Note that this outputs an alpha value on macOS, so it only adheres to the spec on Windows.
– ovs
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
@ovs, OK, good to know, thanks.
– Kirill L.
yesterday
add a comment |
up vote
0
down vote
Bash + coreutils + scrot + netpbm, 90 bytes
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$2+$1)))>f;od -t u1 -N3 -An;)
Loose I/O version
Takes x
and y
as separate command-line arguments.
Prints r
, g
, b
as ints from 0-255 on separate lines
Bash + coreutils + scrot + netpbm + bc + sed, 172 bytes
IFS=, read x y
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$y+$x)))>f;od -vw1 -tu1 -N3 -An|while read p;do bc<<<"scale=2;$p/$m"|tr 'n' ,;done;)|sed s/,$//
Strict I/O version
Input on stdin as x,y
Output on stdout as r.rr,g.gg,b.bb
(no newline).
Note that the output must consist of floats in the range0-1
.
– Shaggy
3 hours ago
add a comment |
up vote
0
down vote
Bash + coreutils + scrot + netpbm, 90 bytes
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$2+$1)))>f;od -t u1 -N3 -An;)
Loose I/O version
Takes x
and y
as separate command-line arguments.
Prints r
, g
, b
as ints from 0-255 on separate lines
Bash + coreutils + scrot + netpbm + bc + sed, 172 bytes
IFS=, read x y
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$y+$x)))>f;od -vw1 -tu1 -N3 -An|while read p;do bc<<<"scale=2;$p/$m"|tr 'n' ,;done;)|sed s/,$//
Strict I/O version
Input on stdin as x,y
Output on stdout as r.rr,g.gg,b.bb
(no newline).
Note that the output must consist of floats in the range0-1
.
– Shaggy
3 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Bash + coreutils + scrot + netpbm, 90 bytes
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$2+$1)))>f;od -t u1 -N3 -An;)
Loose I/O version
Takes x
and y
as separate command-line arguments.
Prints r
, g
, b
as ints from 0-255 on separate lines
Bash + coreutils + scrot + netpbm + bc + sed, 172 bytes
IFS=, read x y
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$y+$x)))>f;od -vw1 -tu1 -N3 -An|while read p;do bc<<<"scale=2;$p/$m"|tr 'n' ,;done;)|sed s/,$//
Strict I/O version
Input on stdin as x,y
Output on stdout as r.rr,g.gg,b.bb
(no newline).
Bash + coreutils + scrot + netpbm, 90 bytes
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$2+$1)))>f;od -t u1 -N3 -An;)
Loose I/O version
Takes x
and y
as separate command-line arguments.
Prints r
, g
, b
as ints from 0-255 on separate lines
Bash + coreutils + scrot + netpbm + bc + sed, 172 bytes
IFS=, read x y
scrot -e'pngtopnm $f'|(read a;read w h;read m;head -c$((3*(w*$y+$x)))>f;od -vw1 -tu1 -N3 -An|while read p;do bc<<<"scale=2;$p/$m"|tr 'n' ,;done;)|sed s/,$//
Strict I/O version
Input on stdin as x,y
Output on stdout as r.rr,g.gg,b.bb
(no newline).
answered 23 hours ago
pizzapants184
2,584716
2,584716
Note that the output must consist of floats in the range0-1
.
– Shaggy
3 hours ago
add a comment |
Note that the output must consist of floats in the range0-1
.
– Shaggy
3 hours ago
Note that the output must consist of floats in the range
0-1
.– Shaggy
3 hours ago
Note that the output must consist of floats in the range
0-1
.– Shaggy
3 hours ago
add a comment |
up vote
0
down vote
TI-Nspire assembly - 112 bytes
50 52 47 00 30 40 2D E9 FF FF FF FA 00 F0 17 F8
04 1C 00 F0 14 F8 85 00 2D 18 AD 01 2D 19 6C 00
C0 21 09 06 09 69 08 5B 3F 25 42 09 2A 40 1F 25
03 1C 2B 40 C1 0A 29 40 0A A0 0A DF 30 BD 00 20
0A 23 07 49 10 25 8A 69 2A 42 FC D1 0A 68 FF 25
2A 40 30 3A 0B DB 85 00 2D 18 6D 00 A8 18 F1 E7
00 00 02 90 25 64 2C 25 64 2C 25 64 0A 00 70 47
This program outputs integers in the range 0-31 for R and B and 0-63 for G, because the device natively uses a RGB565 framebuffer. It uses serial for input and output.
Source:
.string "PRG"
push {r4, r5, lr}
blx main
.thumb
main:
@ read x and y from serial into r4 and r0
bl read_int
mov r4, r0
bl read_int
@ turn x and y into framebuffer offset
@ r4 = ((r0 * 320) + r4) * 2
lsl r5, r0, #2
add r5, r0
lsl r5, #6
add r5, r4
lsl r4, r5, #1
@ load pixel from framebuffer
@ r0 = ((uint16_t **)0xc0000000)[0x10][r4 / 2]
mov r1, #0xC0
lsl r1, #24
ldr r1, [r1, #0x10]
ldrh r0, [r1, r4]
@ unpack RGB565 value into r1, r2, r3
mov r5, #0x3f
lsr r2, r0, #5
and r2, r5
mov r5, #0x1f
mov r3, r0
and r3, r5
lsr r1, r0, #11
and r1, r5
@ call printf
adr r0, fmt
swi #10
@ return
pop {r4, r5, pc}
@ subroutine to read an integer from serial
read_int:
mov r0, #0
mov r3, #10
ldr r1, serial_base
@ loop until characters come in on serial
2:
mov r5, #(1<<4)
1:
ldr r2, [r1, #0x18]
tst r2, r5
bne 1b
@ read character from serial and mask out status bits
ldr r2, [r1]
mov r5, #0xff
and r2, r5
@ subtract 48 ('0') from byte; if result is negative, return
sub r2, #48
blt 1f
@ multiply existing numbers by 10 and add new number to them
lsl r5, r0, #2
add r5, r0
lsl r5, #1
add r0, r5, r2
b 2b
serial_base:.word 0x90020000
fmt:.string "%d,%d,%dn"
@ this instruction is over here because serial_base and fmt need to be word-aligned
1:bx lr
add a comment |
up vote
0
down vote
TI-Nspire assembly - 112 bytes
50 52 47 00 30 40 2D E9 FF FF FF FA 00 F0 17 F8
04 1C 00 F0 14 F8 85 00 2D 18 AD 01 2D 19 6C 00
C0 21 09 06 09 69 08 5B 3F 25 42 09 2A 40 1F 25
03 1C 2B 40 C1 0A 29 40 0A A0 0A DF 30 BD 00 20
0A 23 07 49 10 25 8A 69 2A 42 FC D1 0A 68 FF 25
2A 40 30 3A 0B DB 85 00 2D 18 6D 00 A8 18 F1 E7
00 00 02 90 25 64 2C 25 64 2C 25 64 0A 00 70 47
This program outputs integers in the range 0-31 for R and B and 0-63 for G, because the device natively uses a RGB565 framebuffer. It uses serial for input and output.
Source:
.string "PRG"
push {r4, r5, lr}
blx main
.thumb
main:
@ read x and y from serial into r4 and r0
bl read_int
mov r4, r0
bl read_int
@ turn x and y into framebuffer offset
@ r4 = ((r0 * 320) + r4) * 2
lsl r5, r0, #2
add r5, r0
lsl r5, #6
add r5, r4
lsl r4, r5, #1
@ load pixel from framebuffer
@ r0 = ((uint16_t **)0xc0000000)[0x10][r4 / 2]
mov r1, #0xC0
lsl r1, #24
ldr r1, [r1, #0x10]
ldrh r0, [r1, r4]
@ unpack RGB565 value into r1, r2, r3
mov r5, #0x3f
lsr r2, r0, #5
and r2, r5
mov r5, #0x1f
mov r3, r0
and r3, r5
lsr r1, r0, #11
and r1, r5
@ call printf
adr r0, fmt
swi #10
@ return
pop {r4, r5, pc}
@ subroutine to read an integer from serial
read_int:
mov r0, #0
mov r3, #10
ldr r1, serial_base
@ loop until characters come in on serial
2:
mov r5, #(1<<4)
1:
ldr r2, [r1, #0x18]
tst r2, r5
bne 1b
@ read character from serial and mask out status bits
ldr r2, [r1]
mov r5, #0xff
and r2, r5
@ subtract 48 ('0') from byte; if result is negative, return
sub r2, #48
blt 1f
@ multiply existing numbers by 10 and add new number to them
lsl r5, r0, #2
add r5, r0
lsl r5, #1
add r0, r5, r2
b 2b
serial_base:.word 0x90020000
fmt:.string "%d,%d,%dn"
@ this instruction is over here because serial_base and fmt need to be word-aligned
1:bx lr
add a comment |
up vote
0
down vote
up vote
0
down vote
TI-Nspire assembly - 112 bytes
50 52 47 00 30 40 2D E9 FF FF FF FA 00 F0 17 F8
04 1C 00 F0 14 F8 85 00 2D 18 AD 01 2D 19 6C 00
C0 21 09 06 09 69 08 5B 3F 25 42 09 2A 40 1F 25
03 1C 2B 40 C1 0A 29 40 0A A0 0A DF 30 BD 00 20
0A 23 07 49 10 25 8A 69 2A 42 FC D1 0A 68 FF 25
2A 40 30 3A 0B DB 85 00 2D 18 6D 00 A8 18 F1 E7
00 00 02 90 25 64 2C 25 64 2C 25 64 0A 00 70 47
This program outputs integers in the range 0-31 for R and B and 0-63 for G, because the device natively uses a RGB565 framebuffer. It uses serial for input and output.
Source:
.string "PRG"
push {r4, r5, lr}
blx main
.thumb
main:
@ read x and y from serial into r4 and r0
bl read_int
mov r4, r0
bl read_int
@ turn x and y into framebuffer offset
@ r4 = ((r0 * 320) + r4) * 2
lsl r5, r0, #2
add r5, r0
lsl r5, #6
add r5, r4
lsl r4, r5, #1
@ load pixel from framebuffer
@ r0 = ((uint16_t **)0xc0000000)[0x10][r4 / 2]
mov r1, #0xC0
lsl r1, #24
ldr r1, [r1, #0x10]
ldrh r0, [r1, r4]
@ unpack RGB565 value into r1, r2, r3
mov r5, #0x3f
lsr r2, r0, #5
and r2, r5
mov r5, #0x1f
mov r3, r0
and r3, r5
lsr r1, r0, #11
and r1, r5
@ call printf
adr r0, fmt
swi #10
@ return
pop {r4, r5, pc}
@ subroutine to read an integer from serial
read_int:
mov r0, #0
mov r3, #10
ldr r1, serial_base
@ loop until characters come in on serial
2:
mov r5, #(1<<4)
1:
ldr r2, [r1, #0x18]
tst r2, r5
bne 1b
@ read character from serial and mask out status bits
ldr r2, [r1]
mov r5, #0xff
and r2, r5
@ subtract 48 ('0') from byte; if result is negative, return
sub r2, #48
blt 1f
@ multiply existing numbers by 10 and add new number to them
lsl r5, r0, #2
add r5, r0
lsl r5, #1
add r0, r5, r2
b 2b
serial_base:.word 0x90020000
fmt:.string "%d,%d,%dn"
@ this instruction is over here because serial_base and fmt need to be word-aligned
1:bx lr
TI-Nspire assembly - 112 bytes
50 52 47 00 30 40 2D E9 FF FF FF FA 00 F0 17 F8
04 1C 00 F0 14 F8 85 00 2D 18 AD 01 2D 19 6C 00
C0 21 09 06 09 69 08 5B 3F 25 42 09 2A 40 1F 25
03 1C 2B 40 C1 0A 29 40 0A A0 0A DF 30 BD 00 20
0A 23 07 49 10 25 8A 69 2A 42 FC D1 0A 68 FF 25
2A 40 30 3A 0B DB 85 00 2D 18 6D 00 A8 18 F1 E7
00 00 02 90 25 64 2C 25 64 2C 25 64 0A 00 70 47
This program outputs integers in the range 0-31 for R and B and 0-63 for G, because the device natively uses a RGB565 framebuffer. It uses serial for input and output.
Source:
.string "PRG"
push {r4, r5, lr}
blx main
.thumb
main:
@ read x and y from serial into r4 and r0
bl read_int
mov r4, r0
bl read_int
@ turn x and y into framebuffer offset
@ r4 = ((r0 * 320) + r4) * 2
lsl r5, r0, #2
add r5, r0
lsl r5, #6
add r5, r4
lsl r4, r5, #1
@ load pixel from framebuffer
@ r0 = ((uint16_t **)0xc0000000)[0x10][r4 / 2]
mov r1, #0xC0
lsl r1, #24
ldr r1, [r1, #0x10]
ldrh r0, [r1, r4]
@ unpack RGB565 value into r1, r2, r3
mov r5, #0x3f
lsr r2, r0, #5
and r2, r5
mov r5, #0x1f
mov r3, r0
and r3, r5
lsr r1, r0, #11
and r1, r5
@ call printf
adr r0, fmt
swi #10
@ return
pop {r4, r5, pc}
@ subroutine to read an integer from serial
read_int:
mov r0, #0
mov r3, #10
ldr r1, serial_base
@ loop until characters come in on serial
2:
mov r5, #(1<<4)
1:
ldr r2, [r1, #0x18]
tst r2, r5
bne 1b
@ read character from serial and mask out status bits
ldr r2, [r1]
mov r5, #0xff
and r2, r5
@ subtract 48 ('0') from byte; if result is negative, return
sub r2, #48
blt 1f
@ multiply existing numbers by 10 and add new number to them
lsl r5, r0, #2
add r5, r0
lsl r5, #1
add r0, r5, r2
b 2b
serial_base:.word 0x90020000
fmt:.string "%d,%d,%dn"
@ this instruction is over here because serial_base and fmt need to be word-aligned
1:bx lr
edited 3 hours ago
answered 3 hours ago
pbfy0
1313
1313
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177020%2fcolor-of-a-pixel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
17
Why should
RRR
,GGG
, andBBB
be floating points between 0 and 1? Usually they're integers in the range[0,255]
. I would suggest to allow both.– Kevin Cruijssen
yesterday
3
Can we limit our solutions to the window they are being run in, for the benefit of languages that can't access anything on screen outside of that window?
– Shaggy
yesterday
6
If you allow only the 0 — 1 format you should not use
RRR
,GGG
, andBBB
in the spec, butR
,G
,B
– sergiol
yesterday
4
What if we have multiple monitors?
– tsh
yesterday
3
It could be worth to specify that the program can't modify the pixel at the given coordinate. Otherwise it might be more efficient to black out the entire screen and return
0, 0, 0
, defeating the purpose of the challenge.– maxb
16 hours ago