Message Boards Message Boards

0
|
3580 Views
|
10 Replies
|
7 Total Likes
View groups...
Share
Share this post:

How to use a Pattern test on a List of Background Color values?

Posted 2 years ago

ABSTRACT (OPTIONAL)

Helper utilities are needed for a 2-state toggle Button, A few of these buttons will populate a multi-themed DockedCells toolbar in which both are intended for the Function Repository. Several Button properties (Background, FontColor, and label) shall be selected to provide visual cues on the current state of Button action. This offers flexibility and nice alternatives from the generic "Pressed" Appearance. Helpers would validate or autocorrect user-inputs if possible, else issue an error Message. The solution to this Pattern test shall serve as a model for these helpers.

PATTERN CRITERIA

An argument bgs_List?test is constrained to a Length 2 and its elements by a Pattern test limiting them to any combination of all valid Background values: ColorQ, Automatic, or None.

Edit1: Added ABSTRACT; ColorQ replaces: color names (Symbol), RGBColor, GrayLevel.

PASSING CASES

    pass = ToExpression["e" <> ToString[#]] & /@ Range[9];
    e1 = {None, Automatic};
    e2 = {CMYKColor[1, .75, .25, 0], Cyan};
    e3 = {Black, White};
    e4 = {Green, GrayLevel[.333]};
    e5 = {Darker@Brown, RGBColor[.25, .75, .25]};
    e6 = {Lighter@Pink, Hue[.25]};
    e7 = {Hue["DodgerBlue"], White};
    e8 = {CMYKColor["WhiteSmoke"], Hue["Khaki"]};
    e9 = {RGBColor[0, 0, 0], GrayLevel[1]};

FAILING CASES (Auto-correct if possible or issue a Message)

    fail = ToExpression["f" <> ToString[#]] & /@ Range[9];
    f1 = {};
    f2 = {1};
    f3 = {Silver, Black};
    f4 = {.333, GrayLevel[.333]};
    f5 = {"Darker@Brown", RGBColor[.25, .75, .25]};
    f6 = {Lighter@Pink, automatic};
    f7 = {Hue["GoDodgers"], White};
    f8 = {CMYKColor["SmokeMenthol"], Hue["Khaki"]};
    f9 = {RGBColor[0, 0, 0], GrayLevel[1], Darker@Gray, Lighter@Gray};

Edit2: Reworked both lists, added pass and fail, and refactored code.

THE SOLUTION (Thanks to member Hans Milton)

The pattern-test function bgQ with a simple use-case.

bgQ = (ColorQ@# || # == Automatic || # == None) &;

fn[{a_?bgQ, b_?bgQ}] := "Pass";
fn[_] := "Fail";

The proof of concept test function fn on Pass/Fail cases validate bgQ.

fn[#] & /@ pass (* all Pass as expected *)
fn[#] & /@ fail (* all Fail as expected *)

A WORKING EXAMPLE (As discussed in ABSTRACT)

One of the properties selected for providing a visual cue of toggle Button action state is Background. Therefore, a list of 2 values is required. However, if for example some end-users prefer a styled but static Background they can enter the same value for the 2 list elements or take advantage of some corrections made by the helper validator utility valbgs. The default values for both states is Automatic with a "Pressed" Appearance for state-2. "Pressed" only appears when Background is set to None or Automatic. Any Color Directive even White will disable it.

ClearAll[valbgs, bgQ];
bgQ = (ColorQ@# || # == Automatic || # == None) &;
ToggleAutoSaveButton::invoptbg = "Option Backgrounds  \[Rule] `1`, \
expected a valid Background Option value or a List of 1 or 2 values."; 
valbgs[c_?bgQ] := {c, c};
valbgs[{c_?bgQ}] := {c, Automatic};
valbgs[{a_?bgQ, b_?bgQ}] := {a, b};
valbgs[c_] := Message[ToggleAutoSaveButton::invoptbg, c];

None of the following cases meet all the criteria for a list of 2 Background values, however the helper is successful in correcting them all while validating them.

autocor = ToExpression["c" <> ToString[#]] & /@ Range[0, 5];

c0 = None;
c1 = Automatic;
c2 = Red;
c3 = {None};
c4 = {Automatic};
c5 = {Green};

Column@(valbgs[#] & /@ autocor)

The Out[54]= print with extra curly brackets removed, which generated after transferring results here.

{None, None}
{Automatic, Automatic}
{RGBColor[1, 0, 0], RGBColor[1, 0, 0]}
{None, Automatic},
{Automatic, Automatic},
{RGBColor[0, 1, 0], Automatic}

Edit3: Removed speculative pattern form and intended use. Added sections: SOLUTION and WORKING EXAMPLE.

POSTED BY: Jules Manson
10 Replies
Posted 2 years ago

Jules, a suggestion when posting on this forum: When you post a question, leave that post alone and do not edit it (except for correcting typos, unclarities etc).

You have edited your OP in several iterations. The result is that it is now hard for a newcomer to grasp what it is all about. Since the answers you already received may not make sense anymore.

So: If you have new input, post it separately. Do not modify your original question.

POSTED BY: Hans Milton
Posted 2 years ago

My bad, will do. Thanks for the tip. I do a lot of technical writing at work. I have to learn to not be such a perfectionist in informal situations.

POSTED BY: Jules Manson
Posted 2 years ago

This I love. It is exactly what I was looking for. I edited my post to reflect this solution and of course credited you.Thanks again. :-)

POSTED BY: Jules Manson
Posted 2 years ago

Another variant, allowing more color types:

validQ[x_] := Or[ColorQ[x], x == Automatic, x == None]

fn[{a_?validQ, b_?validQ}] := "Ok"

fn[{Red, Hue[0.5]}]
fn[{None, XYZColor[1, 0, 1]}]
POSTED BY: Hans Milton
Posted 2 years ago

Or

pattern = {Repeated[(_GrayLevel | _RGBColor | None | Automatic), {2}]}

e1 = {Black, GrayLevel[.333]};
e2 = {Darker@Green, RGBColor[.25, 1, .25]};
e3 = {None, None};
e4 = {Automatic, None};

f1 = {Red};
f2 = Blue;
f3 = {None, GrayLevel[0]};
f4 = Automatic;
f5 = {Black, GrayLevel[.333], Black};

MatchQ[#, pattern] & /@ {e1, e2, e3, e4}
MatchQ[#, pattern] & /@ {f1, f2, f3, f4, f5}
POSTED BY: Rohit Namjoshi
Posted 2 years ago

Thanks for your input. This I like and it answers my question. But I have since changed criteria. Pattern now only consists of a more robust ColorQ, None, and Automatic.

POSTED BY: Jules Manson
Posted 2 years ago

Assuming that {None, GrayLevel[0]} is a valid input, the following could be a solution:

pattern = Alternatives @@ Tuples[{_RGBColor, _GrayLevel, Automatic, None}, {2}]

fn[x_ /; MatchQ[x, pattern]] := "Ok"
POSTED BY: Hans Milton
Posted 2 years ago

Thanks for the input. Unfortunately, although it works it feels too heavy. Here is the pattern it generates:

pattern = 
 Alternatives @@ Tuples[{_RGBColor, _GrayLevel, Automatic, None}, {2}]

{_RGBColor, _RGBColor} | {_RGBColor, _GrayLevel} | {_RGBColor, 
  Automatic} | {_RGBColor, 
  None} | {_GrayLevel, _RGBColor} | {_GrayLevel, _GrayLevel} | 
{_GrayLevel, Automatic} | {_GrayLevel, 
  None} | {Automatic, _RGBColor} | {Automatic, _GrayLevel} | 
{Automatic, Automatic} | {Automatic, 
  None} | {None, _RGBColor} | {None, _GrayLevel} | {None, 
  Automatic} | {None, None}
POSTED BY: Jules Manson
Posted 2 years ago

Why is {None, GrayLevel[0]} a failing example? It seems to match the criteria

List may only be of Length 2 and consist of any combination of colors name symbols, RGBColor, GrayLevel, Automatic, or None.

POSTED BY: Rohit Namjoshi
Posted 2 years ago

You're right. I meant to write a non-color there. I shall fix it.

POSTED BY: Jules Manson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract