Skip to content

Instantly share code, notes, and snippets.

@edwinwebb
Created March 24, 2015 17:45
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 edwinwebb/db81ef36536e66a3d69a to your computer and use it in GitHub Desktop.
Save edwinwebb/db81ef36536e66a3d69a to your computer and use it in GitHub Desktop.
Console rainbow
function colorText(str) {
var phase = 0;
var outputStr = new Array();
var args = new Array();
var center = 128;
var width = 127;
var frequency = Math.PI*2/str.length;
var red, green, blue, i;
function componentToHex(c) {
var hex = Math.round(c).toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
for (i = 0; i < str.length; i++) {
red = Math.sin(frequency*i+2+phase) * width + center;
green = Math.sin(frequency*i+0+phase) * width + center;
blue = Math.sin(frequency*i+4+phase) * width + center;
hex = rgbToHex(red, green, blue);
args.push('color:'+ hex +'');
outputStr.push('%c' + str.charAt(i))
}
args.unshift(outputStr.join(''));
console.log.apply(console, args);
}
colorText('Oh console rainbow text, you made my day!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment