Skip to content

Commit

Permalink
Added lighten-by() and darken-by() BIFs
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 9, 2011
1 parent a62b792 commit 029bd76
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/bifs.md
Expand Up @@ -208,6 +208,38 @@ Darken the given `color` by `amount`.
darken(white, 50%)
// => #808080

### lighten-by(color | hsl, amount)

Lighten by `amount` of the current lightness value.

lighten-by(black, 50%)
// => #808080

lighten-by(lighten-by(black, 50%), 50%)
// => #bfbfbf

lighten-by(lighten-by(lighten-by(black, 50%), 50%), 50%)
// => #fff

essentially the same as:

black + hsl(0,0,50%)
black + hsl(0,0,75%)
black + hsl(0,0,100%)

### darken-by(color | hsl, amount)

Darken by `amount` of the current lightness value.

darken-by(white, 50%)
// => #808080

darken-by(darken-by(white, 50%), 50%)
// => #404040

darken-by(darken-by(darken-by(white, 50%), 50%), 50%)
// => #202020

### desaturate(color | hsl, amount)

Desaturate the given `color` by `amount`.
Expand Down
40 changes: 40 additions & 0 deletions lib/functions/index.js
Expand Up @@ -491,6 +491,26 @@ exports.desaturate = function(color, amount){
, hsl.a);
};

/**
* Lighten `color` by `amount` of the current value.
*
* @param {Color|HSLA} color
* @param {Unit} amount
* @return {HSLA}
* @api public
*/

exports['lighten-by'] = function(color, amount){
utils.assertColor(color, 'color');
utils.assertType(amount, nodes.Unit, 'amount');
var hsl = color.hsl;
return new nodes.HSLA(
hsl.h
, hsl.s
, hsl.l + (hsl.l || 100) * amount.val / 100
, hsl.a);
};

/**
* Lighten `color` by `amount`.
*
Expand Down Expand Up @@ -531,6 +551,26 @@ exports.darken = function(color, amount){
, hsl.a);
};

/**
* Darken `color` by `amount` of the current value.
*
* @param {Color|HSLA} color
* @param {Unit} amount
* @return {HSLA}
* @api public
*/

exports['darken-by'] = function(color, amount){
utils.assertColor(color, 'color');
utils.assertType(amount, nodes.Unit, 'amount');
var hsl = color.hsl;
return new nodes.HSLA(
hsl.h
, hsl.s
, hsl.l * amount.val / 100
, hsl.a);
};

/**
* Assign `type` to the given `unit` or return `unit`'s type.
*
Expand Down
5 changes: 5 additions & 0 deletions test/cases/bifs.darken-by.css
@@ -0,0 +1,5 @@
body {
background: #808080;
background: #404040;
background: #202020;
}
4 changes: 4 additions & 0 deletions test/cases/bifs.darken-by.styl
@@ -0,0 +1,4 @@
body
background: darken-by(white, 50%)
background: darken-by(darken-by(white, 50%), 50%)
background: darken-by(darken-by(darken-by(white, 50%), 50%), 50%)
5 changes: 5 additions & 0 deletions test/cases/bifs.lighten-by.css
@@ -0,0 +1,5 @@
body {
background: #808080;
background: #bfbfbf;
background: #fff;
}
4 changes: 4 additions & 0 deletions test/cases/bifs.lighten-by.styl
@@ -0,0 +1,4 @@
body
background: lighten-by(black, 50%)
background: lighten-by(lighten-by(black, 50%), 50%)
background: lighten-by(lighten-by(lighten-by(black, 50%), 50%), 50%)

0 comments on commit 029bd76

Please sign in to comment.