Skip to content

Instantly share code, notes, and snippets.

@boldfacedesign
Last active May 27, 2016 07:50
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 boldfacedesign/57acfc2a63fe6cc1b0ce3fa007695ae8 to your computer and use it in GitHub Desktop.
Save boldfacedesign/57acfc2a63fe6cc1b0ce3fa007695ae8 to your computer and use it in GitHub Desktop.
Take a colour hex and return a base64 image string in gif format
hex64: function(hex) {
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) {
return encode_triplet(0, r, g) + encode_triplet(b, 255, 255);
}
function encode_triplet(e1, e2, e3) {
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
enc1 = e1 >> 2;
enc2 = ((e1 & 3) << 4) | (e2 >> 4);
enc3 = ((e2 & 15) << 2) | (e3 >> 6);
enc4 = e3 & 63;
return keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
}
function generatePixel(color) {
return "data:image/gif;base64,R0lGODlhAQABAPAA" + color + "/yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==";
}
var color = encodeHex(hex);
var data = generatePixel(color);
return data;
}
hex64("#00a8f3");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment