# Extract ColorRange of ContourPlot

I would like to extract the range values Mathematica is assigning colours to in a ListContourPlot (without the range affected by clipping), in order to build custom BarLegends with MaTeX.

I came across this question: ShowLegend values, but since this was already more than 5 years ago, I thought somebody might have a better idea? The function provided in the answer seemingly does not work without modifying in Mathematica 11.3.

Ideas?

Edit

Sorry for not providing an example. Let's look at this plot for instance:

SeedRandom
data = Function[row,
Append[row, RandomReal[{95, 105}]]] /@ (Flatten[
Table[{i, j}, {i, 1, 3}, {j, 1, 3}], 1]);
data[[1, 3]] = 1;
ListContourPlot[data, PlotLegends -> Automatic]


Which gives How can I now find out where Mathematica begins to assign the ColorFunction? Clearly, the value 1 in data[[1,3]] is clipping and therefore not part of the scale. In order to exactly rebuild such a BarLegend, I need the maximum and minimum values for it.

Edit 2

What I finally want, is to rebuild the original legend like so

BarLegend[{ColorData["M10DefaultDensityGradient"], {86, 106}}, 9,
"Ticks" -> Range[88, 104, 2], LegendLabel -> "DIY BarLegend",
ColorFunctionScaling -> True] without having to "guess" the range for the colours (in this case {86,106}) but get them via code, to avoid incorrect Ticks. Because of the clipping, these values won't just be the minimum and maximum of the data anymore, so I'm interested in where Mathematica is clipping.

• Gives us at least an example with corresponding data and code to generate the plot (doesn't have to be your specific case; in fact, a simple one would actually be preferred). – MarcoB May 28 '18 at 17:44

## 1 Answer

If I understand correctly, you'd like to get the RGB values for each of the 10 color swatches in the legend. If so, here's one way to do it. It requires that the ContourPlot has already been created and stored in a symbol. I used your code and o = ListContourPlot[data, PlotLegends -> Automatic].

Clear[legendExtract];
Options[legendExtract] = {"RGBasReal" -> False};
legendExtract[plot_Legended, OptionsPattern[]] := Module[
{legend = plot[[2, 1]], colordata, range, out},
(* Extract colorfunction used in plot and the range of values used in the color scaling *)
{colordata, range} = legend[] /. {b_Blend & :> b[]};
(* Create list of values and colors used in legend *)
out = {#, ColorData[colordata][Rescale[#, range]]} & /@
legend[] /. {{y_Real, _} :> y};
(* If desired, output RGB values as real numbers instead of swatches *)
If[OptionValue["RGBasReal"] == True,
out /. {RGBColor[vals__] :> vals}, out]
]


It has not be thoroughly tested, but seems to work for your test case: • Thanks, that's the right direction and a useful function! I made a second edit to my question to hopefully finally make clear what I want... – T_202 May 28 '18 at 20:25
• The value range in my function has the range you want. You can get this value from o[[2,1,1,2]]. Any solution I propose will require that you make a legended plot first and extract the desired information to remake your bar legend. Others around here are better equipped at the fancy all-in-one solutions. – bobthechemist May 28 '18 at 20:43
• Indeed, I overlooked this. This is neat, thank you! – T_202 May 28 '18 at 20:54