Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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.

Grid[{{PasteButton[
    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?

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

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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 :

   Grid[{{
    Button[
       Graphics[{Dynamic[col], Rectangle[]}, ImageSize -> 26], 
           Paste[col], 
           ImageSize -> 26, 
           ImageMargins -> 0, 
           FrameMargins -> 0], 
       ColorSlider[
           Dynamic[col],
           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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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