@bornova/colorpicker 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.
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Converts HSVA color values to RGBA.
3
+ * @param {HSVA} hsva
4
+ * @returns {RGBA}
5
+ */
6
+ export function HSVAtoRGBA(hsva: HSVA): RGBA;
7
+ /**
8
+ * Converts HSVA color values to HSLA.
9
+ * @param {HSVA} hsva
10
+ * @returns {HSLA}
11
+ */
12
+ export function HSVAtoHSLA(hsva: HSVA): HSLA;
13
+ /**
14
+ * Converts RGBA color values to HSVA.
15
+ * @param {RGBA} rgba
16
+ * @returns {HSVA}
17
+ */
18
+ export function RGBAtoHSVA(rgba: RGBA): HSVA;
19
+ /**
20
+ * Parses any valid CSS color string and returns its RGBA components.
21
+ * @param {string} str - Any valid CSS color string.
22
+ * @returns {RGBA}
23
+ */
24
+ export function strToRGBA(str: string): RGBA;
25
+ /**
26
+ * Converts RGBA color values to a CSS hex color string.
27
+ * @param {RGBA} rgba
28
+ * @returns {string} Hex color string (e.g. `#rrggbb` or `#rrggbbaa`).
29
+ */
30
+ export function RGBAToHex(rgba: RGBA): string;
31
+ /**
32
+ * Converts RGBA color values to a CSS `rgb()` or `rgba()` string.
33
+ * @param {RGBA} rgba
34
+ * @returns {string}
35
+ */
36
+ export function RGBAToStr(rgba: RGBA): string;
37
+ /**
38
+ * Converts HSLA color values to a CSS `hsl()` or `hsla()` string.
39
+ * @param {HSLA} hsla
40
+ * @returns {string}
41
+ */
42
+ export function HSLAToStr(hsla: HSLA): string;
43
+ export type RGBA = {
44
+ /**
45
+ * - Red channel (0–255)
46
+ */
47
+ r: number;
48
+ /**
49
+ * - Green channel (0–255)
50
+ */
51
+ g: number;
52
+ /**
53
+ * - Blue channel (0–255)
54
+ */
55
+ b: number;
56
+ /**
57
+ * - Alpha channel (0–1)
58
+ */
59
+ a: number;
60
+ };
61
+ export type HSVA = {
62
+ /**
63
+ * - Hue (0–360)
64
+ */
65
+ h: number;
66
+ /**
67
+ * - Saturation (0–100)
68
+ */
69
+ s: number;
70
+ /**
71
+ * - Value/brightness (0–100)
72
+ */
73
+ v: number;
74
+ /**
75
+ * - Alpha channel (0–1)
76
+ */
77
+ a: number;
78
+ };
79
+ export type HSLA = {
80
+ /**
81
+ * - Hue (0–360)
82
+ */
83
+ h: number;
84
+ /**
85
+ * - Saturation (0–100)
86
+ */
87
+ s: number;
88
+ /**
89
+ * - Lightness (0–100)
90
+ */
91
+ l: number;
92
+ /**
93
+ * - Alpha channel (0–1)
94
+ */
95
+ a: number;
96
+ };
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Returns the element with the given ID.
3
+ * @param {string} id
4
+ * @returns {HTMLElement | null}
5
+ */
6
+ export function getEl(id: string): HTMLElement | null;
7
+ /**
8
+ * Attaches an event listener to an element, with optional CSS selector delegation.
9
+ * @param {EventTarget} context - The element to attach the listener to.
10
+ * @param {string} type - The event type.
11
+ * @param {string | EventListener} selector - A CSS selector for event delegation, or a direct event handler.
12
+ * @param {EventListener} [fn] - Event handler (required when `selector` is a string).
13
+ * @returns {void}
14
+ */
15
+ export function addListener(context: EventTarget, type: string, selector: string | EventListener, fn?: EventListener): void;
16
+ /**
17
+ * Calls `fn` immediately if the DOM is ready, otherwise defers until `DOMContentLoaded`.
18
+ * @param {Function} fn - The function to call.
19
+ * @param {any[]} [args] - Arguments to pass to `fn`.
20
+ * @returns {void}
21
+ */
22
+ export function DOMReady(fn: Function, args?: any[]): void;
@@ -0,0 +1,18 @@
1
+ export default ColorPicker;
2
+ /**
3
+ * Initializes the color picker, binding it to matching fields or applying global options.
4
+ * @param {string | ColorPickerOptions} [options] - A CSS selector string to bind matching fields, or an options object.
5
+ * @returns {void}
6
+ */
7
+ declare function ColorPicker(options?: string | ColorPickerOptions): void;
8
+ declare namespace ColorPicker {
9
+ export function set(arg0: ColorPickerOptions): void;
10
+ export function wrap(arg0: string | HTMLElement | HTMLElement[]): void;
11
+ export function close(arg0?: boolean | undefined): void;
12
+ export function setInstance(arg0: string, arg1: ColorPickerOptions): void;
13
+ export function removeInstance(arg0: string): void;
14
+ export function updatePosition(): void;
15
+ export { DOMReady as ready };
16
+ }
17
+ import type { ColorPickerOptions } from './settings.js';
18
+ import { DOMReady } from './dom.js';
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Applies configuration options to the color picker.
3
+ * @param {ColorPickerOptions} options
4
+ * @returns {void}
5
+ */
6
+ export function configure(options: ColorPickerOptions): void;
7
+ /**
8
+ * Registers a virtual instance that applies custom options to fields matching a CSS selector.
9
+ * @param {string} selector - CSS selector identifying the target field(s).
10
+ * @param {ColorPickerOptions} options - Options to apply when the picker opens for a matching field.
11
+ * @returns {void}
12
+ */
13
+ export function setVirtualInstance(selector: string, options: ColorPickerOptions): void;
14
+ /**
15
+ * Removes a previously registered virtual instance.
16
+ * @param {string} selector - The CSS selector of the instance to remove.
17
+ * @returns {void}
18
+ */
19
+ export function removeVirtualInstance(selector: string): void;
20
+ /**
21
+ * Binds the color picker to one or more input fields.
22
+ * @param {string | HTMLElement | HTMLElement[]} selector - A CSS selector string, a single element, or an array of elements.
23
+ * @returns {void}
24
+ */
25
+ export function bindFields(selector: string | HTMLElement | HTMLElement[]): void;
26
+ /**
27
+ * Recalculates and applies the picker's position relative to the active input field.
28
+ * @returns {void}
29
+ */
30
+ export function updatePickerPosition(): void;
31
+ /**
32
+ * Wraps one or more color input fields in a `.clr-field` container with a preview button.
33
+ * @param {string | HTMLElement | HTMLElement[]} selector - A CSS selector string, a single element, or an array of elements.
34
+ * @returns {void}
35
+ */
36
+ export function wrapFields(selector: string | HTMLElement | HTMLElement[]): void;
37
+ /**
38
+ * Closes the color picker, optionally reverting the color to its pre-open value.
39
+ * @param {boolean} [revert] - If `true`, restores the field value to what it was when the picker opened.
40
+ * @returns {void}
41
+ */
42
+ export function closePicker(revert?: boolean): void;
43
+ /**
44
+ * Creates the picker DOM, appends it to the document, and binds all event listeners.
45
+ * @returns {void}
46
+ */
47
+ export function init(): void;
48
+ import type { ColorPickerOptions } from './settings.js';
@@ -0,0 +1,132 @@
1
+ /**
2
+ * @typedef {Object} A11yLabels
3
+ * @property {string} open
4
+ * @property {string} close
5
+ * @property {string} clear
6
+ * @property {string} marker
7
+ * @property {string} hueSlider
8
+ * @property {string} alphaSlider
9
+ * @property {string} input
10
+ * @property {string} format
11
+ * @property {string} swatch
12
+ * @property {string} instruction
13
+ */
14
+ /**
15
+ * @typedef {Object} ColorPickerOptions
16
+ * @property {string|HTMLElement|HTMLElement[]} [el]
17
+ * @property {string|HTMLElement} [parent]
18
+ * @property {string} [theme]
19
+ * @property {'light'|'dark'|'auto'} [themeMode]
20
+ * @property {boolean} [rtl]
21
+ * @property {boolean} [wrap]
22
+ * @property {number} [margin]
23
+ * @property {'hex'|'rgb'|'hsl'|'auto'|'mixed'} [format]
24
+ * @property {boolean} [formatToggle]
25
+ * @property {string[]} [swatches]
26
+ * @property {boolean} [swatchesOnly]
27
+ * @property {boolean} [alpha]
28
+ * @property {boolean} [forceAlpha]
29
+ * @property {boolean} [focusInput]
30
+ * @property {boolean} [selectInput]
31
+ * @property {boolean} [inline]
32
+ * @property {string} [defaultColor]
33
+ * @property {boolean|{show: boolean, label: string}} [clearButton]
34
+ * @property {string} [clearLabel]
35
+ * @property {boolean} [closeButton]
36
+ * @property {string} [closeLabel]
37
+ * @property {function(string, HTMLElement=): void} [onChange]
38
+ * @property {Partial<A11yLabels>} [a11y]
39
+ */
40
+ /**
41
+ * @typedef {Object} Settings
42
+ * @property {string} el
43
+ * @property {string} parent
44
+ * @property {string} theme
45
+ * @property {string} themeMode
46
+ * @property {boolean} rtl
47
+ * @property {boolean} wrap
48
+ * @property {number} margin
49
+ * @property {string} format
50
+ * @property {boolean} formatToggle
51
+ * @property {string[]} swatches
52
+ * @property {boolean} swatchesOnly
53
+ * @property {boolean} alpha
54
+ * @property {boolean} forceAlpha
55
+ * @property {boolean} focusInput
56
+ * @property {boolean} selectInput
57
+ * @property {boolean} inline
58
+ * @property {string} defaultColor
59
+ * @property {boolean} clearButton
60
+ * @property {string} clearLabel
61
+ * @property {boolean} closeButton
62
+ * @property {string} closeLabel
63
+ * @property {function(string, HTMLElement=): void} onChange
64
+ * @property {A11yLabels} a11y
65
+ */
66
+ /** @type {Settings} */
67
+ export const settings: Settings;
68
+ export type A11yLabels = {
69
+ open: string;
70
+ close: string;
71
+ clear: string;
72
+ marker: string;
73
+ hueSlider: string;
74
+ alphaSlider: string;
75
+ input: string;
76
+ format: string;
77
+ swatch: string;
78
+ instruction: string;
79
+ };
80
+ export type ColorPickerOptions = {
81
+ el?: string | HTMLElement | HTMLElement[] | undefined;
82
+ parent?: string | HTMLElement | undefined;
83
+ theme?: string | undefined;
84
+ themeMode?: "auto" | "light" | "dark" | undefined;
85
+ rtl?: boolean | undefined;
86
+ wrap?: boolean | undefined;
87
+ margin?: number | undefined;
88
+ format?: "auto" | "rgb" | "hex" | "hsl" | "mixed" | undefined;
89
+ formatToggle?: boolean | undefined;
90
+ swatches?: string[] | undefined;
91
+ swatchesOnly?: boolean | undefined;
92
+ alpha?: boolean | undefined;
93
+ forceAlpha?: boolean | undefined;
94
+ focusInput?: boolean | undefined;
95
+ selectInput?: boolean | undefined;
96
+ inline?: boolean | undefined;
97
+ defaultColor?: string | undefined;
98
+ clearButton?: boolean | {
99
+ show: boolean;
100
+ label: string;
101
+ } | undefined;
102
+ clearLabel?: string | undefined;
103
+ closeButton?: boolean | undefined;
104
+ closeLabel?: string | undefined;
105
+ onChange?: ((arg0: string, arg1?: HTMLElement | undefined) => void) | undefined;
106
+ a11y?: Partial<A11yLabels> | undefined;
107
+ };
108
+ export type Settings = {
109
+ el: string;
110
+ parent: string;
111
+ theme: string;
112
+ themeMode: string;
113
+ rtl: boolean;
114
+ wrap: boolean;
115
+ margin: number;
116
+ format: string;
117
+ formatToggle: boolean;
118
+ swatches: string[];
119
+ swatchesOnly: boolean;
120
+ alpha: boolean;
121
+ forceAlpha: boolean;
122
+ focusInput: boolean;
123
+ selectInput: boolean;
124
+ inline: boolean;
125
+ defaultColor: string;
126
+ clearButton: boolean;
127
+ clearLabel: string;
128
+ closeButton: boolean;
129
+ closeLabel: string;
130
+ onChange: (arg0: string, arg1: HTMLElement | undefined) => void;
131
+ a11y: A11yLabels;
132
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@bornova/colorpicker",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight and elegant JavaScript color picker written in vanilla JavaScript. Zero dependencies.",
5
+ "keywords": [
6
+ "color",
7
+ "picker",
8
+ "javascript"
9
+ ],
10
+ "homepage": "https://github.com/bornova/ColorPicker#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/bornova/ColorPicker/issues"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/bornova/ColorPicker.git"
17
+ },
18
+ "license": "MIT",
19
+ "type": "module",
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/types/index.d.ts",
23
+ "import": "./dist/esm/colorpicker.js"
24
+ }
25
+ },
26
+ "jsdelivr": "./dist/browser/colorpicker.min.js",
27
+ "unpkg": "./dist/browser/colorpicker.min.js",
28
+ "types": "./dist/types/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "scripts": {
33
+ "prebuild": "npm run lint && npm run prettier",
34
+ "build": "rollup -c && npm run types",
35
+ "format": "npm run prettier:fix && npm run lint:fix",
36
+ "lint": "eslint .",
37
+ "lint:fix": "eslint --fix .",
38
+ "prettier": "prettier --check .",
39
+ "prettier:fix": "prettier --write .",
40
+ "types": "tsc"
41
+ },
42
+ "devDependencies": {
43
+ "@eslint/js": "^10.0.1",
44
+ "@rollup/plugin-terser": "^1.0.0",
45
+ "eslint": "^10.4.0",
46
+ "globals": "^17.6.0",
47
+ "prettier": "^3.8.3",
48
+ "rollup": "^4.60.4",
49
+ "typescript": "^6.0.3"
50
+ }
51
+ }