Less 支持的所有内置的函数。

Edit the markdown source for "misc-functions"

color

解析颜色,将代表颜色的字符串转换为颜色值.

参数: string: a string of the specified color.

返回值: color

实例: color("#aaa");

输出结果: #aaa

convert

将数字从一种单位转换到另一种单位。

第一个参数为带单位的数值,第二个参数为单位。如果两个参数的单位是兼容的,则数字的单位被转换。如果两个参数的单位不兼容,则原样返回第一个参数。

参见 unit 不做转换的情况下改变单位

兼容的单位是

  • 长度: m, cm, mm, in, pt and pc,
  • 时间:s and ms,
  • 角度: rad, deg, grad and turn.

参数:

  • number: a floating point number with units.
  • identifier, string or escaped value: units

返回值: number

实例:

convert(9s, "ms")
convert(14cm, mm)
convert(8, mm) // incompatible unit types

输出结果:

9000ms
140mm
8

data-uri

Inlines a resource and falls back to url() if the ieCompat option is on and the resource is too large, or if you use the function in the browser. If the MIME type is not given then node uses the mime package to determine the correct mime type.

参数:

  • mimetype: (Optional) A MIME type string.
  • url: The URL of the file to inline.

实例: data-uri('../data/image.jpg');

输出结果: url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');

浏览器上输出:url('../data/image.jpg');

实例: data-uri('image/jpeg;base64', '../data/image.jpg');

输出结果: url('data:image/jpeg;base64,bm90IGFjdHVhbGx5IGEganBlZyBmaWxlCg==');

default

Available only inside guard conditions and returns true only if no other mixin matches, false otherwise.

实例:

.mixin(1)                   {x: 11}
.mixin(2)                   {y: 22}
.mixin(@x) when (default()) {z: @x}

div {
  .mixin(3);
}

div.special {
  .mixin(1);
}

输出结果:

div {
  z: 3;
}
div.special {
  x: 11;
}

It is possible to use the value returned by default with guard operators. For example .mixin() when not(default()) {} will match only if there's at least one more mixin definition that matches.mixin() call:

.mixin(@value) when (ispixel(@value)) {width: @value}
.mixin(@value) when not(default())    {padding: (@value / 5)}

div-1 {
  .mixin(100px);
}

div-2 {
  /* ... */
  .mixin(100%);
}

result:

div-1 {
  width: 100px;
  padding: 20px;
}
div-2 {
  /* ... */
}

It is allowed to make multiple default() calls in the same guard condition or in a different conditions of a mixins with the same name:

div {
  .m(@x) when (default()), not(default())    {always: @x}
  .m(@x) when (default()) and not(default()) {never:  @x}

  .m(1); // OK
}

However Less will throw a error if it detects a potential conflict between multiple mixin definitions using default():

div {
  .m(@x) when (default())    {}
  .m(@x) when not(default()) {}

  .m(1); // Error
}

In above example it is impossible to determine what value each default() call should return since they recursively depend on each other.

Advanced multiple default() usage:

.x {
  .m(red)                                    {case-1: darkred}
  .m(blue)                                   {case-2: darkblue}
  .m(@x) when (iscolor(@x)) and (default())  {default-color: @x}
  .m('foo')                                  {case-1: I am 'foo'}
  .m('bar')                                  {case-2: I am 'bar'}
  .m(@x) when (isstring(@x)) and (default()) {default-string: and I am the default}

  &-blue  {.m(blue)}
  &-green {.m(green)}
  &-foo   {.m('foo')}
  &-baz   {.m('baz')}
}

Result:

.x-blue {
  case-2: #00008b;
}
.x-green {
  default-color: #008000;
}
.x-foo {
  case-1: I am 'foo';
}
.x-baz {
  default-string: and I am the default;
}

The default function is available as a Less built-in function only inside guard expressions. If used outside of a mixin guard condition it is interpreted as a regular CSS value:

实例:

div {
  foo: default();
  bar: default(42);
}

Result:

div {
  foo: default();
  bar: default(42);
}

unit

Remove or change the unit of a dimension

参数:

  • dimension: A number, with or without a dimension.
  • unit: (Optional) the unit to change to, or if omitted it will remove the unit.

See convert for changing the unit without conversion.

实例: unit(5, px)

输出结果: 5px

实例: unit(5em)

输出结果: 5

get-unit

Returns units of a number.

If the argument contains a number with units, the function returns its units. The argument without units results in an empty return value.

参数:

  • number: a number with or without units.

实例: get-unit(5px)

输出结果: px

实例: get-unit(5)

输出结果: //nothing

svg-gradient

Generates multi-stop svg gradients.

Svg-gradient function generates multi-stop svg gradients. It must have at least three parameters. First parameter specifies gradient type and direction and remaining parameters list colors and their positions. The position of first and last specified color are optional, remaining colors must have positions specified.

The direction must be one of to bottom, to right, to bottom right, to top right, ellipse or ellipse at center. The direction can be specified as both escaped value and space separated list of words.

参数:

  • escaped value or list of identifiers: direction
  • color [percentage] pair: first color and its relative position (position is optional)
  • color percent pair: (optional) second color and its relative position
  • ...
  • color percent pair: (optional) n-th color and its relative position
  • color [percentage] pair: last color and its relative position (position is optional)

返回值: url with base64 encoded svg gradient.

实例:

div {
  background-image: svg-gradient(to right, red, green 30%, blue);
}

results in:

div {
  background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB2ZXJzaW9uPSIxLjEiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIHZpZXdCb3g9IjAgMCAxIDEiIHByZXNlcnZlQXNwZWN0UmF0aW89Im5vbmUiPjxsaW5lYXJHcmFkaWVudCBpZD0iZ3JhZGllbnQiIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIiB4MT0iMCUiIHkxPSIwJSIgeDI9IjEwMCUiIHkyPSIwJSI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2ZmMDAwMCIvPjxzdG9wIG9mZnNldD0iMzAlIiBzdG9wLWNvbG9yPSIjMDA4MDAwIi8+PHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjMDAwMGZmIi8+PC9saW5lYXJHcmFkaWVudD48cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWRpZW50KSIgLz48L3N2Zz4=')
}

Generated background image has red color on the left, green at 30% of its width and ends with a blue color. Base64 encoded part contains following svg-gradient:

<?xml version="1.0" ?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" viewBox="0 0 1 1" preserveAspectRatio="none">
    <linearGradient id="gradient" gradientUnits="userSpaceOnUse" x1="0%" y1="0%" x2="100%" y2="0%">
        <stop offset="0%" stop-color="#ff0000"/>
        <stop offset="30%" stop-color="#008000"/>
        <stop offset="100%" stop-color="#0000ff"/>
    </linearGradient>
    <rect x="0" y="0" width="1" height="1" fill="url(#gradient)" />
</svg>

Edit the markdown source for "string-functions"

escape

Applies URL-encoding to special characters found in the input string.

  • These characters are not encoded: ,, /, ?, @, &, +, ', ~, ! and $.
  • Most common encoded characters are: \<space\>, #, ^, (, ), {, }, |, :, >, <, ;, ], [ and =.

参数: string: a string to escape.

返回值: escaped string content without quotes.

实例:

escape('a=1')

输出结果:

a%3D1

Note: if the parameter is not a string, output is not defined. The current implementation returns undefined on color and unchanged input on any other kind of argument. This behavior should not be relied on and may change in the future.

e

CSS escaping, replaced with ~"value" syntax.

It expects string as a parameter and return its content as is, but without quotes. It can be used to output CSS value which is either not valid CSS syntax, or uses proprietary syntax which Less doesn't recognize.

参数: string - a string to escape.

返回值: string - the escaped string, without quotes.

实例:

filter: e("ms:alwaysHasItsOwnSyntax.For.Stuff()");

输出结果:

filter: ms:alwaysHasItsOwnSyntax.For.Stuff();

Note: The function accepts also ~"" escaped values and numbers as parameters. Anything else returns an error.

% format

The function %(string, arguments ...) formats a string.

The first argument is string with placeholders. All placeholders start with percentage symbol % followed by letter s,S,d,D,a, or A. Remaining arguments contain expressions to replace placeholders. If you need to print the percentage symbol, escape it by another percentage %%.

Use uppercase placeholders if you need to escape special characters into their utf-8 escape codes. The function escapes all special characters except ()'~!. Space is encoded as %20. Lowercase placeholders leave special characters as they are.

Placeholders:

  • d, D, a, A - can be replaced by any kind of argument (color, number, escaped value, expression, ...). If you use them in combination with string, the whole string will be used - including its quotes. However, the quotes are placed into the string as they are, they are not escaped by "/" nor anything similar.
  • s, S - can be replaced by any kind of argument except color. If you use them in combination with string, only the string value will be used - string quotes are omitted.

参数:

  • string: format string with placeholders,
  • anything* : values to replace placeholders.

返回值: formatted string.

实例:

format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less");
format-a-d-upper: %('repetitions: %A file: %D', 1 + 2, "directory/file.less");
format-s: %("repetitions: %s file: %s", 1 + 2, "directory/file.less");
format-s-upper: %('repetitions: %S file: %S', 1 + 2, "directory/file.less");

输出结果:

format-a-d: "repetitions: 3 file: "directory/file.less"";
format-a-d-upper: "repetitions: 3 file: %22directory%2Ffile.less%22";
format-s: "repetitions: 3 file: directory/file.less";
format-s-upper: "repetitions: 3 file: directory%2Ffile.less";

replace

Replaces a text within a string.

Released v1.7.0

参数:

  • string: The string to search and replace in.
  • pattern: A string or regular expression pattern to search for.
  • replacement: The string to replace the matched pattern with.
  • flags: (Optional) regular expression flags.

返回值: a string with the replaced values.

实例:

