gradient - 渐变
选择渐变 API: 两色 RGB 等分 · 空间、缓动和采样 · 多色标和自定义数据域
生成两个颜色之间的渐变色序列,或从单一颜色生成明暗变化序列。
安装
ts
import xcolor from '@xpyjs/color'
import gradientPlugin from '@xpyjs/color/plugins/gradient'
xcolor.use(gradientPlugin)方法
gradient(to, steps?)
在当前颜色和目标颜色之间生成渐变序列。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
to | XColorInput | — | 目标颜色 |
steps | number | 5 | 步数(≥ 2) |
返回值:XColor[]
ts
xcolor('#ff0000').gradient('#0000ff', 5).map(c => c.toHex())
// ['#ff0000', '#bf0040', '#800080', '#4000bf', '#0000ff']tones(steps?)
以当前颜色为中心,生成明暗变化的色调序列(shades + 原色 + tints)。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
steps | number | 5 | 每侧的色数(2–10000) |
返回值:XColor[]
ts
xcolor('#3b82f6').tones(5).map(c => c.toHex())
// 11 色,由 5 个暗色、原色和 5 个浅色组成实际场景
CSS 渐变背景
ts
const stops = xcolor('#6366f1')
.gradient('#ec4899', 5)
.map(c => c.toHex())
.join(', ')
const css = `linear-gradient(to right, ${stops})`图表配色
ts
const chartColors = xcolor('#3b82f6')
.gradient('#10b981', 8)
.map(c => c.toHex())