# How to specify continuous color spectrum in a custom BarLegend?

I want to create a custom BarLegend with continuous colors:

Block[{minmax,ticks, cf},
ticks = Function[{min, max},({#,StringForm[" dBa",#]} )&  /@ Range[min,max]];
minmax = {70,90};
cf = ColorData["LightTemperatureMap"][Rescale[#, minmax]] &;
BarLegend[{cf, minmax}, "Ticks" -> ticks @@minmax]
]


Ticks are properly displayed. But the BarLegend is drawn using a solid color. What did I do wrong?

Moving the minmax constant out of the Block construct works:

With[{
minmax = {70,90}
},Block[{ticks, cf},
ticks = Function[{min, max}, ({#,StringForm[" dBa",#]} )&  /@ Range[min,max]];
cf = ColorData["LightTemperatureMap"][Rescale[#, minmax]] &;
BarLegend[{cf, minmax}, "Ticks" -> ticks @@minmax]

]
]


I can't explain why, though.

• If you look at the InputForm of your BarLegend you can see what's going wrong: The minmax inside cf is stored in its unevaluated form inside the legend expression. When the expression is finally displayed, it fails, as minmax is no longer set. (You can think of BarLegend and similar legend expressions as analogues to Graphics expressions - they don't evaluate on their own, they are simply typeset in a non-standard way, resulting in graphical output instead of the raw expressions.) Feb 23, 2020 at 14:12
• Thanks, Lukas. Actually, this is exactly what I did and how I had the idea to wrap the Block inside a With! Unfortunately, I didn't quite understand why minmax was not bound to the actual value. I didn't use Mathematica for a few weeks, and I think I've forgotten (again!) the difference of semantic between Block and With. :/ Feb 23, 2020 at 14:26