I use the following code How can I create an interactive ColorFunction using Manipulate? to create my dynamic color. If I succeded to create the slider between -1 to 1, I can't see the first colors (Blue color) which has the negative value -1. My legend bar (see picture) start from light blue not blue I don't understand why...

My Legend Bar

With[{cmax = 12},(*maximum number of colors*)
     DynamicModule[{k = 6, 
       cols = {Blue, LightBlue, White, White, Yellow, Red}, 
       vals = {-1, -0.41, 0.45, 0.5, 0.75, 1.}}, 
              Style["colors: " <> IntegerString[k], Bold]], Spacer[20], 
             Slider[Dynamic[k, (k = #; cols = PadRight[cols, k, Gray];  
                 vals = Rescale[ArrayPad[vals, {0, k - Length[vals]}, "Extrapolated"]]) &], {2, cmax, 1}]}], 
                 Row[{Dynamic[Style[vals[[#]], Small, Bold]], Spacer[20], 
                   Slider[Dynamic[vals[[#]]], {-1, 1}, 
                    ImageSize -> Small]}]}] &, k], 6, 
             Appearance -> "Horizontal"], TrackedSymbols :> {k}]}], 
         Dynamic[With[{cl = Transpose[{vals, cols}]}, 
           Column[{Panel[LinearGradientImage[Blend[cl, #] &, {600, 60}]], 
             Button["Copy to clipboard", 
              CopyToClipboard[Defer[Blend[cl, f]]], ImageSize -> Medium, 
              Method -> "Queued"]}]]]}]]]]

1 Answer 1


It boils down to how to control the domain used to create the image. As we can see it is {0, 1} by default (see details section in documentation).

LinearGradientImage[ Blend[{{-1, Blue}, {0, Yellow}, {1, Red}}, #] &]

I failed to understand how to assume different domain using DataRange and LinearGradientImage in general so let's proceed with manual Rescale.

You need to edit the line in the code with:

  Blend[cl, Rescale[#, {0, 1}, MinMax[vals]]] &
, {600, 60}

Please let the author of the gui know.

  • $\begingroup$ Ok thank for your help $\endgroup$ Jul 2, 2018 at 10:46
  • $\begingroup$ @Bigprophete: indeed, I designed that GUI with the assumption that one will be feeding values in $[0,1]$ to Blend[], and I did not expect that someone would try to directly modify the lists in the code as opposed to doing the editing via the GUI provided. :) $\endgroup$ Sep 29, 2018 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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