Commit 549f708f by Aaron Leung

Implemented the "mix" builtin function.

parent e4af18b0
...@@ -36,6 +36,8 @@ namespace Sass { ...@@ -36,6 +36,8 @@ namespace Sass {
register_function(red_descriptor, red); register_function(red_descriptor, red);
register_function(green_descriptor, green); register_function(green_descriptor, green);
register_function(blue_descriptor, blue); register_function(blue_descriptor, blue);
register_function(mix_2_descriptor, mix_2);
register_function(mix_3_descriptor, mix_3);
} }
} }
\ No newline at end of file
...@@ -114,7 +114,12 @@ namespace Sass { ...@@ -114,7 +114,12 @@ namespace Sass {
} }
} break; } break;
case Node::textual_percentage: case Node::textual_percentage: {
Node pct(expr.line_number, std::atof(expr.content.token.begin));
pct.type = Node::numeric_percentage;
return pct;
} break;
case Node::textual_dimension: { case Node::textual_dimension: {
return Node(expr.line_number, return Node(expr.line_number,
std::atof(expr.content.token.begin), std::atof(expr.content.token.begin),
......
...@@ -10,10 +10,11 @@ namespace Sass { ...@@ -10,10 +10,11 @@ namespace Sass {
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) {
Node color(Node::numeric_color, 0, 3); Node color(Node::numeric_color, 0, 4);
color << bindings[parameters[0]] color << bindings[parameters[0]]
<< bindings[parameters[1]] << bindings[parameters[1]]
<< bindings[parameters[2]]; << bindings[parameters[2]]
<< Node(0, 1.0);
return color; return color;
} }
...@@ -31,7 +32,9 @@ namespace Sass { ...@@ -31,7 +32,9 @@ namespace Sass {
Function_Descriptor rgba_2_descriptor = Function_Descriptor rgba_2_descriptor =
{ "rgba", "$color", "$alpha", 0 }; { "rgba", "$color", "$alpha", 0 };
Node rgba_2(const vector<Token>& parameters, map<Token, Node>& bindings) { Node rgba_2(const vector<Token>& parameters, map<Token, Node>& bindings) {
return bindings[parameters[0]].clone() << bindings[parameters[1]]; Node color(bindings[parameters[0]].clone());
color[3] = bindings[parameters[1]];
return color;
} }
Function_Descriptor red_descriptor = Function_Descriptor red_descriptor =
...@@ -52,5 +55,38 @@ namespace Sass { ...@@ -52,5 +55,38 @@ namespace Sass {
return bindings[parameters[0]][2]; return bindings[parameters[0]][2];
} }
Node mix_impl(Node color1, Node color2, double weight) {
double p = weight/100;
double w = 2*p - 1;
double a = color1[3].content.numeric_value - color2[3].content.numeric_value;
double w1 = (((w * a == -1) ? w : (w + a)/(1 + w*a)) + 1)/2.0;
double w2 = 1 - w1;
Node mixed(Node::numeric_color, color1.line_number, 4);
for (int i = 0; i < 3; ++i) {
mixed << Node(mixed.line_number, w1*color1[i].content.numeric_value +
w2*color2[i].content.numeric_value);
}
double alpha = color1[3].content.numeric_value*p + color2[3].content.numeric_value*(1-p);
mixed << Node(mixed.line_number, alpha);
return mixed;
}
Function_Descriptor mix_2_descriptor =
{ "mix", "$color1", "$color2", 0 };
Node mix_2(const vector<Token>& parameters, map<Token, Node>& bindings) {
return mix_impl(bindings[parameters[0]],
bindings[parameters[1]],
50);
}
Function_Descriptor mix_3_descriptor =
{ "mix", "$color1", "$color2", "$weight", 0 };
Node mix_3(const vector<Token>& parameters, map<Token, Node>& bindings) {
return mix_impl(bindings[parameters[0]],
bindings[parameters[1]],
bindings[parameters[2]].content.numeric_value);
}
} }
} }
\ No newline at end of file
...@@ -60,6 +60,12 @@ namespace Sass { ...@@ -60,6 +60,12 @@ namespace Sass {
extern Function_Descriptor blue_descriptor; extern Function_Descriptor blue_descriptor;
Node blue(const vector<Token>& parameters, map<Token, Node>& bindings); Node blue(const vector<Token>& parameters, map<Token, Node>& bindings);
extern Function_Descriptor mix_2_descriptor;
Node mix_2(const vector<Token>& parameters, map<Token, Node>& bindings);
extern Function_Descriptor mix_3_descriptor;
Node mix_3(const vector<Token>& parameters, map<Token, Node>& bindings);
} }
} }
...@@ -141,6 +141,13 @@ namespace Sass { ...@@ -141,6 +141,13 @@ namespace Sass {
return "/"; return "/";
} break; } break;
case numeric_percentage: {
stringstream ss;
ss << content.dimension.numeric_value;
ss << '%';
return ss.str();
}
case numeric_dimension: { case numeric_dimension: {
stringstream ss; stringstream ss;
ss << content.dimension.numeric_value; ss << content.dimension.numeric_value;
...@@ -193,11 +200,11 @@ namespace Sass { ...@@ -193,11 +200,11 @@ namespace Sass {
} }
else { else {
stringstream ss; stringstream ss;
ss << "rgba(" << at(0).content.numeric_value; ss << "rgba(" << static_cast<unsigned long>(at(0).content.numeric_value);
for (int i = 1; i < 4; ++i) { for (int i = 1; i < 3; ++i) {
ss << ", " << at(i).content.numeric_value; ss << ", " << static_cast<unsigned long>(at(i).content.numeric_value);
} }
ss << ')'; ss << ", " << at(3).content.numeric_value << ')';
return ss.str(); return ss.str();
} }
} break; } break;
......
...@@ -16,4 +16,8 @@ div { ...@@ -16,4 +16,8 @@ div {
hoo: red($x); hoo: red($x);
moo: green($x); moo: green($x);
poo: blue($x); poo: blue($x);
roo: mix(#f00, #00f);
doo: mix(#f00, #00f, 25%);
goo: mix(rgba(255, 0, 0, 0.5), #00f);
} }
...@@ -9,4 +9,7 @@ div { ...@@ -9,4 +9,7 @@ div {
groo: aqua; groo: aqua;
hoo: 123; hoo: 123;
moo: 45; moo: 45;
poo: 6; } poo: 6;
roo: #7f007f;
doo: #3f00bf;
goo: rgba(63, 0, 191, 0.75); }
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