MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: ColorData etc.

  • To: mathgroup at
  • Subject: [mg82425] Re: ColorData etc.
  • From: P_ter <peter_van_summeren at>
  • Date: Sat, 20 Oct 2007 05:46:08 -0400 (EDT)

I recently had a question about ColorData. An answer to my question came from Eric Wort (Wolfram). Thank you Eric. Also to Jens. Indeed, I could have been more clear in my question. Sorry for that.
I documented the answer and place it here.
Problem:  to color a number of intervals according to their length with ShowLegend.
Note: the example from the Documentation Center does not explain the link between the color scheme and the coloring.

The possibilities of ColorData can be seen by:  ColorData[]. One of the outcomes is "Gradients". 
Each possibility of this outcome can shown by ColorData["Gradients"]. 
One of them is "Rainbow". It will be used here. 
The properties of "Rainbow" can be seen with: ColorData["Rainbow", "Properties"]. 
If the properties are applicable then one can try: ColorData["Rainbow", "Image"]

The example chosen contains three intervals.  In this case the maximum length is 4, the minimum length is 2. 
Colors are needed for lengths 2,3,and 4. They must be projected on the color scheme used by ColorData["Rainbow"].  
All the schemes go from value 0 to value 1. It depends on the color scheme which color is represented by value 0, etc.
The function linecolor gives in reality the length of the interval in question: linecolor[linelist1,3] is 2. 
This value has to be projected on the interval [0,1] from the "Rainbow" scheme. 
Max[] interval length  and Min[] interval length are needed.

linelist1 = {Line[{{5, 1}, {8, 1}}], Line[{{10, 2}, {14, 2}}], Line[{{13, 3}, {15, 3}}]};
maxline1 = Max[linelist1 /. Line[{{x1_, y1_}, {x2_, y2_}}] :> x2 - x1]; 
minline1 = Min[linelist1 /. Line[{{x1_, y1_}, {x2_, y2_}}] :> x2 - x1]; 
linecolor[ll_, x_] := ll[[x]] /. Line[{{x1_, y1_}, {x2_, y2_}}] :> x2 - x1;
ShowLegend needs  graphics. The list <color,interval>  is put in place here according to ColorData["Rainbow"].

    "Rainbow", (linecolor[linelist1, t] - minline1)/(maxline1 - minline1)], linelist1[[t]]}, {t, 1, Length[linelist1]}

The format used here is remarkable. It seems that the format is: ColorData[<Scheme>,<number between 0 and 1>]. 
Indeed: ColorData["Rainbow", 0.2] gives as result RGBColor[0.248488, 0.386326, 0.813373]. 
The documentation suggests that ColorData["Rainbow",{0.2,0.4}] would be possible. It is not. 

ColorData["Rainbow"]//InputForm gives:

 ColorDataFunction["Rainbow", "Gradients", {0, 1}, 
 Blend[{RGBColor[0.471412, 0.108766, 0.527016], RGBColor[0.31106, 0.11758, 
     0.664469], RGBColor[0.250728, 0.225386, 0.769152], 
    RGBColor[0.24408, 0.361242, 0.816084], RGBColor[0.266122, 0.486664, 0.802529], 
    RGBColor[0.305919, 0.585575, 0.739666], RGBColor[0.36048, 0.655759, 0.645692], 
    RGBColor[0.429842, 0.701849, 0.540321], RGBColor[0.513417, 0.72992, 0.440682], 
    RGBColor[0.607651, 0.743718, 0.358588], RGBColor[0.705038, 0.742591, 0.299167], 
    RGBColor[0.794549, 0.721158, 0.260829], RGBColor[0.863512, 0.670771, 0.236564], 
    RGBColor[0.901014, 0.582826, 0.216542], RGBColor[0.902853, 0.453964, 0.192014], 
    RGBColor[0.878107, 0.293208, 0.160481], RGBColor[0.857359, 0.131106, 0.132128]}, 
   #1] & ]

ColorData can act as a function like Hue.

The second part is the legend. It is an option for Graphics. 

{ColorData["Rainbow"] [1 - #1] &, maxline1 - minline1 + 1, ToString[maxline1],
  ToString[minline1], LegendSize -> 1, LegendPosition -> {1, -0.5}}

This is what one would call an inline piece of code, a kind of loop. It functions on itself.
Here it tells: just take the opposite of what you are used to and take [1-#1]&.  Remarkable.

The full code:
     "Rainbow", (linecolor[linelist1, t] - minline1)/(maxline1 - minline1)], 
    linelist1[[t]]}, {t, 1, Length[linelist1]}]],
 {ColorData["Rainbow"] [1 - #1] &, maxline1 - minline1 + 1, 
  ToString[maxline1], ToString[minline1], LegendSize -> 1, 
  LegendPosition -> {1, -0.5}}]

Hopes this helps other people.
with friendly greetings,

  • Prev by Date: Re: WebMathematica and 6.0 problems
  • Next by Date: Re: Adding current style name to window title or status
  • Previous by thread: Re: ColorData etc.
  • Next by thread: ND's implementation is slow