经常要屏幕取色,以前网上下的工具找不到了,就随手写了一个。
源码地址:
https://github.com/SpaceView/screenColorPicker_Qthttps://github.com/SpaceView/screenColorPicker_Qt这里已经有编译好的screenColorPicker.exe在bin文件夹内,下载即可使用。
库是Qt5.15.0,直接到网上下载找到那几个库就行了。我已经上传了Qt5Widgets.dll,Qt5GUI.dll, Qt5Core.dll,应该可以直接跑起来了。Ubuntu下的话就自己编译一下吧。
源码非常简单,只有短短十多行。
timer = new QTimer(this);
timer->start(400);
image = new QImage;
pm = new QPixmap;
setWindowTitle("Screen Color Picker");
connect(timer,&QTimer::timeout,this, [this](){
this->pos = QCursor::pos();
qDebug()winId());
*pm = screen->grabWindow(QApplication::desktop()->winId());
*image = pm->toImage();
QRgb b = image->pixel(this->pos);
//QColor *c = new QColor;
color.setRgb(b);
QColor ic = QColor(255- color.red(), 255-color.green(), 255-color.blue());
//qDebug() name();
ui->lineEdit->setText(color.name());
QString str = "QLabel { background-color :" + color.name() + " ; color :" + ic.name() + " ; }";
ui->label->setStyleSheet(str);
说明:这里主要就是利用lambda表达式在时钟timeout来临时,读取一次屏幕当前像素的颜色。
其中时间长度设为400ms读一次。需要的话可更设置得更小一些,以消除滞后的感觉。
本文结束。