Skip to content

Instantly share code, notes, and snippets.

@kenchris
Created November 22, 2016 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenchris/7f815230270db73e1bc3d7620721e629 to your computer and use it in GitHub Desktop.
Save kenchris/7f815230270db73e1bc3d7620721e629 to your computer and use it in GitHub Desktop.
function parseColor(colorStr) {
if (!this.div) {
this.div = document.createElement('div');
this.div.style.hidden = true;
document.body.appendChild(this.div)
}
this.div.style.color = colorStr;
if (this.div.style.color === "")
this.div.style.color = `#${colorStr}`;
let style = window.getComputedStyle(this.div);
let colors = style.color.match(/\d+/g).map(a => parseInt(a, 10));
return new Uint8Array(colors);
}
function withdelay(values, delay) {
return {
[Symbol.iterator]: function* () {
for (let i of values) {
yield new Promise(resolve => {
setTimeout(_ => resolve(i), delay);
});
}
}
};
}
async function run() {
for (let color of withdelay(["red", "purple", "blue"], 500)) {
console.log(parseColor(await color));
}
}
//run()
async function run2() {
for (let color of ["green", "yellow", "rebeccapurple"]) {
let c = await new Promise(resolve => {
setTimeout(_ => resolve(color), 1000);
})
console.log(parseColor(c));
}
}
run2()
// noprotect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment