|
1 |
| -'use babel' |
| 1 | +"use babel" |
2 | 2 |
|
3 | 3 | 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') |
9 |
| - div.classList.add('editor', 'editor-colors', 'julia-syntax-color-selector') |
10 |
| - |
11 |
| - for (let style in selectors) { |
12 |
| - let child = document.createElement('span') |
13 |
| - child.innerText = 'foo' |
| 4 | + // const grammar = atom.grammars.grammarForScopeName("source.julia") // TODO ? |
| 5 | + const div = document.createElement("div") |
| 6 | + div.classList.add("editor", "editor-colors", "julia-syntax-color-selector") |
| 7 | + const styled = {} |
| 8 | + const color = {} |
| 9 | + for (const style in selectors) { |
| 10 | + const child = document.createElement("span") |
| 11 | + child.innerText = "foo" |
14 | 12 | child.classList.add(...selectors[style])
|
15 | 13 | div.appendChild(child)
|
16 | 14 | styled[style] = child
|
17 | 15 | }
|
18 |
| - |
19 | 16 | document.body.appendChild(div)
|
20 | 17 | // wait till rendered?
|
21 |
| - for (let style in selectors) { |
| 18 | + for (const style in selectors) { |
| 19 | + // TODO do we need try catch |
22 | 20 | try {
|
23 |
| - color[style] = rgb2hex(window.getComputedStyle(styled[style])['color']) |
| 21 | + color[style] = rgb2hex(window.getComputedStyle(styled[style]).color) |
24 | 22 | } catch (e) {
|
25 | 23 | console.error(e)
|
26 | 24 | }
|
27 | 25 | }
|
28 |
| - color['background'] = rgb2hex(window.getComputedStyle(div)['backgroundColor']) |
| 26 | + color.background = rgb2hex(window.getComputedStyle(div).backgroundColor) |
29 | 27 | document.body.removeChild(div)
|
30 |
| - |
31 | 28 | return color
|
32 | 29 | }
|
33 | 30 |
|
34 | 31 | function rgb2hex(rgb) {
|
35 |
| - if (rgb.search("rgb") == -1) { |
| 32 | + if (rgb.search("rgb") === -1) { |
36 | 33 | return rgb
|
37 | 34 | } 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); |
| 35 | + const rgb_match = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/) |
| 36 | + if (rgb_match) { |
| 37 | + return hex(rgb_match[1]) + hex(rgb_match[2]) + hex(rgb_match[3]) |
| 38 | + } else { |
| 39 | + // TODO should we check for this error? |
| 40 | + console.error(rgb.concat(" isn't a rgb string!")) |
| 41 | + return "#000000" // black |
41 | 42 | }
|
42 |
| - return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); |
43 | 43 | }
|
44 | 44 | }
|
| 45 | + |
| 46 | +function hex(x) { |
| 47 | + return ("0" + parseInt(x, 10).toString(16)).slice(-2) |
| 48 | +} |
0 commit comments