13.15. Sass color function

发布时间 : 2025-10-25 13:35:45 UTC      

Page Views: 9 views

The Sass color function can be divided into three parts: color setting, color acquisition and color operation.

The following table lists the color functions for Sass:

13.15.1. Sass color setting

Function

Description & example

rgb(red, green, blue)

Create a Red-Green-Blue (RGB) color. Where R is “red” for red, G is “green” green, and B is “blue” blue.

Example: rgb (0,0,255)

rgba(red, green, blue, alpha)

Create a color based on red, green, blue, and transparency values.

Example: rgba (0,0,255,0.3)

hsl(hue, saturation, lightness)

Create a color from the values of hue, saturation, and luminance (lightness).

Example: hsl (120,100%, 50%); // green hsl (120,100%, 75%); // light greenhsl (120,100%, 25%); // dark green hsl (120,60%, 70%); // soft green

hsla(hue, saturation, lightness, alpha)

Create a color from the values of hue, saturation, luminance (lightness), and transparency (alpha).

Example: hsl (120,100%, 50%, 0.3); // Green with transparency hsl (120,100%, 75%, 0.3); // Light green with transparency

grayscale(color)

Turning a color to gray is equivalent to desaturate (color,100%).

Example: grayscale (# 7fffd4); result: # c6c6c6

complement(color)

Returns a complementary color equivalent to adjust-hue ($color,180deg).

Example: complement (# 7fffd4); result: # ff7faa

invert(color, weight)

Returns an inverted color with red, green, and blue values reversed, while the transparency remains the same.

Example: invert (white); result: black

13.15.2. Sass color acquisition

Function

Description & example

red(color)

Gets the red value (0-255) from a color.

Example: red (# 7fffd4); result: 127red (red); result: 255th

green(color)

Gets the green value (0-255) from a color.

Example: green (# 7fffd4); result: 255green (blue); result: 0

blue(color)

Gets the blue value from a color (0-255).

Example: blue (# 7fffd4); result: 212 blue (blue); result: 255

hue(color)

Returns the angle value of the color in the HSL color value (0deg-255deg).

Example: hue (# 7fffd4); result: 160deg

saturation(color)

Gets the saturation value of a color (0%-100%).

Example: saturation (# 7fffd4); result: 100%

lightness(color)

Gets the luminance value of a color (0%-100%).

Example: lightness (# 7fffd4); result: 74.9%

alpha(color)

Returns the alpha channel of color as a number between 0 and 1.

Example: alpha (# 7fffd4); result: 1

opacity(color)

Gets the color transparency value (0-1).

Example: opacity (rgba (127,255,212,0.5); result: 0.5

13.15.3. Sass color operation

Function

Description & example

mix(color1, color2, weight)

Mix the two colors. The weight parameter must be 0 to 100%. The default weight is 50%, indicating that the new color takes 50% of the color values of color1 and color2. If weight is 25%, that means the new color is the sum of 25% color1 and 75% color2.

adjust-hue(color, degrees)

Create a new color by changing the hue value of a color (- 360deg-360deg).

Example: adjust-hue (# 7fffd4, 80deg); result: # 8080ff

adjust-color(color, red, green, blue, hue, saturation, ightness, alpha)

This function adjusts one or more attribute values of a given color, including the color value parameters of RGB and HSL colors, as well as the values of the alpha channel. The adjustment of these attribute values depends on the passed-in key value parameters, which are then added and subtracted from the corresponding color values of a given color.

Example: adjust-color (# 7fffd4, blue: 25); result:

change-color(color, red, green, blue, hue, saturation, lightness, alpha)

Similar to the adjust-color above, except that the parameters passed in this function will directly replace the original value without doing any operations.

Example: change-color (# 7fffd4, red: 255); result: # ffffd4

scale-color(color, red, green, blue, saturation, lightness, alpha)

Another practical color adjustment function. Adjust-color simply adds or subtracts from its own color parameters through the passed parameters, whichmay sometimes cause cumulative value overflows. Of course, the function will keep the result within a valid threshold. The scale-color function avoids this situation and allows the parameters to be effectively adjusted within the threshold range without having to worry about overflow.

For example, the luminance lightness of a color is between 0% and 100%. If you perform scale-color ($color, $lightness: 40%), the brightness of the color will increase by (100-original) × 40%.

Another example is to execute scale-color ($color, $lightness:-40%), indicating that the brightness of this color will be reduced by as much as (original value-0) x 40%.

The values of all passed parameters range from 0% to 100%, and the parameters passed by RGB and HSL cannot conflict.

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)

Create a color based on red, green, blue, and transparency values.

Example: rgba (# 7fffd4, 30%); result: rgba (127,255,212,0.3)

lighten(color, amount)

Create a new color by changing the luminance value of the color (0%-100%) to brighten the color.

darken(color, amount)

Darken the color and create a new color by changing the luminance value of the color (0%-100%).

saturate(color, amount)

Increase the color saturation of incoming colors. Equivalent to adjust-color (color, saturation: amount)

desaturate(color, amount)

Lower the saturation of a color to produce a new color value. Similarly, the saturation ranges from 0% to 100%. Equivalent to adjust-color (color, saturation:-amount)

opacify(color, amount)

Reduce the transparency of the color, with a value of 0-1. Equivalent to adjust-color (color, alpha: amount)

fade-in(color, amount)

Reduce the transparency of the color, with a value of 0-1. Equivalent to adjust-color (color, alpha: amount)

transparentize(color, amount)

Improve the transparency of the color, with a value between 0 and 1. Equivalent to adjust-color (color, alpha:-amount)

fade-out(color, amount)

Improve the transparency of the color, with a value between 0 and 1. Equivalent to adjust-color (color, alpha:-amount)

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。