Last active
September 25, 2020 03:53
-
-
Save phu54321/45a597cfacd6dbbf41710c4d992ce359 to your computer and use it in GitHub Desktop.
Simple way to apply each folder opening a unique color
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setInterval(() => { | |
// From https://stackoverflow.com/questions/8022885/rgb-to-hsv-color-in-javascript | |
function hslToRgb(h, s, l) { | |
var r, g, b; | |
if (s == 0) { | |
r = g = b = l; // achromatic | |
} else { | |
function hue2rgb(p, q, t) { | |
if (t < 0) t += 1; | |
if (t > 1) t -= 1; | |
if (t < 1 / 6) return p + (q - p) * 6 * t; | |
if (t < 1 / 2) return q; | |
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6; | |
return p; | |
} | |
var q = l < 0.5 ? l * (1 + s) : l + s - l * s; | |
var p = 2 * l - q; | |
r = hue2rgb(p, q, h + 1 / 3); | |
g = hue2rgb(p, q, h); | |
b = hue2rgb(p, q, h - 1 / 3); | |
} | |
return [r * 255 | 0, g * 255 | 0, b * 255 | 0]; | |
} | |
// From https://stackoverflow.com/questions/6122571/simple-non-secure-hash-function-for-javascript | |
function stringHash(s) { | |
var hash = 0; | |
if (s.length == 0) { | |
return hash; | |
} | |
for (var i = 0; i < s.length; i++) { | |
var char = s.charCodeAt(i); | |
hash = ((hash << 5) - hash) + char; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return hash; | |
} | |
// Main start | |
function normalize(start, end, t) { | |
return start + t * (end - start) | |
} | |
let cssEl = document.getElementById('window-color-override') | |
if (!cssEl) { | |
cssEl = document.createElement('style') | |
cssEl.id = 'window-color-override' | |
document.head.appendChild(cssEl) | |
} | |
const title = document.getElementsByClassName('window-title')[0].innerText | |
const matches1 = title.match(/^.* - (.* - Visual Studio Code)( \[Unsupported\])?$/) | |
const matches2 = title.match(/^(● )?(.* - Visual Studio Code)( \[Unsupported\])?$/) | |
const hashPart = | |
matches1 ? matches1[1] : | |
matches2 ? matches2[2] : | |
null | |
if (!hashPart) { | |
cssEl.innerHTML = '' | |
return | |
} | |
const salt1 = 'title-bar' | |
const hash1 = stringHash(salt1 + hashPart) | |
const h1 = normalize(0, 1, (hash1 & 0xFFFF0000) / 0x100000000) | |
const s1 = normalize(0.2, 0.6, (hash1 & 0xFF00) / 0x10000) | |
const l1 = normalize(0.25, 0.35, (hash1 & 0xFF) / 0x100) | |
const [titleR, titleG, titleB] = hslToRgb(h1, s1, l1) | |
const salt2 = 'activity-bar' | |
const hash2 = stringHash(salt2 + hashPart) | |
const h2 = h1 + normalize(-0.1, 0.1, (hash2 & 0xFFFF0000) / 0x100000000) | |
const s2 = s1 + normalize(-0.1, 0.1, (hash2 & 0xFF00) / 0x10000) | |
const l2 = l1 + normalize(-0.05, 0, (hash2 & 0xFF) / 0x100) | |
const [activityR, activityG, activityB] = hslToRgb(h2, s2, l2 - 0.1) | |
const css = ` | |
#workbench\\.parts\\.titlebar { | |
background-color: rgb(${titleR}, ${titleG}, ${titleB}) !important; | |
} | |
#workbench\\.parts\\.activitybar { | |
background-color: rgb(${activityR}, ${activityG}, ${activityB}) !important; | |
} | |
` | |
cssEl.innerHTML = css | |
}, 1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment