4
\$\begingroup\$

Write a program that takes R, G, and B values as command arguments and then displays that color some way. It doesn't matter how, as long as at least 100 pixels have the color. Shortest code wins.

\$\endgroup\$
  • 5
    \$\begingroup\$ This would be improved a lot if you specified how much of the input colour should be displayed. Is just one pixel OK? I recommend you make it a requirement that the screen/window background colour (or the majority of the screen/window) be displayed in the input colour. Even so, it's rather simple. \$\endgroup\$ – Level River St Jun 23 '14 at 19:24
  • \$\begingroup\$ Is COLOR command of Windows prompt valid? It has only 16 possibilities of color from 0 to F \$\endgroup\$ – sergiol Nov 2 '17 at 22:09
  • \$\begingroup\$ I should win it! \$\endgroup\$ – sergiol Nov 19 '17 at 22:33

11 Answers 11

17
\$\begingroup\$

JavaScript, 25

document.bgColor=prompt()

You'll have to specify the three components in hexadecimal. This is code golf, so no commas or whitespace.

\$\endgroup\$
  • \$\begingroup\$ Hexadecimal: cheeky, but I like it! \$\endgroup\$ – Level River St Jun 23 '14 at 22:46
  • \$\begingroup\$ Wouldn't you need a '#'+, then? \$\endgroup\$ – Doorknob Jun 24 '14 at 23:25
  • 11
    \$\begingroup\$ @Doorknob: HTML is too cool to choke on malformed input: stackoverflow.com/questions/8318911/… \$\endgroup\$ – Blender Jun 24 '14 at 23:56
  • \$\begingroup\$ I should win it! \$\endgroup\$ – sergiol Nov 2 '17 at 21:34
4
\$\begingroup\$

xterm: 18 bytes

xterm -bg \#$1$2$3

Input the color as three 1- or 2-digit hex numbers, e.g., if this command is in the file "xterm.sh", then

./xterm.sh ff 00 00
./xterm.sh f 0 0

opens a terminal window with a red background.

If you prefer, you can omit the blanks between the components:

./xterm.sh f00

The "-bg" option is not available in some old versions of xterm. It's missing from version 271 but present in versions 215, 251, 261, and 297.

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 57 characters

document.body.setAttribute('bgcolor','rgb('+prompt()+')')

Enter the RGB values in the prompt input box, comma separated.

(I feel so dirty, using bgcolor :O )

\$\endgroup\$
  • 3
    \$\begingroup\$ bgcolor? That really brings me back to a simpler time... \$\endgroup\$ – qwr Jun 23 '14 at 19:54
3
\$\begingroup\$

Python 3, 65

from webbrowser import*
open("http://www.colorhexa.com/"+input())

Requires input in hexadecimal form.

If a more human-friendly input format is required, you can accept comma-separated decimal input in 118 characters:

from webbrowser import*
open('http://www.colorhexa.com/'+''.join(hex(int(n))[2:].zfill(2)for n in input().split(',')))
\$\endgroup\$
2
\$\begingroup\$

Bash, 69

read c;set $c;printf '\e]4;1;rgb:'"$1"'/'"$2"'/'"$3"'\e\\\e[31mc\e[m'

Partially stolen from here.

Just input your values in hex (e.g. FF 00 00).

Tested on xterm-256color.

Bash, 43

printf '\e]4;1;rgb:FF/00/00\e\\\e[31mc\e[m'

Here you have to input the values yourself.

Edit: For 100 Pixels+ Just choose font-size infinite :D.

\$\endgroup\$
1
\$\begingroup\$

C# WPF : 54

new Windows(){Background=Color.FromArgb(RGB);}.Show();
\$\endgroup\$
1
\$\begingroup\$

Javascript in PCCG, 65 105

Edit 2 shortened as suggested by @manatwork

Edit Thanks @blender

question.style.background='#'+prompt('R G B').split(' ').join('')

Just for fun

\$\endgroup\$
  • 1
    \$\begingroup\$ You can replace document.getElementById('question') with just question. \$\endgroup\$ – Blender Jun 23 '14 at 22:28
  • \$\begingroup\$ You can assign to the background property. (As a side effect will reset backgroundImage and alike, but not a problem in this case.) \$\endgroup\$ – manatwork Aug 7 '14 at 14:36
1
\$\begingroup\$

Bash/sh + ImageMagick: 28 bytes

display -size 96 xc:\#$1$2$3

Input the color as three 1- or 2-digit hex numbers, with or without whitespace between them.

\$\endgroup\$
  • \$\begingroup\$ I was thinking xsetroot -solid '#xyz', but this is better. (And the effect of xsetroot is only visible if you aren't using a desktop environment that covers the X11 root window with a window showing a background, rather than drawing the background on the root window directly.) \$\endgroup\$ – Peter Cordes Aug 6 '14 at 9:05
  • \$\begingroup\$ xterm is shorter: "xterm -bg \#$1$2$3"; I'll post that as a new answer. \$\endgroup\$ – Glenn Randers-Pehrson Aug 6 '14 at 12:22
1
\$\begingroup\$

Postscript 61 56

Fills the graphics window with the color, components as decimals in the range 0..1.

ARGUMENTS{cvr}forall setrgbcolor clippath fill showpage

Run using ghostscript's -- option which enables argument processing.

gs -- rgb.ps .9 .2 .2
\$\endgroup\$
0
\$\begingroup\$

Java, 215

import java.awt.*;class C{public static void main(String[]a){Frame f=new Frame();f.setSize(9,99);f.setBackground(new Color(Integer.parseInt(a[0]),Integer.parseInt(a[1]),Integer.parseInt(a[2])));f.setVisible(true);}}
\$\endgroup\$
0
\$\begingroup\$

Tcl/Tk, 15

. co -bg #$argv

Tcl/Tk, 22

. configure -bg #$argv

If called with

wish show_color.tcl 6666AA

one gets

enter image description here

Tcl/Tk, 27

grid [canvas .c -bg #$argv]

If called with

wish show_color.tcl 6666AA

one gets

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.