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. A problem i have with palettes i need help with!
Forum Update on Monday, May 27th 2025

A problem i have with palettes i need help with!

Scheduled Pinned Locked Moved Unsolved General and Desktop
27 Posts 5 Posters 2.6k Views
  • 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 Kris Revi
    #1

    So i've made a Qt App that connects to my ESP32 via Socket to controll some WS2812B led strips

    for many months (just to get it working) i've hard coded each palette inn on the ESP32 like this

    DEFINE_GRADIENT_PALETTE( Sunset_Real_gp ) // Size: 28 bytes of program space.
    {
        0, 120,  0,  0,
       22, 179, 22,  0,
       51, 255,104,  0,
       85, 167, 22, 18,
      135, 100,  0, 103,
      198,  16,  0, 130,
      255,   0,  0, 160
    };
    

    then on the Qt side i just add pushButton and some css to style the button with a picture of the palette
    Sunset_Real.png

    when that button was pressed i just send over a int like "selectedPalette": "1"

    but this is not how i wanted the system! so i've done some modifications to the Qt App where i scan the image/palettes folder for .png files and then generate a button for each and connect signals to slots

    the problem im having here / what i wanted to ask for help with is, is it possible in Qt to "load" that image and generate code like this? (hope that was clear enough)

    {
        0, 120,  0,  0,
       22, 179, 22,  0,
       51, 255,104,  0,
       85, 167, 22, 18,
      135, 100,  0, 103,
      198,  16,  0, 130,
      255,   0,  0, 160
    };
    

    so that i can click a palette button then Qt generates that code and sends that over to ESP32 allowing me to just add palette images to folder and it just works!

    if not is there any other way i could do this?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      If I understand you correctly, you want to generate a list of numbers from your image based on the pixel ARGB values, correct ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        If I understand you correctly, you want to generate a list of numbers from your image based on the pixel ARGB values, correct ?

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

        @SGaist correct :)

        Pos, R, G, B

        mrjjM 1 Reply Last reply
        0
        • K Kris Revi

          @SGaist correct :)

          Pos, R, G, B

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Kris-Revi

          Hi
          a (slow) way is

          for (int row = 0; row < img.height(); row++) {
                  for (int col = 0; col < img.width(); col++) {
                      QRgb pixColor = img.pixel(row, col);
                      qDebug() << "red" << qRed(pixColor) << endl
                               << "green" << qGreen(pixColor) << endl
                               << "blue" << qBlue(pixColor);
                  }
              }
          

          read also about constBits() or constScanLine()
          but for a small image the speed is not so critical.

          However, how can we detect the POS ?
          will the color change so much, we can use that to know it must be a POS there ?

          K 1 Reply Last reply
          0
          • mrjjM mrjj

            @Kris-Revi

            Hi
            a (slow) way is

            for (int row = 0; row < img.height(); row++) {
                    for (int col = 0; col < img.width(); col++) {
                        QRgb pixColor = img.pixel(row, col);
                        qDebug() << "red" << qRed(pixColor) << endl
                                 << "green" << qGreen(pixColor) << endl
                                 << "blue" << qBlue(pixColor);
                    }
                }
            

            read also about constBits() or constScanLine()
            but for a small image the speed is not so critical.

            However, how can we detect the POS ?
            will the color change so much, we can use that to know it must be a POS there ?

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

            @mrjj hi

            yea im not sure about the position thing how to move forward with that!

            mrjjM 1 Reply Last reply
            0
            • K Kris Revi

              @mrjj hi

              yea im not sure about the position thing how to move forward with that!

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Kris-Revi
              These images that defines a gradiant.
              Are those user defined or will you make them ?

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Ewan Green
                wrote on last edited by Ewan Green
                #7

                I don't think this is possible without the use of an indexed image (QImage::Format_Indexed8?). Given you're using this format, I think it would be a matter of calling the QImage::color() method for every color in your image (QImage::colorCount()). Then you can process that however you like.

                Ewan Green

                1 Reply Last reply
                2
                • K Offline
                  K Offline
                  Kris Revi
                  wrote on last edited by
                  #8

                  maybe i've been looking at this the wrong way!

                  is it possible in Qt to make a image from this color data Pos, R, G, B?

                  {
                      0, 120,  0,  0,
                     22, 179, 22,  0,
                     51, 255,104,  0,
                     85, 167, 22, 18,
                    135, 100,  0, 103,
                    198,  16,  0, 130,
                    255,   0,  0, 160
                  };
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    So Pos is the end/start of the next gradient ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    K 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      So Pos is the end/start of the next gradient ?

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

                      @SGaist yes

                      0 - Start
                      255 - end

                      mrjjM 1 Reply Last reply
                      0
                      • K Kris Revi

                        @SGaist yes

                        0 - Start
                        255 - end

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Kris-Revi

                        Hi
                        You should be able to use such table with
                        https://doc.qt.io/qt-5/qgradient.html

                        and https://doc.qt.io/qt-5/qgradient.html#setColorAt

                        K 1 Reply Last reply
                        1
                        • mrjjM mrjj

                          @Kris-Revi

                          Hi
                          You should be able to use such table with
                          https://doc.qt.io/qt-5/qgradient.html

                          and https://doc.qt.io/qt-5/qgradient.html#setColorAt

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

                          @mrjj never worked with QPainter or QLinearGradient befor! but i'll give it a go i guess

                          mrjjM 1 Reply Last reply
                          0
                          • K Kris Revi

                            @mrjj never worked with QPainter or QLinearGradient befor! but i'll give it a go i guess

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by mrjj
                            #13

                            @Kris-Revi

                            Well fast example might help you get started.

                            // converts range between 0-255 to range 0 - 1 as gradient uses
                            float mapPos(int x, float in_min, float in_max, float out_min, float out_max)
                            {
                                return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
                            }
                            
                            // data type to hold the info as list of ints is clumsy
                            struct gradPosDef {
                                int pos;
                                int r;
                                int g;
                                int b;
                            };
                            
                            // define a test gradient. this you would load from a file.
                            QList<gradPosDef> gradValues{
                                {0, 120,  0,  0},
                                {22, 179, 22,  0},
                                {51, 255, 104,  0},
                                {85, 167, 22, 18},
                                {135, 100,  0, 103},
                                {198,  16,  0, 130},
                                {255,   0,  0, 160}
                            };
                            
                            // creates pixmap and returns it
                            QPixmap MainWindow::makeGradient()
                            {
                                // size of gradient / pixmap
                                const int gw = 255;
                                const int gh = 100;
                                // define it- make it point straight from left to right
                                QLinearGradient gradient(0, 0, gw, 0);
                               // loop over the test gradient values
                                for (int cc = 0; cc < gradValues.size(); cc++) {
                                    auto cur = gradValues.at(cc);
                                    // convert to new scale. you could directly use 0- 1 instead of  0-255 to avoid
                                    float scaledVal =  mapPos(cur.pos, 0, 255, 0, 1);
                                    // write it out to see if we get sane values
                                    qDebug() << "org: " << cur.pos << " - " << scaledVal;
                                    // make color
                                    QColor col(cur.r, cur.g, cur.b);
                                    // set gradient point
                                    gradient.setColorAt(scaledVal, col);
                                };
                            // allocate a pix map
                                QPixmap pix(gw, gh);
                            //assign painter to it so we can paint the gradiant
                                QPainter p(&pix);
                            // assign gradient to a brush. this way we can paint it
                                QBrush grad(gradient);
                            // fill pixmap with gradiant
                                p.fillRect(pix.rect(), gradient);
                                return pix;
                            }
                            

                            to use it you can do

                            auto pix = makeGradient();
                            pix.save("e:/test.png");
                            ui->label->setPixmap(pix);
                            

                            and get
                            alt text

                            Not perfect but demonstrate one way to do it.

                            K 1 Reply Last reply
                            2
                            • mrjjM mrjj

                              @Kris-Revi

                              Well fast example might help you get started.

                              // converts range between 0-255 to range 0 - 1 as gradient uses
                              float mapPos(int x, float in_min, float in_max, float out_min, float out_max)
                              {
                                  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
                              }
                              
                              // data type to hold the info as list of ints is clumsy
                              struct gradPosDef {
                                  int pos;
                                  int r;
                                  int g;
                                  int b;
                              };
                              
                              // define a test gradient. this you would load from a file.
                              QList<gradPosDef> gradValues{
                                  {0, 120,  0,  0},
                                  {22, 179, 22,  0},
                                  {51, 255, 104,  0},
                                  {85, 167, 22, 18},
                                  {135, 100,  0, 103},
                                  {198,  16,  0, 130},
                                  {255,   0,  0, 160}
                              };
                              
                              // creates pixmap and returns it
                              QPixmap MainWindow::makeGradient()
                              {
                                  // size of gradient / pixmap
                                  const int gw = 255;
                                  const int gh = 100;
                                  // define it- make it point straight from left to right
                                  QLinearGradient gradient(0, 0, gw, 0);
                                 // loop over the test gradient values
                                  for (int cc = 0; cc < gradValues.size(); cc++) {
                                      auto cur = gradValues.at(cc);
                                      // convert to new scale. you could directly use 0- 1 instead of  0-255 to avoid
                                      float scaledVal =  mapPos(cur.pos, 0, 255, 0, 1);
                                      // write it out to see if we get sane values
                                      qDebug() << "org: " << cur.pos << " - " << scaledVal;
                                      // make color
                                      QColor col(cur.r, cur.g, cur.b);
                                      // set gradient point
                                      gradient.setColorAt(scaledVal, col);
                                  };
                              // allocate a pix map
                                  QPixmap pix(gw, gh);
                              //assign painter to it so we can paint the gradiant
                                  QPainter p(&pix);
                              // assign gradient to a brush. this way we can paint it
                                  QBrush grad(gradient);
                              // fill pixmap with gradiant
                                  p.fillRect(pix.rect(), gradient);
                                  return pix;
                              }
                              

                              to use it you can do

                              auto pix = makeGradient();
                              pix.save("e:/test.png");
                              ui->label->setPixmap(pix);
                              

                              and get
                              alt text

                              Not perfect but demonstrate one way to do it.

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

                              @mrjj ty for that bit of code m8! :) <3

                              question! is this the way togo when you want to save the file to the App folder?

                              if ( pix.save(QDir::currentPath() + "/test.png") )
                              
                              jsulmJ 1 Reply Last reply
                              0
                              • K Kris Revi

                                @mrjj ty for that bit of code m8! :) <3

                                question! is this the way togo when you want to save the file to the App folder?

                                if ( pix.save(QDir::currentPath() + "/test.png") )
                                
                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                @Kris-Revi You should not write into app folder as usually normal users have no write access there!
                                There are better options for saving data, see https://doc.qt.io/qt-5/qstandardpaths.html

                                https://forum.qt.io/topic/113070/qt-code-of-conduct

                                K 1 Reply Last reply
                                2
                                • jsulmJ jsulm

                                  @Kris-Revi You should not write into app folder as usually normal users have no write access there!
                                  There are better options for saving data, see https://doc.qt.io/qt-5/qstandardpaths.html

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

                                  @jsulm realy! well what would be a normal place to put this then? :)

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • K Kris Revi

                                    @jsulm realy! well what would be a normal place to put this then? :)

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    @Kris-Revi Did you actually read the link I gave you?
                                    There are several locations depending on type of files:

                                    • QStandardPaths::DocumentsLocation - any documents
                                    • QStandardPaths::PicturesLocation - for pictures (this is probably what you want)
                                    • ...

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    K 1 Reply Last reply
                                    2
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      You can either use QFileDialog so your users can choose where to store them or use one of the suitable proposition of QStandardPaths like QStandardPaths::DocumentsLocation

                                      Interested in AI ? www.idiap.ch
                                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      1 Reply Last reply
                                      1
                                      • jsulmJ jsulm

                                        @Kris-Revi Did you actually read the link I gave you?
                                        There are several locations depending on type of files:

                                        • QStandardPaths::DocumentsLocation - any documents
                                        • QStandardPaths::PicturesLocation - for pictures (this is probably what you want)
                                        • ...
                                        K Offline
                                        K Offline
                                        Kris Revi
                                        wrote on last edited by
                                        #19

                                        @jsulm ofcourse i read it! i was asking where do people usualy put it :) im not THAT lazy! :)

                                        jsulmJ 1 Reply Last reply
                                        0
                                        • K Kris Revi

                                          @jsulm ofcourse i read it! i was asking where do people usualy put it :) im not THAT lazy! :)

                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #20

                                          @Kris-Revi Sorry if I was rude!
                                          Where to put the data depends on data types. It looks like you want to store a picture, so QStandardPaths::PicturesLocation would be the proper location. Or you ask the user where to store as @SGaist suggested.

                                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                                          K 1 Reply Last reply
                                          1

                                          • Login

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