Enter Your Email Address to Watch This Lesson

Your link to unlock this lesson will be sent to this email address.

Unlock this lesson and all 829 of the free egghead.io lessons, plus get JavaScript content delivered directly to your inbox!

Existing egghead members will not see this. Sign in.

Just one more step!

Check your inbox for an email from us and click link to unlock your lesson.

Colors in GIF Loop Coder

6:28 JavaScript lesson by

In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder.

egghead.io comment guidelines


In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder.

Let's look into colors in GIF Loop Coder a bit more. GIF Loop Coder is based on HTML5 canvas and the 2D drawing API. So any way you specify colors there you can use in glc. I've created a bunch of stars here. For the first I'll use a CSS named color string red, then a six-digit hex color string 00FF00, then a three-digit hex string 00F. You can also use RGB and RGBA strings like so.

As I demonstrated earlier, you can animate between any colors defined with any of these methods. Mix and match, it all just works. We can go from a named color string to an RGB string, or a three-digit hex string to a named color string, or any other combination. But still, creating colors with these various types of strings can be tedious. That's why there's a color module built in to GLC. You can see it defined up at the top with color=glc.color, this just makes it easier to type if you wind up using it a lot.

So what can you do with this? For one making RGB colors is much easier, just say color.rgb, passing in values for red, green, and blue, or if you want a shade of gray, just say color.gray, passing in the shade from 0 to 255.

In the next few examples I'll just create a bunch of random polygons. I'll predefine the x and y positions for use later, and I'll give them random positions, radii, and number of sides. Now because there are so many and they're all black by default, that's all you see is black. So first I'll assign a color using color.rgb, calculating the red channel by dividing x by width, and multiplying that by 255. This will give us a range of values from black to red as we move from left to right.

You could do this from an RGB string, but you'd need to round the values down to integers, and concatenate the string which is a pain. With the color module it's all just done for you. You can do the same with color.gray as well. Now there are several methods for creating random colors, there's randomRGB. Here you see each of the polygons has a different color. You can also pass in a min and max value, if I pass in 0, 128 all colors will get values in that range, making the colors darker. Passing in 128,255 makes them all lighter.

Then there's randomGray, now all the polys are different shades of gray. Min max here works as well, darker grays, and lighter grays. There are also a bunch of HSV related functions, HSV standards stands for hue saturation value, and is an alternate way of defining colors. Hue is the base color defined in a spectrum that goes from 0 to 360. Here 30 is an orange color. Saturation determines how pure the color will be. Saturation 1 will contain 100 percent of the hue, 0.5 will be washed out, and 0 will be grayscale.

Value determines how dark the color will be, 1 is full brightness, and 0.5 will be a darkened version of the hue, 0 will be black. With these functions you can create various colors with the same or similar hues to create a coordinated color palette. Here I'll set the hue to the x position divided by the width times 360, that should give me a hue range from 0 to 360 as the polygons go across the screen, and value and saturation I'll leave at 1. So there's a polygon rainbow.

We can set the hue to be a static 30 for example, and use the same technique to vary the saturation, or the value. Then there's randomHSV this takes two values for each component, a min and a max. If we set a min and max hue both to 30, and a min and max saturation and value both to 0.5 and 1, we get a range of different colors based on that same orange hue. If we let the hue go from 30 to 60, now we get some base colors tending more toward yellow, or hue between 120 and 160 will give us a blue-green palette.

Now animating HSV colors is a special case, because the HSV values are immediately converted to RGB values. So say we create a circle here and give it an animated fill style using color.HSV, with hues from 0 to 360. You might expect this to animate through all the colors in the spectrum, but instead we just get red. This is because HSV(0,1,1) gets converted to full saturation, full-brightness red, FF0000, and so does 360,1,1.

There's no visible animation, that's the most extreme case, but in general HSV animations done this way are not going to give you what you expect due to this conversion. Luckily there's another method on the color module, animHSV. This, like randomHSV, takes two values for each property. Hue, saturation, and value, and animates between them. Now we can set the fill style to color.animHSV(0,360,1,1,1,1) and this will animate through the full spectrum of hues.

I'll extend the duration so you can see the transition more clearly. So there's a taste of color management in GLC, check the docs to see a few more useful methods.

Joel's Head
Why are we asking?