g | x | w | all
Bytes Lang Time Link
091Tcl180403T102013Zsergiol
074Red210604T170511Z9214
081Factor210601T153554Zchunes
113Swift180322T151552Zuser3151
063Bash + GNU Utilities180321T174954ZDigital
089Lua180323T170823Zivzem
084Wolfram Language Mathematica180323T143309ZKelly Lo
093R180322T091435ZDS_UNI
072sed180322T074044Zlucasb
085Python 3180322T182835ZDat
023SOGL V0.12180322T101829Zdzaima
088C gcc180322T102124ZGPS
087Python 2180321T135314ZAvahW
077Java JDK 10180321T085524ZOlivier
062J180321T113605ZGalen Iv
029Stax180321T070105Zrecursiv
043Japt180321T212449ZOliver
028Husk180321T213400ZLeo
055q/kdb+180321T215320Zmkst
074C gcc preprocessor macro180321T190144ZDigital
052Vim180321T114310Zoktupol
056Perl 6180321T145251Znwellnho
057Perl 5 p180321T074002ZTon Hosp
067Gema180321T093052Zmanatwor
084SmileBASIC180321T162108Z12Me21
073Kotlin180321T160900ZMakotosa
092PHP180321T142847ZDave
074IBM/Lotus Notes Formula Language180321T084840ZElPedro
074PowerShell180321T134355ZAdmBorkB
058Retina180321T115111ZMartin E
060Ruby n180321T114004ZKirill L
066APL+WIN180321T125805ZGraham
075Haskell180321T073643ZCristian
076Julia 0.6180321T111304Zniczky12
079Coconut180321T111931ZLaikoni
069Ruby n180321T093439ZAsone Tu
085Excel180321T102609ZWernisch
097Batch180321T094906ZNeil
068JavaScript180321T071744Ztsh
03005AB1E180321T082937ZEmigna
079Python180321T073912Zxnor
087Red180321T081113ZGalen Iv

Tcl, 91 bytes

puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet Red}] [lsearch $L $argv]+1]

Try it online!


# [Tcl], 98 bytes
puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet}] [expr ([lsearch $L $argv]+1)%7]]

Try it online!


# [Tcl], 98 bytes
puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet Red}] [expr [lsearch $L $argv]+1]]

Try it online!

Red, 74 bytes

func[c][prin select[Red Orange Yellow Green Blue Indigo Violet Red]load c]

Try it online!

Factor, 81 bytes

[ qw{ Red Orange Yellow Green Blue Indigo Violet } [ index 1 + 7 mod ] keep nth ]

Try it online!

Swift, 121 113 bytes

let s=readLine()!;let i=["Red","Orange","Yellow","Green","Blue","Indigo","Violet"];print(i[(i.index(of:s)!+1)%7])

Try it online!

Bash + GNU Utilities, 63

grep -Po $1\\K.[a-z]+<<<RedOrangeYellowGreenBlueIndigoVioletRed

The list of colours is reversed so that a regex lookahead may be used. The lookahead boilerplate is one byte shorter than the lookbehind boilerplate. Of course we can do better the PCRE \K.

Try it online!

Lua, 89 bytes

c="Red   OrangeYellowGreen Blue  IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5))

Outputs some trailing spaces. Example:

input: Yellow
output: Green 
space here:  ^

Try it online!

Lua, 102 bytes

c="Red   OrangeYellowGreen Blue  IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5):match("%a+"))

Doesn't output any trailing spaces.

Try it online!

Wolfram Language (Mathematica), 84 bytes

TextWords version (84 Bytes but slow)

#/.Thread[#->RotateLeft@#&[TextWords@"Red Orange Yellow Green Blue Indigo Violet"]]&

StringSplit (86 Bytes)

#/.Thread[#->RotateLeft@#&[StringSplit@"Red Orange Yellow Green Blue Indigo Violet"]]&

Built-in color names called by number, also 86 Bytes:

#/.Thread[#->RotateLeft@#&[ColorData["HTML","Range"][[1,{114,100,139,52,10,57,135}]]]]

Try it online!

R, 109 93 bytes

function(x){y=c("Red","Orange","Yellow","Green","Blue","Indigo","Violet");y[match(x,y)%%7+1]}

Try it online!

-16 thanks to Giuseppe for the use of match advice

sed, 72 bytes

s/$/%RedOrangeYellowGreenBlueIndigoVioletRed/;s/(.+)%.*\1(.[a-z]+).*/\2/

Try it Online

Example 1:

Input:

Red
Orange
Yellow
Green
Blue
Indigo
Violet

Output:

Orange
Yellow
Green
Blue
Indigo
Violet
Red

Example 2:

Input:

Indigo
Yellow
Red
Red
Blue
Green
Orange
Violet
Green
Green
Green
Blue
Blue
Violet

Output:

Violet
Green
Orange
Orange
Indigo
Blue
Yellow
Red
Blue
Blue
Blue
Indigo
Indigo
Red

Python 3, 85 bytes

c='Red Orange Yellow Green Blue Indigo Violet'.split()
f=lambda x:c[(c.index(x)+1)%7]

Try it online!

SOGL V0.12, 23 bytes

k‰³d∆|ΝμHō↑≥░δ÷f‘θ⁽,WIw

Try it Here!

Explanation:

...‘θ⁽,WIw  
...‘        push "red orange yellow green blue indigo violet"
    θ       split on spaces
     ⁽      uppercase the 1st letter of every item (SOGLs dictionary only has lowercase words)
      ,W    get the inputs index in the array
        I   increment
         w  and get that item in the array, wrapping if necessary

C (gcc), 88 bytes

f(char*s){printf("%.6s",(strstr("red   violetindigoblue  green yelloworangered",s)+6));}

Try it online!

Python 2, 89 87 bytes

def f(c):a="red orange yellow green blue indigo violet".split();print a[-~a.index(c)%7]

Test cases:

f("red") -> orange

f("orange") -> yellow

f("yellow") -> green

f("green") -> blue

f("blue") -> indigo

f("indigo") -> violet

f("violet") -> red

f("notAColour") -> Error

Java (JDK 10), 77 bytes

s->"Red Orange Yellow Green Blue Indigo Violet Red".split(s)[1].split(" ")[1]

Try it online!

Credits

J, 67 64 62 bytes

-2 bytes thank to FrownyFrog

>:&.((cut'Red Orange Yellow Green Blue Indigo Violet Red')i.<)

Try it online!

Stax, 31 30 29 bytes

ÇôF┘≡▓ƒ◄╙>┘☼░⌂╪B<U[ÇQ╒eöΣQ╔÷n

Run and debug it

This uses the ring translation instruction. It replaces each element in an array with the following one from the "decoder ring". Usually, it's used to do character replacement in a string, but it can be used on an entire string too, if it's wrapped in a singleton array.

Here's the unpacked, ungolfed, commented ascii representation of the same program.

]   wrap input in singleton array
`5^bZ_9G*h]h%oM~X9e-0ZQJkb2`    compressed string literal with color names
:.j title case and split on spaces
:t  do ring translation

Run this one

Japt, 45 43 bytes

-2 bytes thanks to Shaggy

`R‚sOÎÁƒYÁMwsGÎ9sBluƒI˜igosVio¤t`qs
g1+UbNg

Try it online!

Husk, 28 bytes

S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉

Try it online!

Maybe there are better options for managing the arguments, but this is the best I could find

Explanation

S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉
       ¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉    Compressed string with all the colors
                                "Red Orange Yellow Green Blue Indigo Violet"
      w                         Split on spaces
S                               Pass the list to both the following functions:
    €⁰                          1) Find the index of the input in the list
  o→                                  and increase it by one
 !                              2) Get the element of the list at the
                                   resulting position (indexing is cyclical)

q/kdb+, 59 55 bytes

Solution:

.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]

Examples:

q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Red"
"Violet"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Orange"
"Red"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Blue"
"Green"

Explanation:

Create a dictionary of colour => next colour, the input is the key to the dictionary:

.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange] / the solution
.[ ;                                                  ] / apply multiple args to function
           `Red`Violet`Indigo`Blue`Green`Yellow`Orange  / list of colours
         ($)                                            / convert to strings
    2 8#                                                / reshape into 2x8 grid
  !                                                     / create dictionary

Bonus:

It's 53 bytes in K4:

.[!;2 8#$`Red`Violet`Indigo`Blue`Green`Yellow`Orange]

