Skip to content

Commit b7ee608

Browse files
committed
misc./colors
- type definition - take hex function out - optimizations + other edits - add TODOs - prettier
1 parent 0ace234 commit b7ee608

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

lib_src/misc/colors.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,52 @@
1-
'use babel'
1+
// TODO make sure rgb2hex returns string
22

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 } = {}
57

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")
1010

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"
1414
child.classList.add(...selectors[style])
1515
div.appendChild(child)
1616
styled[style] = child
1717
}
1818

1919
document.body.appendChild(div)
2020
// wait till rendered?
21-
for (let style in selectors) {
21+
for (const style in selectors) {
22+
// TODO do we need try catch
2223
try {
23-
color[style] = rgb2hex(window.getComputedStyle(styled[style])['color'])
24+
color[style] = rgb2hex(window.getComputedStyle(styled[style]).color)
2425
} catch (e) {
2526
console.error(e)
2627
}
2728
}
28-
color['background'] = rgb2hex(window.getComputedStyle(div)['backgroundColor'])
29+
color.background = rgb2hex(window.getComputedStyle(div).backgroundColor)
2930
document.body.removeChild(div)
3031

3132
return color
3233
}
3334

34-
function rgb2hex(rgb) {
35-
if (rgb.search("rgb") == -1) {
35+
function rgb2hex(rgb: string) {
36+
if (rgb.search("rgb") === -1) {
3637
return rgb
3738
} 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
4146
}
42-
return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
4347
}
4448
}
49+
50+
function hex(x: string) {
51+
return ("0" + parseInt(x, 10).toString(16)).slice(-2)
52+
}

lib_src/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// "skipLibCheck": false,
3434
"outDir": "../lib/misc"
3535
},
36-
"files" : ["misc/blocks.ts"],
36+
"files" : ["misc/colors.ts"],
3737
"compileOnSave": true
3838
}

0 commit comments

Comments
 (0)