Skip to content

Commit

Permalink
[core] Add color setter function overloads taking stings
Browse files Browse the repository at this point in the history
These new overloads are intended for use via Python.

This generalizes a pythonization that has been so far localized to
RooFit.
  • Loading branch information
guitargeek committed Nov 25, 2024
1 parent ab46cb8 commit 069d935
Show file tree
Hide file tree
Showing 19 changed files with 136 additions and 64 deletions.
3 changes: 3 additions & 0 deletions core/base/inc/TAttAxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "Rtypes.h"

class TColorNumber;

class TAttAxis {
protected:
Expand Down Expand Up @@ -62,6 +63,8 @@ class TAttAxis {
virtual void SetTitleColor(Color_t color=1); // *MENU*
virtual void SetTitleFont(Style_t font=62); // *MENU*

void SetLabelColor(TColorNumber lcolor);

ClassDef(TAttAxis,4); //Axis attributes
};

Expand Down
3 changes: 3 additions & 0 deletions core/base/inc/TAttFill.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "Rtypes.h"

class TColorNumber;

class TAttFill {

Expand All @@ -38,6 +39,8 @@ class TAttFill {
virtual void SetFillColorAlpha(Color_t fcolor, Float_t falpha);
virtual void SetFillStyle(Style_t fstyle) { fFillStyle = fstyle; } ///< Set the fill area style

void SetFillColor(TColorNumber);

ClassDef(TAttFill,2) //Fill area attributes
};

Expand Down
4 changes: 4 additions & 0 deletions core/base/inc/TAttLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "Rtypes.h"

class TColorNumber;

class TAttLine {

protected:
Expand Down Expand Up @@ -42,6 +44,8 @@ class TAttLine {
virtual void SetLineStyle(Style_t lstyle) { fLineStyle = lstyle;} ///< Set the line style
virtual void SetLineWidth(Width_t lwidth) { fLineWidth = lwidth;} ///< Set the line width

void SetLineColor(TColorNumber lcolor);

ClassDef(TAttLine,2); //Line attributes
};

Expand Down
3 changes: 3 additions & 0 deletions core/base/inc/TAttMarker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "Rtypes.h"

class TColorNumber;

class TAttMarker {

Expand Down Expand Up @@ -47,6 +48,8 @@ class TAttMarker {
static Style_t GetMarkerStyleBase(Style_t style);
static Width_t GetMarkerLineWidth(Style_t style);

void SetMarkerColor(TColorNumber lcolor);

ClassDef(TAttMarker,3); //Marker attributes
};

Expand Down
4 changes: 4 additions & 0 deletions core/base/inc/TAttText.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "Rtypes.h"

class TColorNumber;

class TAttText {

protected:
Expand Down Expand Up @@ -47,6 +49,8 @@ class TAttText {
virtual void SetTextSize(Float_t tsize=1) { fTextSize = tsize;} ///< Set the text size
virtual void SetTextSizePixels(Int_t npixels); ///< Set the text size in pixel

void SetTextColor(TColorNumber lcolor);

ClassDef(TAttText,2) //Text attributes
};

Expand Down
11 changes: 11 additions & 0 deletions core/base/inc/TColor.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,16 @@ class TColor : public TNamed {
kWaterMelon=108, kCool=109, kCopper=110,
kGistEarth=111, kViridis=112, kCividis=113,
kRainbow=kRainBow, kDarkRainbow=kDarkRainBow};

class TColorNumber {
public:
TColorNumber(Int_t color) : fNumber{color} {}
TColorNumber(std::string const &color);
Int_t number() const { return fNumber; }

private:
Int_t fNumber; ///< Color number identifier
};

#endif

5 changes: 5 additions & 0 deletions core/base/src/TAttAxis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,8 @@ void TAttAxis::Streamer(TBuffer &R__b)
R__b.WriteClassBuffer(TAttAxis::Class(),this);
}
}

void TAttAxis::SetLabelColor(TColorNumber lcolor)
{
SetLabelColor(lcolor.number());
}
5 changes: 5 additions & 0 deletions core/base/src/TAttFill.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,8 @@ void TAttFill::SetFillColorAlpha(Color_t fcolor, Float_t falpha)
{
fFillColor = TColor::GetColorTransparent(fcolor, falpha);
}

void TAttFill::SetFillColor(TColorNumber lcolor)
{
SetFillColor(lcolor.number());
}
5 changes: 5 additions & 0 deletions core/base/src/TAttLine.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,8 @@ void TAttLine::SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
{
fLineColor = TColor::GetColorTransparent(lcolor, lalpha);
}

void TAttLine::SetLineColor(TColorNumber lcolor)
{
SetLineColor(lcolor.number());
}
5 changes: 5 additions & 0 deletions core/base/src/TAttMarker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,8 @@ void TAttMarker::SetMarkerColorAlpha(Color_t mcolor, Float_t malpha)
{
fMarkerColor = TColor::GetColorTransparent(mcolor, malpha);
}

void TAttMarker::SetMarkerColor(TColorNumber lcolor)
{
SetMarkerColor(lcolor.number());
}
5 changes: 5 additions & 0 deletions core/base/src/TAttText.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -432,3 +432,8 @@ void TAttText::SetTextSizePixels(Int_t npixels)
fTextSize = dy/(pad->GetY2() - pad->GetY1());
}
}

void TAttText::SetTextColor(TColorNumber lcolor)
{
SetTextColor(lcolor.number());
}
43 changes: 41 additions & 2 deletions core/base/src/TColor.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
#include "TApplication.h"
#include "TColorGradient.h"
#include "snprintf.h"

#include <algorithm>
#include <cmath>
#include <iostream>
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>

ClassImp(TColor);

Expand Down Expand Up @@ -2424,7 +2427,7 @@ void TColor::ListColors(Int_t ci, Int_t nb, Bool_t showEmpty)
printf(" +------+-------+-------+-------+-------+--------------------+--------------------+\n");
printf(" | Number of possible colors = %4d |\n",ncolors);
printf(" | Number of defined colors between %4d and %4d = %4d |\n",ci,last,nc);
printf(" | Number of free indeces between %4d and %4d = %4d |\n",ci,last,last-ci-nc);
printf(" | Number of free indices between %4d and %4d = %4d |\n",ci,last,last-ci-nc);
printf(" +--------------------------------------------------------------------------------+\n\n");

}
Expand Down Expand Up @@ -3642,3 +3645,39 @@ void TColor::InvertPalette()
{
std::reverse(fgPalette.fArray, fgPalette.fArray + fgPalette.GetSize());
}

