Help With Random RGB and FILL

A student wrote the following code (comments are mine):

//set noseColor to a random color

var noseColor = rgb(randomNumber(0, 255), randomNumber(0, 255), randomNumber(0, 255));

//use noseColor for the nose

fill(rgb(noseColor, noseColor, noseColor);

ellipse(200,200,50,50);

My understanding is that when r = g = b, the color will be a shade of gray. So looking at this code, I expected the nose to be somewhere on the monochrome scale from black (0, 0, 0) to white (255, 255, 255). I was surprised to see the nose appear in different colors.

My undetstanding is the correct way to write this code would be to change the fill command to this:

fell(noseColor);

As expected, this results in a random color.

I have this demo code that shows that in fact both versions of the fill() command result in the same color (see lines 22 & 29).

Another thing that confuses me is that line 29 in the demo code does not result in a partially transparent fill color (note the alpha = 0.7). If I change all 3 ‘randomColor’ in line 29 to 50 (or any other number 0 to 255), the color is transparent.

Anyone understand what is going on?

Thanks,

John

I’m not 100% sure, but these are my thoughts that I feel are probably what is happening.

randomColor is defined as a color (not a number) in line 6 of the code.

This is confirmed to me when I added a watcher and ran the program and it comes back with an rgba value.

Line 29 probably took that first variable of randomColor and ignored the rest – including the alpha when it saw that variableColor was a valid color assignment in and of itself … in other words, I think it read it as rgba((251,213,98,1), 251,213,98, 1, 251,213,98,1, 0.7). Since the first instance of the variable already has the 4 values it needs, it ignores all of the rest of the parameters. Of course the 251, 213, 98 are different numbers each time it runs…

That’s my understanding of the logic … I do know that extra parameters in gamelab are ignored in my experience so that’s what makes sense to me.

Mike

Mike,

That makes sense and fits with my intuition on it as well. What I did not realize is that rgba values are not seen as a number.

Thanks!

John

John Wilson

Computer Science & Math Teacher

Park Slope Collegiate