colorList = 
 Flatten[{{Gray, Opacity[0.2]}, {Magenta, Opacity[0.2]}, 
   ColorData["Rainbow"][(# - 1)/5] & /@ Range[5]}]

This creates a list {Grey

But I want a list that joins opacity to the gray and magenta colors: {{Gray,Opacity[0.2]},{Magenta,Opacity[0.2]},"Other colors"}

I tried catenate and partition to try and reform the list into the shape I wanted, but it did not work. Any help would be appreciated.

  • $\begingroup$ The list that you want is the same used as a argument to Flatten? $\endgroup$
    – DIEGO R.
    Commented Apr 10, 2020 at 22:06

1 Answer 1


You can construct your colorlist in a number of alternative ways:

colorList1 = {{Gray, Opacity[0.2]}, {Magenta, Opacity[0.2]}, 
   Sequence @@ (ColorData["Rainbow"][(# - 1)/5] & /@ Range[5])};

colorList2 = Join[{{Gray, Opacity[0.2]}, {Magenta, 
    Opacity[0.2]}}, (ColorData["Rainbow"][(# - 1)/5] & /@ Range[5])];

colorList3 = Flatten[{Opacity[0.2, Gray], Opacity[0.2, Magenta], 
    ColorData["Rainbow"][(# - 1)/5] & /@ Range[5]}];

colorList4 = Flatten[{Directive[Opacity[0.2], Gray], Directive[Opacity[0.2], Magenta], 
    ColorData["Rainbow"][(# - 1)/5] & /@ Range[5]}];

SwatchLegend[colorList1, Range[Length@colorList1]]

enter image description here

SwatchLegend[colorList2, Range[Length@colorList1]]

same picture

SwatchLegend[colorList3, Range[Length@colorList3]]

same picture

SwatchLegend[colorList4, Range[Length@colorList4]]

same picture


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.