/// The `color` string argument argument will be evaluated
/// to get the actual ROOT color number, like `kRed`.
/// Here is how the string is parsed:
///
/// 1. Match against single-character color codes following the matplotlib convention.
/// For example, to get red color (equivalent to `kRed`):
/// ~~~ {.cxx}
/// hist.SetLineColor("r")
/// ~~~
/// 2. Check if the string matches an en existing TColor name (see TColor::GetColorByName()).
/// For example:
/// ~~~ {.cxx}
/// hist.SetLineColor("kRed+1")
/// ~~~
///
/// In case no corresponding color is found, a `std::invalid_argument` exception is thrown.
TColorNumber::TColorNumber(std::string const &color)
{
using Map = std::unordered_map<std::string, Int_t>;
// Color dictionary to define matplotlib conventions
static Map colorMap{{"r", kRed}, {"b", kBlue}, {"g", kGreen}, {"y", kYellow},
{"w", kWhite}, {"k", kBlack}, {"m", kMagenta}, {"c", kCyan}};
auto found = colorMap.find(color);
if (found != colorMap.end()) {
fNumber = found->second;
return;
}

fNumber = TColor::GetColorByName(color.c_str());
if (fNumber == -1) {
std::stringstream msg;
msg << "\"" << color << "\" is not a valid color name";
throw std::invalid_argument(msg.str());
}
}
2 changes: 2 additions & 0 deletions graf2d/gpad/inc/TRatioPlot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "TFitResultPtr.h"

class TColorNumber;
class TH1;
class TPad;
class TVirtualPad;
Expand Down Expand Up @@ -242,6 +243,7 @@ class TRatioPlot : public TObject {
virtual void SetGridlines(std::vector<double> gridlines);

void SetConfidenceIntervalColors(Color_t ci1 = kYellow, Color_t ci2 = kGreen);
void SetConfidenceIntervalColors(TColorNumber ci1, TColorNumber ci2);

void SetC1(Double_t c1) { fC1 = c1; }
void SetC2(Double_t c2) { fC2 = c2; }
Expand Down
11 changes: 11 additions & 0 deletions graf2d/gpad/src/TRatioPlot.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*************************************************************************/

#include "TRatioPlot.h"
#include "TColor.h"
#include "TROOT.h"
#include "TBrowser.h"
#include "TH1.h"
Expand Down Expand Up @@ -1703,6 +1704,16 @@ void TRatioPlot::SetConfidenceIntervalColors(Color_t ci1, Color_t ci2)
fCi2Color = ci2;
}


