Skip to content

Instantly share code, notes, and snippets.

@jkriss
Created January 7, 2017 05:13
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 jkriss/044eac82ec9f4f264e0fcc00d09ffe04 to your computer and use it in GitHub Desktop.
Save jkriss/044eac82ec9f4f264e0fcc00d09ffe04 to your computer and use it in GitHub Desktop.
Quick way to generate a solid color data uri (say, for a favicon)
// modified version of https://github.com/CMTegner/favicolor
var size = 1;
var canvas = document.createElement('canvas');
if (typeof canvas.getContext === 'function') {
var ctx = canvas.getContext('2d');
canvas.width = size;
canvas.height = size;
}
var fave = function(red, green, blue) {
if (!ctx) { return false; }
if (typeof red === 'string') {
ctx.fillStyle = red;
} else {
if (Array.isArray(red)) {
blue = red[2];
green = red[1];
red = red[0];
}
ctx.fillStyle = 'rgb(' + red + ',' + green + ',' + blue + ')';
}
ctx.fillRect(0, 0, size, size);
return canvas.toDataURL();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment