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. get the RGB hexadecimal color ( QColor)
Forum Updated to NodeBB v4.3 + New Features

get the RGB hexadecimal color ( QColor)

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 3 Posters 10.1k 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.
  • R raven-worx
    20 Jun 2019, 09:14

    @Zunneh
    QColor::rgba()
    if you want the string respentation as in your screenshot:

    QString("#%1").arg(QColor(...).rgba(), 8, 16)
    
    Z Offline
    Z Offline
    Zunneh
    wrote on 20 Jun 2019, 09:26 last edited by
    #3

    @raven-worx thank you , only one little problem, i get the Hexadecimal value + ff
    for exemple i select #90eeaa but when i click ok i get #ff90eeaa (where this ff come ? )
    another question : i want to get the binary value of this hexadecimal , can you help me ? thanks

    my english is average, please use simple words and try to be the most explicit, thank you

    R 1 Reply Last reply 20 Jun 2019, 09:32
    0
    • Z Zunneh
      20 Jun 2019, 09:26

      @raven-worx thank you , only one little problem, i get the Hexadecimal value + ff
      for exemple i select #90eeaa but when i click ok i get #ff90eeaa (where this ff come ? )
      another question : i want to get the binary value of this hexadecimal , can you help me ? thanks

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 20 Jun 2019, 09:32 last edited by raven-worx
      #4

      @Zunneh
      thats the alpha channel. Limit it to 6 characters then.

      QString("#%1").arg(QColor(...).rgb(), 6, 16)
      

      or compose it by concatenating each color component

      /QColor c(...);
      QString("#%1%2%3").arg(c.red(),2,16).arg(c.green(),2,16).arg(c.blue(),2,16)
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      Z 1 Reply Last reply 20 Jun 2019, 09:42
      3
      • R raven-worx
        20 Jun 2019, 09:32

        @Zunneh
        thats the alpha channel. Limit it to 6 characters then.

        QString("#%1").arg(QColor(...).rgb(), 6, 16)
        

        or compose it by concatenating each color component

        /QColor c(...);
        QString("#%1%2%3").arg(c.red(),2,16).arg(c.green(),2,16).arg(c.blue(),2,16)
        
        Z Offline
        Z Offline
        Zunneh
        wrote on 20 Jun 2019, 09:42 last edited by Zunneh
        #5

        @raven-worx i changed it to 6 but always same problem
        i tried also to concetnating each color and convert from decimal to binary but imagine if one color is 0 , then he will write it 0 not 00000000 and the final result will be false

        my english is average, please use simple words and try to be the most explicit, thank you

        1 Reply Last reply
        0
        • V Offline
          V Offline
          VRonin
          wrote on 20 Jun 2019, 10:06 last edited by VRonin
          #6

          https://doc.qt.io/qt-5/qcolor.html#name

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          R 1 Reply Last reply 20 Jun 2019, 10:15
          4
          • V VRonin
            20 Jun 2019, 10:06

            https://doc.qt.io/qt-5/qcolor.html#name

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 20 Jun 2019, 10:15 last edited by raven-worx
            #7

            @Zunneh said in get the RGB hexadecimal color ( QColor):

            i changed it to 6 but always same problem

            yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

            @VRonin
            does QColor::name() really always return the hex format? I thought i might also return "red" for example.
            I cant try myself right now

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            Z V 2 Replies Last reply 20 Jun 2019, 10:22
            1
            • R raven-worx
              20 Jun 2019, 10:15

              @Zunneh said in get the RGB hexadecimal color ( QColor):

              i changed it to 6 but always same problem

              yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

              @VRonin
              does QColor::name() really always return the hex format? I thought i might also return "red" for example.
              I cant try myself right now

              Z Offline
              Z Offline
              Zunneh
              wrote on 20 Jun 2019, 10:22 last edited by
              #8

              @raven-worx it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )
              @VRonin is there a function that returned the color in Hexadecimal and not in a QString ? thanks

              my english is average, please use simple words and try to be the most explicit, thank you

              R 1 Reply Last reply 20 Jun 2019, 10:39
              0
              • R raven-worx
                20 Jun 2019, 10:15

                @Zunneh said in get the RGB hexadecimal color ( QColor):

                i changed it to 6 but always same problem

                yes i think the second parameter of QString::arg() doesnt limit the output and is only interpreted as minimum length.

                @VRonin
                does QColor::name() really always return the hex format? I thought i might also return "red" for example.
                I cant try myself right now

                V Offline
                V Offline
                VRonin
                wrote on 20 Jun 2019, 10:39 last edited by
                #9

                @raven-worx said in get the RGB hexadecimal color ( QColor):

                does QColor::name() really always return the hex format? I thought i might also return "red" for example.

                qDebug() << QColor(Qt::red).name(); returns "#ff0000"

                @Zunneh said in get the RGB hexadecimal color ( QColor):

                is there a function that returned the color in Hexadecimal and not in a QString ?

                😓 for the numeric representation you can just use unsigned int colNum = QColor(Qt::red).rgb()

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                2
                • Z Zunneh
                  20 Jun 2019, 10:22

                  @raven-worx it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )
                  @VRonin is there a function that returned the color in Hexadecimal and not in a QString ? thanks

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 20 Jun 2019, 10:39 last edited by raven-worx
                  #10

                  @Zunneh said in get the RGB hexadecimal color ( QColor):

                  it returned the Hex format but in QString, so now i have to convert it to Hex and then to binary (because i want my output in binary )

                  what is binary in your case?!
                  QColor::rgb() returns a 32bit value... which contains each color component byte by byte. So its is binary if you want so.

                  something like this?

                  QByteArray a = QByteArray("#") + QByteArray::number(c.red(),16) + QByteArray::number(c.green(),16) + QByteArray::number(c.blue(),16);
                  a.toHex()
                  

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  3
                  • Z Offline
                    Z Offline
                    Zunneh
                    wrote on 20 Jun 2019, 14:27 last edited by
                    #11

                    i tried this and it work , thanks guys

                    bool ok;
                    QColor color;
                    color = QColorDialog::getColor(Qt::white,this); 
                    QString ColorHex=color.name().remove('#');
                    int ColorInt = ColorHex.toInt(&ok,16);
                    QString ColorBin= ColorHex.setNum(ColorInt, 2);
                    lab->setText(ColorBin);
                    

                    my english is average, please use simple words and try to be the most explicit, thank you

                    V 1 Reply Last reply 20 Jun 2019, 15:47
                    0
                    • Z Zunneh
                      20 Jun 2019, 14:27

                      i tried this and it work , thanks guys

                      bool ok;
                      QColor color;
                      color = QColorDialog::getColor(Qt::white,this); 
                      QString ColorHex=color.name().remove('#');
                      int ColorInt = ColorHex.toInt(&ok,16);
                      QString ColorBin= ColorHex.setNum(ColorInt, 2);
                      lab->setText(ColorBin);
                      
                      V Offline
                      V Offline
                      VRonin
                      wrote on 20 Jun 2019, 15:47 last edited by
                      #12

                      @Zunneh said in get the RGB hexadecimal color ( QColor):

                      i tried this

                      But Why?!

                      int ColorInt =QColorDialog::getColor(Qt::white,this).rgb()
                      lab->setText(QString::number(ColorInt ,2));
                      

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0

                      12/12

                      20 Jun 2019, 15:47

                      • Login

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