# Break[] inside ColorFunction

f[x_?NumericQ]:= (Do[Break[],{i,1,2}];x)
Plot[x,{x,0,1},ColorFunction->(ColorData["Rainbow"][f[#]]&)]


Break::nofwd: No enclosing For, While, or Do found for Break[].

What's going on here? I've tried setting HoldFirst attribute on f and I've also tried using Unevaluated which gives surprising results:

(* Works fine, gives plot colored as expected *)
Plot[x, {x, 0, 1},
ColorFunction -> (Unevaluated[ColorData["Rainbow"][f[#1]]] &)]

(* Gives a black and white plot with error:
"Unevaluated is not a Graphics primitive or directive" *)
ListContourPlot[IdentityMatrix[3],
ColorFunction->(Unevaluated[ColorData["Rainbow"][f[#1]]]&)]


Edit This on the other hand works:

Plot[x,{x,0,1},ColorFunction->Function[c,ColorData["Rainbow"][f[c]]]]


Which only difference is that it uses Function[c,ColorData["Rainbow"][f[c]]]] instead of Function[ColorData["Rainbow"][f[Slot[1]]]]]

-
very strange. btw, same error in V8 and V9. So not version dependent. – Nasser Dec 17 '12 at 19:33
and you made me crash the kernel too trying it :) screen shot: !Mathematica graphics – Nasser Dec 17 '12 at 19:42
Would Do[] still return Null even when Break[] interrupts it? – Nasser Dec 17 '12 at 20:24
@NasserM.Abbasi Yes – ssch Dec 17 '12 at 20:28
Perhaps, ColorFunction does some checking on the structure of the option (see that it can receive a string, a number, a function, and treat those cases differently). And, for some reason, when it's a function, Break and Return die. However, not when it's a function in the form Function[Null, ...]. Nonsense – Rojo Dec 17 '12 at 20:46
show 8 more comments