-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (83 loc) · 2.41 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const color = require('color');
const fs = require('fs');
const path = require('path');
const channels = {
saturationl: 100,
lightness: 100,
alpha: 1,
};
const sourceColor = color('#8a49a8');
const targetColor = color('#344767');
const targetHue = targetColor.hue();
const calcDiffEachChannel = () => {
const diff = {};
for (const channel of Object.keys(channels)) {
diff[channel] =
targetColor[channel]() - sourceColor[channel]();
}
return diff;
};
const diff = calcDiffEachChannel();
console.log('difference', diff);
const patterns = [
/(#[\da-f]{6})/gi,
/(#[\da-f]{8})/gi,
/(rgb\([\d]+,[ ]*[\d]+,[ ]*[\d]+\))/gi,
/(rgba\([\d]+,[ ]*[\d]+,[ ]*[\d]+,[ ]*[\d]*[\.]?[\d]*\))/gi,
];
function fromDir(startPath, filter) {
if (!fs.existsSync(startPath)) {
console.log('no dir ', startPath);
return;
}
var files = fs.readdirSync(startPath);
for (var i = 0; i < files.length; i++) {
var filename = path.join(startPath, files[i]);
var stat = fs.lstatSync(filename);
if (stat.isDirectory()) {
fromDir(filename, filter);
} else if (filename.endsWith(filter)) {
var buffer = fs.readFileSync(filename);
var content = buffer.toString();
var replaced = 0;
const convertColor = (colorString) => {
let newColor = color(colorString.toLowerCase()).hue(
targetHue,
);
for (const channel of Object.keys(channels)) {
let newChannelValue =
newColor[channel]() + diff[channel];
if (newChannelValue > channels[channel]) {
newChannelValue = channels[channel];
}
if (newChannelValue < 0) {
newChannelValue = 0;
}
newColor = newColor[channel](newChannelValue);
}
// newColor = color(colorString.toLowerCase());
const replacer =
newColor.alpha() === 1
? newColor.hex()
: newColor.rgb();
replaced++;
return replacer;
};
for (const pattern of patterns) {
content = content.replace(pattern, convertColor);
}
fs.writeFileSync(filename, content);
if (replaced) {
console.log('-- found: ', filename);
console.log(`>>>`, replaced, `replaced.`);
}
}
}
}
const exts = ['.js', '.jsx', '.scss', '.svg'];
const paths = ['../react-portfolio/src/'];
for (const path of paths) {
for (const ext of exts) {
fromDir(path, ext);
}
}