3
$\begingroup$

This question already has an answer here:

Basically I'd like to produce a graphic like this:

rectangle

That is a rectangle with rounded corners, filled with a color gradient. While Rectangle offers the RoundingRadius option that takes care of the round corners, there is no obvious way of getting the color gradient to work. Using Polygon it's easy to implement the gradient, but I can only think of very messy ways of rounding the corners. Can I have both?

Update

I just found this answer. That's very close to what I want, but that solution doesn't allow for nice edges.

$\endgroup$

marked as duplicate by Mr.Wizard Feb 13 '16 at 8:13

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ First ideas: You can use Texture or you can write a formula for a rounded rectangle and use RegionFunction with DensityPlot. $\endgroup$ – Szabolcs Feb 10 '16 at 20:46
  • 1
    $\begingroup$ Define "nice edges." EdgeForm[Thickness[0.02]]? $\endgroup$ – David G. Stork Feb 10 '16 at 21:06
  • $\begingroup$ you can try EdgeForm $\endgroup$ – Algohi Feb 10 '16 at 21:07
  • $\begingroup$ This question is a duplicate. You can just use e.g. BoundaryStyle -> Directive[Red, Thickness[0.01]] in the Accepted solution to get what you want. $\endgroup$ – Mr.Wizard Feb 13 '16 at 8:13
4
$\begingroup$

This may be messy indeed, but it gets us scalable vector graphics:

w = 10;
r = 1.2;
h = 3.5;
dt = π/40;
pts =
  {
   {0, 0}, {w, 0},
   Sequence@@Table[{w, 0} + r { Sin[t], 1 - Cos[t]}, {t, dt, π/2 - dt, dt}],
   {w + r, r}, {w + r, h},
   Sequence@@Table[{w, h} + r { Cos[t], Sin[t]}, {t, dt, π/2 - dt, dt}],
   {w, h + r}, {0, h + r},
   Sequence@@Table[{0, h} + r { -Sin[t], Cos[t]}, {t, dt, π/2 - dt, dt}],
   {-r, h}, {-r, r},
   Sequence@@Table[{0, r} - r { Cos[t], Sin[t]}, {t, dt, π/2 - dt, dt}]
  };

Graphics[
  {
    EdgeForm[{Black, Thickness[0.02]}], 
    Polygon[pts, VertexColors -> (Blend[{White, Red}, #[[2]]/(h + r)] & /@ pts)]
  }
]

Mathematica graphics

$\endgroup$
3
$\begingroup$

Using a tweaked version of my answer here and a graphics expression which draws the rectangle outline separately:

texturedShape[img_, shape_] := 
 Module[{g, p, ar, i}, g = Graphics[shape, PlotRangePadding -> 0];
  p = Polygon[AbsoluteOptions[g, PlotRange][[1, 2]] /.
    {{l_, r_}, {b_, t_}} :> {{l, b}, {l, t}, {r, t}, {r, b}},
    VertexTextureCoordinates -> {{0, 0}, {0, 1}, {1, 1}, {1, 0}}];
  ar = AbsoluteOptions[g, AspectRatio][[1, 2]];
  i = SetAlphaChannel[img, ColorNegate@Rasterize[g, ImageSize -> ImageDimensions@img]];
  {Texture[ImageData@i], p}]

With[{
  rect = Rectangle[{0, 0}, {2, 1}, RoundingRadius -> 0.2],
  tex = LinearGradientImage[{Top, Bottom} -> {Red, White}, {200, 100}]},
 Graphics[{
   (* inside  *) texturedShape[tex, rect],
   (* outline *) FaceForm[None], EdgeForm[{Thickness[0.02], Black}], rect}]]

enter image description here

$\endgroup$

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