Skip to content

Instantly share code, notes, and snippets.

@ethertank
Last active September 29, 2015 10:57
Show Gist options
  • Save ethertank/1590113 to your computer and use it in GitHub Desktop.
Save ethertank/1590113 to your computer and use it in GitHub Desktop.
getRandomColor
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;
};
}
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>");
}
※以下の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