12
$\begingroup$

I am trying to customize the color data

ColorData["AvocadoColors"]

enter image description here

I want to make color data function of the gradient starting from the green to the white instead of the green to the yellow. So I want the output such that,

ColorData["MyColorFunction"]

enter image description here

Any suggestions?

$\endgroup$

1 Answer 1

16
$\begingroup$

If you inspect the InputForm of the "ColorFunction" property for "AvocadoColors" you see that it uses Blend with a list of colors:

ColorData["AvocadoColors", "ColorFunction"] // InputForm
ColorDataFunction["AvocadoColors", "Gradients", {0, 1}, Blend["AvocadoColors", #1] &]

Similarly, for "Rainbow" and other gradient color schemes:

ColorData["Rainbow", "ColorFunction"] // InputForm
 ColorDataFunction["Rainbow", "Gradients", {0, 1}, Blend["Rainbow", #1] & ] 

The function DataPaclets`ColorData`GetBlendArgument gives the list of colors used in Blend:

clist = DataPaclets`ColorData`GetBlendArgument["AvocadoColors"]

enter image description here

Now you can replace the last color with White and use with Blend:

mycolorFunc = Blend[Append[Most[clist], White], #] &;

This function retains the yellow tones in the middle of the list. Alternatively, you can have White play the role of Yellow in your new color scheme using blends of the second color with White to construct an alternative list of blending colors:

myclist  = Prepend[FoldList[Blend[{#, White},#2]&, clist[[2]], {1, 2, 3}/3] , clist[[1]]]

enter image description here

mycolorFunc2 = Blend[myclist, #]&;

Examples:

Row[Plot3D[Sin[x y], {x, 0, 3}, {y, 0, 3}, ColorFunction -> #, ImageSize -> Medium]& /@
  {"AvocadoColors", mycolorFunc[#3] &, mycolorFunc2[#3] &}]

enter image description here

Style[Row[LinearGradientImage/@{"AvocadoColors", mycolorFunc, mycolorFunc2}, 
 Spacer[10]], ImageSizeMultipliers->{1,1}]

enter image description here

To add your custom color function to the existing color schemes you can use this answer by Mr. Wizard:

ColorData[1];
new = {{"myColorFunction", "", {}}, {"Gradients"}, 1, {0, 1}, myclist, ""};
AppendTo[DataPaclets`ColorDataDump`colorSchemes,  new];
AppendTo[DataPaclets`ColorDataDump`colorSchemeNames,  "myColorFunction"];

ColorData["myColorFunction"]

enter image description here

Examples:

BarLegend["myColorFunction", LegendLayout -> "Row", LegendMarkerSize -> {400, 100}]

enter image description here

PieChart[Range[10], ColorFunction -> "myColorFunction"]

enter image description here

$\endgroup$
4
  • $\begingroup$ DataPacletsColorDataGetBlendArgument is not working for me, do I need package? It gave me error "Pick::incomp" "Expressions {xxxxxxx} have incompatible shapes $\endgroup$
    – Saesun Kim
    Commented Jul 16, 2019 at 14:45
  • $\begingroup$ @SaesunKim, it works both in v12 (Wolfram Cloud) and in version 9 (Windows10). What version/os are you using? $\endgroup$
    – kglr
    Commented Jul 16, 2019 at 15:52
  • $\begingroup$ I am using 11, let me update the version and try again. Thank you $\endgroup$
    – Saesun Kim
    Commented Jul 16, 2019 at 20:35
  • 1
    $\begingroup$ It is working, not because of the version. I just restart the MMA. Thank you $\endgroup$
    – Saesun Kim
    Commented Jul 16, 2019 at 20:44

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.