Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc/colors #726

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions lib/misc/colors.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,68 @@
'use babel'

/**
*
* @param {{ [P: string]: string }} selectors
*/
export function getColors(selectors) {
let grammar = atom.grammars.grammarForScopeName("source.julia")

let styled = {}
let color = {}
let div = document.createElement('div')
// const grammar = atom.grammars.grammarForScopeName("source.julia") // TODO ?
const div = document.createElement('div')
div.classList.add('editor', 'editor-colors', 'julia-syntax-color-selector')

for (let style in selectors) {
let child = document.createElement('span')
/**
* @type { [P: string]: HTMLSpanElement }
*/
const styled = {}
/**
* { [P: string]: string }
*/
const color = {}
for (const style in selectors) {
const child = document.createElement('span')
child.innerText = 'foo'
child.classList.add(...selectors[style])
div.appendChild(child)
styled[style] = child
}

document.body.appendChild(div)
// wait till rendered?
for (let style in selectors) {
for (const style in selectors) {
// TODO do we need try catch
try {
color[style] = rgb2hex(window.getComputedStyle(styled[style])['color'])
color[style] = rgb2hex(window.getComputedStyle(styled[style]).color)
} catch (e) {
console.error(e)
}
}
color['background'] = rgb2hex(window.getComputedStyle(div)['backgroundColor'])
color.background = rgb2hex(window.getComputedStyle(div).backgroundColor)
document.body.removeChild(div)

return color
}

/**
*
* @param {string} rgb
* @return {string}
*/
function rgb2hex(rgb) {
if (rgb.search("rgb") == -1) {
if (rgb.search('rgb') === -1) {
return rgb
} else {
rgb = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
function hex(x) {
return ("0" + parseInt(x).toString(16)).slice(-2);
const rgb_match = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)
if (rgb_match) {
return hex(rgb_match[1]) + hex(rgb_match[2]) + hex(rgb_match[3])
} else {
// TODO should we check for this error?
console.error(rgb.concat(" isn't a rgb string!"))
return '#000000' // black
}
return hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}
}

/**
*
* @param {string} x
*/
function hex(x) {
return ('0' + parseInt(x, 10).toString(16)).slice(-2)
}
73 changes: 73 additions & 0 deletions lib_src/misc/colors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use babel'

/**
*
* @param {{ [P: string]: string }} selectors
*/
export function getColors(selectors: { [P: string]: string }) {
// const grammar = atom.grammars.grammarForScopeName("source.julia") // TODO ?

const div = document.createElement('div')
div.classList.add('editor', 'editor-colors', 'julia-syntax-color-selector')

/**
* @type { [P: string]: HTMLSpanElement }
*/
const styled: { [P: string]: HTMLSpanElement } = {}
/**
* { [P: string]: string }
*/
const color: { [P: string]: string } = {}

for (const style in selectors) {
const child = document.createElement('span')
child.innerText = 'foo'
child.classList.add(...selectors[style])
div.appendChild(child)
styled[style] = child
}

document.body.appendChild(div)
// wait till rendered?
for (const style in selectors) {
// TODO do we need try catch
try {
color[style] = rgb2hex(window.getComputedStyle(styled[style]).color)
} catch (e) {
console.error(e)
}
}
color.background = rgb2hex(window.getComputedStyle(div).backgroundColor)
document.body.removeChild(div)

return color
}

/**
*
* @param {string} rgb
* @return {string}
*/
function rgb2hex(rgb: string): string {
if (rgb.search("rgb") === -1) {
return rgb
} else {
const rgb_match = rgb.match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+))?\)$/)

if (rgb_match) {
return hex(rgb_match[1]) + hex(rgb_match[2]) + hex(rgb_match[3])
} else {
// TODO should we check for this error?
console.error(rgb.concat(" isn't a rgb string!"))
return "#000000" // black
}
}
}

/**
*
* @param {string} x
*/
function hex(x: string) {
return ("0" + parseInt(x, 10).toString(16)).slice(-2)
}