10 examples of 'random color generator' in JavaScript

Every line of 'random color generator' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
63function genColor(colors) {
64 var rand = generator.random()
65 var idx = Math.floor(colors.length * generator.random())
66 var color = colors.splice(idx,1)[0]
67 return color
68}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

23randomColor(){
24 var colors = ["#C0392B", "#E74C3C", "#9B59B6", "#8E44AD", "#2980B9",
25 "#3498DB", "#17A589", "#138D75", "#229954", "#28B463", "#D4AC0D",
26 "#D68910", "#CA6F1E", "#BA4A00"];
27 return colors[this.random(0, colors.length-1)]
28}
49function randomColor() {
50 const colors = [
51 '#1abc9c',
52 '#2ecc71',
53 '#3498db',
54 '#9b59b6',
55 '#8e44ad',
56 '#2c3e50',
57 '#f1c40f',
58 '#e67e22',
59 '#e74c3c',
60 '#f39c12'
61 ]
62 return randomElement(colors)
63}
106return (function getRandomColor() {
107 return colors[Math.floor(Math.random() * colors.length)];
108});
87function randomColor() {
88 return color(random(255), random(255), random(255))
89}
175function randomColor(colors) {
176 return colors[Math.floor(Math.random() * colors.length)];
177}
73function randColor() {
74 return new THREE.Color(parseInt(Math.random()*16777216));
75 }
9function random_color() {
10 var r = Math.floor(Math.random() * 256),
11 g = Math.floor(Math.random() * 256),
12 b = Math.floor(Math.random() * 256);
13 return 'rgb('+r+','+g+','+b+')';
14}
153function random(palette) {
154 if (Array.isArray(palette))
155 return derive([palette[0]], palette.length);
156 const letters = '0123456789ABCDEF';
157 let color = '#';
158 for (let i = 0; i < 6; i++) {
159 color += letters[Math.floor(Math.random() * 16)];
160 }
161 return color;
162}
478public static getRandomColor(min: number = 0, max: number = 255, alpha: number = 255): number {
479
480 // Sanity checks
481 if (max > 255)
482 {
483 return Phaser.ColorUtils.getColor(255, 255, 255);
484 }
485
486 if (min > max)
487 {
488 return Phaser.ColorUtils.getColor(255, 255, 255);
489 }
490
491 var red: number = min + Math.round(Math.random() * (max - min));
492 var green: number = min + Math.round(Math.random() * (max - min));
493 var blue: number = min + Math.round(Math.random() * (max - min));
494
495 return Phaser.ColorUtils.getColor32(alpha, red, green, blue);
496
497}

Related snippets