# How to create a Disk with smoothly changing coloring?

I want to have a disk. But this disk shouldn't have a fixed color; instead, I'd like it's color to fade smoothly.

Until now, I came up with two ideas how to achieve this.

Idea #1:

smoothlyFadingColorDisk1[r_] :=
DensityPlot[Max[0, r - Sqrt[x^2 + y^2]], {x, -r, r}, {y, -r, r},
ColorFunction -> (Opacity[#, Purple] &)]


Problems:

• It displays axes, and Axes->False won't remove them. This is meant to be just a part of a more complex graphics, so the axes are here completely inaproppiate. (blocking problem)
• This makes no sense semantically. I want a Disk (a simple image), not a plot.

Idea #2:

smoothlyShiftedColorDisk2[r_, n_] :=
Graphics[Flatten@
Prepend[{Thickness[1/(2*n - 1)]}]@
Table[{Opacity[(n - i + 1)/n, Purple],
Circle[{0, 0}, (i - 1/2)*r/(n - 1/2)]}, {i, 1, n}]]


Problems:

• It looks bad at some values of n and horrible at others. This seems to be because for some reason (inaccurate floating point calculations? my mistake in the maths?) the concentric circles are overlapping, and they shouldn't. They should be ideally next to each other. (blocking problem)
• I have to manually set the number of colors used (the n variable, number of concentric cirlces). And I'd like Mathematica to take care of this for me, as happens in the first example.

So - neither of these two ideas acheves what I am trying to achieve. Er... How to do this properly?

I have a feeling that this should be possible to achieve in a much simpler way than I was trying to do this...

Both, DensityPlot and Graphics, with primitives like Circle and Disk, produce Graphics output. I think it is alright implementing your custom graphics.

Here's my take, following your second idea, with a simple control over how the opacity fades.

smooth[a_, R_: 1, n_: 100, hue_: Purple] :=
Graphics@Table[{
Blend[{Append@hue, Append@hue},
Rescale[r, {R, R/n}, {0, 1}]^a],
Disk[{0, 0}, r]}, {r, R, R/n, -R/n}]

GraphicsRow[smooth /@ {1, 2, 3, 4}, ImageSize -> 500] GraphicsRow[smooth[2.5, 1, 50, #] & /@
{Red, Orange, Yellow, Green}, ImageSize -> 500] Update: Discarding alpha channel for opaque hues.

smooth2[a_, R_: 1, n_: 100, hue_: Hue[.65]] :=
Graphics@Table[{
Blend[{White, hue}, Rescale[r, {R, R/n}, {0, 1}]^a],
Disk[{0, 0}, r]}, {r, R, R/n, -R/n}]

GraphicsRow[smooth2 /@ {1, 4/3, 5/3, 2},
ImageSize -> 500] • What you seem to be doing is putting different Disks with different Opacities on top of each other. But this way the bottom disks are still seen through the top disks. This leads to opacity not being visibly changed near the middle of the disk, and then rapidly fade in the borders; as opposed to a smoother fading as seen in bbgodfrey's answer. Or am I wrong? Jan 28 '15 at 22:45
• @gaazkam You are right. The alpha channel could be discarded though. How does that work for you?
– BoLe
Jan 29 '15 at 8:31
• Now they're not transparent at all... and that's not good. Jan 30 '15 at 11:47

Using Frame->False instead of Axes->False for your first approach seems to work well. Another way:

   circim = Graphics[{White, Disk[]}, Background -> Black]; Or

ImageMultiply[RadialGradientImage[{Blue, Yellow, Red}], circim] One can compose such a figure with transparency as:

ImageCompose[EiffelTower, {mybullseye, .5}] where the .5 is the transparency (opacity) of the graded disk.

You can superimpose the bullseye on a simple graphic this way:

ImageCompose[Image[Plot3D[Sin[x y], {x, -2, 2}, {y, -2, 2}]], {mybullseye, .8}] • Nice, but... It seems I can't use Opacity here, can I? This might be important, since this disk is intended to be put on top of another drawing... Jan 28 '15 at 22:26
• Many thanks, but I was rather thinking of something like this: ImageMultiply[RadialGradientImage[{Opacity[1, Purple], Opacity[0, Purple]}], circim] Jan 28 '15 at 22:47
• But that's the problem! In your examples the opacity of the whole picture is constant: be it 1, 0.5 or 0.8. And I want the opacity to smoothly fade in the picture! So that the middle is not transarent at all, but the borders are fully transparent. Jan 28 '15 at 22:55
• @gaazkam: As far as I can tell, Insert, ImageCompose, ImageAdd, ImageSubtract, etc., to not permit space-dependent Opacity, alas. Perhaps there's an undocumented option for this, but I don't know it. Jan 28 '15 at 23:25
• @gaazkam, a bit late, but: just append an alpha channel argument to whatever colors you are using in RadialGradientImage[]; e.g. RadialGradientImage[{Append[Purple, 1], Append[Purple, 0]}]. Aug 13 '16 at 1:04