Rainbow: Difference between revisions

9,493 bytes added ,  2 months ago
Added a Dart example.
m (→‎{{header|Wren}}: Better alignment.)
(Added a Dart example.)
 
(16 intermediate revisions by 10 users not shown)
Line 2:
 
;Task: Print out the word 'RAINBOW' to the screen with every character being a different color of the rainbow.
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">loop [
#red "R"
#orange "A"
#yellow "I"
#green "N"
#blue "B"
#indigo "O"
#violet "W"
] [c l] -> prints color c l</syntaxhighlight>
 
=={{header|BASIC}}==
Line 30 ⟶ 42:
 
==={{header|FreeBASIC}}===
[[File:RAINBOWFB.png|right]]
<syntaxhighlight lang="vb">Dim As Integer colors(6, 2) => { _
{255, 0, 0}, _ ' red
Line 42 ⟶ 55:
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
Line 71 ⟶ 84:
NEXT i
END</syntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="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;
}</syntaxhighlight>
{{out}}
<div style="background-color:#F6F6F6;padding:1em;">
<font color="#FF0000">R</font><font color="#FF8000">A</font><font color="#FFFF00">I</font><font color="#00FF00">N</font><font color="#0000FF">B</font><font color="#4B0082">O</font><font color="#8000FF">W</font><br></div>
 
=={{header|Dart}}==
<syntaxhighlight lang="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');
}
}
</syntaxhighlight>
 
=={{header|Factor}}==
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
[[File:Factor rainbow.png|center|thumb]]
 
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="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()
}</syntaxhighlight>
 
{{out}}
<pre>
As C example.
</pre>
=={{header|HTML}}==
<syntaxhighlight lang="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>
</syntaxhighlight>
=={{header|jq}}==
The following assumes the terminal supports the ANSI RGB codes,
and has been tested using iTerm2.
<syntaxhighlight lang="jq">
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")
</syntaxhighlight>
Invocation: jq -nr -f rainbow.jq
 
=={{header|Julia}}==
Line 83 ⟶ 224:
[[File:Rainbow.png|center|thumb]]
 
=={{header|Perl}}==
<syntaxhighlight lang="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";</syntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/xpGUI}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.3"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">xpGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">rainbow</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{{</span><span style="color: #008000;">"R"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_RED</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"A"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_ORANGE</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"I"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_YELLOW</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"N"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_GREEN</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"B"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_BLUE</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"O"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_INDIGO</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"W"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_PURPLE</span><span style="color: #0000FF;">}}}</span>
<span style="color: #004080;">gdx</span> <span style="color: #000000;">list</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">gList</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rainbow</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SIZE=240x24"</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">dialog</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Rainbow"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">`SIZE=240x62`</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">gCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">list</span><span style="color: #0000FF;">,</span><span style="color: #004600;">XPG_LIGHT_GREY</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">gShow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dialog</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">gMainLoop</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
[[File:Phix_Rainbow.png|center|thumb]]
 
=={{header|Python}}==
Line 104 ⟶ 279:
use Color::Names::X11 :colors;
 
for 'RainbowRAINBOW',
'Another phrase that happens to contain the word "Rainbow".'
-> $rainbow-text {
Line 115 ⟶ 290:
{{out}}
Displayed here as HTML as ANSI colors don't show up on web interfaces.
<div style="background-color:#f6f6f6black;padding:1em;font-size:150%;"><font color="#FF0000">R</font><font color="#FFA500">aA</font><font color="#FFFF00">iI</font><font color="#00FF00">nN</font><font color="#0000FF">bB</font><font color="#4B0082">oO</font><font color="#EE82EE">wW</font><br>
<font color="#FF0000">A</font><font color="#FFA500">n</font><font color="#FFFF00">o</font><font color="#00FF00">t</font><font color="#0000FF">h</font><font color="#4B0082">e</font><font color="#EE82EE">r</font><font color="#FF0000"> </font><font color="#FFA500">p</font><font color="#FFFF00">h</font><font color="#00FF00">r</font><font color="#0000FF">a</font><font color="#4B0082">s</font><font color="#EE82EE">e</font><font color="#FF0000"> </font><font color="#FFA500">t</font><font color="#FFFF00">h</font><font color="#00FF00">a</font><font color="#0000FF">t</font><font color="#4B0082"> </font><font color="#EE82EE">h</font><font color="#FF0000">a</font><font color="#FFA500">p</font><font color="#FFFF00">p</font><font color="#00FF00">e</font><font color="#0000FF">n</font><font color="#4B0082">s</font><font color="#EE82EE"> </font><font color="#FF0000">t</font><font color="#FFA500">o</font><font color="#FFFF00"> </font><font color="#00FF00">c</font><font color="#0000FF">o</font><font color="#4B0082">n</font><font color="#EE82EE">t</font><font color="#FF0000">a</font><font color="#FFA500">i</font><font color="#FFFF00">n</font><font color="#00FF00"> </font><font color="#0000FF">t</font><font color="#4B0082">h</font><font color="#EE82EE">e</font><font color="#FF0000"> </font><font color="#FFA500">w</font><font color="#FFFF00">o</font><font color="#00FF00">r</font><font color="#0000FF">d</font><font color="#4B0082"> </font><font color="#EE82EE">&quot;</font><font color="#FF0000">R</font><font color="#FFA500">a</font><font color="#FFFF00">i</font><font color="#00FF00">n</font><font color="#0000FF">b</font><font color="#4B0082">o</font><font color="#EE82EE">w</font><font color="#FF0000">&quot;</font><font color="#FFA500">.</font></div>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">var colors = [
[255, 0, 0], // red
[255, 128, 0], // orange
Line 139 ⟶ 314:
<div style="background-color:#F6F6F6;padding:1em;">
<font color="#FF0000">R</font><font color="#FF8000">A</font><font color="#FFFF00">I</font><font color="#00FF00">N</font><font color="#0000FF">B</font><font color="#4B0082">O</font><font color="#8000FF">W</font><br></div>
 
=={{header|X86 Assembly}}==
{{trans|XPL0}}
Twenty-six byte executable program. Output is same as XPL0.
Assemble with: tasm; tlink /t
<syntaxhighlight>
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
</syntaxhighlight>
 
=={{header|XPL0}}==
1

edit