Hello I was making a javascript net art and I needed to make that rgb stroke random can anyone help me i'm kinda new in javascript.
if(this.bbox.collision(Player.bbox)){ strokeColor(255,0,0); }
Asked
Active
Viewed 47 times
0

Tiago Gomes
- 21
- 3
-
3Does this answer your question? [Random color generator](https://stackoverflow.com/questions/1484506/random-color-generator) – Timothy G. Jun 10 '21 at 18:30
-
No it have to get that rgb values – Tiago Gomes Jun 10 '21 at 18:32
2 Answers
1
Use Math.random to get a random value from 0-255 for each of the three values.
let r = Math.floor(Math.random()*255);
let g = Math.floor(Math.random()*255);
let b = Math.floor(Math.random()*255);
Then you can call this by doing this:
strokeColor(r, g, b);

SatvikVejendla
- 424
- 5
- 14
-
-
Glad to hear it. If it works, you can check this answer so other people can find it if they run into a similar problem – SatvikVejendla Jun 10 '21 at 18:44
0
<div id="changer">Contents Here</div> <br> <button onclick="setRandomRgbColor()">Change Color</button> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
function getRandomRgbColor() {
var color = 'rgb(';
color += Math.floor(Math.random() * 256) + ',';
color += Math.floor(Math.random() * 256) + ',';
color += Math.floor(Math.random() * 256) + ')';
return color;
}
function setRandomRgbColor() {
//$("#changer").css("border", ' 2px solid '+ getRandomRgbColor());
//or
document.getElementById("changer").style.border = "2px solid "+getRandomRgbColor();
}

Kallol Ray
- 79
- 1
- 5