{"id":1443,"date":"2010-08-28T08:11:59","date_gmt":"2010-08-28T12:11:59","guid":{"rendered":"http:\/\/www.useragentman.com\/blog\/?p=1443"},"modified":"2011-05-14T00:42:22","modified_gmt":"2011-05-14T04:42:22","slug":"coding-colors-easily-using-css3-hsl-notation","status":"publish","type":"post","link":"http:\/\/www.useragentman.com\/blog\/2010\/08\/28\/coding-colors-easily-using-css3-hsl-notation\/","title":{"rendered":"Coding Colors Easily Using CSS3 <code>hsl()<\/code> Notation"},"content":{"rendered":"<div class=\"importantNotes\">\n<h3>Update: Jan 6, 2011<\/h3>\n<p>Check out Paul Irish&#8217;s <a href=\"http:\/\/mothereffinghsl.com\/\">Mother Effing HSL<\/a> for a nice color-picker you can play with to get the hang of HSL.<\/p>\n<\/div>\n<div >\n<dl id=\"attachment_822\" class=\"wp-caption alignleft\" style=\"width: 267px;\">\n<dt class=\"wp-caption-dt\"><img decoding=\"async\" class=\"size-full wp-image-822 \" title=\"CSS3 Cube Layout Example\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2010\/08\/colorWheel.jpg\" alt=\"\" width=\"257\" \/><\/dt>\n<dd class=\"wp-caption-dd\">\n<div>A remix of <a href=\"http:\/\/commons.wikimedia.org\/wiki\/File:Bezold_Farbentafel_1874.jpg\">Wilhelm von Bezold&#8217;s Farbentafel Color Wheel<\/a>.<\/div>\n<\/dd>\n<\/dl>\n<\/div>\n<p>I have heard stories about <strong>&#8220;<em>those<\/em> people&#8221;<\/strong> that see a color and can immediately calculate the RGB hex code in their head, without any tool whatsoever. Since I have never actually met one of <strong>&#8220;<em>those<\/em> people&#8221;<\/strong>, I don&#8217;t think they really exist.  However, it <strong>would<\/strong> be a nice skill to have, especially when presenting a new design to a client and they say &#8220;Perfect!  But can make that box a brighter orange, and that do-hickey a less pronounced purple?  And can you show us how it would look <strong>right now?<\/strong>&#8221;   <\/p>\n<p>This seemingly impossible task of coming up with color codes off the top of your head can be done using CSS3&#8217;s <code>hsl<\/code> color notation.  <strong>HSL is a much more &#8220;human-friendly&#8221; notation than RGB<\/strong>, and with it you can code colors in your head easily <strong>without using The GIMP&#8217;s or Photoshop&#8217;s color-wheel<\/strong>.<\/p>\n<p>Don&#8217;t believe me?  Read on and I&#8217;ll show you.  You&#8217;ll never want to go back to RGB again.  And even though IE 6-8 doesn&#8217;t support HSL natively, we will cover how support can be added.<\/p>\n<h2>Notation<\/h2>\n<p>An HSL color code uses the following CSS3 notation:<\/p>\n<blockquote class=\"code\">\n<pre>#myObject {\r\n    background: <span class=\"hilite\">hsl(120, 50%, 50%);<\/span>\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>The first number is the <strong>hue<\/strong>, the second number is the <strong>saturation<\/strong>, and the third is the <strong>lightness<\/strong>.  These three terms are what HSL stand for.  But how does one manipulate these numbers?  Easily.<\/p>\n<h2>Step 1: &#8220;Young Guys Can Be Messy Rascals&#8221;<\/h2>\n<div id=\"colorwheel\">\n<div class=\"testBlock\"  id=\"o1\">\n\t\t\t\t<span><strong>R<\/strong>ed: 360&deg;<\/span>\n\t\t\t<\/div>\n<div class=\"testBlock\"  id=\"o2\">\n\t\t\t\t<span><strong>Y<\/strong>ellow: 60&deg;<\/span>\n\t\t\t<\/div>\n<div class=\"testBlock\"  id=\"o3\">\n\t\t\t\t<span><strong>G<\/strong>reen: 120&deg;<\/span>\n\t\t\t<\/div>\n<div class=\"testBlock\"  id=\"o4\">\n\t\t\t\t<span><strong>C<\/strong>yan: 180&deg;<\/span>\n\t\t\t<\/div>\n<div class=\"testBlock\"  id=\"o5\">\n\t\t\t\t<span><strong>B<\/strong>lue: 240&deg;<\/span>\n\t\t\t<\/div>\n<div class=\"testBlock\"  id=\"o6\">\n\t\t\t\t<span><strong>M<\/strong>agenta: 300&deg;<\/span>\n\t\t\t<\/div>\n<p>This diagram is not an image.  It was generated with HTML using <a href=\"\/blog\/2010\/03\/09\/cross-browser-css-transforms-even-in-ie\/\">CSS3 <code>transform<\/code> functions<\/a> and <code>hsl()<\/code> color notation, and cssSandpaper for browsers that don&#8217;t support it.<\/p>\n<\/p><\/div>\n<p><strong>Memorize this sentence.<\/strong>  If you do, you will be to remember the six major colors in the HSV color wheel: Yellow, Green, Cyan, Blue, Magenta and Red.  <strong>These colors are spaced out by angles of 60 degrees.<\/strong>  If you follow the mnemonic, you should be able to remember the diagram on the left very easily.<\/p>\n<p>So, if you want to pick green, you would code:<\/p>\n<blockquote class=\"code smaller\">\n<pre>hsl(<span class=\"hilite\">120<\/span>, 100%, 50%)<\/pre>\n<\/blockquote>\n<p>(Don&#8217;t worry about the second and third parameters for now &#8230; we will get to that in a second).<\/p>\n<p>Blue would be:<\/p>\n<blockquote class=\"code smaller\">\n<pre>hsl(<span class=\"hilite\">240<\/span>, 100%, 50%)<\/pre>\n<\/blockquote>\n<p>Let&#8217;s say you wanted a color that is not on the wheel, like purple.  Purple is halfway between blue (240&deg;) and magenta (300&deg;) so we should pick something in between:<\/p>\n<div class=\"example purple\">background-color: hsl(270, 100%, 50%)<\/div>\n<p>Think it should be more blue?  Choose an angle closer to blue!<\/p>\n<div class=\"example bluePurple\">background-color: hsl(255, 100%, 50%);<\/div>\n<p>Want more magenta instead?  Then choose angle closer to magenta:<\/p>\n<div class=\"example magentaPurple\">background-color: hsl(285, 100%, 50%);<\/div>\n<p>Note that you do not put use the <code>deg<\/code> keyword link in CSS3 Transforms.  It seems a little inconsistent, but at least it&#8217;s less typing. <\/p>\n<h2>Step 2: Choose Your Intensity<\/h2>\n<p>The second parameter is the colorfulness, or <a href=\"http:\/\/en.wikipedia.org\/wiki\/Colorfulness\">saturation<\/a>.  The larger the percentage, the more &#8220;colorful&#8221; this color is.  Let&#8217;s take a look at what happens when we change green&#8217;s (120&deg;) colorfulness:<\/p>\n<table id=\"colorfulnessTable\" class=\"dataTable\">\n<thead>\n<tr>\n<th>hsl(120, 0%, 50%)<\/th>\n<th>hsl(120, 25%, 50%)<\/th>\n<th>hsl(120, 50%, 50%)<\/th>\n<th>hsl(120, 75%, 50%)<\/th>\n<th>hsl(120, 100%, 50%)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"c1\"><\/td>\n<td class=\"c2\"><\/td>\n<td class=\"c3\"><\/td>\n<td class=\"c4\"><\/td>\n<td class=\"c5\"><\/td>\n<\/tr>\n<\/table>\n<p>The more the saturation percentage, the less &#8220;gray&#8221; it is.<\/p>\n<h2>Step 3: Lighten it up<\/h2>\n<p>The final step is to lighten up the color to the right level.  That can be done with the final parameter: <\/p>\n<table id=\"brightnessTable\" class=\"dataTable\">\n<thead>\n<tr>\n<th>hsl(120, 50%, 0%)<\/th>\n<th>hsl(120, 50%, 25%)<\/th>\n<th>hsl(120, 50%, 50%)<\/th>\n<th>hsl(120, 50%, 75%)<\/th>\n<th>hsl(120, 50%, 100%)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"c1\"><\/td>\n<td class=\"c2\"><\/td>\n<td class=\"c3\"><\/td>\n<td class=\"c4\"><\/td>\n<td class=\"c5\"><\/td>\n<\/tr>\n<\/table>\n<h2>That&#8217;s It!<\/h2>\n<p>Try it when you are using color on your next project.  <strong>You&#8217;ll find using the HSL color-space is <em>much<\/em> easier than using RGB<\/strong><\/p>\n<h2>Browser Support<\/h2>\n<p>HSL is supported in almost all the major browsers: Firefox 3.0+, Safari 3.0+, Chrome 5.0+, Opera 10+ and Explorer 9.0+.  <strong>For the browsers that don&#8217;t support it, I have updated <a href=\"\/blog\/2010\/03\/09\/cross-browser-css-transforms-even-in-ie\/\">cssSandpaper<\/a> to implement HSL with the CSS <code>color<\/code> property<\/strong>, as well as the <code>border<\/code>, <code>background<\/code> family of properties (i.e. <code>border-right<\/code>, <code>background-color<\/code>, etc).<\/p>\n<p>Also supported by cssSandpaper is HSLA, which is to HSL, as RGBA is to RGB.  It contains a fourth element, the alpha channel, that is a number from 0 to 1.  To illustrate how this works, let&#8217;s say an HTML element has a grid as a background image.  How can you shade that background image so that it looks, say, more green?<\/p>\n<table id=\"alphaTable\" class=\"dataTable\">\n<thead>\n<tr>\n<th>hsla(120, 50%, 50%, 0.25)<\/th>\n<th>hsla(120, 50%, 50%, 0.5)<\/th>\n<th>hsla(120, 50%, 50%, 0.75)<\/th>\n<th>hsla(120, 50%, 50%, 1)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"c2\"><\/td>\n<td class=\"c3\"><\/td>\n<td class=\"c4\"><\/td>\n<td class=\"c5\"><\/td>\n<\/tr>\n<\/table>\n<p>The table above is not an image &mdash; it is a real example of <code>hsla()<\/code> in action. Pretty neat, huh?  You can also use HSL and HSLA color in gradients.  The <a href=\"\/blog\/2010\/03\/09\/cross-browser-css-transforms-even-in-ie\/\">cssSandpaper<\/a> library has supported WebKit style gradients with it&#8217;s own <code>-sand-gradient<\/code> property.  Now it supports HSLA gradients, like this 80% opaque yellow to 20% opaque blue linear gradient:<\/p>\n<table id=\"gradientTable\" class=\"dataTable\">\n<thead>\n<tr>\n<th>\t-sand-gradient(linear, center top, center bottom, from(hsla(60, 100%, 50%, 0.8)), to(hsla(255, 100%, 50%, 0.2)));\n<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td class=\"c1\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Unfortunately, cssSandpaper only supports HSLA for background coloring only, not border or text coloring, but one would just use progressive enhancement to take care of that edge case, since what it does support, in my humble opinion, is rather cool.  Download the latest version from the link below and use HSL and HSLA color notation now!  You&#8217;ll impress your developer friends with your off-the-top-of-your-head color-coding skillz.  :-)<\/p>\n<p><a class=\"exampleLink\" href=\"\/blog\/2010\/03\/09\/cross-browser-css-transforms-even-in-ie\/\">Get the latest version of cssSandpaper from the official documentation page<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/www.useragentman.com\/blog\/wp-content\/uploads\/2010\/08\/colorWheelFront.png\" \/> The seemingly impossible task of coming up with color codes off the top of your head can be done easily using CSS3&#8217;s <code>hsl<\/code> color notation.  Read how you can use this &#8220;human-friendly&#8221; and how it can work in the few browsers that don&#8217;t support it natively.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123,9,38,122,119],"tags":[],"class_list":["post-1443","post","type-post","status-publish","format-standard","hentry","category-color","category-css","category-css3","category-hslhsla","category-polyfills"],"_links":{"self":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/1443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/comments?post=1443"}],"version-history":[{"count":66,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"predecessor-version":[{"id":3280,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions\/3280"}],"wp:attachment":[{"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.useragentman.com\/blog\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}