Qt "颜色减淡"算法

举报
福州司马懿 发表于 2021/11/19 05:44:15 2021/11/19
【摘要】 开发环境 Qt5.5.1、Qt Creator 3.5.1 “颜色减淡”模式的公式是:基色+(基色*混合色)/(255-混合色)= 结果色,其中(255-混合色)当于混合色的反相。 1、若混合色为0(黑色),则由于(基色*混合色)这项为0,则结果色等于基色,图像不发生变化;基混合色为128(50%的黑),情况分为两种: (1)当...

开发环境 Qt5.5.1、Qt Creator 3.5.1

“颜色减淡”模式的公式是:基色+(基色*混合色)/(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. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. #include <QLabel>
  5. #include <qmath.h>
  6. namespace Ui {
  7. class MainWindow;
  8. }
  9. class MainWindow : public QMainWindow
  10. {
  11. Q_OBJECT
  12. public:
  13. explicit MainWindow(QWidget *parent = 0);
  14. ~MainWindow();
  15. public slots:
  16. void onSliderValueChange(int value);
  17. private:
  18. Ui::MainWindow *ui;
  19. //图像显示宽度
  20. int width;
  21. //图像显示高度
  22. int height;
  23. //图像
  24. QImage image;
  25. //将图片设置到label上作为背景
  26. void setLabelImage(QLabel *widget, const QImage &image);
  27. //获取颜色减淡算法生成的图片
  28. void getNewImage(int secondaryColors, QImage* outImage);
  29. //颜色减淡算法(基色+(基色*混合色)/(255-混合色)= 结果色)
  30. uint colorDodgeAlgorithm(int primaryColor, int secondaryColors);
  31. };
  32. #endif // MAINWINDOW_H

#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;
}

软件实际截图




文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/51426834

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。