Rainbow
Rainbow is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
- Print out the word 'RAINBOW' to the screen with every character being a different color of the rainbow.
Arturo
loop [
#red "R"
#orange "A"
#yellow "I"
#green "N"
#blue "B"
#indigo "O"
#violet "W"
] [c l] -> prints color c l
BASIC
BASIC256
dim colors$(6)
colors$ = {red, orange, yellow, green, blue, darkblue, cyan}
clg
s$ = "RAINBOW"
for i = 1 to length(s$)
color colors$[i-1]
text i*8,150, mid(s$,i,1)
next i
Chipmunk Basic
100 graphics 0
110 dim colors(6,6,6)
120 data 255,0,0,255,128,0,255,255,0,0,255,0,0,0,255,75,0,130,128,0,255
130 s$ = "RAINBOW"
140 for i = 1 to 7
150 read a,b,c
160 graphics color a,b,c
170 graphics moveto i*10,10
180 graphics text mid$(s$,i,1);
190 next i
200 end
FreeBASIC
Dim As Integer colors(6, 2) => { _
{255, 0, 0}, _ ' red
{255, 128, 0}, _ ' orange
{255, 255, 0}, _ ' yellow
{ 0, 255, 0}, _ ' green
{ 0, 0, 255}, _ ' blue
{75, 0, 130}, _ ' indigo
{128, 0, 255}} _ ' violet
Cls
Dim As String s = "RAINBOW"
For i As Byte = 1 To Len(s)
Color Rgb(colors(i,2),colors(i,2),colors(i,2))
Print Mid(s, i, 1);
Next i
Sleep
GW-BASIC
10 CLS
20 DATA 4,6,14,2,1,11,13
30 S$ = "RAINBOW"
40 FOR I = 1 TO 7
50 READ C
60 COLOR C
70 PRINT MID$(S$, I, 1);
80 NEXT I
90 END
True BASIC
CLEAR
DATA 4, 6, 14, 2, 1, 11, 13
LET s$ = "RAINBOW"
FOR i = 1 to 7
READ c
SET COLOR c
PRINT (s$)[i:i+1-1];
NEXT i
END
C
#include <stdio.h>
int main() {
int i, clrs[7][3] = {
{255, 0, 0}, // red
{255, 128, 0}, // orange
{255, 255, 0}, // yellow
{ 0, 255, 0}, // green
{ 0, 0, 255}, // blue
{ 75, 0, 130}, // indigo
{128, 0, 255}, // violet
};
const char *s = "RAINBOW";
for (i = 0; i < 7; ++i) {
printf("\x1B[38;2;%d;%d;%dm%c", clrs[i][0], clrs[i][1], clrs[i][2], s[i]);
}
printf("\n");
return 0;
}
- Output:
RAINBOW
Dart
import 'dart:io';
void main() {
const rainbow = 'RAINBOW';
const spectrum = [
[255, 0, 0], // red
[255, 165, 0], // orange
[255, 255, 0], // yellow
[0, 128, 0], // green
[0, 0, 255], // blue
[75, 0, 130], // indigo
[238, 130, 238], // violet
];
for (int i = 0; i < rainbow.length; i++) {
final red = spectrum[i][0];
final green = spectrum[i][1];
final blue = spectrum[i][2];
stdout.write('\x1B[38;2;${red};${green};${blue}m${rainbow[i]}\x1B[0m');
}
}
EasyLang
s$ = "RAINBOW"
cols[] = [ 900 950 990 090 009 306 509 ]
textsize 15
background 000
clear
for i to 7
color cols[i]
move i * 11.5 60
text substr s$ i 1
.
Factor
USING: colors grouping hashtables io.styles qw sequences ui
ui.gadgets.panes ;
"RAINBOW" 1 group
qw{ red orange yellow green blue indigo violet } [
[ named-color foreground associate format ] 2each
] make-pane "Rainbow" open-window
- Output:
Go
package main
import "fmt"
func main() {
clrs := [7][3]int{
{255, 0, 0}, // red
{255, 128, 0}, // orange
{255, 255, 0}, // yellow
{0, 255, 0}, // green
{0, 0, 255}, // blue
{75, 0, 130}, // indigo
{128, 0, 255}, // violet
}
s := "RAINBOW"
for i := 0; i < 7; i++ {
fmt.Printf("\x1B[38;2;%d;%d;%dm%c", clrs[i][0], clrs[i][1], clrs[i][2], s[i])
}
fmt.Println()
}
- Output:
As C example.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Text in Multiple Colors (No Spaces)</title>
</head>
<body>
<p>
<span style="color: red">R</span><span style="color: orange">A</span><span style="color: yellow">I</span><span style="color: green">N</span><span style="color: blue">B</span><span style="color: indigo">O</span><span style="color: violet">W</span>
</p>
</body>
</html>
jq
The following assumes the terminal supports the ANSI RGB codes, and has been tested using iTerm2.
def colors: [
[255, 0, 0], # red
[255, 128, 0], # orange
[255, 255, 0], # yellow
[ 0, 255, 0], # green
[ 0, 0, 255], # blue
[ 75, 0, 130], # indigo
[128, 0, 255] # violet
];
def rainbow($s):
# ;38 is the extended foreground color code
# ;2 indicates RGB digits follow
def e: "\u001B"; # ESCAPE
reduce range(0; $s|length) as $j ("";
($j % 7) as $i
| . + "\(e)[38;2;\(colors[$i][0]);\(colors[$i][1]);\(colors[$i][2])m\($s[$i:$i+1])" )
+ "\(e)[0m";
rainbow("RAINBOW")
Invocation: jq -nr -f rainbow.jq
Julia
using Crayons
for (letter, color) in [("R", crayon"red"), ("A", crayon"ff7f00"),
("I", crayon"yellow"), ("N", crayon"green"),
("B", crayon"blue"), ("O", crayon"4b0082"), ("W", crayon"7f00ff")]
print(color, letter, " ");
end
Perl
#!/usr/bin/perl
use strict; # https://rosettacode.org/wiki/Rainbow
use warnings;
use List::AllUtils qw( zip_by );
print zip_by { "\e[38;2;$_[1]m$_[0]\e[m" } [ split //, 'RAINBOW' ],
[qw( 255;0;0 255;128;0 255;255;0 0;255;0 0;0;255 75;0;130 128;0;255 )];
print "\n";
Phix
with javascript_semantics
requires("1.0.6")
include xpGUI.e
constant rainbow = {{"R",XPG_RED},
{"A",XPG_ORANGE},
{"I",XPG_YELLOW},
{"N",XPG_GREEN},
{"B",XPG_BLUE},
{"O",XPG_INDIGO},
{"W",XPG_PURPLE}}
function get_data(integer idx)
return iff(idx<=0?1:rainbow)
end function
gdx list = gList(get_data,"=BGCLR",{#F0F0F0}),
vbox = gVbox({list},`MARGIN=10`),
dialog = gDialog(vbox,"Rainbow",`SIZE=240x62`)
gShow(dialog)
gMainLoop()
- Output:
Python
Colored
from colored import Fore, Style
red: str = f'{Fore.rgb(255, 0, 0)}'
orange: str = f'{Fore.rgb(255, 128, 0)}'
yellow: str = f'{Fore.rgb(255, 255, 0)}'
green: str = f'{Fore.rgb(0, 255, 0)}'
blue: str = f'{Fore.rgb(0, 0, 255)}'
indigo: str = f'{Fore.rgb(75, 0, 130)}'
violet: str = f'{Fore.rgb(128, 0, 255)}'
print(f'{red}R{Style.reset}' + f'{orange}A{Style.reset}' + f'{yellow}I{Style.reset}' + f'{green}N{Style.reset}' + f'{blue}B{Style.reset}' + f'{indigo}O{Style.reset}' + f'{violet}W{Style.reset}')
- Output:
RAINBOW
Quackery
[ char m join
$ /
print(("\033["+string_from_stack()),end='')
/ python ] is colour ( $ --> )
[ $ "31" colour ] is red ( --> )
[ $ "38;5;208" colour ] is orange ( --> )
[ $ "33" colour ] is yellow ( --> )
[ $ "32" colour ] is green ( --> )
[ $ "34" colour ] is blue ( --> )
[ $ "38;5;54" colour ] is indigo ( --> )
[ $ "35" colour ] is violet ( --> )
[ $ "30" colour ] is black ( --> )
$ "RAINBOW"
' [ red orange yellow green blue indigo violet ]
witheach [ do behead emit ]
drop
black
- Output:
Raku
use Color::Names:api<2>;
use Color::Names::X11 :colors;
for 'RAINBOW',
'Another phrase that happens to contain the word "Rainbow".'
-> $rainbow-text {
for $rainbow-text.comb Z, flat(<red orange yellow green blue indigo violet> xx *) -> ($l, $c) {
print "\e[38;2;{COLORS{"{$c}-X11"}<rgb>.join(';')}m$l\e[0"
}
say '';
}
- Output:
Displayed here as HTML as ANSI colors don't show up on web interfaces.
RAINBOW
Another phrase that happens to contain the word "Rainbow".
Another phrase that happens to contain the word "Rainbow".
Wren
var colors = [
[255, 0, 0], // red
[255, 128, 0], // orange
[255, 255, 0], // yellow
[ 0, 255, 0], // green
[ 0, 0, 255], // blue
[ 75, 0, 130], // indigo
[128, 0, 255] // violet
]
var s = "RAINBOW"
for (i in 0..6) {
var fore = "\e[38;2;%(colors[i][0]);%(colors[i][1]);%(colors[i][2])m"
System.write("%(fore)%(s[i])")
}
System.print()
- Output:
RAINBOW
X86 Assembly
Twenty-six byte executable program. Output is same as XPL0. Assemble with: tasm; tlink /t
0000 .model tiny
0000 .code
org 100h
;MS-DOS loads .com files with registers set this way:
; ax=0, bx=0, cx=00FFh, dx=cs, si=0100h, di=-2, bp=09xx, sp=-2, es=ds=cs=ss
;The direction flag (d) is clear (incrementing).
0100 52 41 49 4E 42 4F 57 start: db "RAINBOW" ;executed as code shown below
;push dx inc cx dec cx DEC SI inc dx dec di push di
0107 B0 13 mov al, 13h ;call BIOS to set graphic mode 13h (ah=0)
0109 CD 10 int 10h ; 320x200 with 256 colors
010B B3 1E mov bl, 32-2 ;base of rainbow colors
010D B1 08 mov cl, 1+7 ;number of characters to display + null in PSP
010F B4 0E mov ah, 0Eh ;write character in teletype mode
0111 AC rb10: lodsb ;fetch char to write: al:= ds:[si++]
0112 CD 10 int 10h
0114 43 inc bx ;next color
0115 43 inc bx
0116 E2 F9 loop rb10 ;loop for all 1+7 characters (cx--)
0118 EB FE jmp $ ;lock up
end start
XPL0
The Raspberry Pi versions of the language require a graphic mode for colored text (unlike the MS-DOS versions), but they compensate by supporting this unusual string indexing (which only works with the interpreted MS-DOS version).
char I;
[SetVid($13); \set 320x200x8 VGA graphics
for I:= 0 to 6 do
[Attrib(32+I+I); \set color attribute
ChOut(6, I("RAINBOW "));
];
]