Function Repository Resource:

GraphicsDirectiveQ

Source Notebook

Test if an expression is a graphics directive

Contributed by: Enrique Zeleny

ResourceFunction["GraphicsDirectiveQ"][expr]

test if expr is a graphics directive.

Details and Options

ResourceFunction["GraphicsDirectiveQ"] works for 2D and 3D directives.
Symbols that are considered to be graphics directives by ResourceFunction["GraphicsDirectiveQ"] are those mentioned on guide/GraphicsDirectives. As such, it may not support all directives.

Examples

Basic Examples (2) 

Test common directives:

In[1]:=
ResourceFunction["GraphicsDirectiveQ"][AbsoluteDashing[Medium]]
Out[1]=
In[2]:=
ResourceFunction["GraphicsDirectiveQ"][Red]
Out[2]=

Test a more complicated directive:

In[3]:=
ResourceFunction["GraphicsDirectiveQ"][
 Directive[Specularity[GrayLevel[1], 3], RGBColor[
  0.880722, 0.611041, 0.142051], Lighting -> {{"Ambient", RGBColor[
     0.30100577, 0.22414668499999998`, 0.090484535]}, {"Directional", RGBColor[0.2642166, 0.18331229999999998`, 0.04261530000000001], ImageScaled[{0, 2, 2}]}, {"Directional", RGBColor[
     0.2642166, 0.18331229999999998`, 0.04261530000000001], ImageScaled[{2, 2, 2}]}, {"Directional", RGBColor[
     0.2642166, 0.18331229999999998`, 0.04261530000000001], ImageScaled[{2, 0, 2}]}}]]
Out[3]=

Scope (5) 

A plot:

In[4]:=
Plot3D[x y, {x, -1, 1}, {y, -1, 1}]
Out[4]=

Extract directives from the plot:

In[5]:=
Union@Cases[%, _?ResourceFunction["GraphicsDirectiveQ"], \[Infinity]]
Out[5]=

The directive for a sphere with glow:

In[6]:=
Cases[\!\(\*
Graphics3DBox[
{RGBColor[1., 0.5, 0.5], Glow[
RGBColor[0., 0., 0.19999999999999996`]], Specularity[
RGBColor[1, 1, 0], 30], SphereBox[{0, 0, 0}]},
Lighting->{{"Point", 
RGBColor[0.88, 1, 0.88], {0, 0, 3}}}]\), _?ResourceFunction[
  "GraphicsDirectiveQ"], {2}]
Out[6]=

Pass directives from the previous plot to another plot:

In[7]:=
Graphics3D[Flatten@{%, KnotData["SolomonSeal", "ImageData"]}]
Out[7]=

Create a plot containing some directives:

In[8]:=
Plot3D[x/Exp[x^2 + y^2], {x, -2, 2}, {y, -2, 2}, ColorFunction -> Function[{x, y, z}, Hue[.65 (1 - z)]]]
Out[8]=

Delete the directives within the plot:

In[9]:=
MapAt[DeleteCases[#, _?ResourceFunction[
    "GraphicsDirectiveQ"] , \[Infinity]] &, %, 1]
Out[9]=

Create a plot using a PlotTheme:

In[10]:=
Plot3D[Sin[x] Cos[y], {x, 0, 2 \[Pi]}, {y, 0, 2 \[Pi]}, PlotTheme -> "Marketing"]
Out[10]=

Extract directives that appear in the previous output that were generated automatically by the PlotTheme:

In[11]:=
Cases[%, _[_, _?ResourceFunction["GraphicsDirectiveQ"]] , \[Infinity]]
Out[11]=

Create a plot whose output contains some directives:

In[12]:=
gr = Plot3D[Sin[x + y^2], {x, -2, 2}, {y, -2, 2}, Background -> Black,
   Mesh -> None, AxesStyle -> White, Boxed -> False]
Out[12]=

Look at the structure of the code of the plot, showing explicitly only directives:

In[13]:=
MapAt[Inactive, gr /. {v_?VectorQ /; ! MemberQ[v, _?ResourceFunction[
        "GraphicsDirectiveQ"], \[Infinity]] :> Iconize[v, "vector"],
   m_?MatrixQ :> Iconize[m, "matrix"], r_Rule | r_RuleDelayed /; ! MemberQ[r, _?ResourceFunction[
        "GraphicsDirectiveQ"], \[Infinity]] :> Iconize[r, "option"]},
  0]
Out[13]=

Publisher

Enrique Zeleny

Version History

  • 1.0.0 – 15 December 2020

Related Resources

License Information