# Help me to find a way to do this repetitive stuff

I'm trying to do this:

p = 10;
a = ToString[N[Sqrt[3], p^2 - 1]];

a = Characters[a ];
b = Table[ToString[n], {n, 0, 9}];
n = 0; i = 0;
a = StringReplace[a, "." -> "g"];

a /. ToString[0] -> Graphics[{CMYKColor[1, 0, 0, 0], Rectangle[]}]


The idea is to replace the numbers, with a rectangle of each color - small numbers are brighter, bigger numbers are dark, the point is to be transformed in a green square.

But for doing this, I must repeat the code a lot:

    a /.
ToString[0] -> Graphics[{CMYKColor[0, 0, 0, 0], Rectangle[]}]  /.
ToString[1] -> Graphics[{CMYKColor[1, 0, 0, 0], Rectangle[]}]  /.
ToString[2] -> Graphics[{CMYKColor[2, 0, 0, 0], Rectangle[]}]  /.
ToString[3] -> Graphics[{CMYKColor[3, 0, 0, 0], Rectangle[]}] ...


I tried to use Table, but it didn't work. I was trying the following:

Table[a /. ToString[o] -> Graphics[{CMYKColor[o, 0, 0, 0], Rectangle[]}], {0, 0, 9}]


But it didn't work. What would be a nice way to do this?

-
What about g? With what do you want to be replaced with? –  Spawn1701D Apr 13 '13 at 7:17
The g is the point, I switched it because I couldn't make /. ToString[.] -> Graphics[{CMYKColor[x/a1, x/10, 0, x/a3], Rectangle[]}] work. –  Vÿska Apr 13 '13 at 7:19
And what values x,a1,a3 have? –  Spawn1701D Apr 13 '13 at 7:21
Why do you convert to strings? And what are b, n for? I'd just use: Graphics[{CMYKColor[#/10, 0, 0, 0], , Rectangle[]}, ImageSize -> 25] & /@ RealDigits[Sqrt[3], 10, p^2 - 1][[1]] or any other color scheme, as in @Spawn1701D 's answer... but I guess I am missing something –  Pinguin Dirk Apr 13 '13 at 7:40
@PinguinDirk you have to make an exception for the . but apart from that this is the fastest solution. –  Spawn1701D Apr 13 '13 at 7:45

(Too long for a comment now)

looking at the question, I understand you want to take all digits (up to p^2-1) of a given number and map that to colors. Here's how I'd do it:

p=10;


and then, using RealDigits:

Graphics[{CMYKColor[#/9, 0, 0, 0], , Rectangle[]},
ImageSize -> 25] & /@ RealDigits[Sqrt[3], 10, p^2 - 1][[1]]


Note that this does not account for integer- or fractional parts. For that, one might split up the list accordingly or e.g. do:

Map[Graphics[{CMYKColor[#/9, 0, 0, 0], , Rectangle[]},
ImageSize ->
25] &, #] & /@ (RealDigits /@ {IntegerPart@#,
FractionalPart@#} &@N[Sqrt[3], p^2 - 1])[[All, 1]]


to get first a list of the rectangles corresponding to the integer part and then, secondly, for the fractional part. From your question, I cannot see what you might want, thus I leave the output mostly unformatted as a list.

Using Table (based on the first approach), you could use:

Table[Graphics[{CMYKColor[i/9, 0, 0, 0], , Rectangle[]},
ImageSize -> 25], {i, RealDigits[Sqrt[2], 10, p^2 - 1][[1]]}]


Also, see @Spawn1701D 's answer for the use of any other (fancy) colorscheme.

And, not to forget: maybe you'd like (your avatar suggests so...):

ArrayPlot[RealDigits[\[Pi], 10, p^2][[1]]~Partition~p,
ColorFunction -> "Rainbow"]


-
note that I focused on the "repetitive" stuff and tried to give generic outputs that should allow to insert green squares where ever wanted, needed, desired. I hope this helps! –  Pinguin Dirk Apr 13 '13 at 13:10

The idea is to replace the numbers, with a rectangle of each color - small numbers are brighter, bigger numbers are dark, the point is to \ be transformed in a green square.

And borrowing the code from @Pinguin

p = 50;
ArrayPlot[RealDigits[\[Pi], 10, p^2][[1]]~Partition~p,
ColorFunction -> (RGBColor[0, 1 - #, 0] &)]


p = 10;
s = Flatten@ConstantArray[Range[0, 9], p];
ArrayPlot[NestList[RotateLeft[#, 1] &, s, p p],
ColorFunction -> (RGBColor[0, 1 - #, 0] &)]


-

Try this command

a /. s : Except["g", _String] :> Graphics[{CMYKColor[ToExpression[s]/10, 0, 0, 0],
Rectangle[]}]


but this is a more colorful one:

a /. s : Except["g", _String] :> Graphics[{ColorData["Rainbow"][ToExpression[s]/10],
Rectangle[]}]


and if you want just green:

a /. s : Except["g", _String] :> Graphics[{RGBColor[0, 1 - ToExpression[s]/10, 0],
Rectangle[]}]

-
Check on the online help of Mathematica the command ColorData there a slew of different color schemes to choose from. –  Spawn1701D Apr 13 '13 at 7:18
Also if you want to control the brightness Hue is better. –  Spawn1701D Apr 13 '13 at 7:24