image.png

sketch颜色选择器
image.png

color-convert 颜色转换

https://www.npmjs.com/package/color-convert
rgba与16进制颜色空间互相转换方法
https://juejin.cn/post/7035812561027072031
https://juejin.cn/post/7043448389022777351

rgb转换为16进制

  1. // "rgb(255,255,255)".colorHex()
  2. String.prototype.colorHex = function () {
  3. const reg = /^(rgb|RGB)/; // RGB颜色值的正则
  4. if (!reg.test(this)) {
  5. return String(this);
  6. }
  7. let strHex = "#";
  8. // 把RGB的3个数值变成数组
  9. const source = this.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
  10. const {length} = source;
  11. // 转成16进制
  12. for (let i = 0; i < length; i++) {
  13. let hex = Number(source[i]).toString(16);
  14. if (hex === "0") {
  15. hex += hex;
  16. }
  17. strHex += hex;
  18. }
  19. return strHex;
  20. }

16进制转换为rgb

  1. "#fff".colorRgb(); // rgb(255,255,255)
  2. "#ffffff".colorRgb(); // rgb(255,255,255)
  3. String.prototype.colorRgb = function () {
  4. // 16进制颜色值的正则
  5. const reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
  6. // 把颜色值变成小写
  7. let color = this.toLowerCase();
  8. if (!reg.test(color)) return color;
  9. // 如果只有三位的值,需变成六位,如:#fff => #ffffff
  10. if (color.length === 4) {
  11. let colorNew = "#";
  12. for (let i = 1; i < 4; i += 1) {
  13. colorNew += color.slice(i, i + 1).concat(color.slice(i, i + 1));
  14. }
  15. color = colorNew;
  16. }
  17. // 处理六位的颜色值,转为RGB
  18. const source = [];
  19. for (let i = 1; i < 7; i += 2) {
  20. source.push(parseInt("0x" + color.slice(i, i + 2)));
  21. }
  22. return `rgb(${source.join(",")})`;
  23. }

react-colorful

在线案例 https://omgovich.github.io/react-colorful
Github https://github.com/omgovich/react-colorful
https://www.npmjs.com/package/react-colorful
image.png
image.png

ColorfulPicker

基于 react-colorful & @ant-design/colors的封装
image.pngimage.png

react-color-picker

https://www.npmjs.com/package/react-best-gradient-color-picker
Github https://github.com/hxf31891/react-gradient-color-picker
在线案例 https://gradient-package-demo.web.app

  1. yarn add react-best-gradient-color-picker

image.png

react-color-palette

https://www.npmjs.com/package/react-color-palette
image.png
image.png

react-color

在线案例 https://casesandberg.github.io/react-color
大而全颜色选择器

  1. Sketch
  2. Photoshop
  3. Chrome

image.png

  1. yarn add react-color
  2. yarn add @types/react-color --dev