From d04d5523cfdd2d0f91b2113c79ccbecc191b513e Mon Sep 17 00:00:00 2001 From: Nabellaleen Date: Fri, 18 Oct 2013 14:48:20 +0200 Subject: [PATCH 1/2] Minimal changes to fix lighten/saturate functions modifying hue. But result is different from Sass. --- functions.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/functions.cpp b/functions.cpp index 040be540e..499ef455e 100644 --- a/functions.cpp +++ b/functions.cpp @@ -206,12 +206,6 @@ namespace Sass { Color* hsla_impl(double h, double s, double l, double a, Context& ctx, const string& path, size_t line) { h = static_cast(((static_cast(h) % 360) + 360) % 360) / 360.0; - s = (s < 0) ? 0 : - (s > 100) ? 100 : - s; - l = (l < 0) ? 0 : - (l > 100) ? 100 : - l; s /= 100.0; l /= 100.0; @@ -220,9 +214,9 @@ namespace Sass { else m2 = l+s-l*s; double m1 = l*2-m2; // round the results -- consider moving this into the Color constructor - double r = std::floor(h_to_rgb(m1, m2, h+1.0/3.0) * 255.0 + 0.5); - double g = std::floor(h_to_rgb(m1, m2, h) * 255.0 + 0.5); - double b = std::floor(h_to_rgb(m1, m2, h-1.0/3.0) * 255.0 + 0.5); + double r = (h_to_rgb(m1, m2, h+1.0/3.0) * 255.0); + double g = (h_to_rgb(m1, m2, h) * 255.0); + double b = (h_to_rgb(m1, m2, h-1.0/3.0) * 255.0); return new (ctx.mem) Color(path, line, r, g, b, a); } From fea744c34562cb384a512674c584657691ccf907 Mon Sep 17 00:00:00 2001 From: Nabellaleen Date: Fri, 18 Oct 2013 14:57:44 +0200 Subject: [PATCH 2/2] Add range striping to saturation/lightness modification function to work like ruby sass implementation --- functions.cpp | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/functions.cpp b/functions.cpp index 499ef455e..0d3748a7a 100644 --- a/functions.cpp +++ b/functions.cpp @@ -300,9 +300,15 @@ namespace Sass { HSL hsl_color = rgb_to_hsl(rgb_color->r(), rgb_color->g(), rgb_color->b()); + //Check lightness is not negative before lighten it + double hslcolorL = hsl_color.l; + if (hslcolorL < 0) { + hslcolorL = 0; + } + return hsla_impl(hsl_color.h, hsl_color.s, - hsl_color.l + amount->value(), + hslcolorL + amount->value(), rgb_color->a(), ctx, path, @@ -317,9 +323,16 @@ namespace Sass { HSL hsl_color = rgb_to_hsl(rgb_color->r(), rgb_color->g(), rgb_color->b()); + + //Check lightness if not over 100, before darken it + double hslcolorL = hsl_color.l; + if (hslcolorL > 100) { + hslcolorL = 100; + } + return hsla_impl(hsl_color.h, hsl_color.s, - hsl_color.l - amount->value(), + hslcolorL - amount->value(), rgb_color->a(), ctx, path, @@ -334,8 +347,15 @@ namespace Sass { HSL hsl_color = rgb_to_hsl(rgb_color->r(), rgb_color->g(), rgb_color->b()); + //Check saturation is not negative before saturate it + double hslcolorS = hsl_color.s; + if (hslcolorS < 0) { + hslcolorS = 0; + } + + return hsla_impl(hsl_color.h, - hsl_color.s + amount->value(), + hslcolorS + amount->value(), hsl_color.l, rgb_color->a(), ctx, @@ -351,8 +371,14 @@ namespace Sass { HSL hsl_color = rgb_to_hsl(rgb_color->r(), rgb_color->g(), rgb_color->b()); + //Check saturation is not over 100 before desaturate it + double hslcolorS = hsl_color.s; + if (hslcolorS > 100) { + hslcolorS = 100; + } + return hsla_impl(hsl_color.h, - hsl_color.s - amount->value(), + hslcolorS - amount->value(), hsl_color.l, rgb_color->a(), ctx,