Qt实现"颜色减淡"算法
2016-05-16 17:46
351 查看
“颜色减淡”模式的公式是:基色+(基色*混合色)/(255-混合色)= 结果色,其中(255-混合色)当于混合色的反相。
1、若混合色为0(黑色),则由于(基色*混合色)这项为0,则结果色等于基色,图像不发生变化;基混合色为128(50%的黑),情况分为两种:
(1)当基色小于128时,结果色等于2基色,由于这个数值小于255所以呈某种阶调的灰。
(2)而当基色大于128(50%的黑)时,结果色等于2基色,这个值是大于255值,255(白色);
2、若混合色为255(白色),则混合色的反相为0,无论基色为何值,结果色都大于255,归为255(白色)。
下面是Qt的实现代码
1、mainwindow.h
上面的注释中有我演算其它图形图像算法的一些代码,所以看起来有点乱,不过不影响整体效果
软件实际截图
1、若混合色为0(黑色),则由于(基色*混合色)这项为0,则结果色等于基色,图像不发生变化;基混合色为128(50%的黑),情况分为两种:
(1)当基色小于128时,结果色等于2基色,由于这个数值小于255所以呈某种阶调的灰。
(2)而当基色大于128(50%的黑)时,结果色等于2基色,这个值是大于255值,255(白色);
2、若混合色为255(白色),则混合色的反相为0,无论基色为何值,结果色都大于255,归为255(白色)。
下面是Qt的实现代码
1、mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QLabel> #include <qmath.h> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); public slots: void onSliderValueChange(int value); private: Ui::MainWindow *ui; //图像显示宽度 int width; //图像显示高度 int height; //图像 QImage image; //将图片设置到label上作为背景 void setLabelImage(QLabel *widget, const QImage &image); //获取颜色减淡算法生成的图片 void getNewImage(int secondaryColors, QImage* outImage); //颜色减淡算法(基色+(基色*混合色)/(255-混合色)= 结果色) uint colorDodgeAlgorithm(int primaryColor, int secondaryColors); }; #endif // MAINWINDOW_H2、mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->horizontalSlider->setMinimum(0);
ui->horizontalSlider->setMaximum(255);
connect(ui->horizontalSlider, SIGNAL(valueChanged(int)), this, SLOT(onSliderValueChange(int)));
onSliderValueChange(ui->horizontalSlider->value());
width = ui->label_image1->width();
height = ui->label_image1->height();
image = QImage("D:/cx-文件/Desktop/d4b07c36acaf2edd45eece0a8a1001e938019374.jpg").scaled(width, height);
width = image.width();
height = image.height();
setLabelImage(ui->label_image1, image);
setLabelImage(ui->label_image2, image);
}
void MainWindow::setLabelImage(QLabel *widget, const QImage &image) {
widget->setAutoFillBackground(true);
QPalette palette = widget->palette();
palette.setBrush(QPalette::Window, QBrush(image));
widget->setPalette(palette);
}
void MainWindow::getNewImage(int secondaryColors, QImage* outImage) {
for(int i=0; i<width; i++) {
for(int j=0; j<height; j++) {
QRgb rgb = image.pixel(i,j);
// QColor rgbColor = QColor::fromRgb(rgb);
//float hue; // [0,360] 色调(色相)
//float saturation; // [0,100] 饱和度
//float luminance; // [0,100] 亮度
// QColor hslColor = rgbColor.toHsl();
// int h,s,l;
// hslColor.getHsl(&h, &s, &l);
// l *= secondaryColors / 255;
// l = colorDodgeAlgorithm(l, secondaryColors);
// hslColor = QColor::fromHsl(h,s,l);
int r = qRed(rgb);
int g = qGreen(rgb);
int b = qBlue(rgb);
uint resultR = colorDodgeAlgorithm(r, secondaryColors);
uint resultG = colorDodgeAlgorithm(g, secondaryColors);
uint resultB = colorDodgeAlgorithm(b, secondaryColors);
uint resultRgb = ((resultR & 255)<<16 | (resultG & 255)<<8 | (resultB & 255));
outImage->setPixel(i, j, resultRgb);
// outImage->setPixel(i, j, colorDodgeAlgorithm(rgb, secondaryColors));
// outImage->setPixel(i, j, hslColor.rgb());
}
}
}
uint MainWindow::colorDodgeAlgorithm(int primaryColor, int secondaryColors)
{
if(secondaryColors >= 255) {
return 255;
}
//基色 +(基色*混合色)/(255-混合色)= 结果色
int result = (primaryColor + (uint)(primaryColor*secondaryColors) / (255 - secondaryColors));
// int result = primaryColor - secondaryColors;
if(result > 255) {
result = 255;
} else if(result < 0) {
result = 0;
}
return result;
}
void MainWindow::onSliderValueChange(int value)
{
ui->label_slider_value->setText(QString("%1 / %2").arg(QString::number(value)).arg(QString::number(ui->horizontalSlider->maximum())));
if(image.isNull()) {
return;
}
// uint secondaryColors;
// if(value == 0) {
// secondaryColors = 0;
//} else if(value == ui->horizontalSlider->maximum()) {
// secondaryColors = 4294967295;
//} else {
// secondaryColors = (uint)(429496729.5 * value);
//}
QImage *outImage = new QImage(width, height, QImage::Format_RGB32);
getNewImage(value, outImage);
setLabelImage(ui->label_image2, *outImage);
}
MainWindow::~MainWindow()
{
delete ui;
}
上面的注释中有我演算其它图形图像算法的一些代码,所以看起来有点乱,不过不影响整体效果
软件实际截图
相关文章推荐
- Qt加载本地html文件 并且调用JS的函数在Qt中使用
- QT下载地址记录
- Qt实现应用单实例运行(2)-使用自定义类
- qtablewidget 设置居中
- Qt实现应用单实例运行(1)-使用共享内存
- Qt学习笔记:小技巧
- Loadrunner运行QTP脚本
- Qt配置文件中文读写小例子
- QT用API获得文件的版本信息
- QT-Demo-Colck-01
- MQTT 3.1.1协议[翻译]
- Pyqt 浏览器截图小demo
- Pyqt 浏览器禁止对话框小demo
- beagleBone black 中QT的移植
- QDialog对话框中show()和exec()
- Ubuntu下编译ARM平台Qt的MySQL插件
- Qt Windows下的exe执行文件中获取exe的版本号遇到的error
- Qt窗口之QMainWindow、QDialog、QWidget
- 在win10 x64下配置QT5.6.0 Mingw+opencv2.4.12
- Qt:基于Qt开发的开源小工具包:JQTools