1
$\begingroup$
Graphics[{Red, Rectangle[{0,0},{1,1}]}]}

This can create a red rectangle. Is this possible to replace this Red with a gradient color, for example, change the Opacity from 0 to 1 based on the x coordinate?

$\endgroup$
4
$\begingroup$

RegionPlot and ColorFunction

RegionPlot[True, {x, 0, 1}, {y, 0, 1}, 
 ColorFunction -> (Opacity[#, Red] &), Frame -> False, 
 BoundaryStyle -> None]

enter image description here

VertexColors

Graphics[{Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, 
 VertexColors -> {Opacity[0, Red], Opacity[1, Red], Opacity[1, Red], Opacity[0, Red]}]}]

enter image description here

Raster

Graphics@Raster[Table[{1, 0, 0, y}, {x, 0, 1, 1/100}, {y, 0, 1, 1/100}]]

enter image description here

$\endgroup$
  • $\begingroup$ Where the VertexColors will affect the smoothness of other shape? For example, Show[Graphics[{Polygon[{{0, 0}, {1, 0}, {1, 1}, {0, 1}}, VertexColors -> {Opacity[0, Red], Opacity[1, Red], Opacity[1, Red], Opacity[0, Red]}]}], Graphics[Circle[{0.5, 0.5}, 0.2]]] The circle looks bad. But If I just use Red instead of VertexColors, the circle looks smooth. Mine is Methematica 10.4. $\endgroup$ – Qi Zhong Jul 28 '18 at 4:48
  • $\begingroup$ @QiZhong, I get the same effect in version 9. Don't know how to fix it. You might want to post it as a new question perhaps? $\endgroup$ – kglr Jul 28 '18 at 4:57
  • $\begingroup$ Thanks all the same. I will post as a new question. $\endgroup$ – Qi Zhong Jul 28 '18 at 4:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.