Question

I have seen an answer to this problem for plots, but I want to do it for a general graphics box.

Graphics[{Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]}, 
 Background -> Green]

I would like to make the background of this graphic resemble one of the color palette gradients. Ideally I would type "RainbowColors" somewhere in the code and the background would look like the RainbowColors palette.

I could remove the background and make a rectangle with a gradient fill, but it's not exactly what I'd like. I'm hoping someone has a beautiful bit of knowledge.

Thanks so much!!

Was it helpful?

Solution

The closest I am aware of is this combination of Prolog, Polygon, VertexColors, and Scaled coordinates:

Graphics[
 {Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]},
 Prolog ->
  Polygon[
   Scaled /@ {{0, 0}, {0, 1}, {1, 1}, {1, 0}},
   VertexColors -> {Yellow, Green, Magenta, Pink}
  ]
]

enter image description here

More complicated gradients can be formed by stacking Polygons:

Graphics[
 {Disk[], Red, Rectangle[{-.75, 1}, {.5, -.5}]},
 Prolog ->
  {Polygon[
    Scaled /@ {{0, 0}, {0, 1/2}, {1, 1/2}, {1, 0}},
    VertexColors -> {Red, Yellow, Pink, Blue}
   ],
   Polygon[
    Scaled /@ {{0, 1/2}, {0, 1}, {1, 1}, {1, 1/2}},
    VertexColors -> {Yellow, Green, Magenta, Pink}
   ]}
]

enter image description here

If you struggle with turning this into a function let me know where you had trouble and I will try to help.


Please ask your future questions on the dedicated Mathematica StackExchange site:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top