Last active
February 19, 2020 00:20
-
-
Save m4rk3r/e0ff38bfb0283d3d675989581f3d6b7b to your computer and use it in GitHub Desktop.
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
/** | |
* Paste the following block into your dev console to average the colors of your favorite fish | |
* and assign it as your block color (temporarily) : ) 💜🐟 | |
* Bookmarklet: http://duskjacket.com/static/fav-fish/ | |
**/ | |
(function () { | |
const dec2hex = function (v) { return v.toString(16);}; | |
const hex2dec = function (v) { return parseInt(v, 16);}; | |
const friends = document.querySelectorAll('#friends a'); | |
let hex = 0; | |
var rgb = [0, 0, 0]; | |
for (i =0; i < friends.length; i++) { | |
const friend = friends[i]; | |
const c = friend.style.backgroundColor; | |
/* annoying but sometimes devtools converts hex to rgb so need to account for this */ | |
if (c.startsWith('rgb')) { | |
const pc = c.match(/rgb\((\d+), (\d+), (\d+)\)/); | |
rgb[0] += parseInt(pc[1], 10); rgb[1] += parseInt(pc[2], 10); rgb[2] += parseInt(pc[3], 10); | |
} else { | |
hex += hex2dec(friend.style.backgroundColor); | |
} | |
} | |
let final; | |
if (hex !== 0) { | |
const avgHex = dec2hex(Math.round(avg/friends.length)); | |
final = `#${avgHex}`; | |
console.log(final); | |
} else { | |
final = `rgb(${Math.round(rgb[0]/friends.length)}, ${Math.round(rgb[1]/friends.length)}, ${Math.round(rgb[2]/friends.length)})`; | |
console.log(final); | |
} | |
document.querySelector('.box.large').style.backgroundColor = final; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment