Qt 中实现两个颜色叠加的处理方式

设置一个叠加的透明颜色

    QColor qgroundColor = QColor(128, 128, 128, 128); //叠加初始颜色
    QColor tgroundColor = QColor("#ffffff");
    //具体的填充颜色
    __int64 nFillColor = //等于某一个颜色值

    tgroundColor .setRgb(nFillColor);
    QColor color = getNewColor(tgroundColor , qgroundColor );

    float calculateRGBAValue(const float fTranslucent1, const float fTranslucent2, const float RGBVlue1, const float RGBVlue2) const
{
    return (RGBVlue1 * fTranslucent1 * (1.0 - fTranslucent2) + RGBVlue2 * fTranslucent2) / (fTranslucent1 + fTranslucent2 - fTranslucent1 * fTranslucent2);  //计算两个叠加后的值
}

const QColor getNewColor(const QColor& backgroundColor, const QColor& foregroundColor) const
{
    //处理叠加的RGB和透明度
    //处理两种颜色叠加时透明度a-alpha值
    float fTranslucent1 = backgroundColor.alpha() / 255.0;
    float fTranslucent2 = foregroundColor.alpha() / 255.0;
    float fTranslucent = fTranslucent1 + fTranslucent2 - fTranslucent1 * fTranslucent2;
    //计算R-Red值
    float fRed1 = backgroundColor.red() / 255.0;
    float fRed2 = foregroundColor.red() / 255.0;
    float fRed = calculateRGBAValue(fTranslucent1, fTranslucent2, fRed1, fRed2);

    //计算G - Green值
    float fGreen1 = backgroundColor.green() / 255.0;
    float fGreen2 = foregroundColor.green() / 255.0;
    float fGreen = calculateRGBAValue(fTranslucent1, fTranslucent2, fGreen1, fGreen2);

    //计算B - Blue值
    float fBlue1 = backgroundColor.blue() / 255.0;
    float fBlue2 = foregroundColor.blue() / 255.0;
    float fBlue = calculateRGBAValue(fTranslucent1, fTranslucent2, fBlue1, fBlue2);
    return QColor(fRed * 255, fGreen * 255, fBlue * 255, fTranslucent * 255);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值