Trying to follow the examples on this StackOverflow post but having trouble getting it to work with my code - hoping someone can show me what I am doing wrong....
Simply put, I just want a color code I can use to set a translucent DIV to a color that matches the image (in application this is random), then to set the font color to something that's easily readable against the color code, but that's a fight for another day...
MyWall = 'https://images.unsplash.com/photo-1537017469405-7faf1912af7c?ixid=MnwzMDUwMHwwfDF8cmFuZG9tfHx8fHx8fHx8MTY1Mzk5ODA1OQ&ixlib=rb-1.2.1'
function get_average_rgb(img) {
var context = document.createElement('canvas').getContext('2d');
if (typeof img == 'string') {
var src = img;
img = new Image;
img.setAttribute('crossOrigin', '');
img.src = src;
}
context.imageSmoothingEnabled = true;
context.drawImage(img, 0, 0, 1, 1);
return context.getImageData(1, 1, 1, 1).data.slice(0,3);
}
foo = get_average_rgb(MyWall)
Setting the BODY's background to the average color for an image URL provided: