Conditional coloring based on a gradient

2019-06-22 15:38发布

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 ?

2条回答
仙女界的扛把子
2楼-- · 2019-06-22 16:29

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

查看更多
我只想做你的唯一
3楼-- · 2019-06-22 16:39

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

查看更多
登录 后发表回答