对比度 饱和度 调节 问题请教

@恩赐解脱 你好，想跟你请教个问题：

0

0

void brightness_contrast_ajust(RGB *rgb, int contrast, int brightness) {

// 对比度阈值
int threshold = 128;

// 计算对比度数组
double cv = (contrast <= -255) ? -1.0f : contrast / 255.0f;
if (contrast > 0 && contrast < 255) {
cv = 1.0f / (1.0f - cv) - 1.0f;
}

int r = rgb->r;
int g = rgb->g;
int b = rgb->b;

r = (contrast > 0) ? CLAMP(r + brightness) : r;
g = (contrast > 0) ? CLAMP(g + brightness) : g;
b = (contrast > 0) ? CLAMP(b + brightness) : b;

if (contrast >= 255) {
r = (r > threshold) ? 255 : 0;
g = (g > threshold) ? 255 : 0;
b = (b > threshold) ? 255 : 0;
} else {
r = CLAMP(r + (int) ((r - threshold) * cv + 0.5f));
g = CLAMP(g + (int) ((g - threshold) * cv + 0.5f));
b = CLAMP(b + (int) ((b - threshold) * cv + 0.5f));
}

r = (contrast < 0) ? CLAMP(r + brightness) : r;
g = (contrast < 0) ? CLAMP(g + brightness) : g;
b = (contrast < 0) ? CLAMP(b + brightness) : b;

rgb->r = CLAMP(r);
rgb->g = CLAMP(g);
rgb->b = CLAMP(b);
}

void saturation_ajust(RGB *rgb, int saturation) {
int r = rgb->r;
int g = rgb->g;
int b = rgb->b;

double l;
double factor = saturation / 100.0;

// 调整饱和度
int rgb_max = MAX(MAX(r, g), b);
int rgb_min = MIN(MIN(r, g), b);

l = (rgb_max + rgb_min) / (2 * 255.0f);

r = (int) (r + (r - l * 255.0) * factor);
g = (int) (g + (g - l * 255.0) * factor);
b = (int) (b + (b - l * 255.0) * factor);

rgb->r = CLAMP(r);
rgb->g = CLAMP(g);
rgb->b = CLAMP(b);
}