Skip to content

Commit

Permalink
Merge pull request sass#179 from Nabellaleen/FixRoundedColorsLikeRuby…
Browse files Browse the repository at this point in the history
…Sass

Fix rounded colors like ruby sass
  • Loading branch information
HamptonMakes committed Oct 23, 2013
2 parents 7128b45 + fea744c commit 07605b3
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>(((static_cast<int>(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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -306,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,
Expand All @@ -323,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,
Expand All @@ -340,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,
Expand All @@ -357,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,
Expand Down

0 comments on commit 07605b3

Please sign in to comment.