Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get random RGB in Javascript? [duplicate]

Tags:

I have this code that uses RGB color selection and I was wondering how to make JavaScript do a random color using the RGB method and remember it throughout the code.

EDIT: I tried this:

var RGBColor1 = (Math.round, Math.random, 255) var RGBColor2 = (Math.round, Math.random, 255) var RGBColor3 = (Math.round, Math.random, 255) 

but it doesn't work. Help please!

EDIT 2: The code uses this:

g.fillStyle="rgba(R,G,B,0.2)"; g.strokeStyle="rgba(R,G,B,0.3)"; E(); 

The letters represent the color of RGB.

EDIT 3: The doubles of this question are using HEX values, not RGB values.

like image 623
ipodlover3354 Avatar asked Apr 15 '14 22:04

ipodlover3354


People also ask

How do I randomize a color in Javascript?

var randomColor = Math. floor(Math. random()*16777215).

How do I create a randomizer in Javascript?

Javascript creates pseudo-random numbers with the function Math. random() . This function takes no parameters and creates a random decimal number between 0 and 1. The returned value may be 0, but it will never be 1.

How do I generate a random color in TypeScript?

Introduction. Here I show a random color generator for a div using TypeScript and change the color of the div at regular intervals of time using TypeScript. We use the setInterval method in this example. The setInterval method creates a timer that calls the specified function at the specified interval in milliseconds.


2 Answers

function random_rgba() {     var o = Math.round, r = Math.random, s = 255;     return 'rgba(' + o(r()*s) + ',' + o(r()*s) + ',' + o(r()*s) + ',' + r().toFixed(1) + ')'; }  var color = random_rgba();  g.fillStyle   = color; g.strokeStyle = color; 

FIDDLE

like image 142
adeneo Avatar answered Sep 23 '22 14:09

adeneo


const randomBetween = (min, max) => min + Math.floor(Math.random() * (max - min + 1)); const r = randomBetween(0, 255); const g = randomBetween(0, 255); const b = randomBetween(0, 255); const rgb = `rgb(${r},${g},${b})`; // Collect all to a css color string 
like image 41
assembly_wizard Avatar answered Sep 23 '22 14:09

assembly_wizard