C (gcc) preprocessor macro, 74

Score is 74 bytes passed to the compiler.

Same idea as @nwellnhof's Perl answer.

-Df(c)=printf("%.6s","IndigoBlue  VioletYellowOrangeRed   Green"+*c/4%8*6)

Try it online!

Vim, 59 56 53 52 Bytes

-1 byte thanks to tsh

2IYellow Green Blue Indigo Violet Red Orange <Esc>*wywVp

Perl 6, 56 bytes

{<Indigo Blue Violet Yellow Orange Red Green>[.ord/4%8]}

Try it online!

Exploits the fact that bits 2-4 of the ASCII codes of each color's first letter happen to map to 0-6.

say map (*.ord +> 2) % 8, <R O Y G B I V>
# (4 3 6 1 0 2 5)

Here's a nice non-competing solution that uses "purple" instead of "indigo" and "violet" (38 chars, 59 bytes):

{'🍎🧡💛💚💙💜🍎'.uninames~~m/$^a.\S+.<(\S+/}

Try it online!

Perl 5 -p, 58 57 bytes

#!/usr/bin/perl -p
$_={(Red,Orange,Yellow,Green,Blue,Indigo,Violet)x2}->{$_}

Try it online!

Now that the challenge has been changed to be cyclic the regex solution

say RedOrangeYellowGreenBlueIndigoVioletRed=~/$_(.[a-z]+)/

isn't optimal anymore (due to the double Red)

Also 57 bytes:

#!/usr/bin/perl -p
$_=(Indigo,Blue,Violet,Yellow,Orange,Red,Green)[ord>>2&7]

Try it online!

Gema, 67 characters

*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}

Sample run:

bash-4.4$ echo -n Yellow | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Green

bash-4.4$ echo -n Violet | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Red

Gema, 59 characters

R=Orange
O=Yellow
Y=Green
G=Blue
B=Indigo
I=Violet
V=Red
*=

Boring one. Dumbest approach ever, but quite short.

Sample run:

bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Yellow'
Green

bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Violet'
Red

SmileBASIC, 94 84 bytes

C$="Red   OrangeYellowGreen Blue  IndigoViolet
INPUT I$?MID$(C$*2,INSTR(C$,I$)+6,6)

Kotlin, 73 bytes

x->"RedOrangeYellow Green  BlueIndigoVioletRed".substringAfter(x).take(6)

Try it online!

Taking advantage of the fact that many colors are 6 characters, the ones that are not are prefixed with spaces to make them 6 characters long. Hopefully it's acceptable that some of the colors are outputted with spaces before them.
e.g. Red is " Red", Blue is " Blue"

PHP, 92 bytes

$a=" Red   OrangeYellowGreen Blue  IndigoVioletRed";echo substr($a,strpos($a,$argv[1])+6,6)

Try it online!

IBM/Lotus Notes Formula Language, 79 74 bytes

@Left(@Right("Red Orange Yellow Green Blue Indigo Violet Red ";a+" ");" ")

Previous version for 79:

R:=@Explode("Red,Orange,Yellow,Green,Blue,Indigo,Violet,Red");R[@Member(a;R)+1]

Takes input from an editable text field called a.

There is no TIO for formula language so here's a couple of screenshots.

enter image description here

enter image description here

enter image description here

PowerShell, 74 bytes

(-split("Red Orange Yellow Green Blue Indigo Violet "*2-split$args)[1])[0]

Try it online!

Takes the string "Red ... Violet " and string-multiplies it out by two to properly handle the Violet -> Red test case. We then -split that string on input $args to give us an array of two strings. We take the second string thereof [1], then -split that on whitespace to give us an array of strings and take the first [0].

For example, for input "Yellow", the first step will result in @("Red Orange ", " Green Blue Indigo ... Indigo Violet "). We take the second one of that, split it on whitespace (which removes the whitespace), resulting in @("Green", "Blue", ... "Violet"), so taking the [0] one thereof results in the proper next string.

Retina, 65 58 bytes

$
(.[a-z]+)
L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1

Try it online!

Explanation

$
(.[a-z]+)

