color-convert支持如下颜色格式的转换:
rgb, hsl, hsv, hwb, cmyk, ansi, ansi16, hex
文档
- https://www.npmjs.com/package/color-convert
- https://github.com/Qix-/color-convert
安装
$ npm install color-convert
使用示例
import convert from 'color-convert';
// RGB to CMYK
console.log(convert.rgb.cmyk(167, 255, 4)); // [ 35, 0, 98, 0 ]
// RGB to HEX
console.log(convert.rgb.hex(123, 45, 67)); // '7B2D43'
console.log(convert.rgb.hex([123, 45, 67])); // '7B2D43'
// HEX to RGB
console.log(convert.hex.rgb('7B2D43')); // [ 123, 45, 67 ]
console.log(convert.hex.rgb('#7B2D43')); // [ 123, 45, 67 ]
通过这个库,可以做颜色转换的线上工具,例如:
https://mouday.github.io/tools/pages/color-convert/index.html