30
$\begingroup$

Note that this is not a duplicate of the question over there. That one asks for a given point, while I'm asking about the lexical structure (which, of course, is not directly related to any point in the final graphics).

What I want to know is whether there's a way to access the currently set colour. For example, let's assume I write the following function:

mydisk[p_,r_]:={EdgeForm[Darker[CurrentColor]],Disk[p,r]}

and then use it like this:

Graphics[{Red, mydisk[{0,0},1], Green, mydisk[{1,1},1]}]

it should be equivalent to

Graphics[{Red, {EdgeForm[Darker[Red]], Disk[{0,0},1]},
          Green, {EdgeForm[Darker[Green]], Disk[{1,1},1]}}]

Is this possible, and if so, what would I write in the place of CurrentColor?

$\endgroup$
  • 1
    $\begingroup$ +1, good question! I wanted to ask this myself at some point. This will come up when trying to convert a Mathematica Graphics-expression to some other format (e.g. export to some special format). $\endgroup$ – Szabolcs May 4 '12 at 13:22
34
$\begingroup$

CurrentValue["Color"] seems to be doing the trick (not documented).

mydisk[p_, r_] := {Dynamic[EdgeForm[Darker[CurrentValue["Color"]]]], 
  Disk[p, r]}

Dynamic is needed because the value has to be evaluated by FrontEnd at the time of rendering. Here is the result:

Graphics[{EdgeForm[AbsoluteThickness[10]], Red, mydisk[{0, 0}, 1], 
  Green, mydisk[{1, 1}, 1]}]

enter image description here

CurrentValue is like a box of chocolate. It has a lot of features (mostly FE callbacks), many undocumented, but usually a very present surprise when it works.

A few other items that work with CurrentValue: "Thickness", "Opacity", "Dashing", "FontFamily", "FontSize", "FontSlant", "FontWeight", "FontColor" and "FontOpacity".

$\endgroup$
  • $\begingroup$ A real treat, that! $\endgroup$ – Yves Klett May 4 '12 at 15:04
  • 12
    $\begingroup$ Great solution. Some experiments show that other values which can be accessed with CurrentValue are "Thickness", "Opacity", "Dashing", "FontFamily", "FontSize", "FontSlant", "FontWeight", "FontColor" and "FontOpacity". $\endgroup$ – celtschk May 4 '12 at 15:19
  • $\begingroup$ I updated the post with @celtschk's findings. This information is too valuable to keep it in a volatile comment that may disappear any day. $\endgroup$ – Szabolcs May 10 '12 at 15:00
  • $\begingroup$ For many more see Items known by CurrentValue $\endgroup$ – Mr.Wizard Jul 20 '16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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