////////////////////////////////////////////////////////////////////////////////
/// Set the confidence interval colors.

void TRatioPlot::SetConfidenceIntervalColors(TColorNumber ci1, TColorNumber ci2)
{
fCi1Color = ci1.number();
fCi2Color = ci2.number();
}

////////////////////////////////////////////////////////////////////////////////
/// Internal method to import TAxis attributes to a TGaxis. Copied from
/// `TGaxis::ImportAxisAttributes`
Expand Down
4 changes: 4 additions & 0 deletions graf2d/graf/inc/TGaxis.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "TAttText.h"
#include "TString.h"


class TColorNumber;
class TF1;
class TAxis;
class TLatex;
Expand Down Expand Up @@ -133,6 +135,8 @@ class TGaxis : public TLine, public TAttText {
void SetWmax(Double_t wmax) {fWmax = wmax;}
static void SetExponentOffset(Float_t xoff=0., Float_t yoff=0., Option_t *axis="xy");

void SetLabelColor(TColorNumber lcolor);

ClassDefOverride(TGaxis,6) //Graphics axis
};

Expand Down
5 changes: 5 additions & 0 deletions graf2d/graf/src/TGaxis.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3080,3 +3080,8 @@ void TGaxis::Streamer(TBuffer &R__b)
R__b.WriteClassBuffer(TGaxis::Class(),this);
}
}

void TGaxis::SetLabelColor(TColorNumber lcolor)
{
SetLineColor(lcolor.number());
}
13 changes: 6 additions & 7 deletions roofit/roofitcore/inc/RooGlobalFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "RooArgSet.h"

#include "ROOT/RConfig.hxx"
#include <TColor.h>

#include <map>
#include <string>
Expand All @@ -43,6 +44,7 @@ class RooConstVar ;
class RooRealVar ;
class RooAbsCategory ;
class RooNumIntConfig ;

class TH1 ;
class TTree ;

Expand Down Expand Up @@ -102,13 +104,11 @@ RooCmdArg Range(const char* rangeName, bool adjustNorm=true) ;
RooCmdArg Range(double lo, double hi, bool adjustNorm=true) ;
RooCmdArg NormRange(const char* rangeNameList) ;
RooCmdArg VLines() ;
RooCmdArg LineColor(Color_t color) ;
RooCmdArg LineColor(std::string const &color) ;
RooCmdArg LineColor(TColorNumber color) ;
RooCmdArg LineStyle(Style_t style) ;
RooCmdArg LineStyle(std::string const &style) ;
RooCmdArg LineWidth(Width_t width) ;
RooCmdArg FillColor(Color_t color) ;
RooCmdArg FillColor(std::string const &color) ;
RooCmdArg FillColor(TColorNumber color) ;
RooCmdArg FillStyle(Style_t style) ;
RooCmdArg FillStyle(std::string const &style) ;
RooCmdArg ProjectionRange(const char* rangeName) ;
Expand Down Expand Up @@ -141,8 +141,7 @@ RooCmdArg Binning(int nBins, double xlo=0.0, double xhi=0.0) ;
RooCmdArg MarkerStyle(Style_t style) ;
RooCmdArg MarkerStyle(std::string const &style) ;
RooCmdArg MarkerSize(Size_t size) ;
RooCmdArg MarkerColor(Color_t color) ;
RooCmdArg MarkerColor(std::string const &color) ;
RooCmdArg MarkerColor(TColorNumber color) ;
RooCmdArg CutRange(const char* rangeName) ;
RooCmdArg XErrorSize(double width) ;
RooCmdArg RefreshNorm() ;
Expand Down Expand Up @@ -407,7 +406,7 @@ RooCmdArg BaseClassName(const char* name) ;
RooCmdArg TagName(const char* name) ;
RooCmdArg OutputStream(std::ostream& os) ;
RooCmdArg Prefix(bool flag) ;
RooCmdArg Color(Color_t color) ;
RooCmdArg Color(TColorNumber color) ;

// RooWorkspace::import() arguments
RooCmdArg RenameConflictNodes(const char* suffix, bool renameOrigNodes=false) ;
Expand Down
Loading

0 comments on commit 069d935

Please sign in to comment.