-
Notifications
You must be signed in to change notification settings - Fork 181
/
csv2colormap.js
executable file
·48 lines (42 loc) · 1.01 KB
/
csv2colormap.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
const fs = require('fs')
function usage () {
console.log(`
Usage: csv2colormap.js <id> <name> <csv_file> <json_file>
Given a CSV file with red, green, and blue values in the range
of 0-255, create a colormap JSON file that goes in the
common/config/palettes-custom.json directory.
`)
}
if (process.argv.length !== 4 + 2) {
usage()
// process.exit(1)
throw new Error('Incorrect argv length')
}
const [id, name, csvFile, jsonFile] = process.argv.slice(2)
const file = fs.readFileSync(csvFile, 'utf8')
const lines = file.split('\n')
const colors = []
for (const line of lines) {
if (line.trim().length !== 0) {
const [r, g, b] = line.split(',')
const color = `${hex(r)}${hex(g)}${hex(b)}ff`
colors.push(color)
}
}
const colormap = {
[id]: {
id,
name,
colors
}
}
fs.writeFileSync(jsonFile, JSON.stringify(colormap))
// decimal to hex for bytes, 10 => 0a
function hex (dec) {
dec = Number.parseInt(dec, 10)
let h = dec.toString(16)
if (h.length === 1) {
h = `0${h}`
}
return h
}