# Rounded rectangle with color gradient [duplicate]

Basically I'd like to produce a graphic like this: 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.

• First ideas: You can use Texture or you can write a formula for a rounded rectangle and use RegionFunction with DensityPlot. – Szabolcs Feb 10 '16 at 20:46
• Define "nice edges." EdgeForm[Thickness[0.02]]? – David G. Stork Feb 10 '16 at 21:06
• you can try EdgeForm – Algohi Feb 10 '16 at 21:07
• 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. – Mr.Wizard Feb 13 '16 at 8:13

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}, #[]/(h + r)] & /@ pts)]
}
] 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}]] 