1
$\begingroup$

Through this post I've learned how to make a function to map a colour gradient to an interval of numbers like

redblue = 
Blend[Transpose[{{0.5, 4.1`}, {RGBColor[0.986145, 0, 0.0272526], 
  RGBColor[0, 0.0310826, 0.516304]}}], #1] & 

I tried by reversing the Transpose to create a function to do the very opposite...put in an RGBColor between the original limits and get a number. However it failed miserably.

num = Transpose[{{0.5, 4.1`}, {RGBColor[0.986145, 0, 0.0272526], 
 RGBColor[0, 0.0310826, 0.516304]}}] &

How would one create a function to map RGBColor to a number?

$\endgroup$

1 Answer 1

1
$\begingroup$
inversecolorfunc[color_RGBColor, function_] := Module[
  {cd},
  cd[val_Real] := 
   ColorDistance[color, ColorData["TemperatureMap"][val]];
  ReplaceAll[
   x,
   Last@NMinimize[{cd[x], 0 <= x <= 1}, {x}]
   ]]

inversecolorfunc[RandomColor[], ColorData["TemperatureMap"]]
(* 0.0684286 *)

inversecolorfunc[RandomColor[], redblue]
(* 0.330163 *)
$\endgroup$
2
  • $\begingroup$ Ah, the function works nicely, though it doesn't retreive a value from the original boundries of 0.5 to 4.1 in redblue....rather only between 0 and 1, $\endgroup$ Commented Aug 13, 2018 at 17:53
  • $\begingroup$ @morbo By default, color gradients have a single parameter that ranges from 0 to 1. You can easily modify the domain NMinimize[{cd[x], 0,5 <= x <= 4.1}, {x}] $\endgroup$
    – rhermans
    Commented Aug 14, 2018 at 9:12

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.