10
$\begingroup$

I am designing an App and I want to use the "Choose Color Scheme" tools inside Button.

enter image description here

I am looking to have this tool in popup window when the Button is clicked but I can't find the command to get this tools.

Any help?

Thank you

$\endgroup$
11
$\begingroup$

Do you mean

Button[
 Tooltip["click", 
  Column[{"Click this button to select a ColorScheme.", 
    "This button will be replaced with ColorData"}]],
 With[{box = EvaluationBox[]}, SelectionMove[box, All, Expression];
  FrontEndExecute@
   FrontEnd`AttachCell[box, 
    FrontEndResource["ColorSchemeSelector"], {1, {Right, Top}}, {Left, Top}, 
   "ClosingActions" -> {"SelectionDeparture", "ParentChanged", "EvaluatorQuit"}]]]

GIF


Based on a comment by the OP a PopupMenu might be more suitable.

DynamicModule[{colorScheme = "Rainbow"}, 
 Row[{PopupMenu[Dynamic[colorScheme], 
    Thread[ColorData["Gradients"] -> 
      MapThread[
       Column[{Style[#1, 9], #2}, Left, 0] &, 
       {ColorData["Gradients"],
         LinearGradientImage[#, {700, 100}] & /@ ColorData["Gradients"]}]]], 
   Spacer[10], 
   Dynamic[colorScheme]}]]

GIF2

$\endgroup$
  • $\begingroup$ Thanks for the Answer but one thing to mention here is when the Color Scheme is selected the Button is gone. So what is the point of using Button then? Isn't the Button should be there and the selected Color Scheme is assigned to variable. $\endgroup$ – Algohi Jul 18 '16 at 23:51
  • 1
    $\begingroup$ The problem is that the selector is set to disappear once you pick a color, @Algohi. (Try it out yourself: CellPrint[FrontEndResource["ColorSchemeSelector"]] and then pick a color scheme you want before clicking "OK".) It would need to be modified if you want it to be persistent; e.g. CellPrint[ FrontEndResource["ColorSchemeSelector"] /. _NotebookDelete :> Sequence[]]. $\endgroup$ – J. M. is in limbo Jul 19 '16 at 0:35
  • 1
    $\begingroup$ @Algohi there is nothing about any variable in the question. $\endgroup$ – Kuba Jul 19 '16 at 8:16
  • $\begingroup$ @Algohi I can only add to the other comments that your question needs more context and a description of the desired behavior. $\endgroup$ – Karsten 7. Jul 19 '16 at 9:36
  • 1
    $\begingroup$ FWIW: the complete list of available gradients is in DataPaclets`ColorDataDump`gradientSchemeNames. $\endgroup$ – J. M. is in limbo Jul 19 '16 at 10:15
7
$\begingroup$

Here is a modification of Karsten's answer attempting to get the behavior you described in a comment.

  • The selected value is assigned to Global`$color (as shown by Dynamic)
  • I made the picker wider to get rid of the annoying bottom scroll bar

Code:

Button["Pick a color",
 Module[{picker, box = EvaluationBox[]},

  picker = FrontEndResource["ColorSchemeSelector"] /. {
     _FrontEndExecute :> (Global`$color = First @ $CellContext`gsel$$),
     {180, Automatic} -> {195, Automatic}
   };

  SelectionMove[box, All, Expression];

  FrontEnd`AttachCell[box, picker, {1, {Right, Top}}, {Left, Top}
    , "ClosingActions" -> {"SelectionDeparture", "ParentChanged", "EvaluatorQuit"}
  ] // FrontEndExecute

 ]
]

Dynamic[Global`$color]

enter image description here


In a comment below Kuba offered a cleaner implementation using a DialogInput; here is a copy for preservation and easier reading. (comments lose tabulation.)

(Now with a correction from Karsten.)

Button["Pick a color",
  $color =
    DialogInput[
      RawBoxes @ FrontEndResource["ColorSchemeSelector"][[1, 1]] /. {
       _FrontEndExecute :> DialogReturn[First @ $CellContext`gsel$$],
       {180, Automatic} -> {195, Automatic},
       _NotebookDelete  :> DialogReturn[$Canceled]
      }
    ]
  , Method -> "Queued"
]
$\endgroup$
  • 3
    $\begingroup$ Or something like: Button["Pick a color", c$olor = DialogInput[ RawBoxes[ FrontEndResource["ColorSchemeSelector"][[1, 1]]] /. {_FrontEndExecute :> (DialogReturn[ First@$CellContext`gsel$$]), {180, Automatic} -> {195, Automatic}}] , Method -> "Queued" ] though I'm not sure why does it take so long to initialize. $\endgroup$ – Kuba Jul 19 '16 at 8:44
  • $\begingroup$ @Kuba Well that's cleaner, isn't it? I wonder if I had started with a blank slate if I would have arrived at that. Anyway the "pop-up" behavior of Karsten's method which I built upon might be desired by some so I'll leave it as is. Are you going to post that as an answer? $\endgroup$ – Mr.Wizard Jul 19 '16 at 8:48
  • $\begingroup$ No, it is too similar to yours, feel free to incorporate it if you think it can be useful. I don't know what is needed but I felt that this approach is closer to old generic Mathematica's features. $\endgroup$ – Kuba Jul 19 '16 at 8:52
  • 1
    $\begingroup$ There is almost no point in using the "ColorSchemeSelector" in the DialogInput version. It's just a PopupMenu and a ´ListPicker`. $\endgroup$ – Karsten 7. Jul 19 '16 at 10:53
  • $\begingroup$ @Karsten7 I don't think I understand. One would have to construct that dialog with a drop-down menu for Gradients, Physical, and Indexed, and then the actual ListPicker menus themselves. Why on Earth would one not start with FrontEndResource["ColorSchemeSelector"] in this case? $\endgroup$ – Mr.Wizard Jul 19 '16 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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