Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9 blending modes added as LESS operations #596

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
less.js
=======
less.js + blending modes
========================

The **dynamic** stylesheet language.
The **dynamic** stylesheet language, powered with blending modes.

<http://lesscss.org>
The full credits for the original project of LESS goes to Alexis Sellier, more information: <http://lesscss.org>.

about
-----

This is the JavaScript, and now official, stable version of LESS.
This is the JavaScript stable version of LESS with some blending modes (such those from Photoshop, GIMP or Fireworks).

For more information, visit <http://lesscss.org>.
These blending methods are implemented as LESS operations.

blending modes
--------------

**list of implemented blending modes**

* multiply
* screen
* overlay
* softlight
* hardlight
* difference
* exclusion
* average
* negation

license
-------

See `LICENSE` file.

> Copyright (c) 2009-2011 Alexis Sellier
> Copyright (c) 2012 Rubens Mariuzzo
60 changes: 60 additions & 0 deletions lib/less/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,66 @@ tree.functions = {
},
_isa: function (n, Type) {
return (n instanceof Type) ? tree.True : tree.False;
},

/* Blending modes */

multiply: function(color1, color2) {
var r = color1.rgb[0] * color2.rgb[0] / 255;
var g = color1.rgb[1] * color2.rgb[1] / 255;
var b = color1.rgb[2] * color2.rgb[2] / 255;
return this.rgb(r, g, b);
},
screen: function(color1, color2) {
var r = 255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255;
var g = 255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255;
var b = 255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255;
return this.rgb(r, g, b);
},
overlay: function(color1, color2) {
var r = color1.rgb[0] < 128 ? 2 * color1.rgb[0] * color2.rgb[0] / 255 : 255 - 2 * (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255;
var g = color1.rgb[1] < 128 ? 2 * color1.rgb[1] * color2.rgb[1] / 255 : 255 - 2 * (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255;
var b = color1.rgb[2] < 128 ? 2 * color1.rgb[2] * color2.rgb[2] / 255 : 255 - 2 * (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255;
return this.rgb(r, g, b);
},
softlight: function(color1, color2) {
var t = color2.rgb[0] * color1.rgb[0] / 255;
var r = t + color1.rgb[0] * (255 - (255 - color1.rgb[0]) * (255 - color2.rgb[0]) / 255 - t) / 255;
t = color2.rgb[1] * color1.rgb[1] / 255;
var g = t + color1.rgb[1] * (255 - (255 - color1.rgb[1]) * (255 - color2.rgb[1]) / 255 - t) / 255;
t = color2.rgb[2] * color1.rgb[2] / 255;
var b = t + color1.rgb[2] * (255 - (255 - color1.rgb[2]) * (255 - color2.rgb[2]) / 255 - t) / 255;
return this.rgb(r, g, b);
},
hardlight: function(color1, color2) {
var r = color2.rgb[0] < 128 ? 2 * color2.rgb[0] * color1.rgb[0] / 255 : 255 - 2 * (255 - color2.rgb[0]) * (255 - color1.rgb[0]) / 255;
var g = color2.rgb[1] < 128 ? 2 * color2.rgb[1] * color1.rgb[1] / 255 : 255 - 2 * (255 - color2.rgb[1]) * (255 - color1.rgb[1]) / 255;
var b = color2.rgb[2] < 128 ? 2 * color2.rgb[2] * color1.rgb[2] / 255 : 255 - 2 * (255 - color2.rgb[2]) * (255 - color1.rgb[2]) / 255;
return this.rgb(r, g, b);
},
difference: function(color1, color2) {
var r = Math.abs(color1.rgb[0] - color2.rgb[0]);
var g = Math.abs(color1.rgb[1] - color2.rgb[1]);
var b = Math.abs(color1.rgb[2] - color2.rgb[2]);
return this.rgb(r, g, b);
},
exclusion: function(color1, color2) {
var r = color1.rgb[0] + color2.rgb[0] * (255 - color1.rgb[0] - color1.rgb[0]) / 255;
var g = color1.rgb[1] + color2.rgb[1] * (255 - color1.rgb[1] - color1.rgb[1]) / 255;
var b = color1.rgb[2] + color2.rgb[2] * (255 - color1.rgb[2] - color1.rgb[2]) / 255;
return this.rgb(r, g, b);
},
average: function(color1, color2) {
var r = (color1.rgb[0] + color2.rgb[0]) / 2;
var g = (color1.rgb[1] + color2.rgb[1]) / 2;
var b = (color1.rgb[2] + color2.rgb[2]) / 2;
return this.rgb(r, g, b);
},
negation: function(color1, color2) {
var r = 255 - Math.abs(255 - color2.rgb[0] - color1.rgb[0]);
var g = 255 - Math.abs(255 - color2.rgb[1] - color1.rgb[1]);
var b = 255 - Math.abs(255 - color2.rgb[2] - color1.rgb[2]);
return this.rgb(r, g, b);
}
};

Expand Down
11 changes: 11 additions & 0 deletions test/css/functions.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@
#alpha {
alpha: rgba(153, 94, 51, 0.6);
}
#blendmodes {
multiply: #ed0000;
screen: #f600f6;
overlay: #ed0000;
softlight: #ff0000;
hardlight: #0000ed;
difference: #f600f6;
exclusion: #f600f6;
average: #7b007b;
negation: #d73131;
}
12 changes: 12 additions & 0 deletions test/less/functions.less
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@
#alpha {
alpha: darken(hsla(25, 50%, 50%, 0.6), 10%);
}

#blendmodes {
multiply: multiply(#f60000, #f60000);
screen: screen(#f60000, #0000f6);
overlay: overlay(#f60000, #0000f6);
softlight: softlight(#f60000, #ffffff);
hardlight: hardlight(#f60000, #0000f6);
difference: difference(#f60000, #0000f6);
exclusion: exclusion(#f60000, #0000f6);
average: average(#f60000, #0000f6);
negation: negation(#f60000, #313131);
}