调整RGB饱和度

#include <iostream>
#include <cmath>

// 函数用于将 RGB 颜色转换为 HSV 颜色空间
void RGBtoHSV(int r, int g, int b, float& h, float& s, float& v) {
    float rf = r / 255.0f;
    float gf = g / 255.0f;
    float bf = b / 255.0f;
    float cmax = std::max(std::max(rf, gf), bf);
    float cmin = std::min(std::min(rf, gf), bf);
    float delta = cmax - cmin;

    // 计算色调 H
    if (delta == 0) {
        h = 0;
    } else if (cmax == rf) {
        h = 60 * ((gf - bf) / delta);
        if (h < 0) h += 360;
    } else if (cmax == gf) {
        h = 60 * ((bf - rf) / delta + 2);
    } else {
        h = 60 * ((rf - gf) / delta + 4);
    }

    // 计算饱和度 S
    if (cmax == 0) {
        s = 0;
    } else {
        s = delta / cmax;
    }

    // 计算明度 V
    v = cmax;
}

// 函数用于将 HSV 颜色转换为 RGB 颜色空间
void HSVtoRGB(float h, float s, float v, int& r, int& g, int& b) {
    float c = v * s;
    float x = c * (1 - std::abs(std::fmod(h / 60.0f, 2) - 1));
    float m = v - c;
    if (h >= 0 && h < 60) {
        r = (c + m) * 255;
        g = (x + m) * 255;
        b = m * 255;
    } else if (h >= 60 && h < 120) {
        r = (x + m) * 255;
        g = (c + m) * 255;
        b = m * 255;
    } else if (h >= 120 && h < 180) {
        r = m * 255;
        g = (c + m) * 255;
        b = (x + m) * 255;
    } else if (h >= 180 && h < 240) {
        r = m * 255;
        g = (x + m) * 255;
        b = (c + m) * 255;
    } else if (h >= 240 && h < 300) {
        r = (x + m) * 255;
        g = m * 255;
        b = (c + m) * 255;
    } else {
        r = (c + m) * 255;
        g = m * 255;
        b = (x + m) * 255;
    }
}

// 函数用于增加饱和度
void increaseSaturation(int r, int g, int b, int& newR, int& newG, int& newB, float saturationFactor) {
    float h, s, v;
    RGBtoHSV(r, g, b, h, s, v);
    s = std::min(1.0f, s * saturationFactor);  // 增加饱和度
    HSVtoRGB(h, s, v, newR, newG, newB);
}

int main() {
    int r = 100, g = 150, b = 200;
    int newR, newG, newB;
    float saturationFactor = 1.5;  // 饱和度增加的倍数,可根据需要调整
    increaseSaturation(r, g, b, newR, newG, newB, saturationFactor);
    std::cout << "Original RGB: (" << r << ", " << g << ", " << b << ")" << std::endl;
    std::cout << "New RGB: (" << newR << ", " << newG << ", " << newB << ")" << std::endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值