5
$\begingroup$

Is it possible to detect the text color in Mathematica, so that a command like this:

TextColor[<colored text>]

would return the color of the text in some way?

For example, when I input

G[m_] := Module[{k}, 4]

Mathematica colors m_ and k some greenish color(s). I'd like to know precisely what those colors are.

$\endgroup$
4
  • $\begingroup$ How was the text coloured? Provide code that colours the text or at the very least the source of the coloured text. $\endgroup$
    – Edmund
    Commented Apr 28, 2017 at 1:20
  • $\begingroup$ @Edmund : I have added an example of such code. $\endgroup$ Commented Apr 28, 2017 at 1:27
  • $\begingroup$ does this give what you need: textColor[x_] := Cases[x, _?ColorQ]? $\endgroup$
    – kglr
    Commented Apr 28, 2017 at 1:35
  • $\begingroup$ @kglr : I am not sure how to use this. When I input textColor[Module[{k}, 4]] I get {}. F1 yields "ColorQ[color] yields True if color is a valid color directive and False otherwise". $\endgroup$ Commented Apr 28, 2017 at 2:01

1 Answer 1

7
$\begingroup$

These styles are controlled by the AutoStyleOptions setting of a Cell:

oldLVS=CurrentValue[{AutoStyleOptions,"LocalVariableStyle",FontColor}]
oldPVS=CurrentValue[{AutoStyleOptions,"PatternVariableStyle",FontColor}]

RGBColor[0.263, 0.537, 0.345]

RGBColor[0.263, 0.537, 0.345]

Let's change these colors to Red and Blue:

CurrentValue[$FrontEndSession,{AutoStyleOptions,"LocalVariableStyle", FontColor}]=Red;
CurrentValue[$FrontEndSession,{AutoStyleOptions,"PatternVariableStyle", FontColor}]=Blue;

And, you should see that the variables are colored red and blue now:

enter image description here

Finally, return the colors to their old values:

CurrentValue[$FrontEndSession,{AutoStyleOptions,"LocalVariableStyle", FontColor}]=oldLVS;
CurrentValue[$FrontEndSession,{AutoStyleOptions,"PatternVariableStyle", FontColor}]=oldPVS;
$\endgroup$
2
  • $\begingroup$ How to know the other items except "LocalVariableStyle" and "PatternVariableStyle"? $\endgroup$
    – yode
    Commented Apr 28, 2017 at 2:15
  • 1
    $\begingroup$ Options[Cell, AutoStyleOptions] $\endgroup$
    – Carl Woll
    Commented Apr 28, 2017 at 2:16

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.