Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
Created December 19, 2014 16:12
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 mariusGundersen/9eef5eaee13e4a4c6939 to your computer and use it in GitHub Desktop.
Save mariusGundersen/9eef5eaee13e4a4c6939 to your computer and use it in GitHub Desktop.
Css Variable hack
function toHex(rgb){
var a = /rgb\((\d+),\s*(\d+),\s*(\d+)\)/.exec(rgb).reverse();
a.pop();
return '#'+a.map(a => parseInt(a, 10).toString(16)).join('');
}
var variables = {
'#747474': '#FF0000',
'#f8b912': '#00FFBB'
};
Array.from(document.styleSheets).forEach(sheet=>
Array.from(sheet.cssRules)
.filter(r => r.style)
.map(r => Array.from(r.style)
.filter(s => r.style[s] && /rgb\(\d+,\s*\d+,\s*\d+\)/.test(r.style[s]))
.map(s => ({key: s, value: toHex(r.style[s]), original:r.style[s]}))
.filter(({value}) => value in variables)
.map(({key, value, original}) => {r.style[key] = original.replace(/rgb\(\d+,\s*\d+,\s*\d+\)/, variables[value]); return key}))
.filter(a => a.length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment