6
$\begingroup$

I am trying to draw a red circle with a colour gradient fading into white on the edge. The way I am currently doing it is to first define a smooth step function and draw a rectangular block with a color gradient defined by the smooth step function:

smoothstep[x_] := Piecewise[{{0, x <= -(1/2)}, {-20*(x + 1/2)^7 + 70*(x + 1/2)^6 - 84*(x + 1/2)^5 + 35*(x + 1/2)^4, -(1/2) < x < 1/2}, {1, x >= 1/2}}]

img = Rasterize[DensityPlot[smoothstep[x], {x, 0, 4}, {y, -4, 4}, ColorFunction -> Function[{x, y}, Hue[1, x, 1]], Frame -> False, PlotRangePadding -> None]

which seems to be successful

enter image description here

Then I applied this colour gradient to a circle as a texture

ParametricPlot[{r*Cos[t], r*Sin[t]}, {r, 0, 1}, {t, 0, 2 Pi}, Mesh -> False, BoundaryStyle -> None, Axes -> False, Frame -> False, PlotStyle -> {Opacity[1], Texture[img]}]

enter image description here

which is mostly successful, apart from 1) the white dot at the centre of the circle, and 2) the faint concentric red line outside.

How do I remove these imperfections in the resulting image? Should I change my approach?

$\endgroup$
1
  • $\begingroup$ Your code sample can't produce the posted image, please double check it. $\endgroup$
    – xzczd
    Commented Mar 15, 2020 at 4:19

2 Answers 2

11
$\begingroup$

One approach would be to draw a red disk and then filter it to blur the edges:

GaussianFilter[Image[Graphics[{White, Disk[], Red, Disk[{0, 0}, 0.5]}]], 20]

enter image description here

This is, more or less, what Photoshop would call "feathering" of the red region into the white background.

$\endgroup$
9
$\begingroup$

Alternative approaches:

1. RadialGradientImage:

RadialGradientImage[{Red, White, White}, ImageSize -> Medium]

enter image description here

2. ParametricPlot + ColorFunction + Blend

ParametricPlot[r {Cos[t], Sin[t]}, {r, 0, 1}, {t, 0, 2 Pi}, 
 Mesh -> None, BoundaryStyle -> None, Axes -> False, Frame -> False, 
 ColorFunction -> (Blend[{Red, White}, #3] &)]

enter image description here

3. ChartElementDataFunction "GradientBubble"

Graphics[ChartElementDataFunction["GradientBubble", 
   "ColorScheme" -> (Lighter[Red, #] & /@ Subdivide[4]), 
   "GradientDirection" -> "Radial"][{{0, 1}, {0, 1}}]]

enter image description here

4. Blur

Blur[Graphics[{Red, Disk[]}, PlotRangePadding -> Scaled[.2]], 50]

enter image description here

$\endgroup$

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.