项目作者: luncheon

项目描述 :
A vanilla-js touch-friendly HSV color picker inspired by Farbtastic Color Picker.
高级语言: TypeScript
项目地址: git://github.com/luncheon/reinvented-color-wheel.git
创建时间: 2018-08-22T06:17:33Z
项目社区:https://github.com/luncheon/reinvented-color-wheel

开源协议:Do What The F*ck You Want To Public License

下载


Reinvented Color Wheel

npm
jsDelivr
WTFPL

image

A vanilla-js touch-friendly HSV color picker inspired by Farbtastic Color Picker.

Demo

Characteristics

  • HSV (hue, saturation, value) cylindrical color model (unlike Farbtastic that takes HSL)
  • Touch-friendly
  • No need jQuery
  • Lightweight (JS + CSS ~ 3.1 KB minified + gzipped)

Installation

via npm (with a module bundler)

  1. $ npm install reinvented-color-wheel
  1. import "reinvented-color-wheel/css/reinvented-color-wheel.min.css";
  2. import ReinventedColorWheel from "reinvented-color-wheel";

via CDN (jsDelivr)

  1. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reinvented-color-wheel@0.4.0/css/reinvented-color-wheel.min.css">
  2. <script src="https://cdn.jsdelivr.net/npm/reinvented-color-wheel@0.4.0"></script>
  3. <script>/* `window.ReinventedColorWheel` object is available */</script>

or for modern browsers:

  1. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/reinvented-color-wheel@0.4.0/css/reinvented-color-wheel.min.css">
  2. <script type="module">
  3. import ReinventedColorWheel from "https://cdn.jsdelivr.net/npm/reinvented-color-wheel@0.4.0/es/reinvented-color-wheel.bundle.min.js";
  4. </script>

Download directly

wheel@0.4.0/css/reinvented-color-wheel.min.css">reinvented-color-wheel.min.css
wheel@0.4.0/iife/reinvented-color-wheel.min.js">reinvented-color-wheel.min.js

Usage

  1. // create a new color picker
  2. var colorWheel = new ReinventedColorWheel({
  3. // appendTo is the only required property. specify the parent element of the color wheel.
  4. appendTo: document.getElementById("my-color-picker-container"),
  5. // followings are optional properties and their default values.
  6. // initial color (can be specified in hsv / hsl / rgb / hex)
  7. hsv: [0, 100, 100],
  8. // hsl: [0, 100, 50],
  9. // rgb: [255, 0, 0],
  10. // hex: "#ff0000",
  11. // appearance
  12. wheelDiameter: 200,
  13. wheelThickness: 20,
  14. handleDiameter: 16,
  15. wheelReflectsSaturation: true,
  16. // handler
  17. onChange: function (color) {
  18. // the only argument is the ReinventedColorWheel instance itself.
  19. // console.log("hsv:", color.hsv[0], color.hsv[1], color.hsv[2]);
  20. },
  21. });
  22. // set color in HSV / HSL / RGB / HEX
  23. colorWheel.hsv = [240, 100, 100];
  24. colorWheel.hsl = [120, 100, 50];
  25. colorWheel.rgb = [255, 128, 64];
  26. colorWheel.hex = '#888888';
  27. // get color in HSV / HSL / RGB / HEX
  28. console.log("hsv:", colorWheel.hsv[0], colorWheel.hsv[1], colorWheel.hsv[2]);
  29. console.log("hsl:", colorWheel.hsl[0], colorWheel.hsl[1], colorWheel.hsl[2]);
  30. console.log("rgb:", colorWheel.rgb[0], colorWheel.rgb[1], colorWheel.rgb[2]);
  31. console.log("hex:", colorWheel.hex);
  32. // please call redraw() after changing some appearance properties.
  33. colorWheel.wheelDiameter = 400;
  34. colorWheel.wheelThickness = 40;
  35. colorWheel.redraw();

Web Components

This package contains the Web Components wrapping the color wheel.
The tag is <reinvented-color-wheel>.
The options above except for appendTo can be specified with kebab-case, and each option is optional.

  1. <!-- CSS is included in JS: no <link> is needed. -->
  2. <script src="reinvented-color-wheel/webcomponents/index.js"></script>
  3. <reinvented-color-wheel
  4. hex="#ff3e00"
  5. wheel-diameter="200"
  6. wheel-thickness="20"
  7. handle-diameter="16"
  8. wheel-reflects-saturation="false"
  9. ></reinvented-color-wheel>
  10. <!-- hsv, hsl or rgb can be specified as a comma-separated string. -->
  11. <reinvented-color-wheel rgb="255,62,0"></reinvented-color-wheel>

React Component

This package contains the React component wrapping the color wheel.
The options above except for appendTo can be specified, and each option is optional.

  1. import React from 'react'
  2. import ReinventedColorWheel from 'reinvented-color-wheel/react'
  3. import 'reinvented-color-wheel/css/reinvented-color-wheel.min.css'
  4. const App = () => {
  5. const [hex, setHex] = React.useState('#000000')
  6. return (
  7. <>
  8. <ReinventedColorWheel
  9. // hsv={[0, 100, 100]}
  10. // hsl={[0, 100, 50]}
  11. // rgb={[255, 0, 0]}
  12. // hex="#ff0000"
  13. hex={hex}
  14. wheelDiameter={200}
  15. wheelThickness={20}
  16. handleDiameter={16}
  17. wheelReflectsSaturation
  18. onChange={({ hex }) => setHex(hex)}
  19. />
  20. <input value={hex} onChange={e => setHex(e.target.value)} />
  21. </>
  22. )
  23. }

License

WTFPL

Sister Package

lch-color-wheel: L*C*h color wheel