Last active
September 29, 2015 10:57
-
-
Save ethertank/1590113 to your computer and use it in GitHub Desktop.
getRandomColor
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
if (typeof getRandomColor === "undefined") { | |
var getRandomColor = function(type) { | |
var _c; | |
if (type) type = type.toUpperCase(); | |
switch (type) { | |
case "RGB": | |
_c = "rgb(" + (Math.random() * 256 | 0) + ", " + (Math.random() * 256 | 0) + ", " + (Math.random() * 256 | 0) + ")"; | |
break; | |
case "RGBA": | |
_c = "rgba(" + (Math.random() * 256 | 0) + ", " + (Math.random() * 256 | 0) + ", " + (Math.random() * 256 | 0) + ", " + (Math.random() * 101 | 0) / 100 + ")"; | |
break; | |
case "HSL": | |
_c = "hsl(" + (Math.random() * 361 | 0) + ", " + (Math.random() * 101 | 0) + "%, " + (Math.random() * 101 | 0) + "%)"; | |
break; | |
case "HSLA": | |
_c = "hsla(" + (Math.random() * 361 | 0) + ", " + (Math.random() * 101 | 0) + "%, " + (Math.random() * 101 | 0) + "%, " + (Math.random() * 101 | 0) / 100 + ")"; | |
break; | |
default: | |
_c = "#" + ("000000" + (Math.random() * 0x1000000 | 0).toString(16)).substr(-6); | |
break; | |
} | |
if (/^(MSF|AHEX)$/.test(type)) _c += ("00" + (Math.random() * 256 | 0).toString(16)).substr(-2); | |
return _c; | |
}; | |
} |
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
var a = ["RGB", "RGBA", "HSL", "HSLA", "HEX", "MSF", "AHEX", ""]; | |
for (var i = 0; i < a.length; i++) { | |
document.write(a[i] + "<br>"); | |
for (var j = 0; j < 100; j++) { | |
var color = getRandomColor(a[i]); | |
document.write(color + "<br>"); | |
} | |
document.write("<hr>"); | |
} |
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
※以下の2つは同じ | |
if (type === "MSF" || type === "AHEX"){} | |
if (/^(MSF|AHEX)$/.test(type)) {} | |
※参考 : https://github.com/shichuan/javascript-patterns/blob/master/general-patterns/conditionals.html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment