Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Make a palette image from palette data
Forum Updated to NodeBB v4.3 + New Features

Make a palette image from palette data

Scheduled Pinned Locked Moved Solved General and Desktop
35 Posts 4 Posters 4.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kris Revi
    wrote on last edited by
    #26

    am i doing something wrong here?

    QImage MainWindow::createPaletteImage(QJsonArray jsonArr)
    {
        int width = 500;
        int height = 26;
        QImage image(width, height, QImage::Format_RGB32);
        image.fill(Qt::white);
    
        QPainter painter(&image);
    
        int amountPoint;
        amountPoint = jsonArr.count()/4;
        float p=0,s=1.0/(amountPoint-1);
        QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0);
    
        for (int i = 0; i < amountPoint; i++)
        {
            int pos = jsonArr.at(i).toInt();
            int r = jsonArr.at(i+1).toInt();
            int g = jsonArr.at(i+2).toInt();
            int b = jsonArr.at(i+3).toInt();
    
            gradient.setColorAt(p, QColor(r,g,b));
        }
        return image;
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #27

      @Kris-Revi said in Make a palette image from palette data:

      int pos = jsonArr.at(i).toInt();
      gradient.setColorAt(p, QColor(r,g,b));

      Apart from the really strange idea to store a image in such an extensive way you want 'pos' in the second call.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      K 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        @Kris-Revi said in Make a palette image from palette data:

        int pos = jsonArr.at(i).toInt();
        gradient.setColorAt(p, QColor(r,g,b));

        Apart from the really strange idea to store a image in such an extensive way you want 'pos' in the second call.

        K Offline
        K Offline
        Kris Revi
        wrote on last edited by
        #28

        @Christian-Ehrlicher said in Make a palette image from palette data:

        you want 'pos' in the second call.

                int pos = jsonArr.at(i).toInt();
                int r = jsonArr.at(i+1).toInt();
                int g = jsonArr.at(i+2).toInt();
                int b = jsonArr.at(i+3).toInt();
        
                gradient.setColorAt(pos, QColor(r,g,b));
        

        huh like so? :S

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mpergand
          wrote on last edited by mpergand
          #29
          for(int p=0; p<amountPoint; p++)
                  {
                      int pos=jsonArr[p*4];
                      int r=jsonArr[p*4+1];
                      int g=jsonArr[p*4+2];
                      int b=jsonArr[p*4+3];
                      
                      gradient.setColorAt(pos/255.0, QColor(r,g,b));
                  }
                 
          
          K 1 Reply Last reply
          0
          • M mpergand
            for(int p=0; p<amountPoint; p++)
                    {
                        int pos=jsonArr[p*4];
                        int r=jsonArr[p*4+1];
                        int g=jsonArr[p*4+2];
                        int b=jsonArr[p*4+3];
                        
                        gradient.setColorAt(pos/255.0, QColor(r,g,b));
                    }
                   
            
            K Offline
            K Offline
            Kris Revi
            wrote on last edited by
            #30

            @mpergand

            QImage MainWindow::createPaletteImage(QJsonArray jsonArr)
            {
                int width = 500;
                int height = 26;
                QImage image(width, height, QImage::Format_RGB32);
                image.fill(Qt::white);
            
                QPainter painter(&image);
            
                int amountPoint;
                amountPoint = jsonArr.count()/4;
                float p=0,s=1.0/(amountPoint-1);
                QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0);
            
                for (int p = 0; p < amountPoint; p++)
                {
                    int pos=jsonArr[p*4].toInt();
                    int r=jsonArr[p*4+1].toInt();
                    int g=jsonArr[p*4+2].toInt();
                    int b=jsonArr[p*4+3].toInt();
            
                    gradient.setColorAt(pos/255.0, QColor(r,g,b));
                }
                painter.fillRect(rect(),QBrush(gradient));
                return image;
            }
            

            produces
            b31f83ea-6644-4c2d-af87-1ff271f7a8f2-image.png

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mpergand
              wrote on last edited by
              #31

              Check the values in the debugger.

              K 1 Reply Last reply
              0
              • M mpergand

                Check the values in the debugger.

                K Offline
                K Offline
                Kris Revi
                wrote on last edited by Kris Revi
                #32

                @mpergand all i got is

                POS :  0
                RGB :  255 0 0
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.12549
                RGB :  171 85 0
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.25098
                RGB :  171 171 0
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.376471
                RGB :  0 255 0
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.501961
                RGB :  0 171 85
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.627451
                RGB :  0 0 255
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.752941
                RGB :  85 0 171
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  0.878431
                RGB :  171 0 85
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                POS :  1
                RGB :  255 0 0
                GRAD :  QBrush(QColor(Invalid),LinearGradientPattern)
                
                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mpergand
                  wrote on last edited by
                  #33

                  Don't paste my code as this !
                  QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0);

                  should be the image width no ?

                  K 1 Reply Last reply
                  0
                  • M mpergand

                    Don't paste my code as this !
                    QLinearGradient gradient = QLinearGradient(0,0, rect().width(), 0);

                    should be the image width no ?

                    K Offline
                    K Offline
                    Kris Revi
                    wrote on last edited by
                    #34

                    @mpergand oh yea! now it worked :) thank you all

                    here is the working code and the solution

                    QImage MainWindow::createPaletteImage(QJsonArray jsonArr)
                    {
                        int width = 500;
                        int height = 26;
                        QImage image(width, height, QImage::Format_RGB32);
                        image.fill(Qt::white);
                    
                        QPainter painter(&image);
                    
                        int amountPoint;
                        amountPoint = jsonArr.count()/4;
                        float p=0,s=1.0/(amountPoint-1);
                        QLinearGradient gradient = QLinearGradient(0,0, width, 0);
                    
                        for (int p = 0; p < amountPoint; p++)
                        {
                            int pos=jsonArr[p*4].toInt();
                            int r=jsonArr[p*4+1].toInt();
                            int g=jsonArr[p*4+2].toInt();
                            int b=jsonArr[p*4+3].toInt();
                    
                            gradient.setColorAt(pos/255.0, QColor(r,g,b));
                        }
                        painter.fillRect(rect(),QBrush(gradient));
                        return image;
                    }
                    
                    M 1 Reply Last reply
                    0
                    • K Kris Revi

                      @mpergand oh yea! now it worked :) thank you all

                      here is the working code and the solution

                      QImage MainWindow::createPaletteImage(QJsonArray jsonArr)
                      {
                          int width = 500;
                          int height = 26;
                          QImage image(width, height, QImage::Format_RGB32);
                          image.fill(Qt::white);
                      
                          QPainter painter(&image);
                      
                          int amountPoint;
                          amountPoint = jsonArr.count()/4;
                          float p=0,s=1.0/(amountPoint-1);
                          QLinearGradient gradient = QLinearGradient(0,0, width, 0);
                      
                          for (int p = 0; p < amountPoint; p++)
                          {
                              int pos=jsonArr[p*4].toInt();
                              int r=jsonArr[p*4+1].toInt();
                              int g=jsonArr[p*4+2].toInt();
                              int b=jsonArr[p*4+3].toInt();
                      
                              gradient.setColorAt(pos/255.0, QColor(r,g,b));
                          }
                          painter.fillRect(rect(),QBrush(gradient));
                          return image;
                      }
                      
                      M Offline
                      M Offline
                      mpergand
                      wrote on last edited by
                      #35

                      @Kris-Revi said in Make a palette image from palette data:
                      painter.fillRect(rect(),QBrush(gradient));

                      should be I think: QRect(0,0,width,height)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved