-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (39 loc) · 1.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"use strict";
/**
* Return a random color
* @param {object} options The options object
* @returns {string} the color in format requested in options
*/
var colors = require("./colors.js");
function randomColor(options) {
var color;
// Assign default option
if (options === undefined) {
options = {
format: "name"
};
}
// Type check options
if (Object.prototype.toString.call(options) !== "[object Object]") {
throw TypeError("Expected object, but received: " + Object.prototype.toString.call(options));
}
// handle color format
switch (options.format) {
// calculate random RGB color
case "rgb":
color = "rgb(" + Math.floor(Math.random() * 256) +
"," + Math.floor(Math.random() * 256) +
"," + Math.floor(Math.random() * 256) + ")";
break;
// calculate a random Hex value
case "hex":
color = "#" + Math.floor(Math.random() * 16777215).toString(16);
break;
// default calculate random color Name
default:
color = colors[Math.floor(Math.random() * colors.length)]["name"];
break;
}
return color;
}
module.exports = randomColor;