5

Please consider :

Manipulate[
Row[{
Graphics[Disk[]], 
Graphics[{
 Polygon[{{0, 0}, {3, 0}, {3, 1}, {0, 1}},
 VertexColors -> {White, Blend[{White, Blue}], 
 Blend[{White, Blue}], White}],
 Black, Thick,
 Line[{{i, 0}, {i, 1}}]}, ImageSize -> 300]}], 
{i, 0, 3}]

enter image description here

Using Szabolcs`s solution on Gradient Filling

How could I color the disk with the color located underneath the Black Line ?

Community
  • 1
  • 1
500
  • 6,509
  • 8
  • 46
  • 80
  • 1
    I'm not sure there is anything specific to v.8 in this. Can any one with v.8 verify this? If not, let's remove [tag:Mathematica-8]. – rcollyer Nov 15 '11 at 21:52
  • @rcollyer. I systematically put it so the answerers know the system I am using to adapt. Make sense ? – 500 Nov 16 '11 at 13:36
  • I understand. I'm just trying to ensure that is a v.8 specific issues, and not something I can answer. (I have v.7.) – rcollyer Nov 16 '11 at 14:40
  • @rcollyer. So shall I tag only Mathematica and precise that I use M8 in the question ? – 500 Nov 16 '11 at 14:53
  • Probably. Although, if you've noticed that you're using a v.8 feature, then the v.8 tag is definitely appropriate. – rcollyer Nov 16 '11 at 14:59
  • How can I check if I am using a V8 feature ? I have only used v8 ! – 500 Nov 16 '11 at 15:15
  • In the help it says how long a function has been in mma at the bottom of the page. As an example, see [this question](http://stackoverflow.com/questions/8140869/minimal-effort-method-for-integrating-c-functions-into-mathematica) which crosses over into v.8 territory by explicitly mentioning new features. – rcollyer Nov 16 '11 at 16:30

2 Answers2

7

Here is one solution which works because the color on the left is White and the gradient is linear.

With[{max = 3, color = Blend[{White, Blue}]}, 
 Manipulate[
  Row[{Graphics[{Opacity[i/max], color, Disk[]}], 
    Graphics[{Polygon[{{0, 0}, {max, 0}, {max, 1}, {0, 1}}, 
       VertexColors -> {White, color, color, White}], Black, Thick, 
      Line[{{i, 0}, {i, 1}}]}, ImageSize -> 300]}], {i, 0, max}]]

enter image description here


If you had two different colors for each end (i.e., something other than White), the Opacity approach won't work. Instead, you can use the optional blending fraction argument to Blend the colors in the desired proportion. Here's an example:

With[{max = 3, color1 = Red, color2 = Green}, 
 Manipulate[
  Row[{Graphics[{Blend[{color1, color2}, i/max], Disk[]}], 
    Graphics[{Polygon[{{0, 0}, {max, 0}, {max, 1}, {0, 1}}, 
       VertexColors -> {color1, color2, color2, color1}], Black, 
      Thick, Line[{{i, 0}, {i, 1}}]}, ImageSize -> 300]}], {i, 0, 
   max}]]

enter image description here

abcd
  • 41,765
  • 7
  • 81
  • 98
6

If you need to do this for a blend of colours other than something and white, Opacity won't be suitable. You could instead stay closer to Szabolcs' original solution using the second argument to Blend like so:

skyBlue = Blend[{White,Blue}];
Manipulate[ Row[{ Graphics[{Blend[{White,skyBlue},i/3], Disk[]}],  
 Graphics[{  Polygon[{{0, 0}, {3, 0}, {3, 1}, {0, 1}},  
 VertexColors -> {White, skyBlue,   
 skyBlue, White}],  Black, Thick,  
 Line[{{i, 0}, {i, 1}}]}, ImageSize -> 300]}],  {i, 0, 3}]

I have divided i by 3 because that parameter is meant to vary between 0 and 1.

enter image description here

Mr.Wizard
  • 24,179
  • 5
  • 44
  • 125
Verbeia
  • 4,400
  • 2
  • 23
  • 44
  • 2
    *sigh* I just can't type fast enough. – Verbeia Nov 15 '11 at 21:01
  • oooh... I addressed this in an edit that was just 1 second _before_ yours! :) – abcd Nov 15 '11 at 21:02
  • I can delete again - you at least have pictures. But I think it's got to the point that I will stop answering questions. Given my time zone and the other things on my plate, it is almost impossible for me to answer a question that you, Leonid, Sjoerd, Mr.Wizard or belisarius - sometimes more than one of you - haven't already answered more thoroughly. – Verbeia Nov 15 '11 at 21:09
  • 4
    It's sometimes helpful to have more than one answer, though, or more than one "let's see where this approach goes". There are different styles of solution, and beginners such as myself find the view from various angles interesting... – cormullion Nov 15 '11 at 21:39
  • @cormullion That's what we normally do here. In this case, they were identical, hence Verbeia's comments above. – abcd Nov 15 '11 at 21:41
  • 1
    @Verbeia, Since we both had the same answer, I'd be more than willing to fix the small `{}` errors in yours, upload a screenshot to it and rollback my answer :) – abcd Nov 15 '11 at 21:42
  • Please don't stop posting. You have already added a lot of interesting things, and I am sure you could continue to do so. – Mr.Wizard Nov 15 '11 at 22:04
  • @yoda - very kind of you to offer and now I see the `{}` typo, I do need to fix that. I don't know enough about gardening to join you over there. But now you know how I got the Marshal badge - I started reviewing when there were no questions I could decently answer here that hadn't already been answered the same way by someone else. I normally look at SO over my morning coffee at the office, but my Mma is at home, so I don't generally have time to answer the ones that need a bit more research. – Verbeia Nov 15 '11 at 22:07
  • @Verbeia I've fixed the typo for you. I also changed the second `Blend` color to the actual color instead of `Blue` (would've been much darker otherwise) and uploaded a screenshot for you :) Feel free to rollback if you would rather I didn't edit it. – abcd Nov 15 '11 at 22:24
  • 2
    @Verbeia Rep points are a nice game, but learning is nicer. Please don't quit answering. I enjoy your posts a lot. – Dr. belisarius Nov 16 '11 at 00:04
  • @belisarius - please don't read this as being about rep points. It's just about not finding many cases where any answer I can think up wouldn't be a duplicate of an existing answer. – Verbeia Nov 16 '11 at 02:51
  • I agree with @cormullion, being able to show the variety of the possible solutions is a good thing. Consider [this question](http://stackoverflow.com/q/7437144/198315) where there were 4 different answers, and none of them used the same methods. Occasionally, we miss and post the same thing. Oh well. – rcollyer Nov 16 '11 at 04:13