1

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Cham
  • 4,093
  • 21
  • 36

2 Answers2

11

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]

pplot3d

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}} *)
BoLe
  • 5,819
  • 15
  • 33
7

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.

rcollyer
  • 33,976
  • 7
  • 92
  • 191