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.