Skip to content

gradient - Gradient ​

Choose a gradient API: Two-color RGB steps · Space, easing, and sampling · Multiple stops and custom domains

Generate gradient color sequences between two colors, or generate a tonal range from a single color.

Install ​

ts
import xcolor from '@xpyjs/color'
import gradientPlugin from '@xpyjs/color/plugins/gradient'

xcolor.use(gradientPlugin)

Methods ​

From012345To6 steps

gradient(to, steps?) ​

Generate a gradient sequence between the current color and the target color.

ParameterTypeDefaultDescription
toXColorInput—Target color
stepsnumber5Number of steps (≥ 2)

Returns: XColor[]

ts
xcolor('#ff0000').gradient('#0000ff', 5).map(c => c.toHex())
// ['#ff0000', '#bf0040', '#800080', '#4000bf', '#0000ff']

tones(steps?) ​

Generate a tonal range of shades and tints centered on the current color.

ParameterTypeDefaultDescription
stepsnumber5Colors on each side (2–10000)

Returns: XColor[]

ts
xcolor('#3b82f6').tones(5).map(c => c.toHex())
// 5-step variation from dark to light
← shadedarkestbase#3b82f6tint →lightestshades (dark) + original + tints (light) = full tonal range

Practical Scenarios ​

CSS Gradient Background ​

ts
const stops = xcolor('#6366f1')
  .gradient('#ec4899', 5)
  .map(c => c.toHex())
  .join(', ')

const css = `linear-gradient(to right, ${stops})`
linear-gradient(to right, ...)#6366f1#8b5cc5#b35299#db486d#ec48995-step gradient → CSS linear-gradient background

Chart Color Scheme ​

ts
const chartColors = xcolor('#3b82f6')
  .gradient('#10b981', 8)
  .map(c => c.toHex())
Chart Colors (8-step gradient)#3b82f6#3a90e3#399dd0#38abbe#36b8ab#35c698#34d386#33e073

Released under the MIT License.