Permalink
Browse files

Some hsl functions. Buggy.

  • Loading branch information...
1 parent f0efa6e commit e5b7205332aa8f8e7fc44e4135801f71b1bed97b Aaron Leung committed Apr 9, 2012
Showing with 81 additions and 1 deletion.
  1. +4 −1 basic.scss
  2. +8 −0 context.cpp
  3. +1 −0 context.hpp
  4. +47 −0 functions.cpp
  5. +6 −0 functions.hpp
  6. +15 −0 node.hpp
View
@@ -71,4 +71,7 @@ div {
a: unit(10);
b: unit(10%);
c: unit(10boo);
-}
+
+ a: hsl(23, 100%, 50%);
+ b: hsl(120, 100%, 50%);
+}
View
@@ -27,6 +27,12 @@ namespace Sass {
function_env[pair<string, size_t>(f.name, f.parameters.size())] = f;
}
+ inline void Context::register_function(Function_Descriptor d, Implementation ip, size_t arity)
+ {
+ Function f(d, ip);
+ function_env[pair<string, size_t>(f.name, arity)] = f;
+ }
+
void Context::register_functions()
{
using namespace Functions;
@@ -40,6 +46,8 @@ namespace Sass {
register_function(mix_2_descriptor, mix_2);
register_function(mix_3_descriptor, mix_3);
// HSL Functions
+ register_function(hsla_descriptor, hsla);
+ register_function(hsl_descriptor, hsl);
register_function(invert_descriptor, invert);
// Opacity Functions
register_function(alpha_descriptor, alpha);
View
@@ -48,6 +48,7 @@ namespace Sass {
~Context();
void register_function(Function_Descriptor d, Implementation ip);
+ void register_function(Function_Descriptor d, Implementation ip, size_t arity);
void register_functions();
};
View
@@ -95,6 +95,53 @@ namespace Sass {
// HSL Functions ///////////////////////////////////////////////////////
+ double h_to_rgb(double m1, double m2, double h) {
+ if (h < 0) ++h;
+ if (h > 1) --h;
+ if (h*6 < 1) return m1 + (m2 - m1)*h*6;
+ if (h*2 < 1) return m2;
+ if (h*3 < 2) return m1 + (m2 - m1) * (2/3 - h)*6;
+ return m1;
+ }
+
+ Node hsla_impl(double h, double s, double l, double a = 1) {
+ h = (((static_cast<int>(h) % 360) + 360) % 360) / 360;
+ s = s / 100;
+ l = l / 100;
+
+ double m2;
+ if (l <= 0.5) m2 = l*(s+1);
+ else m2 = l+s-l*s;
+ double m1 = l*2-m2;
+ double r = h_to_rgb(m1, m2, h+1/3);
+ double g = h_to_rgb(m1, m2, h);
+ double b = h_to_rgb(m1, m2, h-1/3);
+ return Node(0, r, g, b, a);
+ }
+
+ Function_Descriptor hsla_descriptor =
+ { "hsla", "$hue", "$saturation", "$lightness", "$alpha", 0 };
+ Node hsla(const vector<Token>& parameters, map<Token, Node>& bindings) {
+ double h = bindings[parameters[0]].numeric_value();
+ double s = bindings[parameters[1]].numeric_value();
+ double l = bindings[parameters[2]].numeric_value();
+ double a = bindings[parameters[3]].numeric_value();
+ Node color(hsla_impl(h, s, l, a));
+ color.line_number = bindings[parameters[0]].line_number;
+ return color;
+ }
+
+ Function_Descriptor hsl_descriptor =
+ { "hsl", "$hue", "$saturation", "$lightness", 0 };
+ Node hsl(const vector<Token>& parameters, map<Token, Node>& bindings) {
+ double h = bindings[parameters[0]].numeric_value();
+ double s = bindings[parameters[1]].numeric_value();
+ double l = bindings[parameters[2]].numeric_value();
+ Node color(hsla_impl(h, s, l));
+ color.line_number = bindings[parameters[0]].line_number;
+ return color;
+ }
+
Function_Descriptor invert_descriptor =
{ "invert", "$color", 0 };
Node invert(const vector<Token>& parameters, map<Token, Node>& bindings) {
View
@@ -71,6 +71,12 @@ namespace Sass {
Node mix_3(const vector<Token>& parameters, map<Token, Node>& bindings);
// HSL Functions ///////////////////////////////////////////////////////
+
+ extern Function_Descriptor hsla_descriptor;
+ Node hsla(const vector<Token>& parameters, map<Token, Node>& bindings);
+
+ extern Function_Descriptor hsl_descriptor;
+ Node hsl(const vector<Token>& parameters, map<Token, Node>& bindings);
extern Function_Descriptor invert_descriptor;
Node invert(const vector<Token>& parameters, map<Token, Node>& bindings);
View
@@ -121,6 +121,21 @@ namespace Sass {
return *this;
}
+ double numeric_value()
+ {
+ switch (type)
+ {
+ case number:
+ case numeric_percentage:
+ return content.numeric_value;
+ case numeric_dimension:
+ return content.dimension.numeric_value;
+ default:
+ break;
+ // throw an exception?
+ }
+ }
+
Node& operator+=(const Node& n)
{
for (int i = 0; i < n.size(); ++i) {

0 comments on commit e5b7205

Please sign in to comment.