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?

  • $\begingroup$ 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. $\endgroup$ – bill s Dec 8 '18 at 19:10
  • $\begingroup$ That doesn't really make sense as even with just one colour cell 6 calls are used. $\endgroup$ – void life Dec 10 '18 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.