Skip to content

Commit

Permalink
Add color.tint and color.shade operations
Browse files Browse the repository at this point in the history
These produce tints and shades of the color, i.e. mix with either white
or black at a given ratio.

See https://en.wikipedia.org/wiki/Tints_and_shades

Closes gka#235
  • Loading branch information
tremby committed Oct 19, 2020
1 parent 0a7f607 commit 39b8dff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
26 changes: 26 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,32 @@ chroma('hotpink').mix('blue', 0.25);
chroma('hotpink').mix('blue', 0.75);
```

### color.shade
#### (ratio=0.5)

Produce a shade of the color.
This is syntactic sugar for `color.mix` with a target color of black.

```js
chroma('hotpink').shade();
chroma('hotpink').shade(0.25);
chroma('hotpink').shade(0.75);
chroma('hotpink').shade(1);
```

### color.tint
#### (ratio=0.5)

Produce a tint of the color.
This is syntactic sugar for `color.mix` with a target color of white.

```js
chroma('hotpink').tint();
chroma('hotpink').tint(0.25);
chroma('hotpink').tint(0.75);
chroma('hotpink').tint(1);
```


### color.set
#### (channel, value)
Expand Down
2 changes: 2 additions & 0 deletions index-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require('./src/ops/darken');
require('./src/ops/get');
require('./src/ops/mix');
require('./src/ops/set');
require('./src/ops/shade');
require('./src/ops/tint');

// interpolators
require('./src/interpolator/lrgb');
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ require('./src/ops/mix');
require('./src/ops/premultiply');
require('./src/ops/saturate');
require('./src/ops/set');
require('./src/ops/shade');
require('./src/ops/tint');

// interpolators
require('./src/interpolator/rgb');
Expand Down
6 changes: 6 additions & 0 deletions src/ops/shade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Color = require('../Color');
const mix = require('../generator/mix');

Color.prototype.shade = function(f=0.5, ...rest) {
return mix(this, 'black', f, ...rest);
}
6 changes: 6 additions & 0 deletions src/ops/tint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const Color = require('../Color');
const mix = require('../generator/mix');

Color.prototype.tint = function(f=0.5, ...rest) {
return mix(this, 'white', f, ...rest);
}
8 changes: 7 additions & 1 deletion test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ vows
'saturate'(topic) { return assert.equal(topic.saturate().hex(), '#ff0000'); },
'desaturate'(topic) { return assert.equal(topic.desaturate().hex(), '#ee3a20'); },
'desaturate more'(topic) { return assert.equal(topic.desaturate(2).hex(), '#db5136'); },
'desaturate too much'(topic) { return assert.equal(topic.desaturate(400).hex(), '#7f7f7f'); }
'desaturate too much'(topic) { return assert.equal(topic.desaturate(400).hex(), '#7f7f7f'); },
'shade'(topic) { return assert.equal(topic.shade().hex(), '#b40000'); },
'shade more'(topic) { return assert.equal(topic.shade(0.75).hex(), '#800000'); },
'shade too much'(topic) { return assert.equal(topic.shade(1.5).hex(), '#000000'); },
'tint'(topic) { return assert.equal(topic.tint().hex(), '#ffb4b4'); },
'tint more'(topic) { return assert.equal(topic.tint(0.75).hex(), '#ffdddd'); },
'tint too much'(topic) { return assert.equal(topic.tint(1.5).hex(), '#ffffff'); }
},

'premultiply': {
Expand Down

0 comments on commit 39b8dff

Please sign in to comment.