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?).


  • $\begingroup$ You'll probably need an image to do that, not a string $\endgroup$ Nov 24, 2015 at 21:26
  • $\begingroup$ Related: mathematica.stackexchange.com/questions/56719/… $\endgroup$
    – Michael E2
    Nov 24, 2015 at 21:48
  • $\begingroup$ 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). $\endgroup$
    – Charles
    Nov 25, 2015 at 2:17

1 Answer 1


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]}] &@

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

enter image description here

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"]]

enter image description here

  • 1
    $\begingroup$ 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]]]. $\endgroup$ Nov 24, 2015 at 22:00
  • $\begingroup$ One more note: Rescale@Range@Length@# & can be replaced by Subdivide[Length[#] - 1] &. $\endgroup$ Nov 24, 2015 at 22:33
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 24, 2015 at 23:28
  • $\begingroup$ 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. $\endgroup$
    – Charles
    Nov 25, 2015 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.