Last active
February 11, 2016 17:57
-
-
Save ikarius6/6f5a985c623415117aca to your computer and use it in GitHub Desktop.
Color percentage-based generator
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
<script src="https://code.jquery.com/jquery-1.12.0.min.js"></script> | |
<script> | |
/* | |
Color percentage-based generator - by Jack | |
https://jsfiddle.net/xjmjjvdf/1/ | |
*/ | |
function percentage_to_color( perc ){ //0 = red, 100 = green | |
var mod = perc%101; // 0 > perc <= 100 | |
var v = Math.floor( mod * 5.1 ); | |
var r = ("00"+(v>255? 255-(v%256): 255).toString(16)).slice(-2); | |
var g = ("00"+(v<=255? v: 255).toString(16)).slice(-2); | |
var b = "00"; | |
return "#"+r+g+b; | |
} | |
$(function(){ | |
var $bar = $("<div/>").css({width:"400px", height:"5px"}); | |
for(var i = 0; i <= 100; i++){ | |
$("#content").append( $bar.clone().css("background-color", percentage_to_color( i ) ).addClass("bar_"+i) ); | |
} | |
}); | |
</script> | |
<div id="content"></div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment