1
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ 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. $\endgroup$
    – Cham
    Apr 3, 2013 at 20:27
  • $\begingroup$ Let me rephrase, are you planning on doing ParametricPlot3D[curve ..., ColorFunction-> CurveColor]? Or, do you just want the numbers? $\endgroup$
    – rcollyer
    Apr 3, 2013 at 20:28
  • $\begingroup$ I just want the numbers. I don't need the function to be used in a plot3D. $\endgroup$
    – Cham
    Apr 3, 2013 at 20:29
  • $\begingroup$ Got it. Blending smoothly, though, is a matter of contention, though. Blend uses linear interpolation, so it is "smooth," but not necessarily pretty. $\endgroup$
    – rcollyer
    Apr 3, 2013 at 20:30
  • $\begingroup$ I think that a linear interpolation should be fine. $\endgroup$
    – Cham
    Apr 3, 2013 at 20:30

2 Answers 2

7
$\begingroup$

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.

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

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}} *)
$\endgroup$
5
  • $\begingroup$ This is working, but how can I extract the color data as a list ? $\endgroup$
    – Cham
    Apr 3, 2013 at 20:39
  • $\begingroup$ I was confused with the beginning. What is the point of a curve if not coloring it? $\endgroup$
    – BoLe
    Apr 3, 2013 at 20:46
  • $\begingroup$ 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. $\endgroup$
    – Cham
    Apr 3, 2013 at 20:47
  • $\begingroup$ Very nice plot above, by the way ! :-) $\endgroup$
    – Cham
    Apr 3, 2013 at 20:49
  • $\begingroup$ Yes, one must supply Blend with values from 0 and 1. y(x)=(x-x1)/(x2-x1). ;) $\endgroup$
    – BoLe
    Apr 3, 2013 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.