replace("Hello, Mars?", "Mars\?", "Earth!");
replace("One + one = 4", "one", "2", "gi");
replace('This is a string.', "(string)\.$", "new $1.");
replace(~"bar-1", '1', '2');

Result:

"Hello, Earth!";
"2 + 2 = 4";
'This is a new string.';
bar-2;

Edit the markdown source for "list-functions"

length

Returns the number of elements in a value list.

参数: list - a comma or space separated list of values. Returns: an integer number of elements in a list

实例: length(1px solid #0080ff); Output: 3

实例:

@list: "banana", "tomato", "potato", "peach";
n: length(@list);

输出结果:

n: 4;

extract

Returns the value at a specified position in a list.

参数: list - a comma or space separated list of values. index - an integer that specifies a position of a list element to return. Returns: a value at the specified position in a list.

实例: extract(8px dotted red, 2); Output: dotted

实例:

@list: apple, pear, coconut, orange;
value: extract(@list, 3);

输出结果:

value: coconut;

Edit the markdown source for "math-functions"

ceil

Rounds up to the next highest integer.

参数: number - a floating point number.

返回值: integer

实例: ceil(2.4)

输出结果: 3

floor

Rounds down to the next lowest integer.

参数: number - a floating point number.

返回值: integer

实例: floor(2.6)

输出结果: 2

percentage

Converts a floating point number into a percentage string.

参数: number - a floating point number.

返回值: string

实例: percentage(0.5)

输出结果: 50%

round

Applies rounding.

参数:

  • number: A floating point number.
  • decimalPlaces: Optional: The number of decimal places to round to. Defaults to 0.

返回值: number

实例: round(1.67)

输出结果: 2

实例: round(1.67, 1)

输出结果: 1.7

sqrt

Calculates square root of a number. Keeps units as they are.

参数: number - floating point number.

返回值: number

实例:

sqrt(25cm)

输出结果:

5cm

实例:

sqrt(18.6%)

输出结果:

4.312771730569565%;

abs

Calculates absolute value of a number. Keeps units as they are.

参数: number - a floating point number.

返回值: number

Example #1: abs(25cm)

输出结果: 25cm

Example #2: abs(-18.6%)

输出结果: 18.6%;

sin

Calculates sine function.

Assumes radians on numbers without units.

参数: number - a floating point number.

返回值: number

实例:

sin(1); // sine of 1 radian
sin(1deg); // sine of 1 degree
sin(1grad); // sine of 1 gradian

输出结果:

0.8414709848078965; // sine of 1 radian
0.01745240643728351; // sine of 1 degree
0.015707317311820675; // sine of 1 gradian

asin

Calculates arcsine (inverse of sine) function.

Returns number in radians e.g. a number between -π/2 and π/2.

参数: number - floating point number from [-1, 1] interval.

返回值: number

实例:

asin(-0.8414709848078965)
asin(0)
asin(2)

输出结果:

-1rad
0rad
NaNrad

cos

Calculates cosine function.

Assumes radians on numbers without units.

参数: number - a floating point number.

返回值: number

实例:

cos(1) // cosine of 1 radian
cos(1deg) // cosine of 1 degree
cos(1grad) // cosine of 1 gradian

输出结果:

0.5403023058681398 // cosine of 1 radian
0.9998476951563913 // cosine of 1 degree
0.9998766324816606 // cosine of 1 gradian

acos

Calculates arccosine (inverse of cosine) function.

Returns number in radians e.g. a number between 0 and π.

参数: number - a floating point number from [-1, 1] interval.

返回值: number

实例:

acos(0.5403023058681398)
acos(1)
acos(2)

输出结果:

1rad
0rad
NaNrad

tan

Calculates tangent function.

Assumes radians on numbers without units.

参数: number - a floating point number.

返回值: number

实例:

tan(1) // tangent of 1 radian
tan(1deg) // tangent of 1 degree
tan(1grad) // tangent of 1 gradian

输出结果:

1.5574077246549023   // tangent of 1 radian
0.017455064928217585 // tangent of 1 degree
0.015709255323664916 // tangent of 1 gradian

atan

Calculates arctangent (inverse of tangent) function.

Returns number in radians e.g. a number between -π/2 and π/2.

参数: number - a floating point number.

返回值: number

实例:

atan(-1.5574077246549023)
atan(0)
round(atan(22), 6) // arctangent of 22 rounded to 6 decimal places

输出结果:

-1rad
0rad
1.525373rad;

pi

Returns π (pi);

参数: none

返回值: number

实例:

pi()

输出结果:

3.141592653589793

pow

Returns the value of the first argument raised to the power of the second argument.

Returned value has the same dimension as the first parameter and the dimension of the second parameter is ignored.

参数:

  • number: base -a floating point number.
  • number: exponent - a floating point number.

返回值: number

实例:

pow(0cm, 0px)
pow(25, -2)
pow(25, 0.5)
pow(-25, 0.5)
pow(-25%, -0.5)

输出结果:

1cm
0.0016
5
NaN
NaN%

mod

Returns the value of the first argument modulus second argument.

Returned value has the same dimension as the first parameter, the dimension of the second parameter is ignored. The function is able to handle also negative and floating point numbers.

参数:

  • number: a floating point number.
  • number: a floating point number.

返回值: number

实例:

mod(0cm, 0px)
mod(11cm, 6px);
mod(-26%, -5);

输出结果:

NaNcm;
5cm
-1%;

min

Returns the lowest of one or more values.

参数: value1, ..., valueN - one or more values to compare.

返回值: the lowest value.

实例: min(5, 10);

输出结果: 5

实例: min(3px, 42px, 1px, 16px);

输出结果: 1px

max

Returns the highest of one or more values.

参数: value1, ..., valueN - one or more values to compare.

返回值: the highest value.

实例: max(5, 10);

输出结果: 10

实例: max(3%, 42%, 1%, 16%);

输出结果: 42%


Edit the markdown source for "type-functions"

isnumber

Returns true if a value is a number, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a number, false otherwise.

实例:

isnumber(#ff0);     // false
isnumber(blue);     // false
isnumber("string"); // false
isnumber(1234);     // true
isnumber(56px);     // true
isnumber(7.8%);     // true
isnumber(keyword);  // false
isnumber(url(...)); // false

isstring

Returns true if a value is a string, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a string, false otherwise.

实例:

isstring(#ff0);     // false
isstring(blue);     // false
isstring("string"); // true
isstring(1234);     // false
isstring(56px);     // false
isstring(7.8%);     // false
isstring(keyword);  // false
isstring(url(...)); // false

iscolor

Returns true if a value is a color, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a color, false otherwise.

实例:

iscolor(#ff0);     // true
iscolor(blue);     // true
iscolor("string"); // false
iscolor(1234);     // false
iscolor(56px);     // false
iscolor(7.8%);     // false
iscolor(keyword);  // false
iscolor(url(...)); // false

iskeyword

Returns true if a value is a keyword, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a keyword, false otherwise.

实例:

iskeyword(#ff0);     // false
iskeyword(blue);     // false
iskeyword("string"); // false
iskeyword(1234);     // false
iskeyword(56px);     // false
iskeyword(7.8%);     // false
iskeyword(keyword);  // true
iskeyword(url(...)); // false

isurl

Returns true if a value is a url, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a url, false otherwise.

实例:

isurl(#ff0);     // false
isurl(blue);     // false
isurl("string"); // false
isurl(1234);     // false
isurl(56px);     // false
isurl(7.8%);     // false
isurl(keyword);  // false
isurl(url(...)); // true

ispixel

Returns true if a value is a number in pixels, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a pixel, false otherwise.

实例:

ispixel(#ff0);     // false
ispixel(blue);     // false
ispixel("string"); // false
ispixel(1234);     // false
ispixel(56px);     // true
ispixel(7.8%);     // false
ispixel(keyword);  // false
ispixel(url(...)); // false

isem

Returns true if a value is an em value, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is an em value, false otherwise.

实例:

isem(#ff0);     // false
isem(blue);     // false
isem("string"); // false
isem(1234);     // false
isem(56px);     // false
isem(7.8em);    // true
isem(keyword);  // false
isem(url(...)); // false

ispercentage

Returns true if a value is a percentage value, false otherwise.

参数: value - a value or variable being evaluated.

返回值: true if value is a percentage value, false otherwise.

实例:

ispercentage(#ff0);     // false
ispercentage(blue);     // false
ispercentage("string"); // false
ispercentage(1234);     // false
ispercentage(56px);     // false
ispercentage(7.8%);     // true
ispercentage(keyword);  // false
ispercentage(url(...)); // false

isunit

Returns true if a value is a number in specified units, false otherwise.

参数:

  • value - a value or variable being evaluated.
  • unit - a unit identifier (optionaly quoted) to test for.

返回值: true if value is a number in specified units, false otherwise.

实例:

isunit(11px, px);  // true
isunit(2.2%, px);  // false
isunit(33px, rem); // false
isunit(4rem, rem); // true
isunit(56px, "%"); // false
isunit(7.8%, '%'); // true
isunit(1234, em);  // false
isunit(#ff0, pt);  // false
isunit("mm", mm);  // false

Edit the markdown source for "color-definition"

rgb

Creates an opaque color object from decimal red, green and blue (RGB) values.

Literal color values in standard HTML/CSS formats may also be used to define colors, for example #ff0000.

参数:

  • red: An integer 0-255 or percentage 0-100%.
  • green: An integer 0-255 or percentage 0-100%.
  • blue: An integer 0-255 or percentage 0-100%.

返回值: color

实例: rgb(90, 129, 32)

输出结果: #5a8120

rgba

Creates a transparent color object from decimal red, green, blue and alpha (RGBA) values. Parameters:

  • red: An integer 0-255 or percentage 0-100%.
  • green: An integer 0-255 or percentage 0-100%.
  • blue: An integer 0-255 or percentage 0-100%.
  • alpha: A number 0-1 or percentage 0-100%.

返回值: color

实例: rgba(90, 129, 32, 0.5)

输出结果: rgba(90, 129, 32, 0.5)

argb

Creates a hex representation of a color in #AARRGGBB format (NOT #RRGGBBAA!).

This format is used in Internet Explorer, and .NET and Android development.

参数: color, color object.

返回值: string

实例: argb(rgba(90, 23, 148, 0.5));

输出结果: #805a1794

hsl

Creates an opaque color object from hue, saturation and lightness (HSL) values. Parameters:

  • hue: An integer 0-360 representing degrees.
  • saturation: A percentage 0-100% or number 0-1.
  • lightness: A percentage 0-100% or number 0-1.

返回值: color

实例: hsl(90, 100%, 50%)

输出结果: #80ff00

This is useful if you want to create a new color based on another color's channel, forExample: @new: hsl(hue(@old), 45%, 90%);

@new will have @old's hue, and its own saturation and lightness.

hsla

Creates a transparent color object from hue, saturation, lightness and alpha (HSLA) values.

参数:

  • hue: An integer 0-360 representing degrees.
  • saturation: A percentage 0-100% or number 0-1.
  • lightness: A percentage 0-100% or number 0-1.
  • alpha: A percentage 0-100% or number 0-1.

返回值: color

实例: hsl(90, 100%, 50%, 0.5)

输出结果: rgba(128, 255, 0, 0.5)

hsv

Creates an opaque color object from hue, saturation and value (HSV) values.

Note that this is a color space available in Photoshop, and is not the same as hsl.

参数:

  • hue: An integer 0-360 representing degrees.
  • saturation: A percentage 0-100% or number 0-1.
  • value: A percentage 0-100% or number 0-1.

返回值: color

实例: hsv(90, 100%, 50%)

输出结果: #408000

hsva

Creates a transparent color object from hue, saturation, value and alpha (HSVA) values.

Note that this is not the same as hsla, and is a color space available in Photoshop.

参数:

  • hue: An integer 0-360 representing degrees.
  • saturation: A percentage 0-100% or number 0-1.
  • value: A percentage 0-100% or number 0-1.
  • alpha: A percentage 0-100% or number 0-1.

返回值: color

实例: hsva(90, 100%, 50%, 0.5)

输出结果: rgba(64, 128, 0, 0.5)


Edit the markdown source for "color-channel"

hue

Extracts the hue channel of a color object in the HSL color space.

参数: color - a color object.

返回值: integer 0-360

实例: hue(hsl(90, 100%, 50%))

输出结果: 90

saturation

Extracts the saturation channel of a color object in the HSL color space.

参数: color - a color object.

返回值: percentage 0-100

实例: saturation(hsl(90, 100%, 50%))

输出结果: 100%

lightness

Extracts the lightness channel of a color object in the HSL color space.

参数: color - a color object.

返回值: percentage 0-100

实例: lightness(hsl(90, 100%, 50%))

输出结果: 50%

hsvhue

Extracts the hue channel of a color object in the HSV color space.

参数: color - a color object.

返回值: integer 0-360

实例: hsvhue(hsv(90, 100%, 50%))

输出结果: 90

hsvsaturation

Extracts the saturation channel of a color object in the HSV color space.

参数: color - a color object.

返回值: percentage 0-100

实例: hsvsaturation(hsv(90, 100%, 50%))

输出结果: 100%

hsvvalue

Extracts the value channel of a color object in the HSV color space.

参数: color - a color object.

返回值: percentage 0-100

实例: hsvvalue(hsv(90, 100%, 50%))

输出结果: 50%

red

Extracts the red channel of a color object.

参数: color - a color object.

返回值: integer 0-255

实例: red(rgb(10, 20, 30))

输出结果: 10

green

Extracts the green channel of a color object.

参数: color - a color object.

返回值: integer 0-255

实例: green(rgb(10, 20, 30))

输出结果: 20

blue

Extracts the blue channel of a color object.

参数: color - a color object.

返回值: integer 0-255

实例: blue(rgb(10, 20, 30))

输出结果: 30

alpha

Extracts the alpha channel of a color object.

参数: color - a color object.

返回值: float 0-1

实例: alpha(rgba(10, 20, 30, 0.5))

输出结果: 0.5

luma

Calculates the luma (perceptual brightness) of a color object.

Uses SMPTE C / Rec. 709 coefficients, as recommended in WCAG 2.0. This calculation is also used in the contrast function.

Before v1.7.0 the luma was calculated without gamma correction, use the luminance function to calculate these "old" values.

参数: color - a color object.

返回值: percentage 0-100%

实例: luma(rgb(100, 200, 30))

输出结果: 44%

luminance

Calculates the value of the luma without gamma correction (this function was named luma before v1.7.0)

参数: color - a color object.

返回值: percentage 0-100%

实例: luminance(rgb(100, 200, 30))

输出结果: 65%


Edit the markdown source for "color-operations"

Color operations generally take parameters in the same units as the values they are changing, and percentages are handled as absolutes, so increasing a 10% value by 10% results in 20%, not 11%, and values are clamped to their allowed ranges; they do not wrap around. Where return values are shown, we've used formats that make it clear what each function has done, in addition to the hex versions that you will usually be be working with.

saturate

Increase the saturation of a color in the HSL color space by an absolute amount.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: saturate(hsl(90, 80%, 50%), 20%)

输出结果: #80ff00 // hsl(90, 100%, 50%)

Color 1Color 2

desaturate

Decrease the saturation of a color in the HSL color space by an absolute amount.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: desaturate(hsl(90, 80%, 50%), 20%)

输出结果: #80cc33 // hsl(90, 60%, 50%)

Color 1Color 2

lighten

Increase the lightness of a color in the HSL color space by an absolute amount.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: lighten(hsl(90, 80%, 50%), 20%)

输出结果: #b3f075 // hsl(90, 80%, 70%)

Color 1Color 2

darken

Decrease the lightness of a color in the HSL color space by an absolute amount.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: darken(hsl(90, 80%, 50%), 20%)

输出结果: #4d8a0f // hsl(90, 80%, 30%)

Color 1Color 2

fadein

Decrease the transparency (or increase the opacity) of a color, making it more opaque.

Has no effect on opaque colors. To fade in the other direction use fadeout.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: fadein(hsla(90, 90%, 50%, 0.5), 10%)

输出结果: rgba(128, 242, 13, 0.6) // hsla(90, 90%, 50%, 0.6)

fadeout

Increase the transparency (or decrease the opacity) of a color, making it less opaque. To fade in the other direction use fadein.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: fadeout(hsla(90, 90%, 50%, 0.5), 10%)

输出结果: rgba(128, 242, 13, 0.4) // hsla(90, 90%, 50%, 0.4)

fade

Set the absolute transparency of a color. Can be applied to colors whether they already have an opacity value or not.

参数:

  • color: A color object.
  • amount: A percentage 0-100%.

返回值: color

实例: fade(hsl(90, 90%, 50%), 10%)

输出结果: rgba(128, 242, 13, 0.1) //hsla(90, 90%, 50%, 0.1)

spin

Rotate the hue angle of a color in either direction.

While the angle range is 0-360, it applies a mod 360 operation, so you can pass in much larger (or negative) values and they will wrap around e.g. angles of 360 and 720 will produce the same result. Note that colors are passed through an RGB conversion, which doesn't retain hue value for greys (because hue has no meaning when there is no saturation), so make sure you apply functions in a way that preserves hue, for example don't do this:

@c: saturate(spin(#aaaaaa, 10), 10%);

Do this instead:

@c: spin(saturate(#aaaaaa, 10%), 10);

Colors are always returned as RGB values, so applying spin to a grey value will do nothing.

参数:

  • color: A color object.
  • angle: A number of degrees to rotate (+ or -).

返回值: color

实例:

spin(hsl(10, 90%, 50%), 30)
spin(hsl(10, 90%, 50%), -30)

输出结果:

#f2a60d // hsl(40, 90%, 50%)
#f20d59 // hsl(340, 90%, 50%)

Color 1Color 2

Color 1Color 2

mix

Mix two colors together in variable proportion. Opacity is included in the calculations.

参数:

  • color1: A color object.
  • color2: A color object.
  • weight: Optional, a percentage balance point between the two colors, defaults to 50%.

返回值: color

实例:

mix(#ff0000, #0000ff, 50%)
mix(rgba(100,0,0,1.0), rgba(0,100,0,0.5), 50%)

输出结果:

#800080
rgba(75, 25, 0, 0.75)

Color 1 + Color 2Color 3

greyscale

Remove all saturation from a color in the HSL color space; the same as calling desaturate(@color, 100%).

Because the saturation is not affected by hue, the resulting color mapping may be somewhat dull or muddy; luma may provide a better result as it extracts perceptual rather than linear brightness, for example greyscale('#0000ff') will return the same value as greyscale('#00ff00'), though they appear quite different in brightness to the human eye.

参数: color: A color object.

返回值: color

实例: greyscale(hsl(90, 90%, 50%))

输出结果: #808080 // hsl(90, 0%, 50%)

Color 1Color 2

Notice that the generated grey looks darker than the original green, even though its lightness value is the same.

Compare with using luma (usage is different because luma returns a single value, not a color):

@c: luma(hsl(90, 90%, 50%));
color: rgb(@c, @c, @c);

输出结果: #cacaca

Color 1Color 2

This time the grey's lightness looks about the same as the green, though its value is actually higher.

contrast

Choose which of two colors provides the greatest contrast with another.

This is useful for ensuring that a color is readable against a background, which is also useful for accessibility compliance. This function works the same way as the contrast function in Compass for SASS. In accordance with WCAG 2.0, colors are compared using their gamma-corrected luma value, not their lightness.

The light and dark parameters can be supplied in either order - the function will calculate their luma values and assign light and dark automatically, which means you can't use this function to select the least contrasting color by reversing the order.

参数:

  • color: A color object to compare against.
  • dark: optional - A designated dark color (defaults to black).
  • light: optional - A designated light color (defaults to white).
  • threshold: optional - A percentage 0-100% specifying where the transition from "dark" to "light" is (defaults to 43%, matching SASS). This is used to bias the contrast one way or another, for example to allow you to decide whether a 50% grey background should result in black or white text. You would generally set this lower for 'lighter' palettes, higher for 'darker' ones.

返回值: color

实例:

p {
    a: contrast(#bbbbbb);
    b: contrast(#222222, #101010);
    c: contrast(#222222, #101010, #dddddd);
    d: contrast(hsl(90, 100%, 50%), #000000, #ffffff, 30%);
    e: contrast(hsl(90, 100%, 50%), #000000, #ffffff, 80%);
}

输出结果:

p {
    a: #000000 // black
    b: #ffffff // white
    c: #dddddd
    d: #000000 // black
    e: #ffffff // white
}

These examples use the above calculated colors for background and foreground; you can see that you never end up with white-on-white, nor black-on-black, though it's possible to use the threshold to permit lower-contrast outcomes, as in the last example:

Color 1 Color 1 Color 1 Color 1 Color 1


Edit the markdown source for "color-blending"

These operations are similar (though not necessarily identical) to the blend modes found in image editors like Photoshop, Fireworks or GIMP, so you can use them to make your CSS colors match your images.

multiply

Multiply two colors. Corresponding RGB channels from each of the two colors are multiplied together then divided by 255. The result is a darker color.

参数:

  • color1: A color object.
  • color2: A color object.

返回值: color

Examples:

multiply(#ff6600, #000000);

Color 1 Color 2 Color 3

multiply(#ff6600, #333333);

Color 1 Color 2 Color 3

multiply(#ff6600, #666666);

Color 1 Color 2 Color 3

multiply(#ff6600, #999999);

Color 1 Color 2 Color 3

multiply(#ff6600, #cccccc);

Color 1 Color 2 Color 3

multiply(#ff6600, #ffffff);

Color 1 Color 2 Color 3

multiply(#ff6600, #ff0000);

Color 1 Color 2 Color 3

multiply(#ff6600, #00ff00);

Color 1 Color 2 Color 3

multiply(#ff6600, #0000ff);

Color 1 Color 2 Color 3

screen

Do the opposite of multiply. The result is a brighter color.

参数:

  • color1: A color object.
  • color2: A color object.

返回值: color

实例:

screen(#ff6600, #000000);

Color 1 Color 2 Color 3

screen(#ff6600, #333333);

Color 1 Color 2 Color 3

screen(#ff6600, #666666);

Color 1 Color 2 Color 3

screen(#ff6600, #999999);

Color 1 Color 2 Color 3

screen(#ff6600, #cccccc);

Color 1 Color 2 Color 3

screen(#ff6600, #ffffff);

Color 1 Color 2 Color 3

screen(#ff6600, #ff0000);

Color 1 Color 2 Color 3

screen(#ff6600, #00ff00);

Color 1 Color 2 Color 3

screen(#ff6600, #0000ff);

Color 1 Color 2 Color 3

overlay

Combines the effects of both multiply and screen. Conditionally make light channels lighter and dark channels darker. Note: The results of the conditions are determined by the first color parameter.

参数:

  • color1: A base color object. Also the determinant color to make the result lighter or darker.
  • color2: A color object to overlay.

返回值: color

实例:

overlay(#ff6600, #000000);

Color 1 Color 2 Color 3

overlay(#ff6600, #333333);

Color 1 Color 2 Color 3

overlay(#ff6600, #666666);

Color 1 Color 2 Color 3

overlay(#ff6600, #999999);

Color 1 Color 2 Color 3

overlay(#ff6600, #cccccc);

Color 1 Color 2 Color 3

overlay(#ff6600, #ffffff);

Color 1 Color 2 Color 3

overlay(#ff6600, #ff0000);

Color 1 Color 2 Color 3

overlay(#ff6600, #00ff00);

Color 1 Color 2 Color 3

overlay(#ff6600, #0000ff);

Color 1 Color 2 Color 3

softlight

Similar to overlay but avoids pure black resulting in pure black, and pure white resulting in pure white.

参数:

  • color1: A color object to soft light another.
  • color2: A color object to be soft lighten.

返回值: color

实例:

softlight(#ff6600, #000000);

Color 1 Color 2 Color 3

softlight(#ff6600, #333333);

Color 1 Color 2 Color 3

softlight(#ff6600, #666666);

Color 1 Color 2 Color 3

softlight(#ff6600, #999999);

Color 1 Color 2 Color 3

softlight(#ff6600, #cccccc);

Color 1 Color 2 Color 3

softlight(#ff6600, #ffffff);

Color 1 Color 2 Color 3

softlight(#ff6600, #ff0000);

Color 1 Color 2 Color 3

softlight(#ff6600, #00ff00);

Color 1 Color 2 Color 3

softlight(#ff6600, #0000ff);

Color 1 Color 2 Color 3

hardlight

The same as overlay but with the color roles reversed.

参数:

  • color1: A color object to overlay.
  • color2: A base color object. Also the determinant color to make the result lighter or darker.

返回值: color

实例:

hardlight(#ff6600, #000000);

Color 1 Color 2 Color 3

hardlight(#ff6600, #333333);

Color 1 Color 2 Color 3

hardlight(#ff6600, #666666);

Color 1 Color 2 Color 3

hardlight(#ff6600, #999999);

Color 1 Color 2 Color 3

hardlight(#ff6600, #cccccc);

Color 1 Color 2 Color 3

hardlight(#ff6600, #ffffff);

Color 1 Color 2 Color 3

hardlight(#ff6600, #ff0000);

Color 1 Color 2 Color 3

hardlight(#ff6600, #00ff00);

Color 1 Color 2 Color 3

hardlight(#ff6600, #0000ff);

Color 1 Color 2 Color 3

difference

Subtracts the second color from the first color on a channel-by-channel basis. Negative values are inverted. Subtracting black results in no change; subtracting white results in color inversion.

参数:

  • color1: A color object to act as the minuend.
  • color2: A color object to act as the subtrahend.

返回值: color

实例:

difference(#ff6600, #000000);

Color 1 Color 2 Color 3

difference(#ff6600, #333333);

Color 1 Color 2 Color 3

difference(#ff6600, #666666);

Color 1 Color 2 Color 3

difference(#ff6600, #999999);

Color 1 Color 2 Color 3

difference(#ff6600, #cccccc);

Color 1 Color 2 Color 3

difference(#ff6600, #ffffff);

Color 1 Color 2 Color 3

difference(#ff6600, #ff0000);

Color 1 Color 2 Color 3

difference(#ff6600, #00ff00);

Color 1 Color 2 Color 3

difference(#ff6600, #0000ff);

Color 1 Color 2 Color 3

exclusion

A similar effect to difference with lower contrast.

参数:

  • color1: A color object to act as the minuend.
  • color2: A color object to act as the subtrahend.

返回值: color

实例:

exclusion(#ff6600, #000000);

Color 1 Color 2 Color 3

exclusion(#ff6600, #333333);

Color 1 Color 2 Color 3

exclusion(#ff6600, #666666);

Color 1 Color 2 Color 3

exclusion(#ff6600, #999999);

Color 1 Color 2 Color 3

exclusion(#ff6600, #cccccc);

Color 1 Color 2 Color 3

exclusion(#ff6600, #ffffff);

Color 1 Color 2 Color 3

exclusion(#ff6600, #ff0000);

Color 1 Color 2 Color 3

exclusion(#ff6600, #00ff00);

Color 1 Color 2 Color 3

exclusion(#ff6600, #0000ff);

Color 1 Color 2 Color 3

average

Compute the average of two colors on a per-channel (RGB) basis.

参数:

  • color1: A color object.
  • color2: A color object.

返回值: color

实例:

average(#ff6600, #000000);

Color 1 Color 2 Color 3

average(#ff6600, #333333);

Color 1 Color 2 Color 3

average(#ff6600, #666666);

Color 1 Color 2 Color 3

average(#ff6600, #999999);

Color 1 Color 2 Color 3

average(#ff6600, #cccccc);

Color 1 Color 2 Color 3

average(#ff6600, #ffffff);

Color 1 Color 2 Color 3

average(#ff6600, #ff0000);

Color 1 Color 2 Color 3

average(#ff6600, #00ff00);

Color 1 Color 2 Color 3

average(#ff6600, #0000ff);

Color 1 Color 2 Color 3

negation

Do the opposite effect to difference.

The result is a brighter color. Note: The opposite effect doesn't mean the inverted effect as resulting from an addition operation.

参数:

  • color1: A color object to act as the minuend.
  • color2: A color object to act as the subtrahend.

返回值: color

实例:

negation(#ff6600, #000000);

Color 1 Color 2 Color 3

negation(#ff6600, #333333);

Color 1 Color 2 Color 3

negation(#ff6600, #666666);

Color 1 Color 2 Color 3

negation(#ff6600, #999999);

Color 1 Color 2 Color 3

negation(#ff6600, #cccccc);

Color 1 Color 2 Color 3

negation(#ff6600, #ffffff);

Color 1 Color 2 Color 3

negation(#ff6600, #ff0000);

Color 1 Color 2 Color 3

negation(#ff6600, #00ff00);

Color 1 Color 2 Color 3

negation(#ff6600, #0000ff);

Color 1 Color 2 Color 3