’15 -’16 General Discussion for Lesson 5.17

Use this thread to discuss your questions and comments about how to run the lesson.

Re: setting a random stroke color in the canvas:

We’ve tried a bunch of ways to use setStrokeColor to set a random color, but I think we’re running into an issue where because the function accepts the parameter as a string, we can’t use randomNumber function for rgb values. We’ve also tried setting variables for the red, blue, and green separately and calling them in the setStrokeColor function, to no avail.

We’ve found a (kind of lame) workaround by creating an array made up of different colors, then calling for a random index using randomNumber – this works, but isn’t as “random” as we would like it, if that makes sense.

Hey Michael,

You’re correct there is no random color function. I’ve made use of the following function in the past when trying to do what I believe you are.

function randomColor(){
  var r = randomNumber(0,255);
  var g = randomNumber(0,255);
  var b = randomNumber(0,255);
  //Create a string in the format "rgb(255, 100, 0)" and return it
  var colorString = "rgb(" + r + "," + g + "," + b + ")";
  return colorString; 
}

// In your code you can then call
setStrokeColor(randomColor());
// or to make the steps more explicit instead do
var newRandomColor = randomColor();
setStrokeColor(newRandomColor);

Let me know if that helps!
GT

That’s AWESOME. Makes perfect sense – setStrokeColor requires a string, so create a function that will return a string. Thank you!!!

Glad that helped! :smile: