#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;
}
调整RGB饱和度
于 2025-01-13 22:09:58 首次发布