diff --git a/libs/gtkmm2ext/colors.cc b/libs/gtkmm2ext/colors.cc index b2534b60be..562c570b9a 100644 --- a/libs/gtkmm2ext/colors.cc +++ b/libs/gtkmm2ext/colors.cc @@ -240,6 +240,10 @@ HSV::HSV (double hh, double ss, double vv, double aa) /* normalize negative hue values into positive range */ h = 360.0 + h; } + + s = max (0.0, min (1.0, s)); + v = max (0.0, min (1.0, v)); + a = max (0.0, min (1.0, a)); } HSV::HSV (Color c) @@ -328,6 +332,25 @@ HSV::operator== (const HSV& other) a == other.a; } +HSV +HSV::darker (double factor) const +{ + HSV hsv (*this); + /* factor == 1.0: reduce all the way to zero */ + hsv.v -= max (0.0, min (1.0, factor)) * hsv.v; + return hsv; +} + +HSV +HSV::lighter (double factor) const +{ + HSV hsv (*this); + /* factor == 1.0: increase all the way to 1.0 */ + hsv.v += max (0.0, min (1.0, factor)) * (1.0 - hsv.v); + return hsv; +} + + HSV HSV::shade (double factor) const { @@ -344,15 +367,21 @@ HSV::shade (double factor) const */ if (factor > 1.0) { - if (s < 88) { - hsv.v += (hsv.v * (factor * 10.0)); - } + /* darker */ + /* increase saturation (factor is > 1.0, so s grows) */ hsv.s *= factor; + if (hsv.s >= 0.88) { + /* above saturation threshold, so decrease v a bit */ + hsv.v -= (hsv.v * 0.05); + } } else { - if (s < 88) { - hsv.v -= (hsv.v * (factor * 10.0)); - } + /* lighter */ + /* reduce saturation, (factor is < 1.0, so s shrinks) */ hsv.s *= factor; + if (hsv.s > 0.88) { + /* still above 88% saturation, so increase v a bit */ + hsv.v += (hsv.v * 0.05); + } } hsv.clamp(); diff --git a/libs/gtkmm2ext/gtkmm2ext/colors.h b/libs/gtkmm2ext/gtkmm2ext/colors.h index 56707cb509..89d4182422 100644 --- a/libs/gtkmm2ext/gtkmm2ext/colors.h +++ b/libs/gtkmm2ext/gtkmm2ext/colors.h @@ -115,8 +115,8 @@ struct LIBGTKMM2EXT_API HSV double distance (const HSV& other) const; HSV delta (const HSV& other) const; - HSV darker (double factor = 1.3) const { return shade (factor); } - HSV lighter (double factor = 0.7) const { return shade (factor); } + HSV darker (double factor = 1.3) const; + HSV lighter (double factor = 0.7) const; HSV shade (double factor) const; HSV mix (const HSV& other, double amt) const;