Skip to content

Commit

Permalink
ofxsLut: fix rgb_to_hsl
Browse files Browse the repository at this point in the history
  • Loading branch information
devernay committed Jun 17, 2020
1 parent ac3bf99 commit 0a01bd8
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions ofxsLut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ rgb_to_hsv( float r,
float *s,
float *v )
{
float min = (std::min)((std::min)(r, g), b);
float max = (std::max)((std::max)(r, g), b);
float minv = (std::min)((std::min)(r, g), b);
float maxv = (std::max)((std::max)(r, g), b);

*v = max; // v
*v = maxv; // v

float delta = max - min;
float delta = maxv - minv;

if (max != 0.) {
*s = delta / max; // s
if (maxv != 0.) {
*s = delta / maxv; // s
} else {
// r = g = b = 0 // s = 0, v is undefined
*s = 0.f;
Expand All @@ -154,9 +154,9 @@ rgb_to_hsv( float r,

if (delta == 0.) {
*h = 0.f; // gray
} else if (r == max) {
} else if (r == maxv) {
*h = (g - b) / delta; // between yellow & magenta
} else if (g == max) {
} else if (g == maxv) {
*h = 2 + (b - r) / delta; // between cyan & yellow
} else {
*h = 4 + (r - g) / delta; // between magenta & cyan
Expand Down Expand Up @@ -239,28 +239,27 @@ rgb_to_hsl( float r,
float *s,
float *l )
{
float min = (std::min)((std::min)(r, g), b);
float max = (std::max)((std::max)(r, g), b);
float minv = (std::min)((std::min)(r, g), b);
float maxv = (std::max)((std::max)(r, g), b);

*l = (min + max) / 2;
*l = (minv + maxv) / 2;

float delta = max - min;
minv = (std::max)(0.f, minv);
maxv = (std::min)(1.f, maxv);

if (max != 0.) {
*s = (*l <= 0.5) ? ( delta / (max + min) ) : ( delta / (2 - max - min) ); // s = delta/(1-abs(2L-1))
} else {
// r = g = b = 0 // s = 0
float delta = maxv - minv;

if (delta == 0.) {
*h = 0.f; // gray
*s = 0.f;
*h = 0.f;

return;
}
*s = (*l <= 0.5) ? ( delta / (maxv + minv) ) : ( delta / ( 2 - (maxv + minv) ) ); // s = delta/(1-abs(2L-1))

if (delta == 0.) {
*h = 0.f; // gray
} else if (r == max) {
if (r == maxv) {
*h = (g - b) / delta; // between yellow & magenta
} else if (g == max) {
} else if (g == maxv) {
*h = 2 + (b - r) / delta; // between cyan & yellow
} else {
*h = 4 + (r - g) / delta; // between magenta & cyan
Expand Down

0 comments on commit 0a01bd8

Please sign in to comment.