02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Working with advanced color systems

In the last lecture we spent a lot of time to focus on understanding how RGA works. In this lecture we dive even deeper into the colors and how they work in CSS. In the process, we will learn how to control colors and alpha channels using RGBA, HSL and HSLA.

Video Transcript

00:00We're still talking about colors in the last lecture
00:03We met our GBA red green blue and we've talked about the standard ways to define colors in CSS
00:10Which are widely acceptable throughout the web in this lecture
00:14We're going to talk about more modern color ways such as alpha transparency and other things that we're just not available
00:21Historically in CSS now with that said it's not as widely supported
00:24So you want to make sure that if you're going to use this that you're fine with having or a backup color in case that color
00:30Does not work and making sure that that transparency whatever you're doing is not critical for your application in case someone's on an older browser
00:38So let's see those attributes. Let's jump right into it
00:42deliberately I cut our lecture last lecture and that was because
00:47everything we're talking about here starting from HSL is
00:51Kind of modern web only this did not exist in older browsers
00:56So if you're running or you need to support for example
00:59IE 8 or IE 7 or such then these type of usages will probably something that you would want to avoid
01:07Without further ado, I'm not going to go into every single detail about what is supported and what is not supported
01:12That's going to be part of your job as testing your applications based on who are your users
01:18That's something you're gonna have to ask yourself and we have titles about that and we'll have many more about how to configure and design
01:24Your site based on who your users are
01:27but let's jump right into the code and
01:30Let's talk about this
01:31So I want to really go into the nitty-gritty of how HSL works and it stands for as we said use saturation and lightness
01:40now for I'm gonna go ahead and just set a hundred percent in my
01:44Saturation and lightness so we could just focus for a second on the first property now this two second properties are percentages
01:51while the first property is a value of
01:54degrees really between 0 to
01:57360 now 0 and
02:00360 as in a circle are basically the same value. It's basically a closed cycle
02:06Which basically is red so red is 0 or 300 and oh, let me just make sure that I save it
02:14Let me make sure that I didn't add here anything else anywhere, which I did not and let me just save it one more time
02:20Oh
02:21What I did is basically I put well, you know
02:24Let me just set this for a second here at 5050 here
02:27Just for a second as we focus on the first property and forgive me for that mistake
02:32So let's pretend it never happened. Now what what I really want to do is
02:37I'm gonna go ahead go ahead here and just play with a few of the configurations just so we could ignore them
02:43Really and I'll talk about them in a second. So I'm just trying to find that right match. So we'll get that perfect red
02:49Here we go. We got our perfect red that
02:52360 so if I set my value here at 360 or 0 I would get basically complete red
02:58Which is the exact same value now if I set the value to be 120
03:03I'm gonna change it up to be green because RGB red green blue. So I have your red we move to green at 120 and
03:12We move from green to blue at a hundred to four to forty we move to the blue
03:18realm
03:20Alright, so that's our RGB and and and now let's focus on our second property
03:25So really it's a color combination between them as you saw I had to tweak here a bit to make sure that my colors will
03:30Come out the way. I really wanted them to come out the second property
03:35describes the amount of
03:37Saturation now the amount of saturation if I set the saturation to be zero
03:41I'm more or less basically
03:43Basically removing color out of it and a saturation of zero would basically
03:48Mute it out mute that color out or really turn it in to be a gray image
03:53So it's really taking all the color out of it turning it into gray
03:57So on a contrary if I set it to be a hundred
04:00I'm basically having it a full saturation if I wanted to have my color in less of a saturation
04:07Then I would play around with the value from zero where there was no saturation
04:11All the way up to how much saturation I want so for example, I set it to be 50 then I would get here a semi
04:18saturated blue
04:20All right. Now if you've ever worked with a color wheel, this is exactly how a color wheel works as well
04:25We're only doing it programmatically
04:27The next character the next property that we're gonna control is the lightness where if we set a lightness of no lightness
04:34Then really what we're doing is we're setting our screen to be completely black
04:38But if we set our lightness to be a hundred then we're setting it to be completely white. So really it's how much white or
04:46Darkness are we injecting into the color?
04:49So for example if we wanted to be blue but we wanted to be a darker blue
04:53We would have set it to maybe 40% giving it a little bit of darkness
04:58If we want it to be even darker
05:00Then I would go ahead and make it a really dark blue by maybe setting it to be 25 basically mixing
05:0520 basically 50% because from 0 to 50 is black
05:09So I'm basically mixing something like 50% of blackness into that color making it darker
05:15Now on the contrary if I wanted my blue to be really light
05:18I would play around the values from a little bit more light from 60
05:22so 50 and upwards would make it lighter adding more white into the color and
05:27Up to let's say I want it to be a really sharply red
05:30White or blue and I would go ahead and add that sharpness into it
05:34and literally that's how you play around with your colors when you're talking about the you saturation and
05:40Lightness now, there's one more property that I want to talk about. I'm gonna go ahead here and just copy this
05:46You saturation lightness. I'm just gonna put it right here and just as a recap. We're gonna say that there are
05:53Properties we said who are gonna are you?
05:56And the u-value could be between 0 and 360
06:01360 and we said that 0 is red and we said 120 is RGB. It's green and
06:10240 is blue. We also said that art for our saturation
06:17We said the values are basically about how much grayness or really so our grayness could be from 0%
06:26all the way to 100% when we said we're zero is really totally gray and
06:35Let me just break a line here to make it easier to look at
06:40And we said a hundred percent. What is a hundred percent a hundred percent would basically be
06:45Totally vivid so totally saturated with the color that it's supposed to be
06:52The last property that we talked about we said is the lightness
06:57Now for the lightness the values again are values between zero and a hundred percent
07:02where between zero and fifty percent is shades of
07:08black and
07:09From fifty to a hundred percent is shades of white and there you go
07:17That's really the u-saturation in a nutshell now the next property
07:21I want to look at and there's two more that we're gonna really go over skim through really quickly is the
07:27Hsla and
07:29Hsla basically adds one more property which is an alpha channel and with my alpha channel
07:34I could put a value between zero and one in this case. I want to make it 50% opaque
07:40By setting it to be 50% opaque if I go back and I click on refresh
07:43We're gonna see that now my value itself notice how it's not
07:48It's not as strongly seen inside of our titles here
07:52But is very vividly visible in our text how it became lighter
07:56Now if I go ahead, and I just remove that or set it to be one for a second and click on refresh
08:00And let me just set these values just so they're a lot more easy to notice
08:05I'm gonna go ahead here and just set here a red a vivid red set it in full full capacity
08:11Click on refresh, so we'll have your a nice beautiful red now
08:15I'm gonna go ahead and just mark my element here
08:18Inspect it in Firefox, and I'm just gonna go ahead into my color and what I want to do is
08:22I'm gonna let me just grab this color here
08:25And I'm just gonna go ahead and just dynamically on the fly just change that value because I want you to be able to see it
08:31So I'm gonna go right in here, and I'm just gonna delete that
08:35I'm gonna put that value in and notice how when it's at zero there is no color if it's zero two
08:42Then there's more color to the text down here notice how for headlines
08:46They just appear in Firefox
08:48But if in for the text itself if it's at zero then it's invisible if it's one
08:53It's it's completely visible, but if I said it's zero dot five then my text itself very very vividly if we just look at the text
09:00Itself, and I said it maybe two zero
09:02Seven notice how it's getting sharper and sharper or lighter and lighter
09:10Contrary to our headlines which have an override that a headline is not going to have any alpha opacity
09:16So that's something to note inside of Firefox
09:19The next thing I want to talk about is as we stick to the same topic
09:22And I'm just gonna grab this and I'm just gonna put it right up here right underneath our HSL a
09:29sample that we've created earlier, and I'm just gonna put here just for the sake of clarity our alpha channel
09:36Has a value between zero and one now the next thing that I want to look at is
09:42I want to I want to go back into RGB and I just want to show you the RGBA
09:46We're not going to do too much about it, but it's basically as you could guess
09:50RGBA just enables you to add that extra channel enabling you to control the alpha of the RGB as well
09:57So I'm not gonna even show it. It's just another way to control colors with an alpha channel now
10:04What I do want to do is I'm gonna go ahead here
10:06And what I want to do is I want to find my background that I set for that
10:10Question background and instead of setting this value that I have right here. I'm gonna go ahead and convert it into RGBA
10:18So let's do that. How do we turn it into our GPA? The first value we have here is zero that remains the same
10:23So we'll actually we turn that into zero second value was full third value was full last value
10:29I want to put it 50% so it's gonna have a background alpha of 50% if I go ahead and click on refresh
10:35We should see now that our question box is now has a alpha opacity which is mixing it in with our red
10:41creating this odd ray and
10:45There we go, and if I set it to be a hundred percent there
10:49It's kind of weird because I think the color here was a completely different color, but that
10:54That's fine. Let's see how it works. Like if we set it to be complete
10:57So we see it's a nice complete blue and if I go ahead here and I set it to be zero
11:02Let's say zero dot eight
11:06Oopsie and
11:08If I do set it to zero dot eight and I click on refresher
11:11Then we'll have your a lighter color a lighter tone to our blue behind here
11:15Let's go ahead and do this also for our red channel and make it really less red
11:19So if we find our article background, I'm just gonna go ahead here and it's gonna be basically the exact opposite
11:26So for our background here, I'm gonna go ahead and just set it to be
11:30255 complete red then zero then zero and I'm gonna go ahead and make it like a really light red
11:37So it's zero dot one percent
11:39Sorry ten percent actually but zero dot one as the value and I'm just gonna save it go back into my application and click on refresh
11:46and now we're gonna see that our background is incredibly light and now we could see our other background behind it as we're creating our
11:52Element and look how much smoother it is and really in general as we wrap up this conversation
11:58We talked quite a lot about quite a lot of stuff that are more related to newer browsers
12:04Alpha channels always add
12:06Intensity to your application the less alpha channels you have the better
12:10So unless you have a reason to be able to see things behind your background don't use an alpha channel
12:16For example, if you were trying to create this color just use the color picker grab it and now you have the actual color
12:22Now you could go ahead and just instead of using an RGBA inside of your question
12:27You could literally just paste inside there the actual color that comes out of it
12:31If you don't need to see through it and now that I've done that if I click on refresh
12:35We're gonna see the exact same thing
12:36But it's gonna be a lot better on our processor when it comes to our the way that item is viewed
12:42So in general try to avoid alpha channels
12:45The more alpha channels you have the more intense it is to render the item and the more work your browser needs to do as things
12:53Are overlapping now, it's not a big deal if you need it from a design perspective, but it's good to know in general
12:58Don't overdo it with it, especially if you get into animations and more intricate designs
13:04So that's it about this topic and that's it about colors and we'll see you in the next lecture
13:08We'll remove on to a new property

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!