Skip to content

简体中文

English

颜色操作 ​

所有操作方法原地修改实例并返回 this,支持链式调用。如需保留原色,先 .clone()。

调色方法比较 ​

原色
rgb(51, 102, 153)
hsl(210, 50%, 40%)
lighten
rgb(71, 133, 194)
hsl(210, 50%, 52%)
brighten
rgb(92, 133, 173)
hsl(210, 33%, 52%)
tint
rgb(92, 133, 173)
hsl(210, 33%, 52%)
import xcolor from '@xpyjs/color'
const original = xcolor('#336699').alpha(1)
original.clone().lighten(20).toRgbString()
original.clone().brighten(20).toRgbString()
original.clone().tint(20).toRgbString()

亮度 ​

lighten(amount?) ​

将 HSL 亮度按剩余区间推向白色。amount 为当前亮度到 100% 之间距离的百分比(0–100),默认 10;50 走到剩余距离的一半,100 必定为白色。

ts
xcolor('#ff0000').lighten(20).toHex()  // '#ff3333'

darken(amount?) ​

将 HSL 亮度按当前区间推向黑色。amount 为当前亮度到 0% 之间距离的百分比(0–100),默认 10;50 走到剩余距离的一半,100 必定为黑色。等价于 lighten(-amount)。

ts
xcolor('#ff0000').darken(20).toHex()  // '#cc0000'

brighten(amount?) ​

在 RGB 空间将各通道推向 255。默认 10,范围 0–100。

ts
xcolor('#ff0000').brighten(50).toHex()  // '#ff8080'

饱和度 ​

saturate(amount?) ​

增加 HSL 饱和度。默认 10,范围 0–100。

ts
xcolor('#bf4040').saturate(40).toHex()  // '#f20d0d'

desaturate(amount?) ​

减少 HSL 饱和度。默认 10。

ts
xcolor('#ff0000').desaturate(50).toHex()  // '#bf4040'

grayscale() ​

完全去饱和,等价于 desaturate(100)。

别名:greyscale()

ts
xcolor('#ff0000').grayscale().toHex()  // '#808080'

色相 ​

spin(degrees) ​

在 HSL 色轮上旋转色相。支持正负值,自动取模 360°。

别名:rotate()

ts
xcolor('#ff0000').spin(120).toHex()   // '#00ff00'
xcolor('#ff0000').spin(-120).toHex()  // '#0000ff'

反转 ​

negate() ​

RGB 各通道取反(255 - channel),alpha 不变。

别名:invert()

ts
xcolor('#ff0000').negate().toHex()  // '#00ffff'

透明度 ​

fade(amount?) ​

降低不透明度。alpha 减少 amount / 100,默认 10。

别名:fadeOut()

ts
xcolor('#ff0000').fade(50).alpha()  // 0.5

opaquer(amount?) ​

增加不透明度。alpha 增加 amount / 100,默认 10。

别名:fadeIn()

ts
xcolor('rgba(255,0,0,0.5)').opaquer(30).alpha()  // 0.8

混合 ​

mix(color, amount?) ​

在 RGB 空间与另一个颜色线性混合。amount 为目标颜色的权重(0–100),默认 50。

ts
xcolor('#ff0000').mix('#0000ff', 50).toHex()  // '#800080'

tint(amount?) ​

与白色混合。amount 为白色的权重(0–100),默认 50。

ts
xcolor('#ff0000').tint(50).toHex()  // '#ff8080'

shade(amount?) ​

与黑色混合。amount 为黑色的权重(0–100),默认 50。

ts
xcolor('#ff0000').shade(50).toHex()  // '#800000'

方法签名汇总 ​

方法参数默认值返回值别名
lighten(amount?)0–10010this—
darken(amount?)0–10010this—
brighten(amount?)0–10010this—
saturate(amount?)0–10010this—
desaturate(amount?)0–10010this—
grayscale()——thisgreyscale()
spin(degrees)任意数—thisrotate()
negate()——thisinvert()
fade(amount?)0–10010thisfadeOut()
opaquer(amount?)0–10010thisfadeIn()
mix(color, amount?)0–10050this—
tint(amount?)0–10050this—
shade(amount?)0–10050this—

Released under the MIT License.