-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbToHex.js
executable file
·42 lines (31 loc) · 1.05 KB
/
rgbToHex.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
#!/usr/bin/env node
const alert = require('cli-alerts');
const log = require('./utils/log');
const cli = require('./utils/rgbtohex/cli');
const updateNotifier = require('update-notifier');
const pkg = require('./package.json');
const validate = require('./utils/rgbtohex/validate');
const { flags, input, showHelp } = cli;
const { debug } = flags;
(async () => {
updateNotifier({ pkg }).notify();
if (input.includes('help')) showHelp(0);
let { red: r, green: g, blue: b } = flags;
debug && log(flags);
({ r, g, b } = validate(r, g, b));
const stringified = (num) => {
return Math.max(0, Math.min(255, num)).toString(16);
};
const red =
stringified(r).length === 1 ? `0${stringified(r)}` : stringified(r);
const green =
stringified(g).length === 1 ? `0${stringified(g)}` : stringified(g);
const blue =
stringified(b).length === 1 ? `0${stringified(b)}` : stringified(b);
const res = `#${red}${green}${blue}`.toLowerCase();
alert({
type: 'success',
name: 'Success!',
msg: `Your hex value is ${res}`,
});
})();