# Is it possible to apply a ColorGradient to a string Style?

After reading through the language reference on Style, ColorSchemes, ColorData and ColorFunction, I am at a loss as how to make a string appear with gradient colors. And now that I am curious, I am wondering what the easiest way to achieve this is under various scenarios, for instance:

1. apply gradient to each character independently
2. apply gradient to entire string as a whole
3. apply gradient such that each character is a solid color, but as a string it has a gradient mask applied

From reading elsewhere on stackExchange, I see I can apply a color to each character independently using Row (ex https://stackoverflow.com/questions/8956915/mathematica-how-to-have-text-in-multiple-colors). Programtically that seems rather tedious and impractical as a function.

At this point, I am not too concerned about the complexity of the gradient function - for right now, simple is better (ie, maybe a linear gradient?).

Thoughts?

• You'll probably need an image to do that, not a string – Dr. belisarius Nov 24 '15 at 21:26
• – Michael E2 Nov 24 '15 at 21:48
• Thanks, Michael - the link has an interesting discussion. Again, because I am curious, is the quality of the gradient then only limited to the underlying image? The image as posted by William (original author) has a true gradient (obviously, outside Wolfram). – Charles Nov 25 '15 at 2:17

Case 3: string with gradient, each character in a solid color (thanks to J.M. for improvements)

colorize[str_String, cf_] := "" <> MapThread[
ToString[Style[#, cf@#2], StandardForm] &, {#, Subdivide[Length@# - 1]}] &@
Characters@str;

colorize["Mathematica", ColorData["Rainbow"]]


Or even shorter with a bit different internal structure of the string

colorize2[str_String, cf_] := Row@MapThread[Style,
{#, cf /@ Subdivide[Length@# - 1]} &@Characters@str]~ToString~StandardForm;

colorize2["Mathematica", ColorData["Rainbow"]]


• Alternatively, if you don't mind being limited to the named gradients: colorize[str_String, cf_] := With[{chars = Characters[str]}, StringJoin[MapIndexed[ToString[Style[#, ColorData[{cf, {1, Length[chars]}}] @@ #2], StandardForm] &, chars]]]. – J. M. will be back soon Nov 24 '15 at 22:00
• One more note: Rescale@Range@Length@# & can be replaced by Subdivide[Length[#] - 1] &. – J. M. will be back soon Nov 24 '15 at 22:33
• Very nice, but it gets tricky if your text itself changes base style in midstream, for instance including words in italics or in bold face, or different font. – David G. Stork Nov 24 '15 at 23:28
• Wow, you guys are amazing - thanks @ybeltukov . Unfortunately, the code you provided is way over my head, and it will take a while for me to decipher it. But you did indeed answer the question. Very slick. – Charles Nov 25 '15 at 2:15