< Return to Video

1.2 p5.js: Color

  • 0:03 - 0:08
    This next video - hi, by the way -
    is about color.
  • 0:08 - 0:13
    So like the previous video that I just made,
    I looked at shapes,
  • 0:13 - 0:18
    drawing shapes into a canvas,
    using beginner steps with p5js.
  • 0:18 - 0:21
    Maybe you made something
    just with shapes,
  • 0:21 - 0:25
    the natural next step is to make
    stuff have some color.
  • 0:25 - 0:33
    so maybe you had a canvas,
    perhaps with a rectangle on it,
  • 0:33 - 0:36
    maybe it has an ellipse on it,
  • 0:36 - 0:38
    maybe it has some lines on it,
  • 0:38 - 0:41
    perhaps you came up with
    something much more interesting design
  • 0:41 - 0:41
    than this.
  • 0:41 - 0:47
    And now we have to ask the question:
    How do I, everything was just black lines
  • 0:47 - 0:49
    and white interiors.
  • 0:49 - 0:53
    So how do I color these shapes?
  • 0:54 - 0:58
    So, to draw these shapes we learned
    a bunch of commands,
  • 0:58 - 1:00
    but really the word we should
    be using is 'functions'!
  • 1:00 - 1:09
    I showed you a bunch of functions:
    rect(), ellipse(), line().
  • 1:09 - 1:13
    These are three functions:
    rect() draws a rectangle,
  • 1:13 - 1:14
    ellipse() draws an ellipse,
  • 1:14 - 1:15
    line() draws a line.
  • 1:15 - 1:17
    These are for drawing.
  • 1:17 - 1:20
    Now for color there are also functions.
  • 1:20 - 1:22
    And I think there are three
    relevant ones that I want
  • 1:22 - 1:23
    to look at in this video.
  • 1:23 - 1:26
    background(),
  • 1:29 - 1:31
    stroke(),
  • 1:31 - 1:33
    and fill().
  • 1:34 - 1:39
    The background function will
    set a color for the background,
  • 1:39 - 1:42
    the entire canvas ... what is the color
    that is in the background
  • 1:42 - 1:46
    of the canvas? Although the word 'background'
    is a little bit of a misnomer here
  • 1:46 - 1:50
    even though I'm going to use it
    to set the background,
  • 1:50 - 1:54
    the order of when we call
    these functions is quite crucial!
  • 1:55 - 1:57
    And I will show you that as well.
  • 1:57 - 2:04
    stroke() is the function that
    sets the color for the outline of a shape.
  • 2:04 - 2:07
    So what is the color
    of the pixels of a line?
  • 2:07 - 2:09
    What is the color of the outline
    of a rectangle?
  • 2:09 - 2:14
    And fill() sets the color of
    the interior of a shape.
  • 2:15 - 2:21
    So let's say the code for this is:
    rect, something or other,
  • 2:23 - 2:25
    ellipse, something or other,
  • 2:26 - 2:28
    line, something or other.
  • 2:28 - 2:33
    So now we need to figure out,
    if I want to set the stroke or
  • 2:33 - 2:37
    the fill for this rectangle,
    where do I write these functions?
  • 2:37 - 2:39
    Where do I execute these commands?
  • 2:40 - 2:43
    If I want to set the fill or the stroke
    for the circle (the ellipse),
  • 2:43 - 2:44
    where do I write these commands?
  • 2:44 - 2:48
    And now, order of operations has always
    been important but it is particularly
  • 2:48 - 2:50
    important now. When you call these functions
  • 2:50 - 2:53
    it's like setting the color of a pen.
  • 2:53 - 2:58
    So before I draw the rectangle
    I need to set the color of my pen
  • 2:58 - 3:00
    to red or blue etc.
  • 3:00 - 3:05
    So if I wanna set the stroke or fill
    of this rectangle
  • 3:06 - 3:14
    I need to write these function calls
    before I execute the function rectangle.
  • 3:14 - 3:17
    And then if the next thing
    I do is ellipse()
  • 3:17 - 3:19
    it will also use that same stroke and fill
  • 3:19 - 3:21
    and line() will also use
    that same stroke,
  • 3:21 - 3:23
    not the fill!, because there
    is no fill for a line.
  • 3:23 - 3:27
    So if I want to then change
    the color, to have a different color
  • 3:27 - 3:31
    for the ellipse then I just need
    to add another call
  • 3:31 - 3:34
    to stroke() and fill() .
    After I draw that rectangle,
  • 3:34 - 3:37
    I need to set new 'pen colors'
    for that ellipse.
  • 3:38 - 3:41
    So this is part 1.
  • 3:41 - 3:45
    Part 1 is I need to put these things
    in some proper order.
  • 3:46 - 3:50
    Part 2 is 'what do I put inside here?'
  • 3:50 - 3:53
    What do I put inside these functions?
  • 3:53 - 3:55
    stroke(), fill(), background(), etc?
  • 3:56 - 3:58
    That's a very good question!
  • 3:58 - 4:00
    I wish I had time to answer that question.
  • 4:00 - 4:01
    I do. I have to answer it.
  • 4:01 - 4:04
    Ok, so let's erase all of this
    for a moment,
  • 4:07 - 4:09
    and let's think about color.
  • 4:09 - 4:12
    Now there's a lot of ways
    to describe color, in the world.
  • 4:12 - 4:18
    I could get some paint, which would
    just say the word 'red' on the can,
  • 4:18 - 4:19
    that would mean that there's red there,
  • 4:19 - 4:22
    but we need a mechanism,
    a methodology,
  • 4:22 - 4:23
    we need a system for defining color
  • 4:23 - 4:33
    in p5.js, and we're going to use
    [unintelligible]
  • 4:33 - 4:39
    And a basic way that I'm going to look at
    it with you is RGB color.
  • 4:39 - 4:43
    Meaning: a color is a combination
    of an amount of red,
  • 4:43 - 4:46
    an amount of green and
    and amount of blue.
  • 4:46 - 4:49
    So, for example:
  • 4:50 - 4:57
    if I say stroke(255,0,0);
  • 4:57 - 4:58
    what does that mean?
  • 4:58 - 5:01
    Well, stroke() can accept 3 arguments,
  • 5:01 - 5:04
    the first one being the amount of red,
  • 5:04 - 5:06
    the second one being the amount of green,
  • 5:06 - 5:08
    and the third one being the amount of blue.
  • 5:09 - 5:12
    So you can now sort of guess
    ... there's clearly
  • 5:12 - 5:16
    no green and blue because
    I've set their value to 0.
  • 5:16 - 5:19
    However there is some amount of red.
  • 5:19 - 5:22
    255? Does that mean like a little bit of red?
    A lot of red?
  • 5:23 - 5:26
    It turns out it means
    the maximum amount of red!
  • 5:26 - 5:29
    So the range for these arguments
  • 5:31 - 5:33
    is between 0 and 255.
  • 5:34 - 5:37
    Meaning there are 256 possibilities.
  • 5:37 - 5:44
    And I'm spending a minute
    to mention [ramble]
  • 5:44 - 5:49
    this because this way of counting will
    come up again and again in programming!
  • 5:49 - 5:52
    Let's say I said:
    the range is between 0 and 9,
  • 5:52 - 5:55
    how many possibilities are there?
  • 5:55 - 5:56
    There's 10, right?
  • 5:56 - 5:59
    I'm just gonna take a moment
    to prove this to myself:
  • 5:59 - 6:03
    0, 1, 2, 3, 4,
    5, 6, 7, 8, 9.
  • 6:03 - 6:04
    Woah!
  • 6:04 - 6:06
    Right, I counted to 10!
  • 6:06 - 6:12
    This is confusing. See?
    I got to 9 but there are 10.
  • 6:12 - 6:15
    0, 1, 2, 3, 4,
    5, 6, 7, 8, 9.
  • 6:15 - 6:17
    Boy, I really have turned
    into a crazy person,
  • 6:17 - 6:18
    counting on my fingers
    in a room by myself,
  • 6:18 - 6:19
    with cameras pointing at me!
  • 6:19 - 6:23
    So this is the range.
    Why does it have this range?
  • 6:23 - 6:26
    It has to do with the way
    that information is stored
  • 6:26 - 6:29
    in the computer's memory,
    and bits and binary numbers,
  • 6:29 - 6:31
    and all that stuff,
    and I would love,
  • 6:31 - 6:35
    no, actually wouldn't love,
    but I could talk about that more.
  • 6:35 - 6:38
    But you can find another video,
    or maybe I make another video,
  • 6:38 - 6:39
    or send me an e-mail or something,
  • 6:39 - 6:41
    we'll find a way to talk about it more.
  • 6:41 - 6:45
    Right now it's easiest for us
    to just think of: this is the range!
  • 6:45 - 6:47
    And you can change that range too,
  • 6:47 - 6:49
    there's a function in p5 that
    allows you to adjust that range
  • 6:49 - 6:51
    but for now, let's keep that range!
  • 6:51 - 6:53
    0 to 255.
  • 6:53 - 7:00
    So, by the way, fill()
    will also take RGB with a range of 0-255.
  • 7:00 - 7:07
    background() will also take the same range, RGB.
  • 7:07 - 7:09
    So let's go look at that now!
  • 7:10 - 7:13
    Ok, I have preceded here a little program
  • 7:13 - 7:16
    that's drawing this little alien creature
  • 7:16 - 7:20
    and what I would like to do
    is to take a moment
  • 7:20 - 7:21
    to add some color.
  • 7:21 - 7:25
    And this is an excuse to look
    at a couple of other things.
  • 7:25 - 7:27
    Number 1 is: you can add comments
    into your program.
  • 7:27 - 7:30
    The way that you add comments
    is with a slash, slash //
  • 7:30 - 7:36
    If you add // to a line of code the computer
    will completely ignore that line of code!
  • 7:36 - 7:39
    Meaning it's not executed,
    so you can put whatever you want.
  • 7:39 - 7:42
    You could write little notes to yourself,
    or little secret notes to a friend
  • 7:42 - 7:45
    who's looking at your code later,
    whatever you like to do.
  • 7:45 - 7:47
    But here you can see a typical use.
  • 7:47 - 7:50
    Well, this rectangle is the body of this,
  • 7:50 - 7:51
    this ellipse is the head,
  • 7:51 - 7:59
    these two ellipses are the eyes,
  • 7:59 - 8:01
    and these two lines are the legs.
  • 8:01 - 8:03
    So this is a useful thing
    to get a habit of this now,
  • 8:03 - 8:07
    if you can get yourself to do it
    it will make things easier later.
  • 8:07 - 8:14
    So, for example, if I just add
    fill(255,0,0); and I hit play,
  • 8:14 - 8:16
    you can see:
    everything is all red,
  • 8:16 - 8:18
    all the shapes have now a fill of red.
  • 8:18 - 8:24
    But if I come down here
    and say: fill(0,255,0);
  • 8:24 - 8:25
    and look what I have done ...
  • 8:25 - 8:27
    no red, all green, no blue.
  • 8:27 - 8:33
    And now you can see
    the eyes have a fill of green.
  • 8:33 - 8:35
    Now does anything come after the eyes?
  • 8:35 - 8:37
    Just the legs.
    And the legs don't have a fill,
  • 8:37 - 8:39
    so green only affected the eyes.
  • 8:39 - 8:45
    And I could say here:
    fill(0,0,255); and I could hit 'run' again
  • 8:45 - 8:51
    and we see now that the body is red,
    the eyes are green and the head is blue.
  • 8:51 - 8:52
    Now interestingly,
  • 8:52 - 8:55
    this order of operation is really important!
  • 8:56 - 9:00
    Notice how the head
    is drawn after the body.
  • 9:00 - 9:04
    Let's just vary briefly,
    move the head
  • 9:04 - 9:06
    above the body,
  • 9:06 - 9:10
    and you can see what's going on here:
  • 9:10 - 9:12
    that rectangle is covering that ellipse.
  • 9:12 - 9:18
    So p5 is going to draw the shapes,
    layer them in the order
  • 9:18 - 9:20
    of the sequence of the code in draw().
  • 9:20 - 9:22
    So that's a key piece!
  • 9:22 - 9:25
    Just in the way that this design works
  • 9:25 - 9:27
    is the head has got to come
    after the body,
  • 9:27 - 9:31
    so that it covers up
    that top portion of the rectangle.
  • 9:31 - 9:36
    And the same is true
    in terms of fill() and stroke().
  • 9:36 - 9:39
    If I take this red fill and
    put it after the body,
  • 9:40 - 9:42
    the body is no longer red,
  • 9:42 - 9:45
    because I called fill() after rect().
  • 9:45 - 9:48
    So that red fill has to go before rect()!
  • 9:48 - 9:50
    That's kind of a crucial
    piece of information as well.
  • 9:50 - 9:51
    The other thing
  • 9:51 - 9:54
    we should notice here
  • 9:54 - 9:55
    is that I have background() here.
  • 9:55 - 9:57
    Now let's make background()
    some ...
  • 9:57 - 10:00
    so, first of all, you can combine,
    you don't just have to do
  • 10:00 - 10:01
    all red, and no green and no blue.
  • 10:01 - 10:07
    [ramble]
  • 10:07 - 10:09
    You can obviously combine
    colors,
  • 10:09 - 10:12
    you put red and green together,
  • 10:12 - 10:13
    and you get something,
  • 10:13 - 10:17
    like ... let's put a little red,
    a little green and no blue,
  • 10:19 - 10:21
    you can see you get
    a different kind of green.
  • 10:21 - 10:22
    Let's put a lot more red ...
  • 10:22 - 10:25
    you get kind of a yellowish,
    greenish thing ...
  • 10:27 - 10:29
    So you can see that
    you can start playing around
  • 10:29 - 10:31
    with different amounts of
    red and green.
  • 10:31 - 10:32
    But notice how that background() there
  • 10:32 - 10:37
    background, you think like,
    it's drawing the background,
  • 10:37 - 10:40
    so whenever I call background()
    with a color it should put
  • 10:40 - 10:42
    everything behind,
    but all background() is doing
  • 10:42 - 10:44
    is actually filling the entire canvas
    with a color.
  • 10:44 - 10:48
    So if I were to take background()
    and put it at the very end
  • 10:48 - 10:52
    of draw() ... you can see background()
    is now called after all the code
  • 10:52 - 10:54
    I won't see anything at all!
  • 10:54 - 10:57
    All those things were drawn,
    but when we get to the end of draw()
  • 10:57 - 11:00
    to see the results,
    background() has covered everything!
  • 11:00 - 11:03
    So this is another reason
    why the order of operations is important.
  • 11:04 - 11:09
    And I'm mostly finished now but I want
    to mention two other things about color!
  • 11:10 - 11:14
    So let's look at something:
    in background(), let's say
  • 11:14 - 11:19
    I put 50, 50,50.
  • 11:20 - 11:21
    What did I get?
  • 11:21 - 11:22
    I got this kind of dark grey.
  • 11:22 - 11:27
    Let's make it: 150,150,150.
  • 11:28 - 11:29
    I got this light grey.
  • 11:29 - 11:31
    So one thing you'll notice:
  • 11:31 - 11:34
    if the red value equals the green value
    equals the blue value
  • 11:35 - 11:38
    you get a greyscale color.
  • 11:39 - 11:43
    All 0 being all black,
    all 255 being all white.
  • 11:43 - 11:44
    Why is this?
  • 11:44 - 11:50
    So the reason why this is,
    is that color here works the same way
  • 11:50 - 11:51
    color works with light.
  • 11:51 - 11:56
    If you take a bright red flashlight,
    and a bright green flashlight
  • 11:56 - 11:58
    and a bright blue flashlight,
    you shine em all together,
  • 11:58 - 12:00
    you add all the colors together,
    adding up colors,
  • 12:00 - 12:04
    you get things brighter and brighter,
    you'll get white.
  • 12:04 - 12:06
    No color, the absence of color,
    is black.
  • 12:06 - 12:10
    So greyscale color is something
    you might want to use kind of often
  • 12:10 - 12:13
    and it's inconvenient to write
    150,150,150
  • 12:13 - 12:16
    so one of the things that you
    can do is also call all these functions
  • 12:16 - 12:20
    background(), stroke() and fill()
    with only a single argument!
  • 12:20 - 12:23
    If you use a single argument
    you get the greyscale color.
  • 12:23 - 12:25
    So that's an important thing.
  • 12:25 - 12:28
    The other thing I should mention,
  • 12:28 - 12:29
    kind of briefly,
  • 12:29 - 12:32
    but we'll have to dive in a little more,
  • 12:32 - 12:34
    these functions,
  • 12:34 - 12:37
    we looked at how these functions
    always take 3 arguments,
  • 12:37 - 12:40
    and now we saw how they
    could take one argument.
  • 12:40 - 12:42
    There's another possibility!
  • 12:42 - 12:45
    They can also take 4 arguments.
  • 12:45 - 12:51
    And that last argument
    is transparency.
  • 12:51 - 12:53
    Sometimes referred to as 'alpha'.
  • 12:53 - 12:55
    Transparency.
  • 12:55 - 12:57
    All the colors have been fully opaque,
  • 12:57 - 12:59
    so when you put a shape
    on top of another shape
  • 12:59 - 13:01
    it completely blocks out the other shape.
  • 13:01 - 13:03
    But if you want to create the illusion
    of it being see-through,
  • 13:03 - 13:05
    you can add a 4th argument:
  • 13:05 - 13:09
    0 being completely see-through,
    you can't even see the color at all!,
  • 13:09 - 13:13
    255 being 100% opaque,
    that's what we've been doing already.
  • 13:13 - 13:16
    So let's look at that very briefly.
  • 13:16 - 13:18
    I think a nice play to see that
    would be with the head,
  • 13:18 - 13:21
    let's see what we got here ...
  • 13:21 - 13:24
    So remember how that head
    was covering up this rectangle
  • 13:24 - 13:29
    What if I add a 4th argument
    to the fill and I say like 100?
  • 13:31 - 13:35
    You can see now that that head
    is now transparent.
  • 13:35 - 13:38
    So it has its own fill
    but you can see what's behind it.
  • 13:38 - 13:41
    There's obviously a ton of stuff
    that you could do with,
  • 13:41 - 13:43
    colors, and combining,
    and shapes, in a million
  • 13:43 - 13:44
    different ways.
    Hopefully this gives you
  • 13:44 - 13:48
    a basic start and what I would say is
    if you were working on a design
  • 13:48 - 13:51
    that you have done after watching
    a previous video,
  • 13:51 - 13:53
    now try to add some color with it!
  • 13:53 - 13:57
    And again, just to mention, what I would
    recommend you do ...
  • 14:00 - 14:04
    is go to p5js, go to 'reference',
  • 14:04 - 14:06
    go to 'color'
  • 14:10 - 14:14
    and you can see
    here are the functions
  • 14:14 - 14:16
    that you might want to take a look at.
  • 14:16 - 14:19
    fill(), stroke(), background(),
    by the way,
  • 14:19 - 14:24
    noFill(), if you want to have no color
    for the interior of your shape,
  • 14:24 - 14:26
    noStroke() if you don't want
    to have an outline,
  • 14:26 - 14:30
    colorMode(), that's that function
    I was kind of mentioning you could
  • 14:30 - 14:33
    change the range, if you don't wanna
    have your colors between 0 and 255,
  • 14:33 - 14:35
    you wanna go between 0 and 100,
  • 14:35 - 14:37
    there's also other ways of
    thinking about color,
  • 14:37 - 14:39
    you can dive into that a bit yourself
  • 14:39 - 14:42
    but again, the nice thing about
    working with p5 is you can go
  • 14:42 - 14:45
    here and you can just hit 'edit'
  • 14:45 - 14:47
    and you can play around
    and say: ok, this is greyscale,
  • 14:47 - 14:51
    and I'm gonna change it to RGB,
  • 14:51 - 14:52
    and run it,
  • 14:52 - 14:53
    and see what I get ...
  • 14:53 - 14:57
    but you can see there is lots
    of different ways you can do color
  • 14:57 - 15:02
    actually in p5 and I barely just
    scratched the surface of it!
  • 15:02 - 15:04
    Ok, but this has got to be the end of this video!
  • 15:04 - 15:06
    15 minutes long,
    which is a very long time,
  • 15:06 - 15:08
    in the Internet world,
  • 15:08 - 15:13
    and I'm gonna eat some lunch now
    before I fall over!
  • 15:13 - 15:15
    Ok, have a good rest of your day!
  • 15:15 - 15:17
    And I'll be back for more later.
Title:
1.2 p5.js: Color
Description:

more » « less
Video Language:
English
Duration:
15:19
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color
Jerome Herr edited English subtitles for 1.2 p5.js: Color

English subtitles

Revisions