# How to check the validity of the value of PlotStyle option for 2D graph?

For the 2D graphic function like Plot, ParametricPlot, etc, which own a option PlotStyle.

For instance,

ParametricPlot[{Sin[u], Sin[2 u]}, {u, 0, 2 Pi}]


which gives a normal graphic.

Now, I will give a value for the option PlotStyle, like PlotStyle -> ps.Namely, there are two cases as below:

• If the ps is a valid value, Mathematica will gives a normal result.
• If the the value ofps is invald, Mathematica gives the following warning information

Graphics::gprim: Re was encountered where a Graphics primitive or directive was expected

However, if the Plot or ParametricPlot was used in user-defined function, I would like to check the validity of the value of option PlotStyle

Here is a small case

Options[ClosedBSpline] = {SplineStyle -> Blue};

ClosedBSpline[pts_, deg_, opts : OptionsPattern[]] :=
Module[{ss, points, knots, a, b},
ss = OptionValue[SplineStyle];

(*check the validity of SplineStyle*)
(*if the value is invalid ,gives the warning
information and Return[\$Failed]*)

points = Join[pts, pts[[1 ;; deg]]];
knots = Range[0, Length@points + deg];
a = knots[[deg + 1]];
b = knots[[-(deg + 1)]];
ParametricPlot[
CoxDeBoor[points, {deg, knots}, u], {u, a, b},
PlotStyle -> ss,
Axes -> False]
]


Auxiliary function

searchSpan[knots_, u0_] :=
With[{max = Max[knots]},
If[u0 == max,
Position[knots, max][[1, 1]] - 2,
Ordering[UnitStep[u0 - knots], 1][[1]] - 2]
]

CoxDeBoor[pts_, {deg_, knots_}, u0_?NumericQ] :=
Module[
{i = searchSpan[knots, u0], coeff},
coeff =
Piecewise[
{{(u0 - knots[[#1 + #2 + 1]])/(knots[[#1 + deg + 2]] -
knots[[#1 + #2 + 1]]),
knots[[#1 + deg + 2]] != knots[[#1 + #2 + 1]]}}] &;
First@
Fold[
MapThread[Dot, {#2, Partition[#1, 2, 1]}] &,
pts[[i - deg + 1 ;; i + 1]],
Table[
{1 - coeff[j, k], coeff[j, k]},
{k, 1, deg}, {j, i - deg, i - k}]]
]


TEST

pts = {{-2, 2}, {0, 1}, {-2, 0}, {0, -1}, {-2, -2}, {-4, -4}, {2, -4},
{4, 0}, {2, 4}, {-4, 4}};
ClosedBSpline[
pts, 3, SplineStyle -> Directive[Opacity[0.5], Dashed, Blue]]


ClosedBSpline[pts, 3, SplineStyle -> 1]
ClosedBSpline[pts, 3,
SplineStyle -> Directive[Opacity[0.5], Dashe, Blue]]


]

### QUESTION

• How to check the validity of SplineStyle.Namely, I don't know all the possible valid values for the option PlotStyle?
• Clearly Graphics is capable of making that determination, so an inelegant but possibly effective way to check your option values might be to feed the options you collected to a dummy Graphics and catch any ensuing errors... – MarcoB Sep 22 '15 at 3:55
• @MarcoB THX, Could you give me a simple demo that applied your method? – xyz Sep 22 '15 at 5:48
• Shutao, can you show a specific example of code that contains an "invalid" directive, and that gives you the error you mentioned in the question? In my experience, invalid directives in SplineStyle get silently ignored and no errors are generated. – MarcoB Sep 24 '15 at 18:47
• @MarcoB , For instance, ClosedBSpline[pts, 3, SplineStyle -> Directive[Opacit[0.5], Blue]] or ClosedBSpline[pts, 3, SplineStyle -> 1] will give the warning information:) Please see my update. – xyz Sep 25 '15 at 1:38
• What version of MMA do you run? I am on MMA 10.2 on Win7-64, and your problem examples simply ignore the incorrect directives, without returning an error. See this result. Do you have any special warnings enabled? What does the text of the errors say exactly? – MarcoB Sep 26 '15 at 7:12

Check Menu/Help/DocumentationCenter/PlotStyle/Details. There you will find this table:

listing all acceptable instances.

Have fun!

• Alexei, I'm worried though that the list you refer to might not be complete. You will notice that it says "the directives [...] INCLUDE:". I take it to mean that those are a sample of the most common acceptable ones. It is pretty comprehensive though. – MarcoB Sep 22 '15 at 11:38
• @MarcoB You may be right. May be not though, that is, it may be such an precise expression causing our misunderstanding. A proof would be, if somebody finds a counterexample. I guess, however, that this list might be enough to answer the OP question. – Alexei Boulbitch Sep 22 '15 at 12:11
• Menu/Help/DocumetationCenter/Details does not lead to any useful documentation article. Something important missing before Details? – m_goldberg Sep 22 '15 at 15:23
• @m_goldberg Not that I know. – Alexei Boulbitch Sep 23 '15 at 11:47
• That list isn't complete. At a quick glance it's missing AbsoluteDashing, LABColor, Thick, Dotted, and friends. (ref/Graphics has the more complete table.) – Brett Champion Sep 24 '15 at 4:06

In comments I had proposed the somewhat hackish idea to use a fake call to your actual plotting function itself (i.e. ParametricPlot in your case) to check the PlotStyle expression before using it to generate your actual plot.

I have since realized that unfortunately (or fortunately, depending on the points of view :-) I could not reproduce the behavior you see on version 9 in my installation of MMA v. 10.2, nor do I have have access to a v. 9 installation to play with.

Nevertheless, since there have been no further developments on your question, I decided to cobble together my idea and provide it as is, untested, for what it is worth. It seems to work fine to catch errors in other functions, but I'd appreciate it if you could test it in your case.

I would envision this code block being inserted within your ClosedBSpline module. Of course you will want to provide more appropriate code to handle the incorrect PlotStyle: for instance, on error you could reset ss to a valid default, etc.

Block[
{x, y},
Quiet@Check[
(*fake plot, only here to see if an error is generated*)
(*plot output is suppressed*)
ParametricPlot[1, {x, 0, 1}, {y, 0, 1},
Frame -> None, Axes -> None, PlotStyle -> ss];,

(*Code here that handles the incorrect plotstyle situation*)
Print["The plot style definition is incorrect"],

(*The error(s) to catch*)
Graphics::gprim
]
]


Currently this will only react to the Graphics::gprim message, but you can provide a list of multiple messages to track, or alternatively remove the specification altogether, and it will react to any messages being generated.

Quiet suppresses the printing of the message. I have also prevented the generation of axes and frame in the plot to reduce the amount of wasted work, in hopes to keep the overhead minimal.

• Dear MarcoB, I executed the code you given in MMA 9, however, there is no any warning information. – xyz Sep 29 '15 at 1:48
• @Shutao Thank you for selecting my answer for your bounty! I am sorry to hear that the method I outlined doesn't seem to solve your problem. I hope that somebody else with access to MMA v.9 will weigh in on this as well. – MarcoB Sep 30 '15 at 13:49
• Becacuse @MarcoB I think you have given me some hints and possible useful method:-) – xyz Oct 1 '15 at 9:05