yk-color-picker 1.0.0-alpha

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Yassine KLILICH
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,354 @@
1
+ # YKColorPicker
2
+
3
+ [![Generic badge](https://img.shields.io/badge/npm%20package-v1.0.0%2D%2Dalpha-3FB911.svg)](https://www.npmjs.com/package/ngx-interpolation)
4
+
5
+ YKColorPicker is a flexible color picker library designed with a strong focus on user experience (UX), including full keyboard interaction support. It provides a seamless way to integrate a customizable color picker into your project, offering various color models such as RGB, HSV, HSL, and HEX.
6
+
7
+ ## Table of Contents
8
+
9
+ 1. [Features](#features)
10
+ 2. [Installation](#installation)
11
+ 3. [Usage](#usage)
12
+ 4. [Methods](#methods)
13
+ - [open()](#open)
14
+ - [close()](#close)
15
+ - [getRGB()](#getrgb)
16
+ - [getHSV()](#gethsv)
17
+ - [getHSL()](#gethsl)
18
+ - [getHEX()](#gethex)
19
+ - [getColor()](#getcolor)
20
+ - [setColor(value: string)](#setcolorvalue-string)
21
+ - [updateOptions(options: YKColorPickerOptions)](#updateoptionsoptions-ykcolorpickeroptions)
22
+ 5. [Options](#options)
23
+ 6. [Events](#events)
24
+ 7. [Example](#example)
25
+ 8. [References](#references)
26
+ - [YKColorPickerPosition](#ykcolorpickerposition)
27
+ - [YKColorPickerMode](#ykcolorpickermode)
28
+ - [YKColorPickerOptions](#ykcolorpickeroptions)
29
+ - [YKColorPickerPositionFallback](#ykcolorpickerpositionfallback)
30
+ 9. [License](#license)
31
+
32
+ ## [Features](#features)
33
+
34
+ - **Multiple Color Formats**: Supports RGB, HSV, HSL, and HEX color formats.
35
+ - **Customizable Position**: The color picker can be positioned relative to a target element (top, bottom, left, right) with fallback positions.
36
+ - **Theme Support**: Light and dark themes are available.
37
+ - **Event Handlers**: Provides various event handlers for initialization, opening, closing, input changes, and more.
38
+ - **Copy to Clipboard**: Allows users to copy the selected color to the clipboard.
39
+ - **Keyboard Interaction**: Full keyboard interaction support for accessibility and ease of use.
40
+
41
+ ## [Installation](#installation)
42
+
43
+ To install YKColorPicker, you can use npm:
44
+
45
+ ```sh
46
+ npm install yk-color-picker
47
+ ```
48
+
49
+ ## [Usage](#usage)
50
+
51
+ Simply import `YKColorPicker` class and apply it:
52
+
53
+ ```javascript
54
+ import { YKColorPicker } from "yk-color-picker";
55
+
56
+ const colorPicker = new YKColorPicker({
57
+ target: document.getElementById("color-picker-trigger"),
58
+ color: "red",
59
+ });
60
+ ```
61
+
62
+ ## [Methods](#methods)
63
+
64
+ ### open()
65
+
66
+ Opens the color picker.
67
+
68
+ Example:
69
+
70
+ ```javascript
71
+ const colorPicker = new YKColorPicker({
72
+ target: document.getElementById("color-picker-trigger"),
73
+ color: "red",
74
+ });
75
+
76
+ // Open the color picker
77
+ colorPicker.open();
78
+ ```
79
+
80
+ ### close()
81
+
82
+ Closes the color picker.
83
+
84
+ Example:
85
+
86
+ ```javascript
87
+ const colorPicker = new YKColorPicker({
88
+ target: document.getElementById("color-picker-trigger"),
89
+ color: "red",
90
+ });
91
+
92
+ // Open the color picker
93
+ colorPicker.open();
94
+
95
+ // Close the color picker after 3 seconds
96
+ setTimeout(() => {
97
+ colorPicker.close();
98
+ }, 3000);
99
+ ```
100
+
101
+ ### getRGB()
102
+
103
+ Returns the current color in RGB format.
104
+
105
+ Example:
106
+
107
+ ```javascript
108
+ const colorPicker = new YKColorPicker({
109
+ target: document.getElementById("color-picker-trigger"),
110
+ color: "red",
111
+ });
112
+
113
+ // Get the current color in RGB format
114
+ const rgbColor = colorPicker.getRGB();
115
+ console.log(rgbColor); // { r: 255, g: 0, b: 0, a: 1 }
116
+ ```
117
+
118
+ ### getHSV()
119
+
120
+ Returns the current color in HSV format.
121
+
122
+ Example:
123
+
124
+ ```javascript
125
+ const colorPicker = new YKColorPicker({
126
+ target: document.getElementById("color-picker-trigger"),
127
+ color: "red",
128
+ });
129
+
130
+ // Get the current color in HSV format
131
+ const hsvColor = colorPicker.getHSV();
132
+ console.log(hsvColor); // { h: 0, s: 100, v: 100, a: 1 }
133
+ ```
134
+
135
+ ### getHSL()
136
+
137
+ Returns the current color in HSL format.
138
+
139
+ Example:
140
+
141
+ ```javascript
142
+ const colorPicker = new YKColorPicker({
143
+ target: document.getElementById("color-picker-trigger"),
144
+ color: "red",
145
+ });
146
+
147
+ // Get the current color in HSL format
148
+ const hslColor = colorPicker.getHSL();
149
+ console.log(hslColor); // { h: 0, s: 100, l: 50, a: 1 }
150
+ ```
151
+
152
+ ### getHEX()
153
+
154
+ Returns the current color in HEX format.
155
+
156
+ Example:
157
+
158
+ ```javascript
159
+ const colorPicker = new YKColorPicker({
160
+ target: document.getElementById("color-picker-trigger"),
161
+ color: "red",
162
+ });
163
+
164
+ // Get the current color in HEX format
165
+ const hexColor = colorPicker.getHEX();
166
+ console.log(hexColor); // "#ff0000"
167
+ ```
168
+
169
+ ### getColor()
170
+
171
+ Returns the current color in the selected representation format.
172
+
173
+ Example:
174
+
175
+ ```javascript
176
+ const colorPicker = new YKColorPicker({
177
+ target: document.getElementById("color-picker-trigger"),
178
+ representation: YKColorPickerMode.RGB, // Set representation to RGB
179
+ color: "red",
180
+ });
181
+
182
+ // Get the current color in the selected representation format
183
+ const currentColor = colorPicker.getColor();
184
+ console.log(currentColor); // { r: 255, g: 0, b: 0, a: 1 }
185
+ ```
186
+
187
+ ### setColor(value: string)
188
+
189
+ Sets the color picker to the specified color.
190
+
191
+ Example:
192
+
193
+ ```javascript
194
+ const colorPicker = new YKColorPicker({
195
+ target: document.getElementById("color-picker-trigger"),
196
+ color: "red",
197
+ });
198
+
199
+ // Set the color to green
200
+ colorPicker.setColor("#00FF00");
201
+
202
+ // Verify the color change
203
+ console.log(colorPicker.getHEX()); // "#00ff00"
204
+ ```
205
+
206
+ ### updateOptions(options: YKColorPickerOptions)
207
+
208
+ Updates the color picker options dynamically.
209
+
210
+ Example:
211
+
212
+ ```javascript
213
+ const colorPicker = new YKColorPicker({
214
+ target: document.getElementById("color-picker-trigger"),
215
+ color: "red",
216
+ });
217
+
218
+ // Update options to change the theme and representation
219
+ colorPicker.updateOptions({
220
+ theme: "dark",
221
+ representation: YKColorPickerMode.HEX,
222
+ color: "#0000FF", // Change color to blue
223
+ });
224
+
225
+ // Verify the updated options
226
+ console.log(colorPicker.getHEX()); // "#0000ff"
227
+ ```
228
+
229
+ ### Options
230
+
231
+ - **target**: The element or selector that triggers the color picker.
232
+ - **container**: The container element or selector where the color picker will be appended. If not defined, the color picker will be appended to the `<body>` by default.
233
+ - **position**: The preferred position of the color picker relative to the target. Use `YKColorPickerPosition` to set the position (e.g., `YKColorPickerPosition.TOP`, `YKColorPickerPosition.BOTTOM`, `YKColorPickerPosition.LEFT`, `YKColorPickerPosition.RIGHT`).
234
+ - **positionFallback**: The fallback positions if the preferred position is not available. Use a combination of `b` (bottom), `t` (top), `r` (right), and `l` (left) to define the fallback order (e.g., `'btrl'` means try bottom first, then top, then right, then left).
235
+ - **representation**: The initial color representation format. Use `YKColorPickerMode` to set the representation (e.g., `YKColorPickerMode.RGB`, `YKColorPickerMode.HSV`, `YKColorPickerMode.HSL`, `YKColorPickerMode.HEX`).
236
+ - **color**: The initial color.
237
+ - **closeOnScroll**: Whether to close the color picker on scroll.
238
+ - **closeOnResize**: Whether to close the color picker on window resize.
239
+ - **theme**: The theme of the color picker (light or dark).
240
+ - **onInit**: Callback function triggered when the color picker is initialized.
241
+ - **onOpen**: Callback function triggered when the color picker is opened.
242
+ - **onClose**: Callback function triggered when the color picker is closed.
243
+ - **onInput**: Callback function triggered when the color input changes.
244
+ - **onChange**: Callback function triggered when the color changes.
245
+ - **onCopy**: Callback function triggered when the color is copied to the clipboard.
246
+ - **onRepresentationChange**: Callback function triggered when the color representation changes.
247
+ - **onTargetChange**: Callback function triggered when the target element changes.
248
+ - **onContainerChange**: Callback function triggered when the container changes.
249
+
250
+ ### Events
251
+
252
+ - **onInit**: Triggered when the color picker is initialized.
253
+ - **onOpen**: Triggered when the color picker is opened.
254
+ - **onClose**: Triggered when the color picker is closed.
255
+ - **onInput**: Triggered when the color input changes.
256
+ - **onChange**: Triggered when the color changes.
257
+ - **onCopy**: Triggered when the color is copied to the clipboard.
258
+ - **onRepresentationChange**: Triggered when the color representation changes.
259
+ - **onTargetChange**: Triggered when the target element changes.
260
+ - **onContainerChange**: Triggered when the container changes.
261
+
262
+ ## Example
263
+
264
+ ```html
265
+ <button id="color-picker-trigger">Open Color Picker</button>
266
+
267
+ <script>
268
+ const colorPicker = new YKColorPicker({
269
+ target: document.getElementById("color-picker-trigger"),
270
+ container: "#container",
271
+ position: YKColorPickerPosition.BOTTOM, // Use YKColorPickerPosition to set the position
272
+ positionFallback: "btrl", // Fallback positions: 'b' for bottom, 't' for top, 'r' for right, 'l' for left
273
+ representation: YKColorPickerMode.RGB, // Use YKColorPickerMode to set the representation
274
+ color: "red",
275
+ closeOnScroll: true,
276
+ closeOnResize: false,
277
+ theme: "light",
278
+ onInit: (instance) => {
279
+ console.log("Color picker initialized", instance);
280
+ },
281
+ onOpen: (instance) => {
282
+ console.log("Color picker opened", instance);
283
+ },
284
+ onClose: (instance) => {
285
+ console.log("Color picker closed", instance);
286
+ },
287
+ onInput: (instance) => {
288
+ console.log("Color input changed", instance.getColor());
289
+ },
290
+ onChange: (instance) => {
291
+ console.log("Color changed", instance.getColor());
292
+ },
293
+ onCopy: (instance) => {
294
+ console.log("Color copied to clipboard", instance.getColor());
295
+ },
296
+ onRepresentationChange: (instance) => {
297
+ console.log("Color representation changed", instance.getColor());
298
+ },
299
+ onTargetChange: (instance, previousTarget) => {
300
+ console.log("Target changed", instance, previousTarget);
301
+ },
302
+ onContainerChange: (instance, previousParent) => {
303
+ console.log("Container changed", instance, previousParent);
304
+ },
305
+ });
306
+ </script>
307
+ ```
308
+
309
+ ## References
310
+
311
+ ### YKColorPickerPosition:
312
+
313
+ An enumeration defining the possible positions for the color picker relative to the target element. Available values:
314
+
315
+ - `TOP` (`t`): Positions the picker above the target element.
316
+ - `BOTTOM` (`b`): Positions the picker below the target element.
317
+ - `LEFT` (`l`): Positions the picker to the left of the target element.
318
+ - `RIGHT` (`r`): Positions the picker to the right of the target element.
319
+
320
+ ```javascript
321
+ import { YKColorPicker, YKColorPickerPosition } from "yk-color-picker";
322
+
323
+ const colorPicker = new YKColorPicker({
324
+ target: document.getElementById("color-picker-trigger"),
325
+ position: YKColorPickerPosition.BOTTOM, // Positioning the picker below the target
326
+ color: "#ff0000",
327
+ });
328
+ ```
329
+
330
+ ### YKColorPickerMode:
331
+
332
+ Specifies the available color representation formats, such as RGB, HSV, HSL, and HEX.
333
+
334
+ ```javascript
335
+ import { YKColorPicker, YKColorPickerMode } from "yk-color-picker";
336
+
337
+ const colorPicker = new YKColorPicker({
338
+ target: document.getElementById("color-picker-trigger"),
339
+ representation: YKColorPickerMode.RGB, // Using RGB color format
340
+ color: "#00ff00",
341
+ });
342
+ ```
343
+
344
+ ### YKColorPickerOptions:
345
+
346
+ An interface defining configuration options for the color picker.
347
+
348
+ ### YKColorPickerPositionFallback:
349
+
350
+ A type that defines fallback position strategies when the preferred position is unavailable. Examples include `"btrl"`, `"tblr"`, and `"lrtb"`.
351
+
352
+ ## License
353
+
354
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
@@ -0,0 +1 @@
1
+ :root{--yk-box-shadow-color: rgba(0, 0, 0, .2);--yk-border-color: thin solid white;--yk-box-shadow: 0 0 0 1px black;--yk-slider-box-shadow: 0px 0px 6px #777;--yk-hue-gradient: linear-gradient( 90deg, red, #ff0, #0f0, #0ff, #00f, #f0f, red );--yk-opacity-gradient: linear-gradient(90deg, transparent, red);--yk-light-bg-color: #f7f7f7;--yk-light-thumb-bg-color: #fff;--yk-light-thumb-focus-outline: #383838;--yk-light-input-bg-color: #fcfcfc;--yk-light-input-text-color: #3b3b3b;--yk-light-input-border-color: #d6d6d6;--yk-light-input-focus-border-color: #467dff;--yk-light-label-color: #3b3b3b;--yk-light-preview-stroke: #c8c8c8;--yk-light-icon-fill: #696969;--yk-light-icon-hover-bg-color: #dfdfdf;--yk-dark-bg-color: #383838;--yk-dark-thumb-bg-color: #323232;--yk-dark-thumb-focus-outline: #fff;--yk-dark-input-bg-color: #383838;--yk-dark-input-text-color: #d6d6d6;--yk-dark-input-border-color: #5a5a5a;--yk-dark-input-focus-border-color: #0e639c;--yk-dark-label-color: #d6d6d6;--yk-dark-preview-stroke: gray;--yk-dark-icon-fill: #bcbcbc;--yk-dark-icon-hover-bg-color: #4e4e4e}.yk-overlay-wrapper{touch-action:none;position:fixed;z-index:1;transition:opacity .3s,visibility .3s;opacity:0;visibility:hidden}.yk-overlay-wrapper--open{transition:opacity .3s;opacity:1;visibility:visible}.yk-overlay-wrapper:not(.yk-overlay-wrapper--static){top:4px;left:4px}.yk-overlay-wrapper.yk-overlay-wrapper--static{position:static}.yk-wrapper{box-shadow:var(--yk-box-shadow-color) 0 4px 10px;display:inline-block;width:280px;position:relative;border-radius:6px;font-family:sans-serif;-moz-user-select:none;-webkit-user-select:none;user-select:none}.yk-palette{height:150px;border-top-left-radius:6px;border-top-right-radius:6px;background-image:linear-gradient(180deg,transparent 0%,rgba(0,0,0,1) 100%),linear-gradient(90deg,#fff,red)}.yk-cursor{position:absolute;top:-6px;left:-6px;width:10px;height:10px;border-radius:50%;border:var(--yk-border-color);box-shadow:var(--yk-box-shadow)}.yk-color-settings{display:flex;align-items:center;padding:12px 14px 12px 12px;flex-wrap:wrap;row-gap:12px}.yk-sliders{flex-grow:1;display:flex;row-gap:12px;flex-direction:column}.yk-hue-slider-wrapper,.yk-opacity-slider-wrapper{position:relative;-moz-user-select:none;-webkit-user-select:none;user-select:none;flex-grow:1}.yk-opacity-slider-wrapper{background-image:url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyIDIiPjxwYXRoIGZpbGw9IndoaXRlIiBkPSJNMSwwSDJWMUgxVjBaTTAsMUgxVjJIMFYxWiIvPjxwYXRoIGZpbGw9IiNEQkRCREIiIGQ9Ik0wLDBIMVYxSDBWMFpNMSwxSDJWMkgxVjFaIi8+PC9zdmc+);background-size:8px;border-radius:2px;height:10px}.yk-hue-slider{width:100%;height:10px;border-radius:2px;background-image:var(--yk-hue-gradient)}.yk-opacity-color{background-image:var(--yk-opacity-gradient);position:absolute;width:100%;height:100%;border-radius:2px}.yk-hue-slider-thumb,.yk-opacity-slider-thumb{position:absolute;width:16px;height:16px;border-radius:50px;box-shadow:var(--yk-slider-box-shadow);top:50%;transform:translate(-8px,-50%);outline:none}.yk-hue-slider-thumb:focus,.yk-opacity-slider-thumb:focus{outline:1px solid}.yk-color-preview-wrapper{margin:0 16px 0 10px}.yk-color-preview{position:absolute;width:100%;height:100%}.yk-color-model-wrapper{display:flex;flex-basis:100%;position:relative;align-items:center;column-gap:6px}.yk-color-model{display:flex;flex-direction:column;align-items:center;row-gap:6px;flex-grow:1}.yk-hex-input,.yk-input-wrapper{display:grid;width:100%;justify-items:center;row-gap:4px}.yk-input-wrapper{grid-template-columns:1fr 1fr 1fr 1fr}.yk-rgb-input,.yk-hsv-input,.yk-hsl-input{display:grid;width:100%;justify-items:center;row-gap:4px}.yk-rgb-input,.yk-hsv-input,.yk-hsl-input{grid-template-columns:1fr 1fr 1fr 1fr}.yk-hex-input{grid-template-columns:1fr}.yk-color-input{font-family:inherit;height:20px;outline:none;border:1px solid;font-size:12px;text-align:center;width:42px;border-radius:4px}.yk-hex-input .yk-color-input{width:180px}.yk-color-model-label{font-size:12px}.yk-clipboard-color,.yk-color-model-switch{width:16px;height:16px;background-position:center;background-repeat:no-repeat;padding:5px;border-radius:4px;cursor:pointer;box-sizing:content-box;margin:0;background-color:transparent;border:none;outline:none}.yk-overlay-wrapper--light .yk-wrapper{background-color:var(--yk-light-bg-color)}.yk-overlay-wrapper--light :where(.yk-hue-slider-thumb,.yk-opacity-slider-thumb){background-color:var(--yk-light-thumb-bg-color)}.yk-overlay-wrapper--light .yk-hue-slider-thumb:focus,.yk-overlay-wrapper--light .yk-opacity-slider-thumb:focus{outline-color:var(--yk-light-thumb-focus-outline)}.yk-overlay-wrapper--light .yk-color-input{background-color:var(--yk-light-input-bg-color);color:var(--yk-light-input-text-color);border-color:var(--yk-light-input-border-color)}.yk-overlay-wrapper--light .yk-color-input:focus{border-color:var(--yk-light-input-focus-border-color)}.yk-overlay-wrapper--light .yk-color-model-label{color:var(--yk-light-label-color)}.yk-overlay-wrapper--light .yk-preview-stroke{stroke:var(--yk-light-preview-stroke)}.yk-overlay-wrapper--light :where(.yk-clipboard-color svg,.yk-color-model-switch svg){fill:var(--yk-light-icon-fill)}.yk-overlay-wrapper--light :where(.yk-clipboard-color:hover,.yk-clipboard-color:focus,.yk-color-model-switch:hover,.yk-color-model-switch:focus){background-color:var(--yk-light-icon-hover-bg-color)}.yk-overlay-wrapper--dark .yk-wrapper{background-color:var(--yk-dark-bg-color)}.yk-overlay-wrapper--dark :where(.yk-hue-slider-thumb,.yk-opacity-slider-thumb){background-color:var(--yk-dark-thumb-bg-color)}.yk-overlay-wrapper--dark .yk-hue-slider-thumb:focus,.yk-overlay-wrapper--dark .yk-opacity-slider-thumb:focus{outline-color:var(--yk-dark-thumb-focus-outline)}.yk-overlay-wrapper--dark .yk-color-input{background-color:var(--yk-dark-input-bg-color);color:var(--yk-dark-input-text-color);border-color:var(--yk-dark-input-border-color)}.yk-overlay-wrapper--dark .yk-color-input:focus{border-color:var(--yk-dark-input-focus-border-color)}.yk-overlay-wrapper--dark .yk-color-model-label{color:var(--yk-dark-label-color)}.yk-overlay-wrapper--dark .yk-preview-stroke{stroke:var(--yk-dark-preview-stroke)}.yk-overlay-wrapper--dark :where(.yk-clipboard-color svg,.yk-color-model-switch svg){fill:var(--yk-dark-icon-fill)}.yk-overlay-wrapper--dark :where(.yk-clipboard-color:hover,.yk-clipboard-color:focus,.yk-color-model-switch:hover,.yk-color-model-switch:focus){background-color:var(--yk-dark-icon-hover-bg-color)}