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