From 243f8ea98271c0680a8e99e4465ac0075e55e3a5 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 9 Oct 2012 01:01:59 +0200 Subject: [PATCH] Make the HSV pickers keep the same luminance no matter what the saturation is. Makes for better LGG controls. --- main.cpp | 6 +++--- util.cpp | 15 +++++++++++++++ util.h | 4 ++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 0340cac..3c807a1 100644 --- a/main.cpp +++ b/main.cpp @@ -47,9 +47,9 @@ void update_hsv(Effect *lift_gamma_gain_effect, Effect *saturation_effect) RGBTriplet gamma(1.0f, 1.0f, 1.0f); RGBTriplet gain(1.0f, 1.0f, 1.0f); - hsv2rgb(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b); - hsv2rgb(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b); - hsv2rgb(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b); + hsv2rgb_normalized(lift_theta, lift_rad, lift_v, &lift.r, &lift.g, &lift.b); + hsv2rgb_normalized(gamma_theta, gamma_rad, gamma_v * 2.0f, &gamma.r, &gamma.g, &gamma.b); + hsv2rgb_normalized(gain_theta, gain_rad, gain_v * 4.0f, &gain.r, &gain.g, &gain.b); bool ok = lift_gamma_gain_effect->set_vec3("lift", (float *)&lift); ok = ok && lift_gamma_gain_effect->set_vec3("gamma", (float *)&gamma); diff --git a/util.cpp b/util.cpp index 08298c7..1c47e11 100644 --- a/util.cpp +++ b/util.cpp @@ -46,6 +46,21 @@ void hsv2rgb(float h, float s, float v, float *r, float *g, float *b) *b += m; } +void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b) +{ + float ref_r, ref_g, ref_b; + hsv2rgb(h, s, v, r, g, b); + hsv2rgb(h, 0.0f, v, &ref_r, &ref_g, &ref_b); + float lum = 0.2126 * *r + 0.7152 * *g + 0.0722 * *b; + float ref_lum = 0.2126 * ref_r + 0.7152 * ref_g + 0.0722 * ref_b; + if (lum > 1e-3) { + float fac = ref_lum / lum; + *r *= fac; + *g *= fac; + *b *= fac; + } +} + std::string read_file(const std::string &filename) { static char buf[131072]; diff --git a/util.h b/util.h index da2bf50..4834bbd 100644 --- a/util.h +++ b/util.h @@ -15,6 +15,10 @@ // Converts a HSV color to RGB. Assumes h in [0, 2pi> or [-pi, pi> void hsv2rgb(float h, float s, float v, float *r, float *g, float *b); +// Converts a HSV color to RGB, but keeps luminance constant +// (ie. color luminance is as if S=0). +void hsv2rgb_normalized(float h, float s, float v, float *r, float *g, float *b); + // Column major (same as OpenGL). typedef double Matrix3x3[9]; -- 2.39.2