Qt open source works 23 - color picker

1, Foreword

When doing UI of many projects, I believe that most people have copied other people's UI, especially the color. After all, nine out of ten programmers have no aesthetics, or aesthetics is just like a piece of shit. Everyone's main energy and expertise are in writing functions to achieve specific functions. How can we say this? It's really the main responsibility of programmers, but In most small companies, the UI needs to be done by the programmers themselves. They can't figure out what to do. They can learn from it. They don't know what to do with the color value. Click it with the color picker. The built-in grabWindow method of Qt can specify the handle to obtain the corresponding color, so if you want to obtain the color value of the screen, just pass in the handle of the whole screen. The handle of the screen in Qt is expressed as QApplication:: desktop() - > winid(), and what to do in real time? Of course, the easiest way is to open a timer. The timer keeps calling this method to obtain the color value Screen mouse coordinates and color values.

2, Code thinking

void ColorWidget::showColorValue()
{
    if (!pressed) {
        return;
    }

    int x = QCursor::pos().x();
    int y = QCursor::pos().y();

    txtPoint->setText(tr("x:%1  y:%2").arg(x).arg(y));
    QString strDecimalValue, strHex, strTextColor;
    int red, green, blue;

#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
    QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);
#else
    QScreen *screen = QApplication::primaryScreen();
    QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(), x, y, 2, 2);
#endif

    if (!pixmap.isNull()) {
        QImage image = pixmap.toImage();

        if (!image.isNull()) {
            if (image.valid(0, 0)) {
                QColor color = image.pixel(0, 0);
                red = color.red();
                green = color.green();
                blue = color.blue();
                QString strRed = tr("%1").arg(red & 0xFF, 2, 16, QChar('0'));
                QString strGreen = tr("%1").arg(green & 0xFF, 2, 16, QChar('0'));
                QString strBlue = tr("%1").arg(blue & 0xFF, 2, 16, QChar('0'));

                strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue);
                strHex = tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());
            }
        }
    }

    if (red > 200 && green > 200 && blue > 200) {
        strTextColor = "10, 10, 10";
    } else {
        strTextColor = "255, 255, 255";
    }

    QString str = tr("background-color: rgb(%1);color: rgb(%2)").arg(strDecimalValue).arg(strTextColor);
    labColor->setStyleSheet(str);
    txtRgb->setText(strDecimalValue);
    txtWeb->setText(strHex);
}

3, Renderings

4, Open source Homepage

All of the above works are downloaded from the open source homepage, and the quantity and quality of works will be continuously updated. You are welcome to pay attention.

  1. Domestic site: https://gitee.com/feiyangqingyun/QWidgetDemo
  2. International site: https://github.com/feiyangqingyun/QWidgetDemo
  3. Personal homepage: https://blog.csdn.net/feiyangqingyun
  4. Zhihu homepage: https://www.zhihu.com/people/feiyangqingyun/

Tags: Qt github

Posted on Fri, 29 May 2020 11:14:22 -0400 by john6384