6

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?

Charles
  • 63
  • 2
  • You'll probably need an image to do that, not a string – Dr. belisarius Nov 24 '15 at 21:26
  • Related: http://mathematica.stackexchange.com/questions/56719/apply-an-image-as-a-texture-to-text – 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 02:17

1 Answers1

12

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

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

ybeltukov
  • 43,673
  • 5
  • 108
  • 212
  • 1
    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.'s missing motivation Nov 24 '15 at 22:00
  • One more note: Rescale@Range@Length@# & can be replaced by Subdivide[Length[#] - 1] &. – J. M.'s missing motivation Nov 24 '15 at 22:33
  • 1
    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 02:15