10 Answers
10

If there's no formula or pattern to the colors, then you have to somehow manually indicate how to style each part. Here are a couple of ways to do that using Rule to encode key/value pairs (in the form part -> style):

I see that you are using Alternatives as I did in my first code. Unless this has been universally optimized in v9 (I know it has been optimized in DeleteCases for example) this will slow in proportion to the number of alternatives. I see that you used SparseArray before I did so credit where it's due.
–
Mr.Wizard♦Sep 1 '13 at 22:35

@Mr.Wizard I assumed, since the question is about styling, that speed on a very large set was not an important issue. Alternatives seemed a quite readable alternative way to write the styling rules. (I meant that Alternatives was an alternative form of input, and I've edited the answer to make clearer, I hope.)
–
Michael E2Sep 2 '13 at 0:05

Okay, I see it more clearly now. By the way, I do not assume that this question is about styling, but rather that Style was merely a nice illustration. This is why I did not write my answer to work with a list of colors but rather a list of functions.
–
Mr.Wizard♦Sep 2 '13 at 0:09

@Mr.Wizard Yours is a more interesting interpretation. When I've done such things on a larger scale, the positions have been determined by properties of the data and I've used the properties instead.
–
Michael E2Sep 2 '13 at 11:40

This question looks like fun. Too bad I missed out on the early action. I don't have much time but two ideas came to mind. The first was assignments to Part which I believe ssch already did. The second is creating a list if functions and then using Inner and Compose as I did for Map a function across a list conditionally.

Update #1

I changed my code above to eliminate Alternatives which, at least in version 7, causes a considerable slow-down when applied to a large number of elements. I knew this, but I was in a hurry. I have replaced it with a double-Thread approach that scales better.

Nevertheless this SparseArray construct is slower than assignments to Part, which doesn't surprise me. I could use Part assignments to build the function list that is passed to Inner but then there is little reason I can see not to use the method directly. Part assignment is a favorite method of mine anyway, so here is my variation of the Part assignment method ssch already posted:

This is my favorite method and you have my vote, but IMHO you should not be using Block here. Instead use Module. I used to abuse Block a lot before people like Leonid showed me the error of my ways. Should your list contain the Symbol res you will create a recursion; a simple example: expr = {1, 2, x}; Block[{x = expr}, x]
–
Mr.Wizard♦Sep 1 '13 at 22:27

Noting Mr. Wizard's correction and mainly as (my own) learning opportunity the following is essentially one of Mr. Wizard's approaches slightly different but mapping functions to positions not elements.

I'm afraid this doesn't work quite right. It will only work correctly if all the elements of list are unique, as your replacements are done by pattern rather than position.
–
Mr.Wizard♦Sep 1 '13 at 10:12

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.