Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:36

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWColorManager
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Tue Mar 24 10:10:01 CET 2009
0011 //
0012 
0013 // system include files
0014 #include <iostream>
0015 #include <map>
0016 #include <memory>
0017 #include "TColor.h"
0018 #include "TROOT.h"
0019 #include "TMath.h"
0020 #include "TEveUtil.h"
0021 #include "TEveManager.h"
0022 #include "TGLViewer.h"
0023 
0024 // user include files
0025 #include "Fireworks/Core/interface/FWColorManager.h"
0026 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0027 #include "Fireworks/Core/src/fwPaletteClassic.icc"
0028 #include "Fireworks/Core/src/fwPaletteExtra.h"
0029 
0030 //
0031 // constants, enums and typedefs
0032 //
0033 
0034 //
0035 // static data member definitions
0036 //
0037 const Color_t FWColorManager::s_defaultStartColorIndex = 1000;
0038 Color_t FWColorManager::getDefaultStartColorIndex() { return s_defaultStartColorIndex; }
0039 
0040 static void resetColors(const float (*iColors)[3], unsigned int iSize, unsigned int iStart, float gammaOff) {
0041   // std::cout << "reset colors " << iColors << " start " << iStart << " size " << iSize<< " gamma " << gammaOff << std::endl;
0042   TSeqCollection* colorTable = gROOT->GetListOfColors();
0043 
0044   TColor* c = static_cast<TColor*>(colorTable->At(iStart));
0045   unsigned int index = iStart;
0046   if (nullptr == c || c->GetNumber() != static_cast<int>(iStart)) {
0047     TIter next(colorTable);
0048     while ((c = static_cast<TColor*>(next()))) {
0049       if (c->GetNumber() == static_cast<int>(iStart)) {
0050         index = iStart;
0051         break;
0052       }
0053     }
0054   }
0055   assert(nullptr != c);
0056 
0057   for (unsigned int i = index; i < index + iSize; ++i, ++iColors) {
0058     TColor* c = static_cast<TColor*>(colorTable->At(i));
0059     float red = (*iColors)[0];
0060     float green = (*iColors)[1];
0061     float blue = (*iColors)[2];
0062 
0063     // apply brightness
0064     red = TMath::Power(red, (2.5 + gammaOff) / 2.5);
0065     green = TMath::Power(green, (2.5 + gammaOff) / 2.5);
0066     blue = TMath::Power(blue, (2.5 + gammaOff) / 2.5);
0067 
0068     // printf("--> [%d] (%.1f, %.1f, %.1f) => \n", i,  red, green, blue);
0069     c->SetRGB(red, green, blue);
0070   }
0071 }
0072 //
0073 // constructors and destructor
0074 //
0075 FWColorManager::FWColorManager(FWModelChangeManager* iManager)
0076     : m_paletteId(kClassic),
0077       m_gammaOff(0),
0078       m_background(kBlack),
0079       m_foreground(kWhite),
0080       m_changeManager(iManager),
0081       m_startColorIndex(0),
0082       m_numColorIndices(0),
0083       m_geomTransparency2D(50),
0084       m_geomTransparency3D(90) {
0085   setDefaultGeomColors();
0086 }
0087 
0088 FWColorManager::~FWColorManager() {}
0089 
0090 //
0091 //
0092 // member functions
0093 
0094 void FWColorManager::setDefaultGeomColors() {
0095   m_geomColor[kFWPixelBarrelColorIndex] = 1032;
0096   m_geomColor[kFWPixelEndcapColorIndex] = 1033;
0097 
0098   m_geomColor[kFWTrackerBarrelColorIndex] = 1026;
0099   m_geomColor[kFWTrackerEndcapColorIndex] = 1017;
0100 
0101   m_geomColor[kFWMuonBarrelLineColorIndex] = 1025;
0102   m_geomColor[kFWMuonEndcapLineColorIndex] = 1022;
0103 
0104   m_geomColor[kFwHGCalEEColorIndex] = 1000;
0105   m_geomColor[kFwHGCalHSiColorIndex] = 1000;
0106   m_geomColor[kFwHGCalHScColorIndex] = 1012;
0107 
0108   m_geomColor[kFWMtdBarrelColorIndex] = 1042;
0109   m_geomColor[kFWMtdEndcapColorIndex] = 1043;
0110 
0111   switch (m_paletteId) {
0112     case (kArctic):
0113       // m_geomColor[kFWMuonBarrelLineColorIndex] = 1027;
0114       //m_geomColor[kFWMuonEndcapLineColorIndex] = 1027;
0115       break;
0116     case (kFall):
0117       m_geomColor[kFWMuonBarrelLineColorIndex] = 1030;
0118       break;
0119     case (kSpring):
0120       m_geomColor[kFWMuonBarrelLineColorIndex] = 1032;
0121       m_geomColor[kFWMuonEndcapLineColorIndex] = 1032;
0122       break;
0123     case (kPurple):
0124       m_geomColor[kFWMuonBarrelLineColorIndex] = 1027;  //kBlue -1;
0125       m_geomColor[kFWMuonEndcapLineColorIndex] = 1027;
0126       break;
0127     default:
0128       break;
0129   }
0130 }
0131 
0132 void FWColorManager::initialize() {
0133   m_startColorIndex = s_defaultStartColorIndex;
0134   m_numColorIndices = fireworks::s_size;
0135 
0136   int index = m_startColorIndex;
0137   //std::cout <<"start color index "<<m_startColorIndex<<std::endl;
0138 
0139   const float(*itEnd)[3] = fireworks::s_forBlack + fireworks::s_size;
0140   for (const float(*it)[3] = fireworks::s_forBlack; it != itEnd; ++it) {
0141     //NOTE: this constructor automatically places this color into the gROOT color list
0142     //std::cout <<" color "<< index <<" "<<(*it)[0]<<" "<<(*it)[1]<<" "<<(*it)[2]<<std::endl;
0143     if (index <= gROOT->GetListOfColors()->GetLast())
0144       gROOT->GetListOfColors()->RemoveAt(index);
0145     new TColor(index++, (*it)[0], (*it)[1], (*it)[2]);
0146   }
0147 
0148   // Save default ROOT colors.
0149   TEveUtil::SetColorBrightness(0, kFALSE);
0150 }
0151 
0152 void FWColorManager::setPalette(long long x) {
0153   FWChangeSentry sentry(*m_changeManager);
0154   m_paletteId = (EPalette)x;
0155   setDefaultGeomColors();
0156   initColorTable();
0157 }
0158 
0159 void FWColorManager::initColorTable() {
0160   const float(*colValues)[3];
0161   colValues = isColorSetLight() ? fireworks::s_forWhite : fireworks::s_forBlack;
0162   if (m_paletteId == EPalette::kClassic) {
0163     // std::cout << "initColorTable classic \n";
0164     resetColors(colValues, fireworks::s_size, m_startColorIndex, m_gammaOff);
0165   } else {
0166     // std::cout << "initColorTable extra \n";
0167     float(*ev)[3] = (float(*)[3])calloc(3 * fireworks::s_size, sizeof(float));
0168     for (int ci = 0; ci < 34; ++ci) {
0169       for (int j = 0; j < 3; ++j)
0170         ev[ci][j] = colValues[ci][j];
0171     }
0172     fireworks::GetColorValuesForPaletteExtra(ev, fireworks::s_size, m_paletteId, isColorSetLight());
0173     resetColors(ev, fireworks::s_size, m_startColorIndex, m_gammaOff);
0174   }
0175 
0176   // AMT: Commented out ... Why this is necessary ?
0177   //float eveGamma =  isColorSetLight() ? 1.666*m_gammaOff - 2.5 : 1.666*m_gammaOff;
0178   //TEveUtil::SetColorBrightness(eveGamma);
0179 }
0180 
0181 void FWColorManager::updateColors() {
0182   initColorTable();
0183   propagatePaletteChanges();
0184 }
0185 
0186 void FWColorManager::propagatePaletteChanges() const {
0187   FWChangeSentry sentry(*m_changeManager);
0188   geomColorsHaveChanged_();
0189   colorsHaveChanged_();
0190   colorsHaveChangedFinished_();
0191 }
0192 
0193 void FWColorManager::setBrightness(int b) {
0194   // Called from CmsShowBrightnessPopup slider where range is set
0195   // to: -15, 15.
0196   m_gammaOff = -b * 0.1f;
0197   updateColors();
0198 }
0199 
0200 int FWColorManager::brightness() { return TMath::FloorNint(-m_gammaOff * 10); }
0201 
0202 void FWColorManager::defaultBrightness() {
0203   m_gammaOff = 0;
0204   updateColors();
0205 }
0206 
0207 void FWColorManager::switchBackground() { setBackgroundColorIndex(isColorSetDark() ? kWhiteIndex : kBlackIndex); }
0208 
0209 void FWColorManager::setBackgroundColorIndex(BackgroundColorIndex iIndex) {
0210   if (backgroundColorIndex() != iIndex) {
0211     if (backgroundColorIndex() == kBlackIndex) {
0212       m_background = kWhiteIndex;
0213       m_foreground = kBlackIndex;
0214     } else {
0215       m_background = kBlackIndex;
0216       m_foreground = kWhiteIndex;
0217     }
0218     updateColors();
0219   }
0220 }
0221 
0222 void FWColorManager::setBackgroundAndBrightness(BackgroundColorIndex iIndex, int b) {
0223   m_gammaOff = -b * 0.1f;
0224   setBackgroundColorIndex(iIndex);
0225 }
0226 
0227 Bool_t FWColorManager::setColorSetViewer(TGLViewer* v, Color_t iColor) {
0228   if ((iColor == kBlackIndex && !v->IsColorSetDark()) || (iColor == kWhiteIndex && v->IsColorSetDark())) {
0229     v->SwitchColorSet();
0230     return kTRUE;
0231   }
0232   return kFALSE;
0233 }
0234 
0235 void FWColorManager::setGeomColor(FWGeomColorIndex idx, Color_t iColor) {
0236   // printf("set geom color %d \n", iColor);
0237   m_geomColor[idx] = iColor;
0238   geomColorsHaveChanged_();
0239   gEve->Redraw3D();
0240 }
0241 void FWColorManager::setGeomTransparency(Color_t iTransp, bool projectedType) {
0242   if (projectedType)
0243     m_geomTransparency2D = iTransp;
0244   else
0245     m_geomTransparency3D = iTransp;
0246 
0247   geomTransparencyHaveChanged_.emit(projectedType);
0248 
0249   gEve->Redraw3D();
0250 }
0251 
0252 //
0253 // const member functions
0254 //
0255 
0256 void FWColorManager::fillLimitedColors(std::vector<Color_t>& cv) const {
0257   cv.reserve(cv.size() + m_numColorIndices);
0258   for (Color_t i = m_startColorIndex; i < borderOfLimitedColors(); ++i) {
0259     cv.push_back(i);
0260   }
0261 }
0262 
0263 FWColorManager::BackgroundColorIndex FWColorManager::backgroundColorIndex() const {
0264   if (m_background == kBlack) {
0265     return kBlackIndex;
0266   }
0267   return kWhiteIndex;
0268 }
0269 
0270 bool FWColorManager::colorHasIndex(Color_t iColor) const {
0271   return iColor > 0 && iColor < m_startColorIndex + m_numColorIndices;
0272 }
0273 
0274 Color_t FWColorManager::geomColor(FWGeomColorIndex iIndex) const { return m_geomColor[iIndex]; }
0275 
0276 static std::shared_ptr<std::map<Color_t, Color_t> > m_oldColorToIndexMap;
0277 
0278 Color_t FWColorManager::oldColorToIndex(Color_t iColor, int version) const {
0279   enum {
0280     kFWRed = 1008,
0281     kFWBlue = 1005,
0282     kFWCyan = 1007,
0283     kFWGreen = 1009,
0284     kFWMagenta = 1001,
0285     kFWOrange = 1004,
0286     kFWYellow = 1000
0287   };
0288 
0289   if (version < 3) {
0290     if (nullptr == m_oldColorToIndexMap.get()) {
0291       m_oldColorToIndexMap = std::make_shared<std::map<Color_t, Color_t> >();
0292       (*m_oldColorToIndexMap)[kRed] = kFWRed;
0293       (*m_oldColorToIndexMap)[kBlue] = kFWBlue;
0294       (*m_oldColorToIndexMap)[kYellow] = kFWYellow;
0295       (*m_oldColorToIndexMap)[kGreen] = kFWGreen;
0296       (*m_oldColorToIndexMap)[kCyan] = kFWCyan;
0297       (*m_oldColorToIndexMap)[kTeal] = kFWCyan;
0298       (*m_oldColorToIndexMap)[kMagenta] = kFWMagenta;
0299       (*m_oldColorToIndexMap)[kViolet] = kFWMagenta;
0300       (*m_oldColorToIndexMap)[kOrange] = kFWOrange;
0301       (*m_oldColorToIndexMap)[3] = kFWGreen;
0302     }
0303     return (*m_oldColorToIndexMap)[iColor];
0304   } else if (version == 3) {
0305     return iColor + 1000;
0306   } else {
0307     const static unsigned int s_version45offset = 5;
0308     return iColor < 1011 ? iColor : iColor + s_version45offset;
0309   }
0310 }