1-螢幕顏色提取小工具
阿新 • • 發佈:2021-02-03
螢幕顏色提取小工具
程式不難,程式碼不詳細解釋了,話不多說,請看圖:
Widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTimer> QT_BEGIN_NAMESPACE namespace Ui { class Widget; } QT_END_NAMESPACE class Widget : public QWidget { Q_OBJECT public: Widget(QWidget *parent = nullptr); ~Widget(); private slots: void on_pushButton_show_clicked(); void on_pushButton_min_clicked(); void on_pushButton_close_clicked(); void on_pushButton_color_clicked(); void showColorValue(); void selectColor(); void on_comboBox_currentIndexChanged(int index); private: void init(); void mousePressEvent(QMouseEvent *event); private: QString rgb2hsb(int rgbR, int rgbG, int rgbB); QString rgb2cmyk(int r,int g,int b); int rgb2hsv_max(int a,int b,int c); int rgb2hsv_min(int a,int b,int c); void getColorValue(const QPixmap &pixmap1); private: QTimer *timer; QPointF pressPoint; bool isScaled=false; bool isLock; private: Ui::Widget *ui; }; #endif // WIDGET_H
Widget.cpp
#include "widget.h" #include "ui_widget.h" #include <QVBoxLayout> #include <QDebug> #include <QScreen> #include "qdesktopwidget.h" #include <QPointer> #include <QPainter> #include <QBitmap> #include <stdio.h> #pragma execution_character_set("utf-8") Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); init(); } Widget::~Widget() { delete ui; } void Widget::init() { ui->label_image->setCursor(Qt::CrossCursor); /* 注意:如果單純開啟視窗透明層效果,在Windows系統中必須設定, 其他系統可忽略。*/ setWindowFlags(Qt::FramelessWindowHint | windowFlags()); setAttribute(Qt::WA_TranslucentBackground); ui->pushButton_show->setIconSize(QSize(14,14)); ui->pushButton_show->setIcon(QIcon(":/image/image/rightArrows.png")); ui->comboBox->setVisible(false); ui->label_image->setVisible(false); this->setFixedSize(277,400-189); ui->widget_4->setFixedSize(277,400-189); ui->widget_5->setFixedSize(277,21); timer = new QTimer(this); timer->setInterval(100); connect(timer, SIGNAL(timeout()), this, SLOT(showColorValue())); timer->start(); } void Widget::mousePressEvent(QMouseEvent *event) { if(isLock)return; if(ui->label_image->underMouse()){ selectColor(); } } void Widget::on_pushButton_show_clicked() { if(ui->pushButton_show->text()=="展開"){ ui->pushButton_show->setIconSize(QSize(14,14)); ui->pushButton_show->setIcon(QIcon(":/image/image/buttonArrows.png")); ui->pushButton_show->setText("收起"); ui->comboBox->setVisible(true); ui->label_image->setVisible(true); this->setFixedSize(277,400); ui->widget_4->setFixedSize(277,400); ui->widget_5->setFixedSize(277,210); } else{ ui->pushButton_show->setIconSize(QSize(14,14)); ui->pushButton_show->setIcon(QIcon(":/image/image/rightArrows.png")); ui->pushButton_show->setText("展開"); ui->comboBox->setVisible(false); ui->label_image->setVisible(false); this->setFixedSize(277,400-189); ui->widget_4->setFixedSize(277,400-189); ui->widget_5->setFixedSize(277,21); } } void Widget::on_pushButton_min_clicked() { this->showMinimized(); } void Widget::on_pushButton_close_clicked() { this->close(); } void Widget::showColorValue() { if(isLock)return; if(this->underMouse())return; int x = QCursor::pos().x(); int y = QCursor::pos().y(); ui->lineEdit_point->setText(tr("x:%1 y:%2").arg(x).arg(y)); int width=ui->label_image->width(); int height=ui->label_image->height(); #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x-width/2, y-height/2, width, height); #else QScreen *screen = QApplication::primaryScreen(); QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(), x-width/2, y-height/2, width, height); #endif if(isScaled){ QPixmap pix=pixmap.scaled(pixmap.width()*2,pixmap.height()*2, Qt::KeepAspectRatio, Qt::SmoothTransformation); ui->label_image->setPixmap(pix); }else{ ui->label_image->setPixmap(pixmap); } #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) QPixmap pixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), x, y, 1, 1); #else QScreen *screen1 = QApplication::primaryScreen(); QPixmap pixmap1 = screen1->grabWindow(QApplication::desktop()->winId(), x, y, 1, 1); #endif getColorValue(pixmap1); } void Widget::on_pushButton_color_clicked() { if(!isLock){ isLock=true; QPixmap pixmap = QPixmap(":/image/image/lock.png").scaled(ui->label_lock->width(), ui->label_lock->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); // 飽滿填充 ui->label_lock->setPixmap(pixmap); } else{ isLock=false; ui->label_lock->setPixmap(QPixmap("")); } } void Widget::selectColor() { int x1 = QCursor::pos().x(); int y1 = QCursor::pos().y(); #if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) QPixmap pixmap1 = QPixmap::grabWindow(QApplication::desktop()->winId(), x1, y1, 1, 1); #else QScreen *screen = QApplication::primaryScreen(); QPixmap pixmap = screen->grabWindow(QApplication::desktop()->winId(), x1, y1, 1, 1); #endif getColorValue(pixmap); } void Widget::getColorValue(const QPixmap &pixmap1) { int red, green, blue; QString strDecimalValue, strHex,strHsb,strCmyk; if (!pixmap1.isNull()) { QImage image = pixmap1.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')); strCmyk=rgb2cmyk(red,green,blue); strHsb=rgb2hsb(red,green,blue); 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()); QString str = tr("background:rgb(%1);").arg(strDecimalValue); ui->pushButton_color->setStyleSheet(str); } } } ui->lineEdit_cmyk->setText(strCmyk); ui->lineEdit_hsb->setText(strHsb); ui->lineEdit_hex->setText(strHex); ui->lineEdit_rgb->setText(strDecimalValue); } int Widget::rgb2hsv_max(int a,int b,int c) { int max = a; if(b > max) max = b; if(c > max) max = c; return max; } int Widget::rgb2hsv_min(int a,int b,int c) { int min = a; if(b < min) min = b; if(c < min) min = c; return min; } QString Widget::rgb2hsb(int r, int g, int b) { int h,s,v; int imax,imin,diff; imax = rgb2hsv_max(r,g,b); imin = rgb2hsv_min(r,g,b); diff = imax - imin; v = imax; if(imax == 0) s = 0; else s = diff; if(diff != 0) { if(r == imax) { h = 60 * (g - b) / diff; } else if(g == imax) { h = 60 * (b - r) / diff + 120; } else { h = 60 * (r - g) / diff + 240; } if(h < 0) h = h + 360; } else h = 0; return QString("%1,%2,%3").arg(h).arg(s).arg(v); } QString Widget::rgb2cmyk(int r, int g, int b) { int c,m,y,k; k=rgb2hsv_min(255-r,255-g,255-b)/2.55; int MyR = (int)(r/2.55); int Div = 100-k; if (Div == 0)Div = 1; c = ((100-MyR-k)/Div)*100; int MyG = (int)(g/2.55); m = ((100-MyG-k)/Div)*100; int MyB = (int)(b/2.55); y = ((100-MyB-k)/Div)*100; return QString("%1,%2,%3,%4").arg(c).arg(m).arg(y).arg(k); } void Widget::on_comboBox_currentIndexChanged(int index) { if(index==1){ isScaled=true; } else{ isScaled=false; } }
WidgetMove.h
#ifndef WIDGETMOVE_H #define WIDGETMOVE_H #include <QObject> #include <QWidget> #include <QEvent> #include <QMouseEvent> #include <qt_windows.h> class WidgetMove:public QWidget { Q_OBJECT public: explicit WidgetMove(QWidget *parent=0); protected: // 進行鼠介面的拖動 virtual void mousePressEvent(QMouseEvent *event); }; #endif // WIDGETMOVE_H
WidgetMove.cpp
#include "widgetmove.h"
#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif
WidgetMove::WidgetMove(QWidget *parent):QWidget(parent)
{
}
void WidgetMove::mousePressEvent(QMouseEvent *event)
{
if (ReleaseCapture())
{
QWidget *pWindow = this->window();
if (pWindow->isTopLevel())
{
SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
event->ignore();
}
WidgetMove是用來提升自定義標題欄的類