I might be missing something obvious here. I am building a palette for constructing code to make certain graphics. The idea is that my colleagues can click buttons instead of having to know the right commands. So PasteButton[] is an obvious help. Where I have gotten stuck is in color definition. What I want is for the user to be able to select a color on a ColorSlider, and then click a button to paste the RGBColor[] value selected in that ColorSlider at the notebook insertion point. I do not want the code pasted in this way to update subsequently when the user chooses a different color in the ColorSlider

This is what I have so far, but the resulting pasted code does not pick up when the user has selected a different color in the slider. Pasting Dynamic[col] instead of Setting[col] results in a piece of code that changes when a different color is selected in the slider, which as I mentioned is not the desired behaviour.

    Graphics[{Dynamic[col], Rectangle[]}, ImageSize -> 26], 
    Setting@col, ImageSize -> 26, ImageMargins -> 0, FrameMargins -> 0], 
   ColorSlider[Dynamic[col], ImageSize -> {75, 25}, 
   AppearanceElements -> "Spectrum"]}}, Spacings -> 0, Alignment -> Bottom]

enter image description here

The idea is that the user clicks on a color in the spectrum zone, and then clicks the button on the left that looks like a swatch.

How do I modify / rewrite this code to achieve the desired behaviour?

  • 3
    $\begingroup$ Wrap Dynamic around your entire widget. $\endgroup$ – Leonid Shifrin Apr 2 '13 at 7:25
  • $\begingroup$ Yep @LeonidShifrin is right - it's working ;) Maybe this will come handy too Button["color", FrontEndExecute[FrontEndToken["ColorSelectorDialog"]]] $\endgroup$ – Vitaliy Kaurov Apr 2 '13 at 7:28

If you wrap your entire widget (as currently shown in your code), in Dynamic, it will work. The reason is that without it, changes is col are not propagated to PasteButton.


Here is a solution.

I have made 2 modifications on your code:

ButtonPaste[] replaced by Button[] (the Attributes are not the same)
Setting[] (useless) replaced by Paste[]

Full code :

       Graphics[{Dynamic[col], Rectangle[]}, ImageSize -> 26], 
           ImageSize -> 26, 
           ImageMargins -> 0, 
           FrameMargins -> 0], 
           ImageSize -> {75, 25}, 
           AppearanceElements -> "Spectrum"
    Spacings -> 0, 
    Alignment -> Bottom

Button[] has the attribute HoldRest (ButtonPaste[] has not), so the col (of Paste[col]) is only evaluated when the Button is clicked.


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.