color-convert

中文文档
3.1.3发布于 2025/11/15MITTypeScriptES Module近 30 天下载 13.5亿

Plain color conversion functions

原始使用文档
文档目录17 节
  1. Arrays
  2. Routing
  3. Color Space Scales
  4. rgb
  5. hsl
  6. hsv
  7. hwb
  8. xyz
  9. lab
  10. lch
  11. oklab
  12. oklch
  13. cmyk
  14. hex
  15. keyword
  16. apple
  17. gray

color-convert

Color-convert is a color conversion library for JavaScript and node. It converts all ways between rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex strings, and CSS keywords (will round to closest):

import convert from 'color-convert';

convert.rgb.hsl(140, 200, 100);             // [96, 48, 59]
convert.keyword.rgb('blue');                // [0, 0, 255]

const rgbChannels = convert.rgb.channels;     // 3
const cmykChannels = convert.cmyk.channels;   // 4
const ansiChannels = convert.ansi16.channels; // 1

Install

npm install color-convert

API

Simply get the property of the from and to conversion that you're looking for.

All functions have a rounded and unrounded variant. By default, return values are rounded. To get the unrounded (raw) results, simply tack on .raw to the function.

All 'from' functions have a hidden property called .channels that indicates the number of channels the function expects (not including alpha).

import convert from 'color-convert';

// Hex to LAB
convert.hex.lab('DEADBF');         // [ 76, 21, -2 ]
convert.hex.lab.raw('DEADBF');     // [ 75.56213190997677, 20.653827952644754, -2.290532499330533 ]

// RGB to CMYK
convert.rgb.cmyk(167, 255, 4);     // [ 35, 0, 98, 0 ]
convert.rgb.cmyk.raw(167, 255, 4); // [ 34.509803921568626, 0, 98.43137254901961, 0 ]

Arrays

All functions that accept multiple arguments also support passing an array.

Note that this does not apply to functions that convert from a color that only requires one value (e.g. keyword, ansi256, hex, etc.)

import convert from 'color-convert';

convert.rgb.hex(123, 45, 67);      // '7B2D43'
convert.rgb.hex([123, 45, 67]);    // '7B2D43'

Routing

Conversions that don't have an explicitly defined conversion (in conversions.js), but can be converted by means of sub-conversions (e.g. XYZ -> RGB -> CMYK), are automatically routed together. This allows just about any color model supported by color-convert to be converted to any other model, so long as a sub-conversion path exists. This is also true for conversions requiring more than one step in between (e.g. LCH -> LAB -> XYZ -> RGB -> Hex).

Keep in mind that extensive conversions may result in a loss of precision, and exist only to be complete. For a list of "direct" (single-step) conversions, see conversions.js.

Color Space Scales

Conversions rely on an agreed upon 'full-scale' value for each of the channels. Listed here are those values for the most common color spaces

rgb

channelfull-scale value
r255
g255
b255

hsl

channelfull-scale value
h360
s100
l100

hsv

channelfull-scale value
h360
s100
v100

hwb

channelfull-scale value
h360
w100
b100

xyz

channelfull-scale value
x94
y99
z108

lab

channelfull-scale value
l100
a-86, 98
b-108, 94

lch

channelfull-scale value
l100
c133
h360

oklab

channelfull-scale value
l100
a-23, 28
b-31, 20

oklch

channelfull-scale value
l100
c32
h360

cmyk

channelfull-scale value
c100
m100
y100
k100

hex

channelfull-scale value
hex0xffffff

keyword

channelvalue
nameany key from color-name

apple

channelfull-scale value
065535
165535
265535

gray

channelfull-scale value
gray100

Contribute

If there is a new model you would like to support, or want to add a direct conversion between two existing models, please send us a pull request.

License

Copyright © 2011-2016, Heather Arthur. Copyright © 2016-2021, Josh Junon.

Licensed under the MIT License.

版本与发布记录

color-convert 共发布 41 个版本,首次发布于 2011/06/10;过去 12 个月发布了 1 个版本。

版本发布日期距今
3.1.3latest2025/11/1511 个月前
3.1.22025/09/141.1 年前
3.1.12025/09/081.2 年前
3.1.02025/05/131.5 年前
3.0.12025/02/071.7 年前
3.0.02025/02/061.7 年前

关于 color-convert 的常见问题

color-convert 是什么?

Plain color conversion functions

如何安装 color-convert?

在项目目录运行 npm install color-convert,或使用 pnpm add / yarn add / bun add。

color-convert 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

color-convert 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

color-convert 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14.6。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

color-convert 可以商用吗?

color-convert 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

color-convert 的下载量有多少?

根据 npm 官方下载统计,color-convert 在 2026/10/05 – 2026/11/03 这 30 天内被下载 1,350,157,780 次,日均约 45,005,259 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

color-convert 还在维护吗?

最新版本 3.1.3 发布于 2025/11/15(11 个月前);至今共发布 41 个版本,其中过去 12 个月发布了 1 个;首次发布于 2011/06/10。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。