Last active
August 29, 2015 14:23
-
-
Save iamstarkov/b4a7e1b2bcad4de4d7c4 to your computer and use it in GitHub Desktop.
C-to-js ported of sass’s mix https://github.com/sass/libsass/blob/0e6b4a2850092356aa3ece07c6b249f0221caced/functions.cpp#L209
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Signature mix_sig = "mix($color-1, $color-2, $weight: 50%)"; | |
BUILT_IN(mix) | |
{ | |
Color* color1 = ARG("$color-1", Color); | |
Color* color2 = ARG("$color-2", Color); | |
Number* weight = ARGR("$weight", Number, 0, 100); | |
double p = weight->value()/100; | |
double w = 2*p - 1; | |
double a = color1->a() - color2->a(); | |
double w1 = (((w * a == -1) ? w : (w + a)/(1 + w*a)) + 1)/2.0; | |
double w2 = 1 - w1; | |
return new (ctx.mem) Color(pstate, | |
std::round(w1*color1->r() + w2*color2->r()), | |
std::round(w1*color1->g() + w2*color2->g()), | |
std::round(w1*color1->b() + w2*color2->b()), | |
color1->a()*p + color2->a()*(1-p)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mix(color1, color2, weight) { | |
var weight = weight || 50; | |
var p = weight / 100; | |
var w = 2 * p - 1; | |
var a = color1.alpha() - color2.alpha(); | |
var w1 = (((w * a == -1) ? w : (w + a)/(1 + w*a)) + 1) / 2.0; | |
var w2 = 1 - w1; | |
return this | |
.rgb( | |
w1 * color1.red() + w2 * color2.red(), | |
w1 * color1.green() + w2 * color2.green(), | |
w1 * color1.blue() + w2 * color2.blue() | |
) | |
.alpha(color1.alpha() * p + color2.alpha() * (1 - p)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment