Commit ac99e122 by Aaron Leung

Implemented the "invert" builtin.

parent 549f708f
...@@ -30,6 +30,7 @@ namespace Sass { ...@@ -30,6 +30,7 @@ namespace Sass {
void Context::register_functions() void Context::register_functions()
{ {
using namespace Functions; using namespace Functions;
// RGB Functions
register_function(rgb_descriptor, rgb); register_function(rgb_descriptor, rgb);
register_function(rgba_4_descriptor, rgba_4); register_function(rgba_4_descriptor, rgba_4);
register_function(rgba_2_descriptor, rgba_2); register_function(rgba_2_descriptor, rgba_2);
...@@ -38,6 +39,8 @@ namespace Sass { ...@@ -38,6 +39,8 @@ namespace Sass {
register_function(blue_descriptor, blue); register_function(blue_descriptor, blue);
register_function(mix_2_descriptor, mix_2); register_function(mix_2_descriptor, mix_2);
register_function(mix_3_descriptor, mix_3); register_function(mix_3_descriptor, mix_3);
// HSL Functions
register_function(invert_descriptor, invert);
} }
} }
\ No newline at end of file
...@@ -7,6 +7,8 @@ namespace Sass { ...@@ -7,6 +7,8 @@ namespace Sass {
// TO DO: functions need to check the types of their arguments // TO DO: functions need to check the types of their arguments
// RGB Functions ///////////////////////////////////////////////////////
Function_Descriptor rgb_descriptor = Function_Descriptor rgb_descriptor =
{ "rgb", "$red", "$green", "$blue", 0 }; { "rgb", "$red", "$green", "$blue", 0 };
Node rgb(const vector<Token>& parameters, map<Token, Node>& bindings) { Node rgb(const vector<Token>& parameters, map<Token, Node>& bindings) {
...@@ -55,7 +57,7 @@ namespace Sass { ...@@ -55,7 +57,7 @@ namespace Sass {
return bindings[parameters[0]][2]; return bindings[parameters[0]][2];
} }
Node mix_impl(Node color1, Node color2, double weight) { Node mix_impl(Node color1, Node color2, double weight = 50) {
double p = weight/100; double p = weight/100;
double w = 2*p - 1; double w = 2*p - 1;
double a = color1[3].content.numeric_value - color2[3].content.numeric_value; double a = color1[3].content.numeric_value - color2[3].content.numeric_value;
...@@ -76,9 +78,7 @@ namespace Sass { ...@@ -76,9 +78,7 @@ namespace Sass {
Function_Descriptor mix_2_descriptor = Function_Descriptor mix_2_descriptor =
{ "mix", "$color1", "$color2", 0 }; { "mix", "$color1", "$color2", 0 };
Node mix_2(const vector<Token>& parameters, map<Token, Node>& bindings) { Node mix_2(const vector<Token>& parameters, map<Token, Node>& bindings) {
return mix_impl(bindings[parameters[0]], return mix_impl(bindings[parameters[0]], bindings[parameters[1]]);
bindings[parameters[1]],
50);
} }
Function_Descriptor mix_3_descriptor = Function_Descriptor mix_3_descriptor =
...@@ -88,5 +88,57 @@ namespace Sass { ...@@ -88,5 +88,57 @@ namespace Sass {
bindings[parameters[1]], bindings[parameters[1]],
bindings[parameters[2]].content.numeric_value); bindings[parameters[2]].content.numeric_value);
} }
// HSL Functions ///////////////////////////////////////////////////////
Function_Descriptor invert_descriptor =
{ "invert", "$color", 0 };
Node invert(const vector<Token>& parameters, map<Token, Node>& bindings) {
Node orig(bindings[parameters[0]]);
return Node(orig.line_number,
255 - orig[0].content.numeric_value,
255 - orig[1].content.numeric_value,
255 - orig[2].content.numeric_value,
orig[3].content.numeric_value);
}
} }
} }
\ No newline at end of file
...@@ -66,6 +66,9 @@ namespace Sass { ...@@ -66,6 +66,9 @@ namespace Sass {
extern Function_Descriptor mix_3_descriptor; extern Function_Descriptor mix_3_descriptor;
Node mix_3(const vector<Token>& parameters, map<Token, Node>& bindings); Node mix_3(const vector<Token>& parameters, map<Token, Node>& bindings);
extern Function_Descriptor invert_descriptor;
Node invert(const vector<Token>& parameters, map<Token, Node>& bindings);
} }
} }
...@@ -20,4 +20,6 @@ div { ...@@ -20,4 +20,6 @@ div {
roo: mix(#f00, #00f); roo: mix(#f00, #00f);
doo: mix(#f00, #00f, 25%); doo: mix(#f00, #00f, 25%);
goo: mix(rgba(255, 0, 0, 0.5), #00f); goo: mix(rgba(255, 0, 0, 0.5), #00f);
boo: invert(#123456);
} }
...@@ -12,4 +12,5 @@ div { ...@@ -12,4 +12,5 @@ div {
poo: 6; poo: 6;
roo: #7f007f; roo: #7f007f;
doo: #3f00bf; doo: #3f00bf;
goo: rgba(63, 0, 191, 0.75); } goo: rgba(63, 0, 191, 0.75);
boo: #edcba9; }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment