Manipulating Sass Colors With HSL And Opacity Functions - Vanseo Design |
Manipulating Sass Colors With HSL And Opacity Functions Posted: 02 Aug 2016 05:30 AM PDT One of my favorite aspects of Sass is how easy the functions make it to work with colors. When I've studied color as a designer I learned color in HSL. When I've worked with color in CSS, it's been either hexadecimal or rgb(a) values. The color functions in Sass helped me to bridge the gap between the different notations.
For the last few weeks, I've been talking about data types in Sass. I started with an overview before digging into the number data type and the string data type. Last week I began looking at colors in Sass and I'll finish today covering the remaining color functions I didn't get to last time. I covered the RGB functions last week. This week I'll walk you through the HSL functions, the opacity functions, and a few other functions that don't fall under any of the other groups. HSL Color FunctionsSass includes a larger set of HSL functions than it does RGB functions, though don't worry if you don't work with HSL colors much. Aside from the functions for setting HSL colors, you can use any type of color notation with these functions. You can set an HSL color using either the hsl($hue, $saturation, $lightness) or hsla($hue, $saturation, $lightness, $alpha) functions.
This would lead to the same horrible red on green combination for paragraphs as in my example from last week. The same way Sass provides functions to get the red, green, or blue component of RGB colors, it provides three functions for getting each of the three components of HSL colors. Use the hue($color), saturation($color), and/or lightness($color) functions to determine the specific component you want to know. I used all three myself a moment ago to determine the HSL value for green.
Where the RGB functions give you one way to actually manipulate an existing color, the HSL functions give you eight. You can adjust the hue of any color using the adjust-hue($color, $degrees) function. You can specify the color using any notation and you can add the deg unit or not to the $degrees value. Sass will ignore whatever units you add and use degrees instead. Try adjusting a hue by 120px and 120% to see what I mean.
compiles to:
Sass offers two functions to manipulate the saturation of a color. saturate($color, $amount) will increase the saturation by $amount and desaturate($color, $amount) will reduce the saturation by $amount. With either function $color can be a named color, a hexadecimal color, an rgb color or an hsl color. $amount must be a number
compiles to:
There are also two functions to manipulate the lightness value of a color. lighten($color, $amount) increase the lightness by $amount and darken($color, $amount) decreases the lightness by $amount.
which compiles to:
Even though I used hsl() colors in the last few examples, you can work with a named color, hex colors, or rgb colors if you prefer. I used hsl to make it clearer what value is being changed. You can covert any color to grayscale using the grayscale($color) function
compiles to:
The grayscale function works by removing all saturation from the color so it's equivalent to using desaturate(color, 100%) if you prefer the desaturate function. Either can be a great way to test the accessibility of your color scheme. You can find the complement of a color using the complement($color) function.
which compiles to:
Since the complement of any color is 180 degrees away on the color wheel, you could use adjust-hue(color, 180deg) to do the same thing. I think the complement function makes it clearer what you're doing, but you can certainly use either. Finally you can use the invert($color) function to find the inverse of a color, which is the color that most contrasts with the initial color. Sometimes this will be the same as the complementary color as in the case of red and cyan, but not always.
which compiles to:
Opacity FunctionsSass provides seven opacity functions. The first two functions get the alpha component of a color and the five others allow you to manipulate the alpha component or a color. To get the alpha component use either the alpha($color) or opacity($color) functions. Both do the same thing. Each takes a color (in any notation) and returns the alpha component
The first function to change the alpha component, rgba($color, $alpha), might look like the RGB function to set a color, but note the parameters, $color and $alpha as opposed to parameters for the red, green, and blue color components.
which compiles to:
You can make a color more opaque using either the opacify($color, $amount) or fade-in($color, $amount) functions. Both functions take a color in any notation and an amount between 0.0 and 1.0 and increase the opacity.
You can do the reverse and make a color more transparent using either the transparentize($color, $amount) or fade-out($color, $amount) functions.
Other Color FunctionsSass also comes with four built-in functions for manipulating color that don't fall under one of the previous categories so they're grouped together as other functions. The adjust-color($color, [$red], [$green], [$blue], [$hue], [$saturation], [$lightness], [$alpha]) function increases or decreases one or more components of a color. It takes a lot of optional parameters as you can see.
The function is a one stop function to adjust all the different components of a color.
The change-color($color, [$red], [$green], [$blue], [$hue], [$saturation], [$lightness], [$alpha]) function looks similar to adjust-color() and it takes the same parameters however it works a little differently in that it doesn't adjust the color component, but resets it to the specified value.
You can see the difference comparing the adjust-color examples above to the change-color examples below. Note that because change-color() sets the color, negative values aren't allowed, except for the hue which can be a negative number.
The scale-color($color, [$red], [$green], [$blue], [$saturation], [$lightness], [$alpha]) is another similar looking function, that does something different. The idea with scale-color() is to adjust the colors in a more fluid way. Where adjust-color alters color components by a fixed amount, scale-color() adjusts them based on how high or low they already are. For example:
The lightness was 40% in the initial color and the $lightness parameter is set to 50%. That tells the function that the resulting lightness value should be 50% of the way between the initial value of 40% and it's maximum value of 100% All the parameters, other than the color being manipulated accept only values between –100% and 100%.
Because of the way it works, scale-color has a gradation effect where adjust-color has a step effect. The final function ie-hex-str($color) converts a color into a format understood by Internet Explorer filters.
It's probably not the most useful function unless you find yourself working with a lot of IE filters. I've never used it myself and don't expect I will any time soon. Closing ThoughtsAdding or subtracting colors might be a nice thing to do, I suspect the main way you'll manipulate colors in Sass is through the different color functions. When I first learned about Sass' color functions my use of Sass immediately increased. I now often create color schemes using one to three different values and then use different functions to create variations of the initial colors. If you want you can create color schemes based on mathematical proportions, taking one color and lightening it or darkening by 10%, 20%, 30%, etc. Next week I want to continue and talk about the list data type. Lists are similar to arrays and they're how Sass represents any series of values regardless of the data type of the values. Download a free sample from my book Design Fundamentals. Join me as I share my creative process and journey as a writer. This posting includes an audio/video/photo media file: Download Now |
You are subscribed to email updates from Vanseo Design. To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 1600 Amphitheatre Parkway, Mountain View, CA 94043, United States |