Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions core/base/inc/TColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#include <vector>

class TArrayI;
class TVirtualX;

class TColor : public TNamed {
friend class TVirtualX; // to let configure colors change handler
protected:
Int_t fNumber; ///< Color number identifier
private:
Expand All @@ -34,6 +36,8 @@ class TColor : public TNamed {
void Allocate();
static Float_t HLStoRGB1(Float_t rn1, Float_t rn2, Float_t huei);

static void ConfigureColorsHandler(Int_t depth, void (*colorfuncptr)(TColor *));

public:
TColor();
TColor(Int_t color, Float_t r, Float_t g, Float_t b, const char *name="", Float_t a = 1);
Expand All @@ -57,7 +61,7 @@ class TColor : public TNamed {
virtual void GetHLS(Float_t &h, Float_t &l, Float_t &s) const
{ h=GetHue(); l=GetLight(); s=GetSaturation(); }
Int_t GetNumber() const { return fNumber; }
ULong_t GetPixel() const;
ULong_t R__DEPRECATED(7,00, "Use gVirtualX->GetPixel(number) instead") GetPixel() const;
Float_t GetRed() const { return IsGrayscale() ? GetGrayscale() : fRed; }
Float_t GetGreen() const { return IsGrayscale() ? GetGrayscale() : fGreen; }
Float_t GetBlue() const { return IsGrayscale() ? GetGrayscale() : fBlue; }
Expand Down Expand Up @@ -86,7 +90,7 @@ class TColor : public TNamed {
static Int_t GetColor(const char *hexcolor);
static Int_t GetColor(Float_t r, Float_t g, Float_t b, Float_t a = 1.);
static Int_t GetColor(Int_t r, Int_t g, Int_t b, Float_t a = 1.);
static Int_t GetColor(ULong_t pixel);
static Int_t R__DEPRECATED(7,00, "Use TVirtualX::Pixel2Number instead") GetColor(ULong_t pixel);
static Int_t GetColorBright(Int_t color);
static Int_t GetColorDark(Int_t color);
static Int_t GetColorTransparent(Int_t color, Float_t a);
Expand All @@ -96,12 +100,12 @@ class TColor : public TNamed {
static const TArrayI& GetPalette();
static Int_t GetLinearGradient(Double_t angle, const std::vector<Int_t> &colors, const std::vector<Double_t> &positions = {});
static Int_t GetRadialGradient(Double_t r, const std::vector<Int_t> &colors, const std::vector<Double_t> &positions = {});
static ULong_t Number2Pixel(Int_t ci);
static ULong_t RGB2Pixel(Int_t r, Int_t g, Int_t b);
static ULong_t RGB2Pixel(Float_t r, Float_t g, Float_t b);
static void Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b);
static void Pixel2RGB(ULong_t pixel, Float_t &r, Float_t &g, Float_t &b);
static const char *PixelAsHexString(ULong_t pixel);
static ULong_t R__DEPRECATED(7,00, "Use TVirtualX::Number2Pixel instead") Number2Pixel(Int_t ci);
static ULong_t R__DEPRECATED(7,00, "Use TVirtualX::RGB2Pixel instead") RGB2Pixel(Int_t r, Int_t g, Int_t b);
static ULong_t R__DEPRECATED(7,00, "Use TVirtualX::RGB2Pixel instead") RGB2Pixel(Float_t r, Float_t g, Float_t b);
static void R__DEPRECATED(7,00, "Use TVirtualX::Pixel2RGB instead") Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b);
static void R__DEPRECATED(7,00, "Use TVirtualX::Pixel2RGB instead") Pixel2RGB(ULong_t pixel, Float_t &r, Float_t &g, Float_t &b);
static const char * R__DEPRECATED(7,00, "Use TVirtualX::PixelAsHexString instead") PixelAsHexString(ULong_t pixel);
static TString SavePrimitiveColor(Int_t ci);
static Bool_t SaveColor(std::ostream &out, Int_t ci);
static void SaveColorsPalette(std::ostream &out);
Expand Down
12 changes: 12 additions & 0 deletions core/base/inc/TVirtualX.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class TVirtualX : public TNamed, public TAttLine, public TAttFill, public TAttTe
protected:
EDrawMode fDrawMode; //Drawing mode

static void ConfigureColorsHandler();

public:
TVirtualX(): fDrawMode() {}
TVirtualX(const char *name, const char *title);
Expand Down Expand Up @@ -371,6 +373,16 @@ class TVirtualX : public TNamed, public TAttLine, public TAttFill, public TAttTe

virtual void MapGCFont(GContext_t, FontStruct_t);

//---- Colors and pixels -----
static ULong_t RGB2Pixel(Int_t r, Int_t g, Int_t b);
static ULong_t RGB2Pixel(Float_t r, Float_t g, Float_t b);
static void Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b);
static void Pixel2RGB(ULong_t pixel, Float_t &r, Float_t &g, Float_t &b);
static TString PixelAsHexString(ULong_t pixel);
static ULong_t Number2Pixel(Int_t ci);
static Int_t Pixel2Number(ULong_t pixel);


static TVirtualX *&Instance();

ClassDefOverride(TVirtualX,0) //ABC defining a generic interface to graphics system
Expand Down
150 changes: 62 additions & 88 deletions core/base/src/TColor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ namespace {

static Int_t gHighestColorIndex = 0; ///< Highest color index defined
static Float_t gColorThreshold = -1.; ///< Color threshold used by GetColor
static Int_t gNColorPlanes = 24; ///< number of color planes, set from TVirtualX
static void (*gColorHandler)(TColor*) = nullptr; ///< callback for handling new colors
static Int_t gDefinedColors = 0; ///< Number of defined colors.
static Int_t gLastDefinedColors = 649; ///< Previous number of defined colors
static Int_t gPaletteType = 0; ///< selected palete type


#define fgGrayscaleMode TColor__GrayScaleMode()
#define fgPalette TColor__Palette()
#define fgPalettesList TColor__PalettesList()
Expand Down Expand Up @@ -1571,7 +1572,9 @@ Bool_t TColor::DefinedColors(Int_t set_always_on)
}

////////////////////////////////////////////////////////////////////////////////
/// Return pixel value corresponding to this color. This pixel value can
/// @deprecated Use gVirtualX->GetPixel(number) instead
///
/// @brief Return pixel value corresponding to this color. This pixel value can
/// be used in the GUI classes. This call does not work in batch mode since
/// it needs to communicate with the graphics system.

Expand Down Expand Up @@ -1878,11 +1881,10 @@ void TColor::SetRGB(Float_t r, Float_t g, Float_t b)
RGBtoHLS(r, g, b, fHue, fLight, fSaturation);
SetTitle(AsHexString());

Int_t nplanes = 16;
if (gVirtualX) gVirtualX->GetPlanes(nplanes);
if (nplanes == 0) nplanes = 16;
Int_t nplanes = gNColorPlanes;
if (nplanes <= 0) nplanes = 16;

// allocate color now (can be delayed when we have a large colormap)
// update color now (can be delayed when we have a large colormap)
#ifndef R__WIN32
if (nplanes < 15)
#endif
Expand Down Expand Up @@ -1922,8 +1924,8 @@ void TColor::SetRGB(Float_t r, Float_t g, Float_t b)

void TColor::Allocate()
{
if (gVirtualX && !gROOT->IsBatch())
gVirtualX->SetRGB(fNumber, GetRed(), GetGreen(), GetBlue());
if (gColorHandler && !gROOT->IsBatch())
gColorHandler(this);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1966,20 +1968,17 @@ Int_t TColor::GetColor(Float_t r, Float_t g, Float_t b, Float_t a)
}

////////////////////////////////////////////////////////////////////////////////
/// Static method returning color number for color specified by
/// @deprecated Use TVirtualX::Pixel2Number instead
///
/// @brief Static method returning color number for color specified by
/// system dependent pixel value. Pixel values can be obtained, e.g.,
/// from the GUI color picker.
///
/// The color retrieval is done using a threshold defined by SetColorThreshold.


Int_t TColor::GetColor(ULong_t pixel)
{
Int_t r, g, b;

Pixel2RGB(pixel, r, g, b);

return GetColor(r, g, b);
return TVirtualX::Pixel2Number(pixel);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2011,6 +2010,19 @@ void TColor::SetColorThreshold(Float_t t)
gColorThreshold = t;
}

////////////////////////////////////////////////////////////////////////////////
/// This method defines number of color planes in the current graphic display
///
/// Normally set by TVirtualX-derived class when display is initialized
/// and used for color precision calculations. To set user-defined precision
/// better use TColor::SetColorThreshold() method

void TColor::ConfigureColorsHandler(Int_t depth, void (*colorfuncptr)(TColor*))
{
gNColorPlanes = depth;
gColorHandler = colorfuncptr;
}

////////////////////////////////////////////////////////////////////////////////
/// Static method returning color number for color specified by
/// r, g and b. The r,g,b should be in the range [0,255].
Expand Down Expand Up @@ -2060,14 +2072,12 @@ Int_t TColor::GetColor(Int_t r, Int_t g, Int_t b, Float_t a)
TIter next(colors);

Float_t thres;
if (gColorThreshold >= 0) {
if (gColorThreshold >= 0)
thres = gColorThreshold;
} else {
Int_t nplanes = 24;
else if ((gNColorPlanes > 0) && (gNColorPlanes <= 16))
thres = 1.0f/31.0f; // 5 bits per color : 0 - 0x1F !
else
thres = 1.0f/255.0f; // 8 bits per color : 0 - 0xFF !
if (gVirtualX) gVirtualX->GetPlanes(nplanes);
if ((nplanes > 0) && (nplanes <= 16)) thres = 1.0f/31.0f; // 5 bits per color : 0 - 0x1F !
}

// Loop over all defined colors
while (auto color = static_cast<TColor *>(next())) {
Expand Down Expand Up @@ -2451,103 +2461,68 @@ void TColor::ListColors(Int_t ci, Int_t nb, Bool_t showEmpty)
}

////////////////////////////////////////////////////////////////////////////////
/// Static method that given a color index number, returns the corresponding
/// @deprecated Use TVirtualX::Number2Pixel instead
///
/// @brief Static method that given a color index number, returns the corresponding
/// pixel value. This pixel value can be used in the GUI classes. This call
/// does not work in batch mode since it needs to communicate with the
/// graphics system.

ULong_t TColor::Number2Pixel(Int_t ci)
{
TColor::InitializeColors();
TColor *color = gROOT->GetColor(ci);
if (color)
return color->GetPixel();
else
::Warning("TColor::Number2Pixel", "color with index %d not defined", ci);

return 0;
return TVirtualX::Number2Pixel(ci);
}

////////////////////////////////////////////////////////////////////////////////
/// Convert r,g,b to graphics system dependent pixel value.
/// @deprecated Use TVirtualX::RGB2Pixel instead
///
/// @brief Convert r,g,b to graphics system dependent pixel value.
/// The r,g,b triplet must be [0,1].

ULong_t TColor::RGB2Pixel(Float_t r, Float_t g, Float_t b)
{
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
if (r > 1) r = 1;
if (g > 1) g = 1;
if (b > 1) b = 1;

ColorStruct_t color;
color.fRed = UShort_t(r * 65535);
color.fGreen = UShort_t(g * 65535);
color.fBlue = UShort_t(b * 65535);
color.fMask = kDoRed | kDoGreen | kDoBlue;
gVirtualX->AllocColor(gVirtualX->GetColormap(), color);
return color.fPixel;
return TVirtualX::RGB2Pixel(r, g, b);
}

////////////////////////////////////////////////////////////////////////////////
/// Convert r,g,b to graphics system dependent pixel value.
/// @deprecated Use TVirtualX::RGB2Pixel instead
///
/// @brief Convert r,g,b to graphics system dependent pixel value.
/// The r,g,b triplet must be [0,255].

ULong_t TColor::RGB2Pixel(Int_t r, Int_t g, Int_t b)
{
if (r < 0) r = 0;
if (g < 0) g = 0;
if (b < 0) b = 0;
if (r > 255) r = 255;
if (g > 255) g = 255;
if (b > 255) b = 255;

ColorStruct_t color;
color.fRed = UShort_t(r * 256); // 65536/256
color.fGreen = UShort_t(g * 256);
color.fBlue = UShort_t(b * 256);
color.fMask = kDoRed | kDoGreen | kDoBlue;
gVirtualX->AllocColor(gVirtualX->GetColormap(), color);
return color.fPixel;
return TVirtualX::RGB2Pixel(r, g, b);
}

////////////////////////////////////////////////////////////////////////////////
/// Convert machine dependent pixel value (obtained via RGB2Pixel or
/// @deprecated Use TVirtualX::Pixel2RGB instead
///
/// @brief Convert machine dependent pixel value (obtained via RGB2Pixel or
/// via Number2Pixel() or via TColor::GetPixel()) to r,g,b triplet.
/// The r,g,b triplet will be [0,1].

void TColor::Pixel2RGB(ULong_t pixel, Float_t &r, Float_t &g, Float_t &b)
{
ColorStruct_t color;
color.fPixel = pixel;
gVirtualX->QueryColor(gVirtualX->GetColormap(), color);
r = (Float_t)color.fRed / 65535.0f;
g = (Float_t)color.fGreen / 65535.0f;
b = (Float_t)color.fBlue / 65535.0f;
TVirtualX::Pixel2RGB(pixel, r, g, b);
}

////////////////////////////////////////////////////////////////////////////////
/// Convert machine dependent pixel value (obtained via RGB2Pixel or
/// @deprecated Use TVirtualX::Pixel2RGB instead
///
/// @brief Convert machine dependent pixel value (obtained via RGB2Pixel or
/// via Number2Pixel() or via TColor::GetPixel()) to r,g,b triplet.
/// The r,g,b triplet will be [0,255].

void TColor::Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b)
{
ColorStruct_t color;
color.fPixel = pixel;
gVirtualX->QueryColor(gVirtualX->GetColormap(), color);
// color is between 0 and 65535 inclusive.
// We need to move it to the 0 to 255 range (inclusive).
// So we need to resample the 65536 values into 256 indices.
// This would mean equal blocks of 256 high-res values per color index.
r = color.fRed / 256;
g = color.fGreen / 256;
b = color.fBlue / 256;
TVirtualX::Pixel2RGB(pixel, r, g, b);
}

////////////////////////////////////////////////////////////////////////////////
/// Convert machine dependent pixel value (obtained via RGB2Pixel or
/// @deprecated Use TVirtualX::PixelAsHexString instead
///
/// @brief Convert machine dependent pixel value (obtained via RGB2Pixel or
/// via Number2Pixel() or via TColor::GetPixel()) to a hexadecimal string.
/// This string can be directly passed to, for example,
/// TGClient::GetColorByName(). String will be reused so copy immediately
Expand All @@ -2556,10 +2531,8 @@ void TColor::Pixel2RGB(ULong_t pixel, Int_t &r, Int_t &g, Int_t &b)
const char *TColor::PixelAsHexString(ULong_t pixel)
{
static TString tempbuf;
Int_t r, g, b;
Pixel2RGB(pixel, r, g, b);
tempbuf.Form("#%02x%02x%02x", r, g, b);
return tempbuf;
tempbuf = TVirtualX::PixelAsHexString(pixel);
return tempbuf.Data();
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -2617,16 +2590,17 @@ Bool_t TColor::IsGrayscale()

void TColor::SetGrayscale(Bool_t set /*= kTRUE*/)
{
if (fgGrayscaleMode == set) return;
if (fgGrayscaleMode == set)
return;

fgGrayscaleMode = set;

if (!gVirtualX || gROOT->IsBatch()) return;
if (gROOT->IsBatch())
return;

TColor::InitializeColors();
TIter iColor(gROOT->GetListOfColors());
TColor* color = nullptr;
while ((color = (TColor*) iColor()))
TIter iter(gROOT->GetListOfColors());
while (auto color = static_cast<TColor *> (iter()))
color->Allocate();
}

Expand Down
Loading
Loading