|
1 | 1 | 'use babel' |
2 | 2 |
|
| 3 | +/** |
| 4 | + * |
| 5 | + * @param {{ [P: string]: string }} selectors |
| 6 | + */ |
3 | 7 | 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') |
9 | 10 | div.classList.add('editor', 'editor-colors', 'julia-syntax-color-selector') |
10 | 11 |
|
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') |
13 | 22 | child.innerText = 'foo' |
14 | 23 | child.classList.add(...selectors[style]) |
15 | 24 | div.appendChild(child) |
16 | 25 | styled[style] = child |
17 | 26 | } |
18 | | - |
19 | 27 | document.body.appendChild(div) |
20 | 28 | // wait till rendered? |
21 | | - for (let style in selectors) { |
| 29 | + for (const style in selectors) { |
| 30 | + // TODO do we need try catch |
22 | 31 | try { |
23 | | - color[style] = rgb2hex(window.getComputedStyle(styled[style])['color']) |
| 32 | + color[style] = rgb2hex(window.getComputedStyle(styled[style]).color) |
24 | 33 | } catch (e) { |
25 | 34 | console.error(e) |
26 | 35 | } |
27 | 36 | } |
28 | | - color['background'] = rgb2hex(window.getComputedStyle(div)['backgroundColor']) |
| 37 | + color.background = rgb2hex(window.getComputedStyle(div).backgroundColor) |
29 | 38 | document.body.removeChild(div) |
30 | | - |
31 | 39 | return color |
32 | 40 | } |
33 | 41 |
|
| 42 | +/** |
| 43 | + * |
| 44 | + * @param {string} rgb |
| 45 | + * @return {string} |
| 46 | + */ |
34 | 47 | function rgb2hex(rgb) { |
35 | | - if (rgb.search("rgb") == -1) { |
| 48 | + if (rgb.search('rgb') === -1) { |
36 | 49 | return rgb |
37 | 50 | } 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 |
41 | 58 | } |
42 | | - return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]); |
43 | 59 | } |
44 | 60 | } |
| 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