-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·98 lines (79 loc) · 2.18 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
95
96
97
98
#!/usr/bin/env node
const chroma = require('chroma-js');
const importJsx = require('import-jsx')
const meow = require('meow');
const updateNotifier = require('update-notifier');
const { convertColour, getContrast, getLevel, isColour } = require('./settings/settings.utils');
const bgValue = process.argv[2];
const fgValue = process.argv[3];
const contrastCli = importJsx('./components/cli');
const cli = meow(
`
Usage
$ contrast black white
Options
--ratio, -r Show only contrast ratio
--grades, -g Show only WCAG grades
Examples
$ contrast black white
$ contrast '#000000' '#ffffff'
$ contrast 0,0,0 255,255,255
`,
{
flags: {
ratio: {
type: 'boolean',
alias: 'f'
},
grades: {
type: 'boolean',
alias: 'g'
},
version: {
type: 'boolean',
alias: 'v'
}
}
}
);
const [input] = cli.input;
updateNotifier({
pkg: cli.pkg,
shouldNotifyInNpmScript: true,
isGlobal: cli.pkg.preferGlobal
}).notify()
function printResults(background, foreground, contrast, level) {
const flags = cli.flags;
const data = { background, foreground, contrast, level, flags };
return contrastCli(data);
}
function checkContrast(bg, fg) {
const background = chroma(bg).hex();
const foreground = chroma(fg).hex();
const contrast = getContrast(background, foreground).toFixed(2);
const level = getLevel(contrast);
printResults(background, foreground, contrast, level);
}
if (!input && process.stdin.isTTY) {
cli.showHelp();
}
if (cli.flags.v) {
cli.showVersion();
}
if (isColour(bgValue) && isColour(fgValue)) {
const background = convertColour(bgValue);
const foreground = convertColour(fgValue);
return checkContrast(background, foreground);
}
if (!isColour(bgValue) && isColour(fgValue)) {
console.error('😱 Awww nah. The first value entered is not a valid colour.');
process.exit(1);
}
if (isColour(bgValue) && !isColour(fgValue)) {
console.error('😱 Aww nah. The second value entered is not a valid colour.');
process.exit(1);
}
if (!isColour(bgValue) && !isColour(fgValue)) {
console.error('😱 Aww nah. The values entered are not valid colours you.');
process.exit(1);
}