Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way to force Mathematica to convert an already rendered Graphic to the simplified Directive form of that Graphic.

Background: I am currently editing Graphics using Drawing Tools and then calling InputForm[gr], but this often doesn't work well because the output of InputForm are much larger then the original input.

For example I am looking for the following code

gr = Graphics[{Red, Circle[]}];

To output the simplest form possible.

For example HoldForm[InputForm[gr]] could print Graphics[{Red, Circle[]}] or Graphics[{Red, Circle[0,0]}].

EDIT: There are a lot of Graphic directives listed in the docs, so there probably isn't a direct answer, but simply answers that get closer to respecting the original input. The best technique IMO would somehow create a trained function which evolves based on the version of Mathematica that you are currently using. Obviously manual transformation are less then ideal.

share|improve this question

closed as unclear what you're asking by William, ssch, Leonid Shifrin, Artes, m_goldberg Sep 14 '13 at 0:59

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

I'm not sure what you're asking, can you clarify? Do you just want to get Red instead of RGBColor[1,0,0]? Can you show what InputForm shows you and point out specifically what you don't like about it? – Szabolcs Sep 10 '13 at 23:26
@Szabolcs small typo. It should have said InputForm[gr]. Halirutan and I have been discussing this in the chat room. Ultimately I would like the output of InputForm[gr] to be a close to the original input as possible. Currently the ouptut is Graphics[{RGBColor[1, 0, 0], Circle[{0, 0}]}] instead of Graphics[{Red, Circle[]}]. There are a lot of Graphic directives listed in the docs, so there probably isn't a direct answer, but simply answers that get closer to respecting the original input. – William Sep 10 '13 at 23:32
It's probably just a typo, but Graphics[{Red, Circle[0, 0]}] is invalid syntax. – m_goldberg Sep 11 '13 at 0:34
I would like to nominate this question for reopening. Even though it is a bit misguided I think it is interesting. – Mr.Wizard Jan 15 '15 at 15:10
up vote 4 down vote accepted

This strikes me as a flawed question (sorry) as you are converting between forms that are not entirely equivalent. I mean that for example there are a number of different sets of named colors, and you cannot ultimately know what set may have been used to create the Graphics therefore you cannot "put Humpty Dumpty together again."

Nevertheless if we are going to attempt this I think I can give cleaner code that what halirutan provided:

rules = MakeExpression@# /. _[x_] :> #2 :> x & @@@ ColorData["Legacy", "ColorRules"];
AppendTo[rules, Circle[] :> Circle[]];


gr = Graphics[{Red, Circle[]}];
HoldForm @@ {InputForm@gr} /. rules
Graphics[{Red, Circle[]}]
share|improve this answer
This isn't an attempt to retrieve the original input, but instead have a cleaner more pleasant output to look at. I have modified your code slightly. Is there a way to get a list of shapes that have default parameters? I am currently manually listing them like the following Map[(#[] :> #[]) &, {Circle, Rectangle, Disk,Cylinder, Sphere, Cone, Tube}] – William Sep 14 '13 at 18:36
@Liam That makes more sense to me now; thanks. Regarding shapes, no, not that I am aware of; I'd do it the same way you are. – Mr.Wizard Sep 15 '13 at 17:14

As discussed in chat, one way to revert the colors to their names is to use ColorData to create a list of named colors and inject them back into your held graphics:

rules = Cases[
   MakeExpression[#] /.
      HoldComplete[expr_] :> (HoldPattern[Evaluate[expr]] :> expr) & /@
     ColorData["HTML", "ColorFunction"][[3, 1]],
   HoldPattern[Verbatim[HoldPattern][_RGBColor] :> _]];

gr = Graphics[{Red, Circle[]}];
With[{expr = InputForm[gr]},
 HoldForm[expr] /. rules
share|improve this answer

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