Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional coloring based on a gradient

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 ?

like image 587
500 Avatar asked Nov 15 '11 20:11

500


People also ask

How do you do gradient in conditional formatting?

Select the range of cells, the table, or the whole sheet that you want to apply conditional formatting to. On the Home tab, under Format, click Conditional Formatting. Point to Data Bars, and then click a gradient fill or a solid fill.

How do you color a sheet of gradient?

On the cell immediately to the side of the initial cell (depends on what direction you want the gradient to move in), type the formula "=(original cell)+1". With the cell still selected, drag that across the surface you want to add your gradient to – the numbers will automatically add up!


2 Answers

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

like image 113
abcd Avatar answered Sep 30 '22 10:09

abcd


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

like image 34
Verbeia Avatar answered Sep 30 '22 10:09

Verbeia