Color spaces

We learn about three color spaces (rgb, hsl, and hcl) and the pros and cons of each.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Fullstack D3 Masterclass course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Fullstack D3 Masterclass, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Fullstack D3 Masterclass

Sometimes when we're creating data visualizations, we need to create our own custom colors. So far we've been using color keywords that the browser understands and spits out a specific color. So this is something like cornflower blue or cyan or tomato or red. The browser understands that it'll spit out a specific color and it's really great for if you need to think of a color on the spot. You can just think of purple, red, blue, type that in and it'll spit out a color. There's also some important color keywords that can come in handy like custom color which is basically just the color CSS attribute. It's going to use that for where we use custom colors. So you can set a fill to custom color or a stroke. There's also transparent which is we've used for interactions so we use transparent for the fill rectangle that will listen for mouse move events within our interaction lesson. But the place where keywords don't come in handy is if we want to use corn flower blue but make it 10% darker, there is no way to do that with just using the keyword. So we're going to talk a little bit about color spaces. So there's three color spaces we're going to talk about. The first is RGB. The second is HSL. The third is HCL. So the first we're going to talk about RGB. So in RGB, the RGB and the letters stand for certain colors. So R is red, green is green, RGB is green and B is blue. And basically the higher the number for any of these, the more we have of that color. And then the browser is going to take those three values and turn them into one color. This is kind of how LCD screens work. Each pixel is made up of red, green and blue light. And if you have more of that light, it's going to have more of that color. And this is an additive color space. So it's unlike paint where if you add red to blue, it's going to get a little bit darker and then it approaches black. Basically, the way RGB works is it starts at black. And as you add more colors, it approaches white and gets lighter and lighter. All of these color spaces we're going to talk about also have an alpha channel. So A, it's optional. If you don't include it, it's just going to be one or 100% alpha. So this is the opacity of the color. So we can have a totally opaque color or we can have almost a transparent color with a really low alpha value. So if we want to represent an RGB color, there's two notation formats. The first is likely what you'll see most often. It's going to be the letters R, G, and B. And then in parentheses, the numerical values for each of those colors. It can also be represented as R, G, B, A. And then there's going to be a fourth color which corresponds to the alpha channel. So if we wanted to have this string for the color cyan, then we would write R, G, B, A optionally. And then in parentheses, there's going to be zero red color, max green color and max blue color and then max alpha. And that's what's cyan. It's green and blue without any red. And the way the RGB works in the functional notation is all of these values go from zero to 255. So if it's zero, it's none of the color. If it's 255, it's all of the color. The other format is the hexadecimal notation. You're probably not going to see this as much. Although I do use hex codes a lot for web filament, it's the same thing except the colors go from zero zero to FF. They go from zero zero all the way up to nine nine and they switch over to letters. And it's AA all the way to FF. And so it's very similar to this last representation for cyan except we have these different values and a different syntax. So it starts with this hashtag or pound sign. And then zero R value is going to be represented as zero zero and then both GNB have two Fs and alpha as well to have the full effect of both green and blue and alpha. So although you might see the RGB format a lot, it can be a little bit unenjoy ed. Intuitive. So this teal color on the left is going to be RGB 39204199. And then if we wanted to shift this color over to orange but have the same brightness and saturation, we're going to have a completely different set of numbers. So 204118 and 41. And this isn't exactly how we as humans think about colors. So that's where this next color space comes in. So the next one we're going to talk about is HSL where the H stands for hue, the S stands for saturation and the L stands for lightness. So for hue, we're talking about a circular color wheel and it's corresponding to the angle around the color wheel. So zero degrees is red. And then as we go around the color wheel, we go through orange and yellow all the way back to red. So this first value goes from zero to 360 because that's the number of degrees in a circle. Saturation is how saturated the color is. So at zero it starts at kind of like a gray, like there's no color here, all the way to 100 which is a fully saturated color. So pastels are going to be, they're going to have a lower saturation value. And then L for lightness, it's pretty similar except it starts at zero which is black and it goes to 100% which is white. So that's how light is this color. And the same as RGB, we have that optional alpha channel. So it's going to be either HSL and then there's numbers and parentheses or HSLA including that fourth value. You can see that it's a little bit more intuitive when we use this color space. If we have this teal and kind of this same color but moved around to an orange, the only number that we need to change here is that first number which changes the hue. So this can be a little bit more intuitive to work with. One of the issues with this color space that we'll see coming up is that as humans we don't visually perceive different colors the same way. So HCL is very similar to HSL where H is hue and L is lightness but in the middle we have C which is chroma which is actually pretty similar to saturation but just a different representation of colorfulness. So this is all going to be the same except for this middle C. And the difference here is that when we look at, this is the HSL color space, say we're looking at all of the hues with the same lightness and saturation, we can see these bands around yellow and cyan and purple that look a lot brighter to our visual system. Whereas if we look at the HCL color space we don't see those bands anymore. And that's because this is kind of like the mathematically correct way to go around the color wheel and with HCL we're taking into account the way we perceive colors and kind of getting rid of those bands where yellow looks a lot brighter to us than blue . So we're going to over compensate for that and have this different color space. If we look at them in 3D, this is the HSL color space. It's a perfect cylinder where hue goes around the circle, saturation increases as you move out and lightness increases as you go up and you can not super important to understand this but you can see that it's a full cylinder. And if we look at the HCL color space with the same visual where hue goes around the circle and the look from increases as you go out and lightness increases as you go up, we can see that it doesn't fill out the entire cylinder and that's because it's kind of just taking into account the way we perceive different colors.