1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = generate;
|
7 |
|
8 | var _tinycolor = require("@ctrl/tinycolor");
|
9 |
|
10 | var hueStep = 2;
|
11 |
|
12 | var saturationStep = 0.16;
|
13 |
|
14 | var saturationStep2 = 0.05;
|
15 |
|
16 | var brightnessStep1 = 0.05;
|
17 |
|
18 | var brightnessStep2 = 0.15;
|
19 |
|
20 | var lightColorCount = 5;
|
21 |
|
22 | var darkColorCount = 4;
|
23 |
|
24 |
|
25 | var darkColorMap = [{
|
26 | index: 7,
|
27 | opacity: 0.15
|
28 | }, {
|
29 | index: 6,
|
30 | opacity: 0.25
|
31 | }, {
|
32 | index: 5,
|
33 | opacity: 0.3
|
34 | }, {
|
35 | index: 5,
|
36 | opacity: 0.45
|
37 | }, {
|
38 | index: 5,
|
39 | opacity: 0.65
|
40 | }, {
|
41 | index: 5,
|
42 | opacity: 0.85
|
43 | }, {
|
44 | index: 4,
|
45 | opacity: 0.9
|
46 | }, {
|
47 | index: 3,
|
48 | opacity: 0.95
|
49 | }, {
|
50 | index: 2,
|
51 | opacity: 0.97
|
52 | }, {
|
53 | index: 1,
|
54 | opacity: 0.98
|
55 | }];
|
56 |
|
57 |
|
58 |
|
59 | function toHsv(_ref) {
|
60 | var r = _ref.r,
|
61 | g = _ref.g,
|
62 | b = _ref.b;
|
63 | var hsv = (0, _tinycolor.rgbToHsv)(r, g, b);
|
64 | return {
|
65 | h: hsv.h * 360,
|
66 | s: hsv.s,
|
67 | v: hsv.v
|
68 | };
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 | function toHex(_ref2) {
|
74 | var r = _ref2.r,
|
75 | g = _ref2.g,
|
76 | b = _ref2.b;
|
77 | return "#".concat((0, _tinycolor.rgbToHex)(r, g, b, false));
|
78 | }
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | function mix(rgb1, rgb2, amount) {
|
84 | var p = amount / 100;
|
85 | var rgb = {
|
86 | r: (rgb2.r - rgb1.r) * p + rgb1.r,
|
87 | g: (rgb2.g - rgb1.g) * p + rgb1.g,
|
88 | b: (rgb2.b - rgb1.b) * p + rgb1.b
|
89 | };
|
90 | return rgb;
|
91 | }
|
92 |
|
93 | function getHue(hsv, i, light) {
|
94 | var hue;
|
95 |
|
96 | if (Math.round(hsv.h) >= 60 && Math.round(hsv.h) <= 240) {
|
97 | hue = light ? Math.round(hsv.h) - hueStep * i : Math.round(hsv.h) + hueStep * i;
|
98 | } else {
|
99 | hue = light ? Math.round(hsv.h) + hueStep * i : Math.round(hsv.h) - hueStep * i;
|
100 | }
|
101 |
|
102 | if (hue < 0) {
|
103 | hue += 360;
|
104 | } else if (hue >= 360) {
|
105 | hue -= 360;
|
106 | }
|
107 |
|
108 | return hue;
|
109 | }
|
110 |
|
111 | function getSaturation(hsv, i, light) {
|
112 |
|
113 | if (hsv.h === 0 && hsv.s === 0) {
|
114 | return hsv.s;
|
115 | }
|
116 |
|
117 | var saturation;
|
118 |
|
119 | if (light) {
|
120 | saturation = hsv.s - saturationStep * i;
|
121 | } else if (i === darkColorCount) {
|
122 | saturation = hsv.s + saturationStep;
|
123 | } else {
|
124 | saturation = hsv.s + saturationStep2 * i;
|
125 | }
|
126 |
|
127 |
|
128 | if (saturation > 1) {
|
129 | saturation = 1;
|
130 | }
|
131 |
|
132 |
|
133 | if (light && i === lightColorCount && saturation > 0.1) {
|
134 | saturation = 0.1;
|
135 | }
|
136 |
|
137 | if (saturation < 0.06) {
|
138 | saturation = 0.06;
|
139 | }
|
140 |
|
141 | return Number(saturation.toFixed(2));
|
142 | }
|
143 |
|
144 | function getValue(hsv, i, light) {
|
145 | var value;
|
146 |
|
147 | if (light) {
|
148 | value = hsv.v + brightnessStep1 * i;
|
149 | } else {
|
150 | value = hsv.v - brightnessStep2 * i;
|
151 | }
|
152 |
|
153 | if (value > 1) {
|
154 | value = 1;
|
155 | }
|
156 |
|
157 | return Number(value.toFixed(2));
|
158 | }
|
159 |
|
160 | function generate(color) {
|
161 | var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
162 | var patterns = [];
|
163 | var pColor = (0, _tinycolor.inputToRGB)(color);
|
164 |
|
165 | for (var i = lightColorCount; i > 0; i -= 1) {
|
166 | var hsv = toHsv(pColor);
|
167 | var colorString = toHex((0, _tinycolor.inputToRGB)({
|
168 | h: getHue(hsv, i, true),
|
169 | s: getSaturation(hsv, i, true),
|
170 | v: getValue(hsv, i, true)
|
171 | }));
|
172 | patterns.push(colorString);
|
173 | }
|
174 |
|
175 | patterns.push(toHex(pColor));
|
176 |
|
177 | for (var _i = 1; _i <= darkColorCount; _i += 1) {
|
178 | var _hsv = toHsv(pColor);
|
179 |
|
180 | var _colorString = toHex((0, _tinycolor.inputToRGB)({
|
181 | h: getHue(_hsv, _i),
|
182 | s: getSaturation(_hsv, _i),
|
183 | v: getValue(_hsv, _i)
|
184 | }));
|
185 |
|
186 | patterns.push(_colorString);
|
187 | }
|
188 |
|
189 |
|
190 | if (opts.theme === 'dark') {
|
191 | return darkColorMap.map(function (_ref3) {
|
192 | var index = _ref3.index,
|
193 | opacity = _ref3.opacity;
|
194 | var darkColorString = toHex(mix((0, _tinycolor.inputToRGB)(opts.backgroundColor || '#141414'), (0, _tinycolor.inputToRGB)(patterns[index]), opacity * 100));
|
195 | return darkColorString;
|
196 | });
|
197 | }
|
198 |
|
199 | return patterns;
|
200 | } |
\ | No newline at end of file |