5
$\begingroup$

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...

$\endgroup$
10
$\begingroup$

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[0]@hue, Append[1]@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]

enter image description here

GraphicsRow[smooth[2.5, 1, 50, #] & /@
  {Red, Orange, Yellow, Green}, ImageSize -> 500]

enter image description here

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]

enter image description here

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – gaazkam Jan 28 '15 at 22:45
  • $\begingroup$ @gaazkam You are right. The alpha channel could be discarded though. How does that work for you? $\endgroup$ – BoLe Jan 29 '15 at 8:31
  • $\begingroup$ Now they're not transparent at all... and that's not good. $\endgroup$ – gaazkam Jan 30 '15 at 11:47
8
$\begingroup$

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

plot

$\endgroup$
5
$\begingroup$

Another way:

   circim = Graphics[{White, Disk[]}, Background -> Black];
   ImageMultiply[LinearGradientImage[{Blue, Yellow, Red}], circim]

enter image description here

Or

ImageMultiply[RadialGradientImage[{Blue, Yellow, Red}], circim]

enter image description here

One can compose such a figure with transparency as:

ImageCompose[EiffelTower, {mybullseye, .5}]

enter image description here

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}]

enter image description here

$\endgroup$
  • $\begingroup$ 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... $\endgroup$ – gaazkam Jan 28 '15 at 22:26
  • $\begingroup$ Many thanks, but I was rather thinking of something like this: ImageMultiply[RadialGradientImage[{Opacity[1, Purple], Opacity[0, Purple]}], circim] $\endgroup$ – gaazkam Jan 28 '15 at 22:47
  • $\begingroup$ 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. $\endgroup$ – gaazkam Jan 28 '15 at 22:55
  • $\begingroup$ @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. $\endgroup$ – David G. Stork Jan 28 '15 at 23:25
  • 1
    $\begingroup$ @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]}]. $\endgroup$ – J. M. will be back soon Aug 13 '16 at 1:04

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.