I was trying to use a custom ColorFunction with Raster and I noticed it gets called seemingly a random number of times for each colour cell.

mycolourfn[a_] := (
i++; Print[i, a];
RGBColor@Append[Take[RotateLeft[a], 3], Last[a]]);

{{1, 0, 0, 1}, {0, 1, 0, 1}, {0, 0, 1, 1}}, 
{{1, 0, 0, 0.5}, {0, 1, 0, 0.5}, {0, 0, 1, 0.5}}}, 
ColorFunction -> mycolourfn]]

1{1.,0.,0.,1.} 2{1.,0.,0.,1.} .... 20{0,1,0,0.5} 21{0,0,1,0.5}

So here mycolourfn gets called 21 times while there are only 6 colour cells and also a different number of times per colour cell. It seems completely random how many times mycolourfn gets called if you change the number of colour cells.

What I wanted to do was create a ColorFunction with a variable like i here, to keep track of what cell it was on and do something slightly different based on where it was. Of course, that didn't quite work out properly. I realised a short while later that this sort of modification is probably best done on the data itself and then passing that to Raster instead of messing around with ColorFunction.

But I'm still curious. Why is it not called the same number of times as there are colour cells?

  • It seems to use 3 or 4 calls per box. Maybe it needs to know the color at the start/end of each box it draws, and so makes calls to ensure that the color hasn't changed. – bill s yesterday
  • That doesn't really make sense as even with just one colour cell 6 calls are used. – void life 35 mins ago

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.