We start by appending (.[a-z]+) to the input, thereby turning it into a regex which matches the input colour, immediately followed by exactly one more colour (capturing the latter).

L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1

Now the : swaps the stage's input with its own regex. So the previous result becomes the regex and it's matched against the list of colours. The (single) match gets replaced with its first capturing group (i.e. the next colour in the cycle) and returned. Output at the end of the program happens automatically.

Ruby -n, 62 60 bytes

-2 by Asone Tuhid.

p"RedVioletIndigoBlueGreenYellowOrangeRed"[/.[a-z]+(?=#$_)/]

Try it online!

Regex approach looks promising for Ruby too. However, I arrived at a shorter solution using a lookahead and directly printing the match, rather than playing with capturing groups. The list of colors is in reverse direction since lookahead is 1 byte cheaper than lookbehind.

APL+WIN, 66 bytes

↑(c⍳⊂⎕)⌽c←'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Indigo' 'Violet'

Prompts for screen input of colour as a string.

Haskell, 80 71 75 bytes

Thanks to Laikoni for shortening 9 bytes!

g x=snd(span(/=x)$words"Red Orange Yellow Green Blue Indigo Violet Red")!!1

Try it online!


Another solution, slightly more idiomatic, but I could not get it shorter:

data R=Red|Orange|Yellow|Green|Blue|Indigo|Violet deriving(Enum,Read,Eq)
succ.read

It needs to derive Read because of the requirement that the input is a string and at least Eq or Show in order to either test for equality or show the result.

Julia 0.6, 76 bytes

f(s)=match(Regex("$s(.[a-z]*)"),"RedOrangeYellowGreenBlueIndigoViolet"^2)[1]

Try it online!

This handles the Violet->Red by recycling the string with the power ^ operator.

Here's a slightly longer solution without regexes:

g(s,l=split("Red Orange Yellow Green Blue Indigo Violet"," "))=l[(findin(l,[s])[1])%7+1]

Coconut, 79 bytes

s->"# Violet Red # # Green Indigo Yellow # # Orange Blue".split()[ord(s[0])%12]

Try it online!

Ruby -n, 75 69 bytes

a=%w{Red Orange Yellow Green Blue Indigo Violet};p a[-~(a.index$_)%7]

Try it online!

Excel, 85 bytes

=CHOOSE(MOD(CODE(A1),12),"green","indigo","yellow",,,"orange","blue",,"violet","red")

Uses lowercase names.

Same approach, with Uppercase letters 86 bytes:

=CHOOSE(MOD(CODE(A1),12),"Violet","Red",,,"Green","Indigo","Yellow",,,"Orange","Blue")

Batch, 97 bytes

@set s=Red Orange Yellow Green Blue Indigo Violet Red
@call set s=%%s:*%1 =%%
@echo %s: =&rem %

Explanation: The call on the second line has the effect of substituting the parameter into the command and evaluating it, turning it into e.g. set s=%s:Red =%, which deletes the prefix of the string that includes the parameter. The substitution on the third line then replaces all the spaces with statement separators and comments. This works because string substitution happens before parsing.

JavaScript, 68 bytes

s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]

For input "Red", this function first construct an RegExp /Red(.[a-z]*)/ to match the string 'RedOrangeYellowGreenBlueIndigoVioletRed' and then return the first capture result.

f=
s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]

document.write('<table><tr><th>Input<th>Output')
for(i='Red';;){
document.write(`<tr><td>${i}<td>${i=f(i)}`);
if(i=='Red')break;
}

05AB1E, 30 bytes

“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“#™DIk>è

Try it online!

Explanation

“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“          # push a string of colours
                       #         # split on spaces
                        ™        # title-case each
                         D       # duplicate
                          Ik     # get the index of the input
                            >    # increment
                             è   # get the element at that index

Python, 79 bytes

z="Red Orange Yellow Green Blue Indigo Violet".split()*2
dict(zip(z,z[1:])).get

Try it online!

Handles Violet -> Red. The desired function is given anonymously in the second line.


80 bytes

lambda x:"Red Orange Yellow Green Blue Indigo Violet Red".split(x)[1].split()[0]

Try it online!

Red, 87 bytes

func[c][print first find/tail[Red Orange Yellow Green Blue Indigo Violet Red]to-word c]

Try it online!