Skip to content

Instantly share code, notes, and snippets.

@prail
Last active September 24, 2017 03:18
Show Gist options
  • Save prail/0f4aaf5bc632add74652372ec4f04f2f to your computer and use it in GitHub Desktop.
Save prail/0f4aaf5bc632add74652372ec4f04f2f to your computer and use it in GitHub Desktop.
Ooh... Shiny!

This is a project that mainly changes the color of the favicon dynamically using JavaScript. :) Feel free to use this for whatever you'd like. :) Preferably with credit.

<!DOCTYPE html>
<html>
<head>
<title>Ooh... Shiny!</title>
<link id="dynamicicon" rel="icon" type="image/png" href="#">
<script>
/*
Colortab by Andrew, 2017.
The functions byte2Hex and RGB2Color were taken from https://krazydad.com/tutorials/makecolors.php
*/
function byte2Hex(n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}
function RGB2Color(r,g,b) {
return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}
window.addEventListener("load",function() {
var canvas=document.createElement("canvas");
canvas.width=32;
canvas.height=32;
var ctx=canvas.getContext("2d");
var icon=document.getElementById("dynamicicon");
var frequency = .3,i=0,color="";
window.setInterval(function() {
i=(i+1)%128;
ctx.clearRect(0,0,canvas.width,canvas.height);
r = Math.sin(frequency*i + 0) * 127 + 128;
g = Math.sin(frequency*i + 2) * 127 + 128;
b = Math.sin(frequency*i + 4) * 127 + 128;
color=RGB2Color(r,g,b);
ctx.fillStyle = color;
document.bgColor = color;
//document.title=".".repeat(10)+ctx.fillStyle.toUpperCase(); //This sets the tab's title to the hex color code of the favicon.
ctx.fillRect(0,0,canvas.width,canvas.height);
icon.href=canvas.toDataURL("image/png");
},1000);
});
</script>
</head>
<body>
</body>
</html>
The Lil License v1
Copyright (c) 2017 Andrew T.
Permission is hereby granted by the authors of this software, to any person,
to use the software for any purpose, free of charge, including the rights
to run, read, copy, change, distribute and sell it, and including usage rights
to any patents the authors may hold on it, subject to the following conditions:
This license, or a link to its text, must be included with all copies of
the software and any derivative works.
Any modification to the software submitted to the authors may be incorporated
into the software under the terms of this license.
The software is provided "as is", without warranty of any kind, including
but not limited to the warranties of title, fitness, merchantability and
non-infringement. The authors have no obligation to provide support or updates
for the software, and may not be held liable for any damages, claims or other
liability arising from its use.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment