# How to blend some colors on a curve

I need to define a color gradient on a 3D curve, but I'm having some problems with this.

Suppose we have a 3D curve defined as a parametric function of some real variable phi :

curve[phi_] := {...};


where phi runs from phi1 to phi2. Now, I defined the start, middle and end colors like this :

Color1 := RGBColor[0.99, 0.2, 0.2, 0.2];
Color2 := RGBColor[0.2, 0.99, 0.2, 0.8];
Color3 := RGBColor[0.2, 0.2, 0.99, 0.2];

CurveColor[phi_] = Blend[{Color1, Color2, Color3}, phi];


The last definition doesn't work. I need the function CurveColor[phi] to output four real positive numbers smaller than 1, like this :

CurveColor[phi1] := {0.99, 0.2, 0.2, 0.2}
CurveColor[phi2] := {0.2, 0.2, 0.99, 0.2}


The colors should blend smoothly between Color1 to Color2 to Color3, and be uniformly distributed along the interval phi1 to phi2).

So how should I define that color function ? I don't need that function to be used in a plot3D. I only need the color data as a list of numbers.

Please, notice that I'm working with Mathematica 7.

-
Well, yes, in the sense that I need that function to output a list of four numbers (RGB and Opacity). The colors should be uniformly distributed on the interval phi1 to phi2 only, and blend smoothly. – Cham Apr 3 '13 at 20:27
Let me rephrase, are you planning on doing ParametricPlot3D[curve ..., ColorFunction-> CurveColor]? Or, do you just want the numbers? – rcollyer Apr 3 '13 at 20:28
I just want the numbers. I don't need the function to be used in a plot3D. – Cham Apr 3 '13 at 20:29
Got it. Blending smoothly, though, is a matter of contention, though. Blend uses linear interpolation, so it is "smooth," but not necessarily pretty. – rcollyer Apr 3 '13 at 20:30
I think that a linear interpolation should be fine. – Cham Apr 3 '13 at 20:30

Since you only want the numbers, you can get them by using Apply:

CurveColor[phi_] = List@@Blend[{Color1, Color2, Color3},
Rescale[phi, {phimin, phimax}]]


which will ensure that you always use the same blending functionality that Blend uses. Note, the use of Rescale.

-
The issue is actually that he used = when he should have used :=. – Szabolcs Apr 3 '13 at 20:36
@Szabolcs that is why he is getting the message, but CurveColor still works. So, it isn't entirely the issue. – rcollyer Apr 3 '13 at 20:38
Yes, of course for four numbers the List@@ is important. – Szabolcs Apr 3 '13 at 20:43
This solution is what I was looking for. It's working, thanks a lot ! – Cham Apr 3 '13 at 20:45
Why the Rescale ? – Cham Apr 3 '13 at 21:14

You don't := constant colors.

Color1 = RGBColor[0.99, 0.2, 0.2, 0.5];


This space curve for example:

curve = KnotData["Trefoil", "SpaceCurve"]
(* {Sin[#1] + 2 Sin[2 #1], Cos[#1] - 2 Cos[2 #1], -Sin[3 #1]} & *)

ParametricPlot3D[curve[t], {t, 0, 2 Pi},
PlotStyle -> AbsoluteThickness[10],
ColorFunction -> (CurveColor[#4] &),
ColorFunctionScaling -> True]


Since the curve loops I appended Color1 at the end for color looping.

CurveColor[t_] := Blend[{Color1, Color2, Color3, Color1}, t]


Edit: Color values only.

CurveColor[t_] := List @@ Blend[{Color1, Color2, Color3}, t]


Applying List for a list of numbers instead of a RGBColor expression.

With[{t1 = Pi, t2 = 2 Pi},
CurveColor[(# - t1)/(t2 - t1)] & /@ Range[t1, t2, (t2 - t1)/5.]]

(* {{0.99, 0.2, 0.2, 0.2}, {0.674, 0.516, 0.2, 0.2}, *)
(* {0.358, 0.832, 0.2, 0.2}, {0.2, 0.832, 0.358, 0.26}, *)
(* {0.2, 0.516, 0.674, 0.38}, {0.2, 0.2, 0.99, 0.5}} *)

-
+1 Beat me by a minute – Sjoerd C. de Vries Apr 3 '13 at 20:35
This is working, but how can I extract the color data as a list ? – Cham Apr 3 '13 at 20:39
I was confused with the beginning. What is the point of a curve if not coloring it? – BoLe Apr 3 '13 at 20:46
I need the color data to be exported to an ASCII file, to be used with another program. Maybe my question wasn't clear. The solution below is working great. – Cham Apr 3 '13 at 20:47
Very nice plot above, by the way ! :-) – Cham Apr 3 '13 at 20:49