JSFiddle - React, Tailwind, and code Playground
by mjansen
HTML
<pre>
<script>
run();
</script>
</pre>
JavaScript
function encodeHex(s) {
s = s.substring(1, 7);
if (s.length < 6) {
s = s[0]+s[0]+s[1]+s[1]+s[2]+s[2];
}
return encodeRGB(
parseInt(s[0]+s[1], 16),
parseInt(s[2]+s[3], 16),
parseInt(s[4]+s[5], 16));
}
function encodeRGB(r, g, b) {
var k = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
return k.charAt(((0 & 3) << 4) | (r >> 4)) + k.charAt(((r & 15) << 2) | (g >> 6)) + k.charAt(g & 63) + k.charAt(b >> 2) + k.charAt(((b & 3) << 4) | (255 >> 4))
}
function run() {
document.writeln(encodeRGB(255, 255, 255));
document.writeln(encodeRGB(0, 0, 0));
document.writeln(encodeHex("#fff"));
document.writeln(encodeHex("#ffffff"));
document.writeln(encodeHex("#000"));
}