2
$\begingroup$

I've used a Table[] to generate multiple rounded squares, with Rectangle[{m, n}, RoundingRadius -> .35]. So far, I've been able to color all the squares with the same color, for instance, with LightBlue:

Graphics[{EdgeForm[Black], Table[{LightBlue, Rectangle[{m, n}, RoundingRadius -> .35]}, {m, 5}, {n, 5}]}, Frame -> True]

enter image description here

Or generate random colors for each square, for instance, with Hue[RandomReal[]]:

Graphics[{EdgeForm[Black], Table[{Hue[RandomReal[]], Rectangle[{m, n}, RoundingRadius -> .35]}, {m, 5}, {n, 5}]}, Frame -> True]

enter image description here

But, generally, I'd like to control the color of each of the squares independently, because I already have a vector of Hue[#] colors that I want to use. For instance, I'd like to use this 25 colors in correspondence to each of the (m,n) squares in the grid:

m=5; n=5; Split@Hue[#] & /@ Range[1/(m*n), 1, 1/(m*n)]

enter image description here

I've tried to do something like the following, but it doesn't seem right:

Graphics[{EdgeForm[Black], Table[{c, Rectangle[{m, n}, RoundingRadius -> .35]}, {m, 5}, {n, 5}, {c, Split@Hue[#] & /@ Range[1/(m*n), 1, 1/(m*n)]}]}, Frame -> True]

enter image description here

I've also tried to Map[] the colors or use a ColorFunction-> but had no success.

I'd appreciate any help, thanks! -Pedro

$\endgroup$
4
$\begingroup$

Starting with slightly modified color code:

m = 5;
n = 5;
colors = Hue /@ Range[1/(m*n), 1, 1/(m*n)];

rectangles = Table[Rectangle[{m, n}, RoundingRadius -> .35], {m, 5}, {n, 5}];

We may use either of these, among others of course:

Graphics[{EdgeForm[Black], {colors, Flatten@rectangles}\[Transpose]}, Frame -> True]

Graphics[{EdgeForm[Black], Riffle[colors, Flatten@rectangles]}, Frame -> True]

enter image description here

Reference Transpose and Riffle.

Also possible:

i = 1;

Graphics[{EdgeForm[Black], 
  Table[{colors[[i++]], Rectangle[{m, n}, RoundingRadius -> .35]}, {m, 5}, {n, 5}]},
  Frame -> True]
$\endgroup$
  • $\begingroup$ Thanks @Mr.Wizard! I wasn't aware of Riffle[]. This is a simple solution :) $\endgroup$ – TumbiSapichu Feb 18 '18 at 20:09
  • $\begingroup$ @Pedro Glad I could help. $\endgroup$ – Mr.Wizard Feb 18 '18 at 20:10

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.