基于渐变的条件着色

时间:2011-11-15 20:41:45

标签: graphics wolfram-mathematica

请考虑:

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

Gradient Filling

上使用Szabolcs的解决方案

如何使用位于黑线下面的颜色为磁盘着色?

2 个答案:

答案 0 :(得分:7)

这是一个有效的解决方案,因为左边的颜色是White,渐变是线性的。

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


如果每个端都有两种不同的颜色(即White以外的其他颜色),则Opacity方法将无效。相反,您可以将可选的混合分数参数用于Blend所需比例的颜色。这是一个例子:

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

答案 1 :(得分:6)

如果您需要对除了某些颜色和白色之外的颜色混合执行此操作,Opacity将不适用。你可以使用Blend的第二个参数来更接近Szabolcs的原始解决方案,如下所示:

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除以3,因为该参数在01之间有所不同。

enter image description here