Skip to content

Commit c3d069a

Browse files
committed
npm run build
1 parent 585b92b commit c3d069a

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

lib/misc/colors.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
*
3+
* @param {{ [P: string]: string }} selectors
4+
*/
5+
export declare function getColors(selectors: {
6+
[P: string]: string;
7+
}): {
8+
[P: string]: string;
9+
};

lib/misc/colors.js

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,68 @@
11
'use babel'
22

3+
/**
4+
*
5+
* @param {{ [P: string]: string }} selectors
6+
*/
37
export function getColors(selectors) {
4-
let grammar = atom.grammars.grammarForScopeName("source.julia")
5-
6-
let styled = {}
7-
let color = {}
8-
let div = document.createElement('div')
8+
// const grammar = atom.grammars.grammarForScopeName("source.julia") // TODO ?
9+
const div = document.createElement('div')
910
div.classList.add('editor', 'editor-colors', 'julia-syntax-color-selector')
1011

11-
for (let style in selectors) {
12-
let child = document.createElement('span')
12+
/**
13+
* @type { [P: string]: HTMLSpanElement }
14+
*/
15+
const styled = {}
16+
/**
17+
* { [P: string]: string }
18+
*/
19+
const color = {}
20+
for (const style in selectors) {
21+
const child = document.createElement('span')
1322
child.innerText = 'foo'
1423
child.classList.add(...selectors[style])
1524
div.appendChild(child)
1625
styled[style] = child
1726
}
18-
1927
document.body.appendChild(div)
2028
// wait till rendered?
21-
for (let style in selectors) {
29+
for (const style in selectors) {
30+
// TODO do we need try catch
2231
try {
23-
color[style] = rgb2hex(window.getComputedStyle(styled[style])['color'])
32+
color[style] = rgb2hex(window.getComputedStyle(styled[style]).color)
2433
} catch (e) {
2534
console.error(e)
2635
}
2736
}
28-
color['background'] = rgb2hex(window.getComputedStyle(div)['backgroundColor'])
37+
color.background = rgb2hex(window.getComputedStyle(div).backgroundColor)
2938
document.body.removeChild(div)
30-
3139
return color
3240
}
3341

42+
/**
43+
*
44+
* @param {string} rgb
45+
* @return {string}
46+
*/
3447
function rgb2hex(rgb) {
35-
if (rgb.search("rgb") == -1) {
48+
if (rgb.search('rgb') === -1) {
3649
return rgb
3750
} else {
38-
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
39-
function hex(x) {
40-
return ("0" + parseInt(x).toString(16)).slice(-2);
51+
const rgb_match = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
52+
if (rgb_match) {
53+
return hex(rgb_match[1]) + hex(rgb_match[2]) + hex(rgb_match[3])
54+
} else {
55+
// TODO should we check for this error?
56+
console.error(rgb.concat(" isn't a rgb string!"))
57+
return '#000000' // black
4158
}
42-
return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
4359
}
4460
}
61+
62+
/**
63+
*
64+
* @param {string} x
65+
*/
66+
function hex(x) {
67+
return ('0' + parseInt(x, 10).toString(16)).slice(-2)
68+
}

0 commit comments

Comments
 (0)