networkstyle.cpp raw

   1  // Copyright (c) 2014-2021 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #include <qt/networkstyle.h>
   6  
   7  #include <qt/guiconstants.h>
   8  
   9  #include <tinyformat.h>
  10  #include <util/chaintype.h>
  11  
  12  #include <QApplication>
  13  
  14  static const struct {
  15      const ChainType networkId;
  16      const char *appName;
  17      const int iconColorHueShift;
  18      const int iconColorSaturationReduction;
  19  } network_styles[] = {
  20      {ChainType::MAIN, QAPP_APP_NAME_DEFAULT, 0, 0},
  21      {ChainType::TESTNET, QAPP_APP_NAME_TESTNET, 70, 30},
  22      {ChainType::TESTNET4, QAPP_APP_NAME_TESTNET4, 70, 30},
  23      {ChainType::SIGNET, QAPP_APP_NAME_SIGNET, 35, 15},
  24      {ChainType::REGTEST, QAPP_APP_NAME_REGTEST, 160, 30},
  25  };
  26  
  27  // titleAddText needs to be const char* for tr()
  28  NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *_titleAddText):
  29      m_colour_shift(std::make_pair(iconColorHueShift, iconColorSaturationReduction)),
  30      appName(_appName),
  31      titleAddText(qApp->translate("SplashScreen", _titleAddText))
  32  {
  33      // load pixmap
  34      QPixmap pixmap(":/icons/limenka");
  35  
  36      if(iconColorHueShift != 0 && iconColorSaturationReduction != 0)
  37      {
  38          // generate QImage from QPixmap
  39          QImage img = pixmap.toImage();
  40  
  41          int h,s,l,a;
  42  
  43          // traverse though lines
  44          for(int y=0;y<img.height();y++)
  45          {
  46              QRgb *scL = reinterpret_cast< QRgb *>( img.scanLine( y ) );
  47  
  48              // loop through pixels
  49              for(int x=0;x<img.width();x++)
  50              {
  51                  // preserve alpha because QColor::getHsl doesn't return the alpha value
  52                  a = qAlpha(scL[x]);
  53                  QColor col(scL[x]);
  54  
  55                  // get hue value
  56                  col.getHsl(&h,&s,&l);
  57  
  58                  // rotate color on RGB color circle
  59                  // 70° should end up with the typical "testnet" green
  60                  h+=iconColorHueShift;
  61  
  62                  // change saturation value
  63                  if(s>iconColorSaturationReduction)
  64                  {
  65                      s -= iconColorSaturationReduction;
  66                  }
  67                  col.setHsl(h,s,l,a);
  68  
  69                  // set the pixel
  70                  scL[x] = col.rgba();
  71              }
  72          }
  73  
  74          //convert back to QPixmap
  75          pixmap.convertFromImage(img);
  76      }
  77  
  78      trayAndWindowIcon   = QIcon(pixmap.scaled(QSize(256,256)));
  79  }
  80  
  81  QColor NetworkStyle::AdjustColour(QColor colour) const
  82  {
  83      int h, s, l, a;
  84  
  85      // preserve alpha because QColor::getHsl doesn't return the alpha value
  86      a = colour.alpha();
  87  
  88      // get hue value
  89      colour.getHsl(&h, &s, &l);
  90  
  91      // rotate color on RGB color circle
  92      // 70° should end up with the typical "testnet" green
  93      h += m_colour_shift.first;
  94  
  95      // change saturation value
  96      if (s > m_colour_shift.second) {
  97          s -= m_colour_shift.second;
  98      }
  99      colour.setHsl(h, s, l, a);
 100  
 101      return colour;
 102  }
 103  
 104  const NetworkStyle* NetworkStyle::instantiate(const ChainType networkId)
 105  {
 106      std::string titleAddText = networkId == ChainType::MAIN ? "" : strprintf("[%s]", ChainTypeToString(networkId));
 107      for (const auto& network_style : network_styles) {
 108          if (networkId == network_style.networkId) {
 109              return new NetworkStyle(
 110                      network_style.appName,
 111                      network_style.iconColorHueShift,
 112                      network_style.iconColorSaturationReduction,
 113                      titleAddText.c_str());
 114          }
 115      }
 116      return nullptr;
 117  }
 118