Problem with Raster and custom ColorTable

5

2

Context

Let me define two colour tables: (which come from brewer and xmedcom respectively)

   GoldColor = Blend[{{0, Black}, 
   {1/9, RGBColor[32/97, 1/62, 0]}, 
   {2/5, RGBColor[44/59, 23/78, 1/32]}, 
   {3/5, RGBColor[84/85, 1/2, 4/51]}, 
   {2/3, RGBColor[84/85, 53/87, 1/10]}, 
   {4/5, RGBColor[84/85, 4/5, 11/32]}, 
   {9/10, RGBColor[84/85, 68/75, 46/75]}, 
   {1, White}}, #1] & 

and

GalColor = Blend[{
     {0, RGBColor[7/11, 0, 1/7]}, 
     {1/17, RGBColor[10/13, 1/9, 1/7]}, 
     {31/255, RGBColor[7/8, 1/4, 1/6]}, 
     {47/255, RGBColor[13/14, 5/13, 1/4]}, 
     {21/85,RGBColor[31/32, 5/9, 4/13]}, 
     {79/255,RGBColor[46/47, 7/10, 2/5]}, 
     {19/51, RGBColor[46/47, 5/6, 9/17]}, 
     {37/85, RGBColor[1, 10/11, 5/8]}, 
     {127/255, RGBColor[1, 1, 3/4]},
     {143/255, RGBColor[10/11, 22/23, 7/8]}, 
     {53/85, RGBColor[5/6, 11/12, 17/18]}, 
     {35/51, RGBColor[7/10, 6/7, 10/11]}, 
     {191/255, RGBColor[4/7, 10/13, 6/7]}, 
     {69/85, RGBColor[3/7, 21/32, 4/5]}, 
     {223/255, RGBColor[1/3, 8/15, 11/15]}, 
     {239/255,RGBColor[1/4, 5/13, 2/3]}, 
     {1, RGBColor[1/5, 3/13, 3/5]}}, #1] & 

which looks like this:

GraphicsRow[{ContourPlot[x y , {x, -1, 1}, {y, -1, 1}, 
   ColorFunction -> GalColor],
  ContourPlot[x y , {x, -1, 1}, {y, -1, 1}, 
   ColorFunction -> GoldColor]}]

Mathematica graphics

But if I use it on large data sets, the first one works fine

 dat = RandomReal[{0, 1}, {512, 512}];
 Graphics[Raster[dat, ColorFunction -> GoldColor]]

Mathematica graphics

whereas the second one fails with Raster

 Graphics[Raster[dat, ColorFunction -> GalColor]]

 (* void plot *)

while it works fine with MatrixPlot

Mathematica graphics

Note that the problem depends on the size of dat; e.g. it works fine with 256^2.

Question

How come Raster fails on this color table?

chris

Posted 2013-03-16T16:05:35.100

Reputation: 20 653

Both colour functions fail for me with Raster. A workaround is to inject the evaluated colour data into Function using With : GalColor = With[{x = big list}, Blend[x, #]&] – Simon Woods – 2013-03-16T17:02:04.633

Indeed it does. May be you should write it up as an answer? – chris – 2013-03-16T17:23:34.180

Intriguingly it is also much faster! @Mr.Wizard would this answer my other question? Why? – chris – 2013-03-16T17:26:54.637

Answers

5

Both colour functions fail for me with Raster.

I don't have an answer to why it happens, but I do have a workaround, which is to inject the evaluated colour data into Function using With:

GoldColor = With[{x = {{0, Black}, 
{1/9, RGBColor[32/97, 1/62, 0]}, 
{2/5, RGBColor[44/59, 23/78, 1/32]}, 
{3/5, RGBColor[84/85, 1/2, 4/51]}, 
{2/3, RGBColor[84/85, 53/87, 1/10]}, 
{4/5, RGBColor[84/85, 4/5, 11/32]}, 
{9/10, RGBColor[84/85, 68/75, 46/75]}, 
{1, White}}}, Blend[x, #]& ]

The only difference between the function defined this way, and the original, is that the evaluation converts the fractions to Rational, e.g. Times[7, Power[11, -1]] becomes Rational[7,11]. Why this makes a difference which affects only Raster, and only for large data sets, I don't know.

Simon Woods

Posted 2013-03-16T16:05:35.100

Reputation: 81 905

5

Simon's method does seem to work, and I cannot explain why either. However, I recommend a different method. I suggest you convert all the exact numbers to machine precision as this typically evaluates much faster. In fact my fix for your problem with the other question was to add N so that the array could be packed, and the same method works here.

Using your original definition for GalColor:

dat = RandomReal[{0, 1}, {512, 512}];

Graphics[Raster[dat, ColorFunction -> N@GalColor]]

et voila:

Mathematica graphics

A two character fix. How about that? :-)

Mr.Wizard

Posted 2013-03-16T16:05:35.100

Reputation: 259 163

Had a similar problem, and this helped just magically. Thank you! – Alexey Bobrick – 2013-09-19T20:36:28.663

@Alexey Glad I could help. :-) – Mr.Wizard – 2013-09-19T20:37:47.977

ah ! the only reason I was using rationals is to make the function more compact... – chris – 2013-03-16T19:28:30.957