-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (61 loc) · 1.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#! /usr/bin/env node
/*!
* generate-palette
* MIT Licensed
*/
'use strict'
/**
* Module dependencies.
* @private
*/
const chroma = require('chroma-js');
/**
* Module exports.
* @public
*/
module.exports = generatePalette;
/**
* Get the color palette.
*
* @param {string} color
* @param {number} [step]
* @public
*/
function generatePalette (color, step = 10) {
if (!chroma.valid(color)) {
throw 'ERROR: Color should be present in hex format (#000000)';
}
if (step <= 0 || step >= 100) {
throw 'ERROR: Step should be between 0 and 100';
}
const colors = [color];
colors.unshift(
chroma.scale(['white', color])
.domain([0, 1])
.mode("lrgb")
.colors(50)[1]
);
colors.push(
chroma.scale(['black', color])
.domain([0, 1])
.mode("lrgb")
.colors(10)[1]
);
const domain = [
0,
...Object.entries({})
.filter(([key, value]) => value)
.map(([key]) => parseInt(key) / 100),
1
];
const scale = chroma.scale(colors)
.domain(domain)
.mode('lrgb');
let result = {
50: color
};
for (let i = step; i < 100; i += step) {
result[i] = scale(i / 100).hex();
}
return result;
}