Return to Video

Learn programming 4: Shades of gray and colors, frameRate()

  • 0:00 - 0:04
    First thing we're going to do today is...
  • 0:04 - 0:07
    How to save and open our programs
  • 0:07 - 0:12
    This is something we didn't do yesterday because we were doing very small programs.
  • 0:12 - 0:18
    This works like in any other application there's a 'Save' and 'Save as' option.
  • 0:18 - 0:29
    There's an 'Open' option. So if we write a program here ...
  • 0:29 - 0:44
    ... then we can just go ahead and 'Save as'. First thing chose a folder, then type a name and save it.
  • 0:44 - 0:47
    The next thing is just coming back and opening the program.
  • 0:47 - 0:56
    So all the little programs we do are not lost. We can come back and make some changes and try them out again.
  • 0:56 - 1:05
    Now that we now how to save and open programs let's go back to the function we were giving a look yesterday...
  • 1:05 - 1:10
    ... 'background'.
  • 1:10 - 1:15
    Let's check it again on the reference.
  • 1:15 - 1:21
    I can show you were I found it. Right here at the top.
  • 1:21 - 1:32
    We saw that there's two options or more. Type one number inside parentheses or three numbers
  • 1:32 - 1:40
    Later I'll explain how to load images, but let's try just the colors first.
  • 1:40 - 1:49
    We can try typing a few numbers here inside the parentheses to see which effect it has.
  • 1:49 - 1:54
    With a 0 we get a black color.
  • 1:54 - 1:57
    Let's try 100.
  • 1:57 - 2:02
    We get some dark grey.
  • 2:02 - 2:06
    With 200 we get a light grey.
  • 2:06 - 2:11
    And I know the maximum is 255. Then we get white.
  • 2:11 - 2:17
    So we see that just having one number here we'll get different shades of grey.
  • 2:17 - 2:20
    How do I know that 255 is the maximum?
  • 2:20 - 2:32
    Well that's a very usual number in computers. Something being valid between 0 and 255.
  • 2:32 - 2:40
    That means there are 256 possible values.
  • 2:40 - 2:48
    If we check on the reference, not on the 'background' page, but maybe 'colorMode' or 'stroke'.
  • 2:48 - 2:54
    It will mention this 255 value.
  • 2:54 - 3:01
    This is the page for 'stroke'. It's used to change the color for the border, we'll see that later.
  • 3:01 - 3:07
    I just wanted to show you that it says here, the default color space is RGB.
  • 3:07 - 3:15
    With each value in the range 0...255
  • 3:15 - 3:20
    So there's 256 possible values.
  • 3:20 - 3:30
    If one number gets us different shades of grey then I guess if I type the other option with 3 values...
  • 3:30 - 3:34
    ... I should be able to get colors.
  • 3:34 - 3:45
    So I run the program and I get the same! It's the same if I type 255 or if I type 255 three times.
  • 3:45 - 3:48
    I'll explain why in a second.
  • 3:48 - 3:59
    This will happen anytime we type the same value three times. We'll get a shade of grey.
  • 3:59 - 4:08
    Actually what these values are is what is called RGB, which means: red, green and blue.
  • 4:08 - 4:14
    When we have the same amount of red, green and blue we get a shade of grey.
  • 4:14 - 4:23
    So the way to get colors is to have different values. To have them different.
  • 4:23 - 4:34
    We'll try (200, 100, 50) to see what we get. We get this brown, orange?
  • 4:34 - 4:44
    One way to see that this is really RGB is... let's enter (255, 0, 0).
  • 4:44 - 4:52
    That should mean we're only having red light and the green light and blue light are gone.
  • 4:52 - 4:56
    We only have the red component here.
  • 4:56 - 4:59
    If I run this program I get red.
  • 4:59 - 5:08
    The same will happen if instead of red I try it on green.
  • 5:08 - 5:11
    It works, so you can try that for blue.
  • 5:11 - 5:22
    What happens if you combine red and green? So let's use the maximum amount of red and green.
  • 5:22 - 5:29
    Then we get yellow. There's two or at least two ways of generating color.
  • 5:29 - 5:40
    This is additive light, so it's the amount of red, blue and green light.
  • 5:40 - 5:45
    If you have (0,0,0) that means you don't have any light at all and that's why it's black.
  • 5:45 - 5:56
    And if you have the maximum 255 three times. Then you have the maximum amount of each light so you get white.
  • 5:56 - 6:03
    The other way of generating color is subtractive. This is how printers work.
  • 6:03 - 6:15
    Instead of (red, green, blue) it uses (cyan, magenta, yellow and black).
  • 6:15 - 6:22
    In this case you start with white, which is the white paper, and the more ink you add the darker it becomes.
  • 6:22 - 6:27
    You could say that with computer screens you start with black and you add light.
  • 6:27 - 6:34
    The more light you add the brighter the color. Until you get white eventually.
  • 6:34 - 6:41
    That's the difference between additive and subtractive color.
  • 6:41 - 6:52
    So now we know that we can generate colors adding red, green and blue components to change the background.
  • 6:52 - 6:57
    Like yesterday, we could replace these numbers for a random value.
  • 6:57 - 7:07
    Where we have a 0 let's write a 'random(256)',
  • 7:07 - 7:24
    Let's replace each 0 with our 'random(256)'. Which is a function that converts itself into a number between 0 and 255.
  • 7:25 - 7:29
    Now we're gonna get unexpected colors.
  • 7:29 - 7:40
    Everytime we run the program it will make up a random amount of red, green and blue.
  • 7:40 - 7:49
    See that's some kind of blue. Run it again it's like purple. Dark pink...
  • 7:49 - 7:59
    Ok. This is just running once. We can do it like yesterday and draw it many times per second.
  • 7:59 - 8:21
    For that we wrote 'void draw() { }'. Everything that is between the '{' and '}' is executed multiple times per second.
  • 8:21 - 8:31
    I'm going to click run and this is gonna go really fast. Too fast to actually notice the colors.
  • 8:31 - 8:34
    So there must be a way to make it slower.
  • 8:34 - 8:43
    Let's check on the reference manual. I don't know how it could be called but I'll hit 'ctrl+f' ...
  • 8:43 - 8:51
    Search for 'frame'. Maybe there's something like frames per second? Here there's a 'frameRate'.
  • 8:51 - 9:01
    That sounds right. "frameRate: Specifies the number of frames to be displayed every second."
  • 9:01 - 9:04
    So that's what we want, to make this smaller.
  • 9:04 - 9:11
    Here says the default rate is 60 frames per second. That's quite fast, let's make it slower.
  • 9:11 - 9:17
    Here it says it's recommended to set the frame rate within 'setup()'.
  • 9:17 - 9:20
    What is this 'setup()'?
  • 9:20 - 9:25
    You can see we have this 'void draw()' function. We're using it already.
  • 9:25 - 9:33
    In this example all this code will run multiple times per second.
  • 9:33 - 9:41
    And we can add this, we'll check the manual in a minute.
  • 9:41 - 9:49
    Put this outside. Now we have two functions: we have 'void draw()' with a group sentences...
  • 9:49 - 9:54
    ... in this case only the one that says "change the background color".
  • 9:54 - 10:01
    Now we have here another function that says 'setup' and 'frameRate(4)'.
  • 10:01 - 10:09
    Let's make it even slower. Just 1. One frame per second and then run this program.
  • 10:09 - 10:16
    Now the background is changing one time per second.
  • 10:16 - 10:21
    Finally let's check on the manual what is this setup.
  • 10:21 - 10:30
    I go back here and I search for 'setup'.
  • 10:30 - 10:38
    Here it says: "Called once when the program is started. Use to define initial environment properties such as...
  • 10:38 - 10:45
    screen size, background color, loading images, etc... before the draw begins executing."
  • 10:45 - 10:51
    So these two functions are used in many Processing programs.
  • 10:51 - 11:00
    First you configure some things so in the setup you define some characteristics for your movie.
  • 11:00 - 11:05
    Like in this case how many frames per second it displays and later...
  • 11:05 - 11:12
    ... you have another function that is executed this many times per second. In this case just one.
  • 11:12 - 11:19
    You can also check the reference page for 'draw'.
  • 11:19 - 11:28
    Which will say: "Called directly after setup and continuously executes the lines of code contained inside it's block..."
  • 11:28 - 11:33
    "...until the program is stopped or 'noLoop' is called."
  • 11:33 - 11:42
    Usually we'll not understand every detail on the manual until we have more experience and know more functions.
  • 11:42 - 11:46
    Like here it says 'mousePress'. We don't know anything about the mouse yet ... but we'll find out.
Title:
Learn programming 4: Shades of gray and colors, frameRate()
Description:

When you write a program, you can save it on your hard disk. You can open the saved program later and continue working on it. Discover gray scale shades and RGB colors, then animate the background color and change the frame rate.

more » « less
Video Language:
English
Duration:
11:48

English subtitles

Revisions