Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:09

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   switch (m_paletteId) {
0109     case (kArctic):
0110       // m_geomColor[kFWMuonBarrelLineColorIndex] = 1027;
0111       //m_geomColor[kFWMuonEndcapLineColorIndex] = 1027;
0112       break;
0113     case (kFall):
0114       m_geomColor[kFWMuonBarrelLineColorIndex] = 1030;
0115       break;
0116     case (kSpring):
0117       m_geomColor[kFWMuonBarrelLineColorIndex] = 1032;
0118       m_geomColor[kFWMuonEndcapLineColorIndex] = 1032;
0119       break;
0120     case (kPurple):
0121       m_geomColor[kFWMuonBarrelLineColorIndex] = 1027;  //kBlue -1;
0122       m_geomColor[kFWMuonEndcapLineColorIndex] = 1027;
0123       break;
0124     default:
0125       break;
0126   }
0127 }
0128 
0129 void FWColorManager::initialize() {
0130   m_startColorIndex = s_defaultStartColorIndex;
0131   m_numColorIndices = fireworks::s_size;
0132 
0133   int index = m_startColorIndex;
0134   //std::cout <<"start color index "<<m_startColorIndex<<std::endl;
0135 
0136   const float(*itEnd)[3] = fireworks::s_forBlack + fireworks::s_size;
0137   for (const float(*it)[3] = fireworks::s_forBlack; it != itEnd; ++it) {
0138     //NOTE: this constructor automatically places this color into the gROOT color list
0139     //std::cout <<" color "<< index <<" "<<(*it)[0]<<" "<<(*it)[1]<<" "<<(*it)[2]<<std::endl;
0140     if (index <= gROOT->GetListOfColors()->GetLast())
0141       gROOT->GetListOfColors()->RemoveAt(index);
0142     new TColor(index++, (*it)[0], (*it)[1], (*it)[2]);
0143   }
0144 
0145   // Save default ROOT colors.
0146   TEveUtil::SetColorBrightness(0, kFALSE);
0147 }
0148 
0149 void FWColorManager::setPalette(long long x) {
0150   FWChangeSentry sentry(*m_changeManager);
0151   m_paletteId = (EPalette)x;
0152   setDefaultGeomColors();
0153   initColorTable();
0154 }
0155 
0156 void FWColorManager::initColorTable() {
0157   const float(*colValues)[3];
0158   colValues = isColorSetLight() ? fireworks::s_forWhite : fireworks::s_forBlack;
0159   if (m_paletteId == EPalette::kClassic) {
0160     // std::cout << "initColorTable classic \n";
0161     resetColors(colValues, fireworks::s_size, m_startColorIndex, m_gammaOff);
0162   } else {
0163     // std::cout << "initColorTable extra \n";
0164     float(*ev)[3] = (float(*)[3])calloc(3 * fireworks::s_size, sizeof(float));
0165     for (int ci = 0; ci < 34; ++ci) {
0166       for (int j = 0; j < 3; ++j)
0167         ev[ci][j] = colValues[ci][j];
0168     }
0169     fireworks::GetColorValuesForPaletteExtra(ev, fireworks::s_size, m_paletteId, isColorSetLight());
0170     resetColors(ev, fireworks::s_size, m_startColorIndex, m_gammaOff);
0171   }
0172 
0173   // AMT: Commented out ... Why this is necessary ?
0174   //float eveGamma =  isColorSetLight() ? 1.666*m_gammaOff - 2.5 : 1.666*m_gammaOff;
0175   //TEveUtil::SetColorBrightness(eveGamma);
0176 }
0177 
0178 void FWColorManager::updateColors() {
0179   initColorTable();
0180   propagatePaletteChanges();
0181 }
0182 
0183 void FWColorManager::propagatePaletteChanges() const {
0184   FWChangeSentry sentry(*m_changeManager);
0185   geomColorsHaveChanged_();
0186   colorsHaveChanged_();
0187   colorsHaveChangedFinished_();
0188 }
0189 
0190 void FWColorManager::setBrightness(int b) {
0191   // Called from CmsShowBrightnessPopup slider where range is set
0192   // to: -15, 15.
0193   m_gammaOff = -b * 0.1f;
0194   updateColors();
0195 }
0196 
0197 int FWColorManager::brightness() { return TMath::FloorNint(-m_gammaOff * 10); }
0198 
0199 void FWColorManager::defaultBrightness() {
0200   m_gammaOff = 0;
0201   updateColors();
0202 }
0203 
0204 void FWColorManager::switchBackground() { setBackgroundColorIndex(isColorSetDark() ? kWhiteIndex : kBlackIndex); }
0205 
0206 void FWColorManager::setBackgroundColorIndex(BackgroundColorIndex iIndex) {
0207   if (backgroundColorIndex() != iIndex) {
0208     if (backgroundColorIndex() == kBlackIndex) {
0209       m_background = kWhiteIndex;
0210       m_foreground = kBlackIndex;
0211     } else {
0212       m_background = kBlackIndex;
0213       m_foreground = kWhiteIndex;
0214     }
0215     updateColors();
0216   }
0217 }
0218 
0219 void FWColorManager::setBackgroundAndBrightness(BackgroundColorIndex iIndex, int b) {
0220   m_gammaOff = -b * 0.1f;
0221   setBackgroundColorIndex(iIndex);
0222 }
0223 
0224 Bool_t FWColorManager::setColorSetViewer(TGLViewer* v, Color_t iColor) {
0225   if ((iColor == kBlackIndex && !v->IsColorSetDark()) || (iColor == kWhiteIndex && v->IsColorSetDark())) {
0226     v->SwitchColorSet();
0227     return kTRUE;
0228   }
0229   return kFALSE;
0230 }
0231 
0232 void FWColorManager::setGeomColor(FWGeomColorIndex idx, Color_t iColor) {
0233   // printf("set geom color %d \n", iColor);
0234   m_geomColor[idx] = iColor;
0235   geomColorsHaveChanged_();
0236   gEve->Redraw3D();
0237 }
0238 void FWColorManager::setGeomTransparency(Color_t iTransp, bool projectedType) {
0239   if (projectedType)
0240     m_geomTransparency2D = iTransp;
0241   else
0242     m_geomTransparency3D = iTransp;
0243 
0244   geomTransparencyHaveChanged_.emit(projectedType);
0245 
0246   gEve->Redraw3D();
0247 }
0248 
0249 //
0250 // const member functions
0251 //
0252 
0253 void FWColorManager::fillLimitedColors(std::vector<Color_t>& cv) const {
0254   cv.reserve(cv.size() + m_numColorIndices);
0255   for (Color_t i = m_startColorIndex; i < borderOfLimitedColors(); ++i) {
0256     cv.push_back(i);
0257   }
0258 }
0259 
0260 FWColorManager::BackgroundColorIndex FWColorManager::backgroundColorIndex() const {
0261   if (m_background == kBlack) {
0262     return kBlackIndex;
0263   }
0264   return kWhiteIndex;
0265 }
0266 
0267 bool FWColorManager::colorHasIndex(Color_t iColor) const {
0268   return iColor > 0 && iColor < m_startColorIndex + m_numColorIndices;
0269 }
0270 
0271 Color_t FWColorManager::geomColor(FWGeomColorIndex iIndex) const { return m_geomColor[iIndex]; }
0272 
0273 static std::shared_ptr<std::map<Color_t, Color_t> > m_oldColorToIndexMap;
0274 
0275 Color_t FWColorManager::oldColorToIndex(Color_t iColor, int version) const {
0276   enum {
0277     kFWRed = 1008,
0278     kFWBlue = 1005,
0279     kFWCyan = 1007,
0280     kFWGreen = 1009,
0281     kFWMagenta = 1001,
0282     kFWOrange = 1004,
0283     kFWYellow = 1000
0284   };
0285 
0286   if (version < 3) {
0287     if (nullptr == m_oldColorToIndexMap.get()) {
0288       m_oldColorToIndexMap = std::make_shared<std::map<Color_t, Color_t> >();
0289       (*m_oldColorToIndexMap)[kRed] = kFWRed;
0290       (*m_oldColorToIndexMap)[kBlue] = kFWBlue;
0291       (*m_oldColorToIndexMap)[kYellow] = kFWYellow;
0292       (*m_oldColorToIndexMap)[kGreen] = kFWGreen;
0293       (*m_oldColorToIndexMap)[kCyan] = kFWCyan;
0294       (*m_oldColorToIndexMap)[kTeal] = kFWCyan;
0295       (*m_oldColorToIndexMap)[kMagenta] = kFWMagenta;
0296       (*m_oldColorToIndexMap)[kViolet] = kFWMagenta;
0297       (*m_oldColorToIndexMap)[kOrange] = kFWOrange;
0298       (*m_oldColorToIndexMap)[3] = kFWGreen;
0299     }
0300     return (*m_oldColorToIndexMap)[iColor];
0301   } else if (version == 3) {
0302     return iColor + 1000;
0303   } else {
0304     const static unsigned int s_version45offset = 5;
0305     return iColor < 1011 ? iColor : iColor + s_version45offset;
0306   }
0307 }