Rene Brun
On Fri, 25 Jun 1999, Jon Gans wrote:
> // To run: save as color.C, type ".L color.C" then "main()"
>
> // Create a new color palette with colors ranging
> // from blue (low) to red (high).
> //
> // How it works:
> // We have to assign a new color palette to the global
> // instance of TStyle called gStyle.
> // This palette is simply an array of integers which hold
> // the color ID for the referring level.
> // In ROOT colors are created through TColor where they also
> // get their ID assigned.
> // ROOT reserves all colors below 50 and above 100.
> // Here we restrict ourselves to the 50 IDs from 51 to 100.
> //
> // The color model used here is based on the HLS model which
> // is much more suitable for creating palettes than RGB.
> // Fixing the saturation and lightness we can scan through the
> // spectrum of visible light by using "hue" alone.
> // In Root hue takes values from 0 to 360.
> //
> // Problems: Often it is not possible to allocate a new color.
> // This is especially true in X11. ROOT unfortunately gives no
> // direct information on the success of TColor. There is however
> // a workaround which is used here and shall not be explained
> // in detail. If we fail to allocate one color we simply use the
> // last successfully allocated color.
> // This makes the macro independent of the graphics package and
> // the underlying hardware.
> //
>
>
> void palette()
> {
> const float saturation = 1;
> const float lightness = 0.5;
> const float MaxHue = 280;
> const float MinHue = 0;
> const int MaxColors = 50;
> int palette[MaxColors];
> int index;
> float hue, r, g, b, rv, gv, bv;
> TColor *color;
> unsigned int failures = 0;
>
> for (int i=0 ; i<MaxColors ; i++) {
> index = palette[i] = MaxColors+1+i;
> color = new TColor(index, 0, 0, 0);
> hue = MaxHue-(i+1)*((MaxHue-MinHue)/MaxColors);
> color->HLStoRGB(hue, lightness, saturation, r, g, b);
> color->SetRGB(r, g, b);
> gGXW->GetRGB(index, rv, gv, bv);
> if (r != rv || g != gv || b != bv) {
> failures++;
> palette[i] = i ? palette[i-1] : 1;
> }
> }
> if (failures)
> printf("palette(): couldn't allocate %d of %d colors\n", failures, MaxColors);
> gStyle->SetPalette(MaxColors, palette);
> }
>
>
> //
> // The following macro will produce an example figure
> //
> int main()
> {
> gROOT->Reset();
>
> palette(); // Call The Palette Function
> TCanvas *c1 = new TCanvas("c1","Spectrum Palette",200,10,900,500);
> c1->Divide(2,1);
>
> // Generate a 2-D function
> TF2 *f2 = new TF2("f2","exp( -5.*(x^2+y^2)^(1/2))*cos((x^2+y^2)^(1/2)*10.0) ",-1.0,1.0,-1.0,1.0); // Example graph
>
> //The following calls are important to set up the color scheme for the 'f2' calls.
>
> f2->SetContour(48); // This is needed to use all 50 colors. (If above 48 it will cycle through color map).
> f2->SetFillColor(0); // If using a histogram it is not nessesary.
>
> c1->cd(1);
> f2->Draw("SURF1");
>
> c1->cd(2);
> f2->Draw("COLZ");
>
> return 0;
> }
>
>
>
>
>