Created
January 7, 2017 05:13
-
-
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)
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
// 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