Sass Color Functions
Sass color functions can be divided into three parts: color setting, color retrieval, and color manipulation.
The following table lists Sass's color functions:
Sass Color Settings
| function | Description & Examples |
|---|---|
| rgb(red, green, blue) | Create a Red-Green-Blue (RGB) color. R stands for "red", G stands for "green", and B stands for "blue". |
**实例:**
rgb(0, 0, 255); |
| rgba(red, green, blue, alpha) | Create a color based on red, green, blue, and transparency values.
**实例:**
rgba(0, 0, 255, 0.3); |
| hsl(hue, saturation, lightness) | Create a color using hue, saturation, and lightness values.
**实例:**
hsl(120, 100%, 50%); // 绿色
hsl(120, 100%, 75%); // 浅绿色
hsl(120, 100%, 25%); // dark green
hsl(120, 60%, 70%); // 柔和的绿色 |
| hsla(hue, saturation, lightness, alpha) | Create a color using hue, saturation, lightness, and alpha values.
**实例:**
hsl(120, 100%, 50%, 0.3); // 绿色带有透明度
hsl(120, 100%, 75%, 0.3); // 浅绿色带有透明度 |
| grayscale(color) | Convert a color to gray, equivalent to desaturate(color, 100%).
**实例:**
grayscale(#7fffd4);
结果: #c6c6c6 |
| complement(color) | Return a complementary color, equivalent to adjust-hue($color, 180deg).
**实例:**
complement(#7fffd4);
结果: #ff7faa |
| invert(color, weight) | Return an inverted color, with red, green, and blue values reversed while transparency stays unchanged.
**实例:**
invert(white);
结果: black |
Sass Color Retrieval
| function | Description & Examples |
|---|---|
| red(color) | Get the red value (0-255) from a color. |
**实例:**
red(#7fffd4);
结果: 127
red(red);
结果: 255 |
| green(color) | Get the green value (0-255) from a color.
**实例:**
green(#7fffd4);
结果: 255
green(blue);
结果: 0 |
| blue(color) | Get the blue value (0-255) from a color.
**实例:**
blue(#7fffd4);
结果: 212
blue(blue);
结果: 255 |
| hue(color) | Return the angle value (0deg - 255deg) of the color in HSL.
**实例:**
hue(#7fffd4);
结果: 160deg |
| saturation(color) | Get the saturation value (0% - 100%) of a color.
**实例:**
saturation(#7fffd4);
结果: 100% |
| lightness(color) | Get the lightness value (0% - 100%) of a color.
**实例:**
lightness(#7fffd4);
结果: 74.9% |
| alpha(color) | Returns the alpha channel of color as a number between 0 and 1.
**实例:**
alpha(#7fffd4);
结果: 1 |
| opacity(color) | Get the color's transparency value (0-1).
**实例:**
opacity(rgba(127, 255, 212, 0.5);
结果: 0.5 |
Sass Color Manipulation
| function | Description & Examples |
|---|---|
| mix(color1, color2, weight) | Mix two colors together. weight The parameter must be between 0% and 100%. The default weight is 50%, meaning the new color is 50% of color1 plus 50% of color2. If weight is 25%, the new color is 25% of color1 plus 75% of color2. |
| adjust-hue(color, degrees) | Create a new color by changing a color's hue value (-360deg - 360deg). |
**实例:**
adjust-hue(#7fffd4, 80deg);
结果: #8080ff |
| adjust-color(color, red, green, blue, hue, saturation, lightness, alpha) | This function can adjust one or more property values of a given color, including the RGB and HSL color value parameters, as well as the alpha channel value. The adjustment depends on the passed key-value parameters, which are then added to or subtracted from the corresponding color values.
**实例:**
adjust-color(#7fffd4, blue: 25);
结果: |
| change-color(color, red, green, blue, hue, saturation, lightness, alpha) | Similar to adjust-color above, except the passed parameters directly replace the original values without any calculation.
**实例:**
change-color(#7fffd4, red: 255);
结果: #ffffd4 |
| scale-color(color, red, green, blue, saturation, lightness, alpha) | Another practical color adjustment function. adjust-color simply adds or subtracts the passed parameters from its own color value parameters, which can sometimes cause overflow; however, the function keeps the result within valid thresholds. The scale-color function avoids this situation, allowing parameters to be adjusted effectively within the threshold range without worrying about overflow.
举个例子,一个颜色的亮度 lightness 取值在 0% ~ 100% 之间,假如执行 scale-color($color, $lightness: 40%),表明该颜色的亮度将有 (100 - 原始值) × 40% 的增幅。
另一个例子,执行 scale-color($color, $lightness: -40%),表明这个颜色的亮度将减少 (原始值 - 0) × 40% 这么多的值。
所有传参的取值范围都在 0% ~ 100% 之间,并且 RGB 同 HSL 的传参不能冲突。
scale-color(hsl(120, 70%, 80%), $lightness: 50%) => hsl(120, 70%, 90%) scale-color(rgb(200, 150, 170), $green: -40%, $blue: 70%) => rgb(200, 90, 229) scale-color(hsl(200, 70%, 80%), $saturation: -90%, $alpha: -30%) => hsla(200, 7%, 80%, 0.7) | | rgba(color, alpha) | Creates a color from red, green, blue, and alpha values.
**实例:**
rgba(#7fffd4, 30%);
结果: rgba(127, 255, 212, 0.3) |
| lighten(color, amount) | Lightens a color by changing its lightness value (0% - 100%) to create a new color. | | darken(color, amount) | Darkens a color by changing its lightness value (0% - 100%) to create a new color. | | saturate(color, amount) | Increases the saturation of the passed-in color. Equivalent to adjust-color( color, saturation: amount) | | desaturate(color, amount) | Produces a new color value after lowering a color's saturation. Similarly, the saturation range is 0% ~ 100%. Equivalent to adjust-color(color, saturation: -amount) | | opacify(color, amount) | Reduces the opacity of a color, with values between 0-1. Equivalent to adjust-color(color, alpha: amount) | | fade-in(color, amount) | Reduces the opacity of a color, with values between 0-1. Equivalent to adjust-color(color, alpha: amount) | | transparentize(color, amount) | Increases the opacity of a color, with values between 0-1. Equivalent to adjust-color(color, alpha: -amount) | | fade-out(color, amount) | Increases the opacity of a color, with values between 0-1. Equivalent to adjust-color(color, alpha: -amount) |
Sass Functions