# Adding colors to objects in a loop

I'm just starting out in Mathematica and still learning the ropes.

I'm making a "necklace" which is a circle with squares around it. The following works:

necklace[n_] :=
Block[    {minCenter, maxCenter, circ, center, squares, out}, (
circ := Circle[{0, 0}, 10];
center :=
Table[    {10*Cos[k*2*Pi/n] , 10*Sin[k*2*Pi/n]  }  , {k, 1, n} ];
minCenter := center - 0.5;
maxCenter := center + 0.5;
squares :=
Table  [
Rectangle[ minCenter[[i]], maxCenter[[i]]    ] , {i, 1, n}    ];
out := Graphics[{circ, {Red, squares}}];
print["hi"];
Return[out]
)]
necklace


The problem is that I want each square to have a different color, right now they're all red. But when I edit the out:= Graphics at the bottom, it will change every square. For testing I was using Randomcolor[] but 1 random color is assigned to the whole necklace instead of for each one. Do I need a table of colors? Multiple outs? What's the best way?

• Welcome! Take a look at Thread or Map or Riffle. – Yves Klett Jan 16 '15 at 10:50

Here's another way to make a random necklace that uses much less code:

necklace[n_] :=
Graphics[{Circle[{0, 0}, 10], {RandomColor[], Rectangle[# - 1/2]}
& /@ (10 {Cos@#, Sin@#} & /@ (2 π Range[n]/n))}]

necklace Here's a rainbow-necklace generator:

necklace2[n_] :=
Graphics[{Circle[{0, 0}, 10], MapIndexed[{Hue[First@#2/n],
Rectangle[# - 1/2]} &, 10 {Cos@#, Sin@#} & /@ (2 π Range[n]/n)]}]

necklace2 • This is excellent thanks! Is there a cheatsheet on where I can find the meaning of those symbols? – dukevin Jan 16 '15 at 22:32
• @KevinDuke: If you search for @ in the Documentation Center, you will get a link to Prefix, which explains that (for example) f@a is shorthand for f[a]. The #, #2 and & are pure function notation, the details of which can be learned at the Documentation Center page for Function. Likewise, if you search the documentation for /@ you will find that it is shorthand notation for Map, for example f/@a is Map[f,a]. All the other stuff is built-in, the details of which can be found in the docs. BTW, the documentation center is wonderful, it's how I learned the language. – DumpsterDoofus Jan 16 '15 at 22:47

You can do this:

necklace[n_, colors_: {Red}] :=
Block[{minCenter, maxCenter, circ, center, squares, out,
colorslist}, (circ := Circle[{0, 0}, 10];
center := Table[{10*Cos[k*2*Pi/n], 10*Sin[k*2*Pi/n]}, {k, 1, n}];
minCenter := center - 0.5;
maxCenter := center + 0.5;
colorslist = Flatten[Nest[Append[colors, #] &, colors, Floor[n/Length[colors]]]];
squares :=
Table[{colorslist[[i]],
Rectangle[minCenter[[i]], maxCenter[[i]]]}, {i, 1, n}];
out := Graphics[{circ, squares}];
Return[out])
]
{necklace[20, {Red, Green, Blue}], necklace}


Which yields a necklace with the colors specified (repeated). Default value is red What I basically do, is to take the list of colors provided at execution of the command (or its default value a list with only Red) and repeat it several times until it is longer than the number of circles.

Edit:

If you want a colorchange, you can use colorfunctions. Use Colordata[{schemename,{minValue,maxValue}}][value] to color your rectangles. Then you have another solution: (as default we will take rainbow colors)

 necklaceCF[n_, colorsfunctionname_: "Rainbow"] := Block[{minCenter, maxCenter, circ, center, squares, out,
colorslist}, (circ := Circle[{0, 0}, 10];
center := Table[{10*Cos[k*2*Pi/n], 10*Sin[k*2*Pi/n]}, {k, 1, n}];
minCenter := center - 0.5;
maxCenter := center + 0.5;
squares :=
Table[{ColorData[{colorsfunctionname, {1, n}}][i],
Rectangle[minCenter[[i]], maxCenter[[i]]]}, {i, 1, n}];
out := Graphics[{circ, squares}];
Return[out])]
{necklaceCF, necklaceCF[20, "FruitPunchColors"]}


This yields: • Ahh that makes sense, thanks a lot! Sort of a separate question but let's say I wanted a gradient of colors where the first color is red, last purple, while compensating for the size of the necklace. What else would have to be done to do this? – dukevin Jan 16 '15 at 12:40
• Since you seem new to SE, when an answer solved your problem, consider accepting the answer to credit it. – Philipp Jan 16 '15 at 15:29