hcg-color-picker 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of hcg-color-picker might be problematic. Click here for more details.

Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +367 -0
  3. package/index.esm.js +621 -0
  4. package/index.js +679 -0
  5. package/package.json +40 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 hcg-color-picker contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,367 @@
1
+ # Google Chrome Style Color Picker
2
+
3
+ A lightweight, dependency-free color picker widget inspired by Google Chrome's built-in color picker. Supports multiple instances, alpha control, touch input, and the EyeDropper API.
4
+
5
+ ---
6
+
7
+ ## Features
8
+
9
+ - **No dependencies** - pure vanilla JavaScript, no libraries required
10
+ - **Multiple instances** - attach a picker to any number of buttons, each remembers its own color
11
+ - **Single shared UI** - one picker element is reused across all instances, keeping memory usage minimal
12
+ - **Three color modes** - switch between HEX, RGBA, and HSLA input formats
13
+ - **Alpha / opacity control** - full transparency support, can be enabled or disabled per instance
14
+ - **EyeDropper API** - pick any color from the screen (supported browsers only)
15
+ - **Touch support** - works on mobile and tablet devices
16
+ - **Color preview swatch** - live preview inside the picker
17
+ - **Keyboard accessible** - color text inputs are fully keyboard navigable
18
+ - **`data-color` attribute** - current color is always available on the trigger element via `element.dataset.color`
19
+ - **Event system** - subscribe and unsubscribe to color change events
20
+ - **Programmatic API** - set color, enable/disable, and destroy instances at runtime
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ Include the script in your HTML file:
27
+
28
+ ```html
29
+ <script src="hcg-color.js"></script>
30
+ ```
31
+
32
+ ---
33
+
34
+ ## Basic Usage
35
+
36
+ ```html
37
+ <button id="my-color-btn">Pick Color</button>
38
+
39
+ <script>
40
+ const picker = new hcgColor(
41
+ document.getElementById('my-color-btn'),
42
+ '#ff0000'
43
+ );
44
+
45
+ picker.on('change', function (colors) {
46
+ console.log(colors.hex);
47
+ });
48
+ </script>
49
+ ```
50
+
51
+ ---
52
+
53
+ ## Constructor
54
+
55
+ ```js
56
+ new hcgColor(element, color, onChange, options)
57
+ ```
58
+
59
+ | Parameter | Type | Required | Description |
60
+ |------------|---------------|----------|--------------------------------------------------|
61
+ | `element` | `HTMLElement` | ✅ | The trigger button element |
62
+ | `color` | `string` | ✅ | Initial color - supports HEX, RGB, HSL formats |
63
+ | `onChange` | `function` | ❌ | Shorthand change callback (same as `.on()`) |
64
+ | `options` | `object` | ❌ | Configuration options (see below) |
65
+
66
+ ---
67
+
68
+ ## Options
69
+
70
+ | Option | Type | Default | Description |
71
+ |---------|-----------|---------|--------------------------------------|
72
+ | `alpha` | `boolean` | `true` | Set to `false` to disable alpha control |
73
+
74
+ ```js
75
+ const picker = new hcgColor(element, '#ff0000', null, { alpha: false });
76
+ ```
77
+
78
+ ---
79
+
80
+ ## Initial Color Formats
81
+
82
+ The `color` parameter accepts any of the following formats:
83
+
84
+ ```js
85
+ new hcgColor(el, '#ff0000'); // 6-digit HEX
86
+ new hcgColor(el, '#ff0000ff'); // 8-digit HEX with alpha
87
+ new hcgColor(el, '#f00'); // 3-digit HEX shorthand
88
+ new hcgColor(el, 'rgb(255, 0, 0)'); // RGB
89
+ new hcgColor(el, 'rgba(255, 0, 0, 0.5)'); // RGBA
90
+ new hcgColor(el, 'hsl(0, 100%, 50%)'); // HSL
91
+ new hcgColor(el, 'hsla(0, 100%, 50%, 1)');// HSLA
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Instance Methods
97
+
98
+ ### `.on(event, callback)`
99
+ Subscribe to an event.
100
+ ```js
101
+ picker.on('change', function (colors) {
102
+ console.log(colors.hex); // "#ff0000"
103
+ console.log(colors.rgb); // "rgb(255, 0, 0)"
104
+ console.log(colors.rgba); // "rgba(255, 0, 0, 1)"
105
+ console.log(colors.hsl); // "hsl(0, 100%, 50%)"
106
+ console.log(colors.hsla); // "hsla(0, 100%, 50%, 1)"
107
+ });
108
+ ```
109
+
110
+ ### `.off(event, callback)`
111
+ Unsubscribe a specific listener.
112
+ ```js
113
+ function onChange(colors) { console.log(colors.hex); }
114
+
115
+ picker.on('change', onChange);
116
+ picker.off('change', onChange);
117
+ ```
118
+
119
+ ### `.setColor(color)`
120
+ Programmatically set the color. Fires the `change` event.
121
+ ```js
122
+ picker.setColor('#00ff00');
123
+ picker.setColor('rgb(0, 255, 0)');
124
+ ```
125
+
126
+ ### `.getColor()`
127
+ Returns the current color as an 8-digit or 6-digit HEX string.
128
+ ```js
129
+ const hex = picker.getColor(); // "#ff0000"
130
+ ```
131
+
132
+ ### `.setAlphaEnabled(boolean)`
133
+ Enable or disable the alpha slider at runtime.
134
+ ```js
135
+ picker.setAlphaEnabled(false); // hide alpha controls
136
+ picker.setAlphaEnabled(true); // show alpha controls
137
+ ```
138
+
139
+ ### `.disable()`
140
+ Disable the picker - prevents the picker from opening on click.
141
+ ```js
142
+ picker.disable();
143
+ ```
144
+
145
+ ### `.enable()`
146
+ Re-enable a previously disabled picker.
147
+ ```js
148
+ picker.enable();
149
+ ```
150
+
151
+ ### `.destroy()`
152
+ Fully remove the picker instance - cleans up event listeners, clears state, and removes the color from the element.
153
+ ```js
154
+ picker.destroy();
155
+ ```
156
+
157
+ ---
158
+
159
+ ## Static Methods
160
+
161
+ ### `hcgColor.eyeDropper()`
162
+ Opens the EyeDropper tool and applies the picked color to the currently active picker instance.
163
+ ```js
164
+ hcgColor.eyeDropper();
165
+ ```
166
+
167
+ ---
168
+
169
+ ## Events
170
+
171
+ | Event | Callback data | Description |
172
+ |----------|---------------|------------------------------------|
173
+ | `change` | `colors` | Fired every time the color changes |
174
+
175
+ ### `colors` object
176
+
177
+ ```js
178
+ {
179
+ hex: "#ff0000",
180
+ hexa: "#ff0000",
181
+ rgb: "rgb(255, 0, 0)",
182
+ rgba: "rgba(255, 0, 0, 1)",
183
+ hsl: "hsl(0, 100%, 50%)",
184
+ hsla: "hsla(0, 100%, 50%, 1)"
185
+ }
186
+ ```
187
+
188
+ ---
189
+
190
+ ## Multiple Instances
191
+
192
+ Each instance is independent - they share one picker UI but each stores its own color state.
193
+
194
+ ```js
195
+ const picker1 = new hcgColor(document.getElementById('btn1'), '#ff0000');
196
+ const picker2 = new hcgColor(document.getElementById('btn2'), '#0000ff');
197
+ const picker3 = new hcgColor(document.getElementById('btn3'), '#00ff00', null, { alpha: false });
198
+
199
+ picker1.on('change', colors => console.log('Picker 1:', colors.hex));
200
+ picker2.on('change', colors => console.log('Picker 2:', colors.hex));
201
+ ```
202
+
203
+ ---
204
+
205
+ ## Reading Color Without an Instance Reference
206
+
207
+ The current color is always stored on the trigger element via `data-color`, so you can read it anywhere without keeping a reference to the picker instance:
208
+
209
+ ```js
210
+ // On form submit, collect all picker colors
211
+ document.querySelectorAll('.color-btn').forEach(btn => {
212
+ console.log(btn.dataset.color); // "#ff0000"
213
+ });
214
+ ```
215
+
216
+ ---
217
+
218
+ ## Usage in React
219
+
220
+ A dedicated React component is available as a separate package.
221
+
222
+ ### Installation
223
+
224
+ ```bash
225
+ npm install hcg-color-picker-react
226
+ ```
227
+
228
+ ### Import
229
+
230
+ ```jsx
231
+ import ColorPicker from 'hcg-color-picker-react';
232
+ import 'hcg-color-picker-react/ColorPicker.css';
233
+ ```
234
+
235
+ > `createPortal` is used internally and imported from `react-dom` - no extra setup needed.
236
+
237
+ ---
238
+
239
+ ### Props
240
+
241
+ | Prop | Type | Default | Description |
242
+ |------------|------------|-------------|------------------------------------------|
243
+ | `color` | `string` | `'#ff0000'` | Initial color - HEX, RGB, HSL formats |
244
+ | `onChange` | `function` | - | Called every time the color changes |
245
+ | `alpha` | `boolean` | `true` | Set to `false` to disable alpha control |
246
+ | `disabled` | `boolean` | `false` | Prevents the picker from opening |
247
+ | `className`| `string` | - | CSS class applied to the trigger button |
248
+ | `style` | `object` | - | Inline styles for the trigger button |
249
+
250
+ ---
251
+
252
+ ### Basic usage
253
+
254
+ ```jsx
255
+ import ColorPicker from 'hcg-color-picker-react';
256
+ import 'hcg-color-picker-react/ColorPicker.css';
257
+
258
+ function App() {
259
+ function handleChange(colors) {
260
+ console.log(colors.hex); // "#ff0000"
261
+ console.log(colors.rgba); // "rgba(255, 0, 0, 1)"
262
+ console.log(colors.hsla); // "hsla(0, 100%, 50%, 1)"
263
+ }
264
+
265
+ return (
266
+ <div>
267
+ {/* Basic */}
268
+ <ColorPicker color="#ff0000" onChange={handleChange} />
269
+
270
+ {/* No alpha */}
271
+ <ColorPicker color="#0000ff" alpha={false} onChange={handleChange} />
272
+
273
+ {/* Disabled */}
274
+ <ColorPicker color="#00ff00" disabled={true} />
275
+ </div>
276
+ );
277
+ }
278
+
279
+ export default App;
280
+ ```
281
+
282
+ ---
283
+
284
+ ### Programmatic API via `ref`
285
+
286
+ Use `ref` to call methods directly from a parent component:
287
+
288
+ ```jsx
289
+ import { useRef } from 'react';
290
+ import ColorPicker from 'hcg-color-picker-react';
291
+ import 'hcg-color-picker-react/ColorPicker.css';
292
+
293
+ function App() {
294
+ const pickerRef = useRef(null);
295
+
296
+ return (
297
+ <div>
298
+ <ColorPicker
299
+ ref={pickerRef}
300
+ color="#ff9800"
301
+ onChange={colors => console.log(colors.hex)}
302
+ />
303
+
304
+ <button onClick={() => pickerRef.current.setColor('#e91e63')}>Set Pink</button>
305
+ <button onClick={() => alert(pickerRef.current.getColor())}>Get Color</button>
306
+ <button onClick={() => pickerRef.current.setAlphaEnabled(false)}>Disable Alpha</button>
307
+ </div>
308
+ );
309
+ }
310
+
311
+ export default App;
312
+ ```
313
+
314
+ ### Ref methods
315
+
316
+ | Method | Description |
317
+ |---------------------------|------------------------------------------|
318
+ | `.setColor(color)` | Programmatically set the color |
319
+ | `.getColor()` | Returns current color as HEX string |
320
+ | `.setAlphaEnabled(bool)` | Show or hide the alpha slider at runtime |
321
+ | `.enable()` | Enable the picker |
322
+ | `.disable()` | Disable the picker |
323
+
324
+ ---
325
+
326
+ ### Multiple instances
327
+
328
+ Each `<ColorPicker>` is a fully independent instance - no shared state between them:
329
+
330
+ ```jsx
331
+ function App() {
332
+ return (
333
+ <div>
334
+ <ColorPicker color="#f44336" onChange={c => console.log('Red picker:', c.hex)} />
335
+ <ColorPicker color="#4caf50" onChange={c => console.log('Green picker:', c.hex)} />
336
+ <ColorPicker color="#2196f3" alpha={false} onChange={c => console.log('Blue picker:', c.hex)} />
337
+ </div>
338
+ );
339
+ }
340
+ ```
341
+
342
+ ---
343
+
344
+ ### React - Key notes
345
+
346
+ | | Reason |
347
+ |---|---|
348
+ | `forwardRef` | Allows parent components to access ref methods |
349
+ | `useImperativeHandle` | Exposes `setColor`, `getColor` etc. via ref |
350
+ | `createPortal` (from `react-dom`) | Renders picker popup at `document.body` level to avoid overflow/z-index issues |
351
+ | Unique SVG IDs per instance | Each picker generates unique gradient IDs - no conflicts with multiple instances |
352
+
353
+ ---
354
+
355
+ ## Browser Support
356
+
357
+ | Feature | Support |
358
+ |------------------|----------------------------------|
359
+ | Color picker UI | All modern browsers |
360
+ | Touch events | iOS Safari, Android Chrome |
361
+ | EyeDropper API | Chrome 95+, Edge 95+ (not Firefox/Safari) |
362
+
363
+ ---
364
+
365
+ ## License
366
+
367
+ MIT