Created
March 24, 2015 17:45
-
-
Save edwinwebb/db81ef36536e66a3d69a to your computer and use it in GitHub Desktop.
Console rainbow
This file contains hidden or 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
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