Going Green explainer


#21

What am I doing wrong?


#22

Your code will always draw at least 1 drawBox, but it should only be making a box if the green component is greater than 200. You simply need to remove the extra drawBox(rgbObject) that comes before your if statement.

–Frankie


#23


I’m confused. I’m pretty sure it’s right. The box lights up green but it never says I’m wrong but never says I’m right… ?


#24

Hi,

If you look at the 2 posts right before yours, you’ll see the someone with the same problem.

–Frankie


#25

I was wondering if I could use a for loop here to make it always draw the box once it hit the correct situation rgbObject.green > 200. Here is my code but it doesn’t draw anything or give me an error, any suggestions?


#26

There are 3 changes you can make to your code:

  1. Change > to < since you want your loop to continue increasing the green value while it’s less than 200
  2. Change print(rgbObject) to print(rgbObject.green) to get a number value. print(rgbObject) will output [object Object] because the print() function doesn’t know how to display all the information stored inside the object.
  3. Once the loop makes rgbObject.green >= 200 it doesn’t run the code inside the loop again to draw the box or print the number. That also means if the value starts at 200 or above, you won’t see any box or number. Add another drawBox() and print() after the loop to always print the box and green value at least 1 time.

Example solution:

var rgbObject = {
  green: pickRandom(255),
  red: pickRandom(255),
  blue: pickRandom(255)
}
for (rgbObject.green; rgbObject.green < 200; rgbObject.green = rgbObject.green + 25) {
  drawBox(rgbObject);
  print(rgbObject.green);
}
drawBox(rgbObject);
print(rgbObject.green);

–Frankie


#27

Awesome, thank you, that makes sense, this is the best learning tool for coding on the go, truly appreciate the quick response too.


#28

I solved the puzzle without much trouble, but am not entirely sure where the color is coming from. When assigning a property to an object, such as “red: pickrandom(255)”, as was used in this example, I thought the word before the colon (‘red’ in this case) was simply a name. Do colors behave differently than other words when used as properties? By using the word red does the pickrandom(255) function automatically choose a value of red between 0 and 255?
Could it be that by having the name of a color within the drawbox function, regardless of the other words, the code automatically relates the number to that color?


#29

The magic is happening in drawBox, not the rgbObject. You’re correct, the red is just a name, but when an object is given to drawBox(), drawBox looks for a property with that specific name (red) along with green and blue. You can take any object, and as long as it has those 3 properties, drawBox() can use it.

The pickRandom function is a custom Grasshopper function (so are drawBox and drawBoxes). If you give a whole number to pickRandom(), it will choose a whole number from 1 to that number. So, pickRandom(255) will actually pick a number between 1 and 255. Colors can use 0 too, so if you wanted to have all the possible colors you could use pickRandom(256)-1.


Extra info:

There is something a bit special about the choice of colors red, green, and blue. You know how TVs, computer monitors, and phone screens are made up of pixels. And usually each pixel is made up of 3 subpixels: a red, green, and blue light. Since these tiny lights are placed so close together, they seem to your eye to be mixed into a single color. If you’ve ever had a little drop of water fall on your phone (especially on a white part of the screen), you’ve probably seen the grid of sub pixels. The number 0 to 255 will turn into how bright that subpixel is lit up.

So you can see how if you gave three numbers between 0 and 255, there should be a way to turn that into a color on the screen as long as it knows which number goes with which color. If you wanted to use a different color model, like cyan, magenta, yellow, you could make a separate function that converts those numbers into an rgb color. At some point, the numbers have to turn into how bright to make each subpixel.

But realize you don’t need to use a different system just to make any single color. You can make a magenta box using rgb, or a yellow box, or a grey box. It’s similar to how you can describe the position of a plane in the sky as “1000 feet forward, then 200 feet to the right, then 3000 feet up” or as “12 degrees to the right, 71 degrees up from the horizon, and 3200 feet away”. Those are 2 ways to describe a position using 2 different coordinate systems.

–Frankie


#30

I love this app. I am learning a lot and in a fun engaging way.

I am going to go through the lessons again at this point because I need more experience with the code to remember how to utilise it correctly.

Is there a way to have more optional work?


#31

We’ll be adding new lessons and courses routinely. You should try out the lessons in the Practice course if you haven’t yet.

–Frankie


#32

I didn’t know there was a practice course. Where do I find it?

This app gets better and better!


#33

Go to the scrolling lessons page. There is a main menu button in the top left . You can also just swipe over from the left side of the screen. The top section lists all the courses:

  • What Is Code?
  • Fundamentals
  • Fundamentals II
  • Animations
  • Animations II
  • Practice

You have to complete the previous course to move on to the next, except for the Practice course. In the Practice course, lessons are added as you learn new concepts.

–Frankie