Suppose I create a Module that is expected to return a graphic that I will later use in a plotting procedure (e.g., with Show[]). One of my inputs is an optional input that I want to test (to prevent breaking etc.) inside the Module. This optional input is supposed to be a color (e.g., Red). I am running into several issues that I think should not be issues. Suppose the name of the optional input is lcolor for the discussion below. Here is a very simple analogous example to what I refer.

test[lcolor_: Red] := Module[{}, Red == lcolor]
test[RGBColor[1, 0, 0]]
  1. I can perform a boolean test Red == RGBColor[1,0,0] which results in True. However, if I try Red == RGBColor[0,0,1], Mathematica just returns the input rather than False. Can anyone explain why this is? I tried using Evaluate to force it to compare the two, but received the same result.
  2. Is there an equivalent/parallel to something like NumberQ for colors in Mathematica? I want to test this input to make sure it is a color that will be accepted by Directive or Graphics3D.
    • To try and test the input, I first tried the following example (which worked on the command line): (Context[lcolor] == Context[Red]) && (Attributes[lcolor] == Attributes[Red]). However, if I define lcolor as Red it turns into RGBColor[1,0,0] inside the Module, which causes both Context and Attributes to return errors. However, simple checks like that discussed in (1) do not work, so I am left confused. I even tried to wrap the inputs for Context and Attributes in ToExpression[ToBoxes[], StandardForm] and MakeExpression[ToBoxes[], StandardForm], but neither method worked.
    • Then I tried just forcing lcolor to be an integer and compared it to a list of possible integers, but this is very restrictive. I do not want to explicitly write out (nor can I) every possible color that Mathematica can handle.

Does anyone have any suggestions?

  • 1
    $\begingroup$ I guess you want a pattern that matches colors. $\endgroup$ – user484 Sep 26 '14 at 16:30
  • $\begingroup$ in version 10 there is ColorQ $\endgroup$ – kglr Sep 26 '14 at 17:43
  • $\begingroup$ @kguler Unfortunately, I have v8.0.1 and do not want to spend another several hundred dollars for a personal license. Is there any kludge/hack that could approximate ColorQ in v8? $\endgroup$ – honeste_vivere Sep 26 '14 at 17:50
  • $\begingroup$ @RahulNarain oh thanks, I think the pattern matching link you supplied might work. I will check that out to see if it works. $\endgroup$ – honeste_vivere Sep 26 '14 at 17:55

You have to use SameQ (===):

test[lcolor_: Red] := Module[{}, Red === lcolor]

test /@ {RGBColor[1, 0, 0], Red, Blue}

{True, True, False}

For a full explanation read the Details sections for Equal and SameQ

| improve this answer | |
  • $\begingroup$ First thanks for the clarification. The answer you supplied works for comparison to a specific color. Is there a general test to determine if the user actually supplied an acceptable color? I only used my test[] module as an overly simplified example. Is there a way to test if the user entered something like Red (or Green or Blue etc.) or a graphics directive like RGBColor (or Hue or CMYKColor etc.)? Also, why does Module convert Red to RGBColor[1,0,0]? $\endgroup$ – honeste_vivere Sep 26 '14 at 16:52

My concerns have been mostly addressed here. The correct answer that works is given by:

colorQ = Quiet @ Check[Blend @ {#, Red}; True, False] &;

which can be implement in either of the following methods:

colorQ /@ {Red,Hue[0.5],GrayLevel[0.5],CMYKColor[0,1,1,1/2],Opacity[0.5,Purple],blah}
{True, True, True, True, True, False}
| improve this answer | |

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