@molecule/app-color-picker-react 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.
- package/LICENSE +115 -0
- package/dist/ColorPicker.d.ts +24 -0
- package/dist/ColorPicker.d.ts.map +1 -0
- package/dist/ColorPicker.js +89 -0
- package/dist/conversions.d.ts +54 -0
- package/dist/conversions.d.ts.map +1 -0
- package/dist/conversions.js +124 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +19 -0
- package/dist/types.d.ts +35 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ColorPickerProps } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Controlled color picker — HSV (hue + saturation + value) sliders, RGB
|
|
4
|
+
* sliders, and a HEX text input. All four representations stay in lockstep
|
|
5
|
+
* with the controlling `value`.
|
|
6
|
+
*
|
|
7
|
+
* Designed for design canvases, photo editors, animation tools, and brand
|
|
8
|
+
* editors. Pure UI — no popover/anchor logic; parents render this inside
|
|
9
|
+
* whatever portal/popover they prefer.
|
|
10
|
+
*
|
|
11
|
+
* Styling is delegated to `getClassMap()`; only inline styles are used for
|
|
12
|
+
* the live swatch background and slider track gradients.
|
|
13
|
+
*
|
|
14
|
+
* @param props - Component props.
|
|
15
|
+
* @returns The rendered picker element.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```tsx
|
|
19
|
+
* const [color, setColor] = useState('#3366ff')
|
|
20
|
+
* <ColorPicker value={color} onChange={setColor} />
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function ColorPicker({ value, onChange, dataMolId, className, }: ColorPickerProps): React.JSX.Element;
|
|
24
|
+
//# sourceMappingURL=ColorPicker.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ColorPicker.d.ts","sourceRoot":"","sources":["../src/ColorPicker.tsx"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,WAAW,CAAC,EAC1B,KAAK,EACL,QAAQ,EACR,SAAS,EACT,SAAS,GACV,EAAE,gBAAgB,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CA8KtC"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useState } from 'react';
|
|
3
|
+
import { useTranslation } from '@molecule/app-react';
|
|
4
|
+
import { getClassMap } from '@molecule/app-ui';
|
|
5
|
+
import { hexToRgb, hsvToRgb, isValidHex, rgbToHex, rgbToHsv } from './conversions.js';
|
|
6
|
+
/**
|
|
7
|
+
* Controlled color picker — HSV (hue + saturation + value) sliders, RGB
|
|
8
|
+
* sliders, and a HEX text input. All four representations stay in lockstep
|
|
9
|
+
* with the controlling `value`.
|
|
10
|
+
*
|
|
11
|
+
* Designed for design canvases, photo editors, animation tools, and brand
|
|
12
|
+
* editors. Pure UI — no popover/anchor logic; parents render this inside
|
|
13
|
+
* whatever portal/popover they prefer.
|
|
14
|
+
*
|
|
15
|
+
* Styling is delegated to `getClassMap()`; only inline styles are used for
|
|
16
|
+
* the live swatch background and slider track gradients.
|
|
17
|
+
*
|
|
18
|
+
* @param props - Component props.
|
|
19
|
+
* @returns The rendered picker element.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```tsx
|
|
23
|
+
* const [color, setColor] = useState('#3366ff')
|
|
24
|
+
* <ColorPicker value={color} onChange={setColor} />
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function ColorPicker({ value, onChange, dataMolId, className, }) {
|
|
28
|
+
const cm = getClassMap();
|
|
29
|
+
const { t } = useTranslation();
|
|
30
|
+
const rgb = hexToRgb(value);
|
|
31
|
+
const hsv = rgbToHsv(rgb);
|
|
32
|
+
// Local hex-input state — lets the user type partial values without the
|
|
33
|
+
// controlled `value` snapping back on every keystroke.
|
|
34
|
+
const [hexDraft, setHexDraft] = useState(null);
|
|
35
|
+
const hexShown = hexDraft ?? rgbToHex(rgb);
|
|
36
|
+
/**
|
|
37
|
+
* Merges partial HSV overrides with the current HSV value and emits the result via `onChange`.
|
|
38
|
+
*/
|
|
39
|
+
function emitFromHsv(next) {
|
|
40
|
+
const merged = { h: next.h ?? hsv.h, s: next.s ?? hsv.s, v: next.v ?? hsv.v };
|
|
41
|
+
onChange(rgbToHex(hsvToRgb(merged)));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Merges partial RGB overrides with the current RGB value and emits the result via `onChange`.
|
|
45
|
+
*/
|
|
46
|
+
function emitFromRgb(next) {
|
|
47
|
+
onChange(rgbToHex({
|
|
48
|
+
r: next.r ?? rgb.r,
|
|
49
|
+
g: next.g ?? rgb.g,
|
|
50
|
+
b: next.b ?? rgb.b,
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Validates and commits the raw hex string: normalises and calls `onChange`, then clears the draft.
|
|
55
|
+
*/
|
|
56
|
+
function commitHex(raw) {
|
|
57
|
+
if (isValidHex(raw)) {
|
|
58
|
+
const norm = rgbToHex(hexToRgb(raw));
|
|
59
|
+
onChange(norm);
|
|
60
|
+
setHexDraft(null);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// Reset draft on blur for invalid input.
|
|
64
|
+
setHexDraft(null);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const swatchStyle = {
|
|
68
|
+
background: rgbToHex(rgb),
|
|
69
|
+
width: 56,
|
|
70
|
+
height: 56,
|
|
71
|
+
borderRadius: 8,
|
|
72
|
+
border: '1px solid var(--mol-color-outline, rgba(0,0,0,0.15))',
|
|
73
|
+
flexShrink: 0,
|
|
74
|
+
};
|
|
75
|
+
const hueGradient = 'linear-gradient(to right, #ff0000 0%, #ffff00 17%, #00ff00 33%, #00ffff 50%, #0000ff 67%, #ff00ff 83%, #ff0000 100%)';
|
|
76
|
+
const sliderBase = {
|
|
77
|
+
width: '100%',
|
|
78
|
+
height: 14,
|
|
79
|
+
appearance: 'none',
|
|
80
|
+
borderRadius: 7,
|
|
81
|
+
cursor: 'pointer',
|
|
82
|
+
};
|
|
83
|
+
const wrapperClass = cm.cn(cm.flex({ direction: 'col', gap: 'sm' }), className);
|
|
84
|
+
return (_jsxs("div", { className: wrapperClass, "data-mol-id": dataMolId ?? 'color-picker', role: "group", "aria-label": t('colorPicker.group', {}, { defaultValue: 'Color picker' }), children: [_jsxs("div", { className: cm.flex({ align: 'center', gap: 'sm' }), children: [_jsx("span", { "aria-hidden": true, style: swatchStyle, "data-mol-id": "color-picker-swatch" }), _jsx("input", { type: "text", value: hexShown, onChange: (e) => setHexDraft(e.target.value), onBlur: (e) => commitHex(e.target.value), onKeyDown: (e) => {
|
|
85
|
+
if (e.key === 'Enter') {
|
|
86
|
+
commitHex(e.target.value);
|
|
87
|
+
}
|
|
88
|
+
}, "aria-label": t('colorPicker.hex', {}, { defaultValue: 'HEX color' }), "data-mol-id": "color-picker-hex", spellCheck: false, maxLength: 7, style: { fontFamily: 'monospace', padding: '0.3rem 0.5rem' } })] }), _jsxs("label", { className: cm.cn(cm.textSize('xs'), cm.flex({ direction: 'col', gap: 'xs' })), children: [_jsx("span", { children: t('colorPicker.hue', {}, { defaultValue: 'Hue' }) }), _jsx("input", { type: "range", min: 0, max: 360, step: 1, value: Math.round(hsv.h), onChange: (e) => emitFromHsv({ h: Number(e.target.value) }), "aria-label": t('colorPicker.hue', {}, { defaultValue: 'Hue' }), "data-mol-id": "color-picker-hue", style: { ...sliderBase, background: hueGradient } })] }), _jsxs("label", { className: cm.cn(cm.textSize('xs'), cm.flex({ direction: 'col', gap: 'xs' })), children: [_jsx("span", { children: t('colorPicker.saturation', {}, { defaultValue: 'Saturation' }) }), _jsx("input", { type: "range", min: 0, max: 100, step: 1, value: Math.round(hsv.s * 100), onChange: (e) => emitFromHsv({ s: Number(e.target.value) / 100 }), "aria-label": t('colorPicker.saturation', {}, { defaultValue: 'Saturation' }), "data-mol-id": "color-picker-saturation", style: sliderBase })] }), _jsxs("label", { className: cm.cn(cm.textSize('xs'), cm.flex({ direction: 'col', gap: 'xs' })), children: [_jsx("span", { children: t('colorPicker.value', {}, { defaultValue: 'Value' }) }), _jsx("input", { type: "range", min: 0, max: 100, step: 1, value: Math.round(hsv.v * 100), onChange: (e) => emitFromHsv({ v: Number(e.target.value) / 100 }), "aria-label": t('colorPicker.value', {}, { defaultValue: 'Value' }), "data-mol-id": "color-picker-value", style: sliderBase })] }), _jsx("div", { className: cm.flex({ gap: 'sm' }), children: ['r', 'g', 'b'].map((channel) => (_jsxs("label", { className: cm.cn(cm.textSize('xs'), cm.flex({ direction: 'col', gap: 'xs' })), style: { flex: 1 }, children: [_jsx("span", { children: t(`colorPicker.${channel}`, {}, { defaultValue: channel === 'r' ? 'Red' : channel === 'g' ? 'Green' : 'Blue' }) }), _jsx("input", { type: "number", min: 0, max: 255, step: 1, value: rgb[channel], onChange: (e) => emitFromRgb({ [channel]: Number(e.target.value) }), "aria-label": t(`colorPicker.${channel}`, {}, { defaultValue: channel === 'r' ? 'Red' : channel === 'g' ? 'Green' : 'Blue' }), "data-mol-id": `color-picker-${channel}`, style: { padding: '0.3rem 0.5rem' } })] }, channel))) })] }));
|
|
89
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure color-space conversion helpers — HEX ⇄ RGB ⇄ HSV. Exported so
|
|
3
|
+
* downstream code can drive the picker programmatically.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import type { HsvColor, RgbColor } from './types.js';
|
|
8
|
+
/**
|
|
9
|
+
* Clamp a number to the inclusive range `[lo, hi]`.
|
|
10
|
+
*
|
|
11
|
+
* @param n - Input value.
|
|
12
|
+
* @param lo - Lower bound.
|
|
13
|
+
* @param hi - Upper bound.
|
|
14
|
+
* @returns The clamped value.
|
|
15
|
+
*/
|
|
16
|
+
export declare function clamp(n: number, lo: number, hi: number): number;
|
|
17
|
+
/**
|
|
18
|
+
* Parse a `#rgb`/`#rrggbb` hex string into an RGB color. Falls back to black
|
|
19
|
+
* for unparseable input rather than throwing — pickers shouldn't crash on
|
|
20
|
+
* a stray empty string.
|
|
21
|
+
*
|
|
22
|
+
* @param hex - Color string (with or without the leading `#`).
|
|
23
|
+
* @returns The parsed RGB color.
|
|
24
|
+
*/
|
|
25
|
+
export declare function hexToRgb(hex: string): RgbColor;
|
|
26
|
+
/**
|
|
27
|
+
* Format an RGB color as a `#rrggbb` hex string (lowercase).
|
|
28
|
+
*
|
|
29
|
+
* @param rgb - Color to format.
|
|
30
|
+
* @returns The hex string.
|
|
31
|
+
*/
|
|
32
|
+
export declare function rgbToHex({ r, g, b }: RgbColor): string;
|
|
33
|
+
/**
|
|
34
|
+
* Convert RGB (0–255) to HSV (h: 0–360, s/v: 0–1).
|
|
35
|
+
*
|
|
36
|
+
* @param rgb - Color to convert.
|
|
37
|
+
* @returns The equivalent HSV color.
|
|
38
|
+
*/
|
|
39
|
+
export declare function rgbToHsv({ r, g, b }: RgbColor): HsvColor;
|
|
40
|
+
/**
|
|
41
|
+
* Convert HSV (h: 0–360, s/v: 0–1) to RGB (0–255).
|
|
42
|
+
*
|
|
43
|
+
* @param hsv - Color to convert.
|
|
44
|
+
* @returns The equivalent RGB color.
|
|
45
|
+
*/
|
|
46
|
+
export declare function hsvToRgb({ h, s, v }: HsvColor): RgbColor;
|
|
47
|
+
/**
|
|
48
|
+
* Whether `hex` parses to a 6-character hex color.
|
|
49
|
+
*
|
|
50
|
+
* @param hex - Candidate hex string (with or without `#`).
|
|
51
|
+
* @returns `true` when the string parses cleanly.
|
|
52
|
+
*/
|
|
53
|
+
export declare function isValidHex(hex: string): boolean;
|
|
54
|
+
//# sourceMappingURL=conversions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversions.d.ts","sourceRoot":"","sources":["../src/conversions.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAEpD;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED;;;;;;;GAOG;AACH,wBAAgB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,QAAQ,CAe9C;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,GAAG,MAAM,CAGtD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,GAAG,QAAQ,CAiBxD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,QAAQ,GAAG,QAAQ,CAqBxD;AAED;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE/C"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure color-space conversion helpers — HEX ⇄ RGB ⇄ HSV. Exported so
|
|
3
|
+
* downstream code can drive the picker programmatically.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Clamp a number to the inclusive range `[lo, hi]`.
|
|
9
|
+
*
|
|
10
|
+
* @param n - Input value.
|
|
11
|
+
* @param lo - Lower bound.
|
|
12
|
+
* @param hi - Upper bound.
|
|
13
|
+
* @returns The clamped value.
|
|
14
|
+
*/
|
|
15
|
+
export function clamp(n, lo, hi) {
|
|
16
|
+
return Math.max(lo, Math.min(hi, n));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parse a `#rgb`/`#rrggbb` hex string into an RGB color. Falls back to black
|
|
20
|
+
* for unparseable input rather than throwing — pickers shouldn't crash on
|
|
21
|
+
* a stray empty string.
|
|
22
|
+
*
|
|
23
|
+
* @param hex - Color string (with or without the leading `#`).
|
|
24
|
+
* @returns The parsed RGB color.
|
|
25
|
+
*/
|
|
26
|
+
export function hexToRgb(hex) {
|
|
27
|
+
const m = String(hex).trim().replace(/^#/, '');
|
|
28
|
+
if (m.length === 3) {
|
|
29
|
+
const r = parseInt(m[0] + m[0], 16);
|
|
30
|
+
const g = parseInt(m[1] + m[1], 16);
|
|
31
|
+
const b = parseInt(m[2] + m[2], 16);
|
|
32
|
+
if ([r, g, b].every((v) => Number.isFinite(v)))
|
|
33
|
+
return { r, g, b };
|
|
34
|
+
}
|
|
35
|
+
if (m.length === 6) {
|
|
36
|
+
const r = parseInt(m.slice(0, 2), 16);
|
|
37
|
+
const g = parseInt(m.slice(2, 4), 16);
|
|
38
|
+
const b = parseInt(m.slice(4, 6), 16);
|
|
39
|
+
if ([r, g, b].every((v) => Number.isFinite(v)))
|
|
40
|
+
return { r, g, b };
|
|
41
|
+
}
|
|
42
|
+
return { r: 0, g: 0, b: 0 };
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Format an RGB color as a `#rrggbb` hex string (lowercase).
|
|
46
|
+
*
|
|
47
|
+
* @param rgb - Color to format.
|
|
48
|
+
* @returns The hex string.
|
|
49
|
+
*/
|
|
50
|
+
export function rgbToHex({ r, g, b }) {
|
|
51
|
+
const to = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, '0');
|
|
52
|
+
return `#${to(r)}${to(g)}${to(b)}`;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Convert RGB (0–255) to HSV (h: 0–360, s/v: 0–1).
|
|
56
|
+
*
|
|
57
|
+
* @param rgb - Color to convert.
|
|
58
|
+
* @returns The equivalent HSV color.
|
|
59
|
+
*/
|
|
60
|
+
export function rgbToHsv({ r, g, b }) {
|
|
61
|
+
const rn = clamp(r, 0, 255) / 255;
|
|
62
|
+
const gn = clamp(g, 0, 255) / 255;
|
|
63
|
+
const bn = clamp(b, 0, 255) / 255;
|
|
64
|
+
const max = Math.max(rn, gn, bn);
|
|
65
|
+
const min = Math.min(rn, gn, bn);
|
|
66
|
+
const d = max - min;
|
|
67
|
+
let h = 0;
|
|
68
|
+
if (d !== 0) {
|
|
69
|
+
if (max === rn)
|
|
70
|
+
h = ((gn - bn) / d) % 6;
|
|
71
|
+
else if (max === gn)
|
|
72
|
+
h = (bn - rn) / d + 2;
|
|
73
|
+
else
|
|
74
|
+
h = (rn - gn) / d + 4;
|
|
75
|
+
h *= 60;
|
|
76
|
+
if (h < 0)
|
|
77
|
+
h += 360;
|
|
78
|
+
}
|
|
79
|
+
const s = max === 0 ? 0 : d / max;
|
|
80
|
+
return { h, s, v: max };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Convert HSV (h: 0–360, s/v: 0–1) to RGB (0–255).
|
|
84
|
+
*
|
|
85
|
+
* @param hsv - Color to convert.
|
|
86
|
+
* @returns The equivalent RGB color.
|
|
87
|
+
*/
|
|
88
|
+
export function hsvToRgb({ h, s, v }) {
|
|
89
|
+
const hh = ((h % 360) + 360) % 360;
|
|
90
|
+
const ss = clamp(s, 0, 1);
|
|
91
|
+
const vv = clamp(v, 0, 1);
|
|
92
|
+
const c = vv * ss;
|
|
93
|
+
const x = c * (1 - Math.abs(((hh / 60) % 2) - 1));
|
|
94
|
+
const m = vv - c;
|
|
95
|
+
let r1;
|
|
96
|
+
let g1;
|
|
97
|
+
let b1;
|
|
98
|
+
if (hh < 60)
|
|
99
|
+
[r1, g1, b1] = [c, x, 0];
|
|
100
|
+
else if (hh < 120)
|
|
101
|
+
[r1, g1, b1] = [x, c, 0];
|
|
102
|
+
else if (hh < 180)
|
|
103
|
+
[r1, g1, b1] = [0, c, x];
|
|
104
|
+
else if (hh < 240)
|
|
105
|
+
[r1, g1, b1] = [0, x, c];
|
|
106
|
+
else if (hh < 300)
|
|
107
|
+
[r1, g1, b1] = [x, 0, c];
|
|
108
|
+
else
|
|
109
|
+
[r1, g1, b1] = [c, 0, x];
|
|
110
|
+
return {
|
|
111
|
+
r: Math.round((r1 + m) * 255),
|
|
112
|
+
g: Math.round((g1 + m) * 255),
|
|
113
|
+
b: Math.round((b1 + m) * 255),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Whether `hex` parses to a 6-character hex color.
|
|
118
|
+
*
|
|
119
|
+
* @param hex - Candidate hex string (with or without `#`).
|
|
120
|
+
* @returns `true` when the string parses cleanly.
|
|
121
|
+
*/
|
|
122
|
+
export function isValidHex(hex) {
|
|
123
|
+
return /^#?[0-9a-fA-F]{6}$|^#?[0-9a-fA-F]{3}$/.test(String(hex).trim());
|
|
124
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled HSV + RGB + HEX color picker — design canvases, photo editors,
|
|
3
|
+
* animation tools, brand editors.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* import { ColorPicker } from '@molecule/app-color-picker-react'
|
|
8
|
+
*
|
|
9
|
+
* function StrokeColorField() {
|
|
10
|
+
* const [color, setColor] = useState('#3366ff')
|
|
11
|
+
* return <ColorPicker value={color} onChange={setColor} />
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * from './ColorPicker.js';
|
|
18
|
+
export * from './conversions.js';
|
|
19
|
+
export * from './types.js';
|
|
20
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA;AAChC,cAAc,YAAY,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Controlled HSV + RGB + HEX color picker — design canvases, photo editors,
|
|
3
|
+
* animation tools, brand editors.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```tsx
|
|
7
|
+
* import { ColorPicker } from '@molecule/app-color-picker-react'
|
|
8
|
+
*
|
|
9
|
+
* function StrokeColorField() {
|
|
10
|
+
* const [color, setColor] = useState('#3366ff')
|
|
11
|
+
* return <ColorPicker value={color} onChange={setColor} />
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
export * from './ColorPicker.js';
|
|
18
|
+
export * from './conversions.js';
|
|
19
|
+
export * from './types.js';
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public types for `<ColorPicker>`.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
/** RGB color, 8-bit components (0–255). */
|
|
7
|
+
export interface RgbColor {
|
|
8
|
+
/** Red (0–255). */
|
|
9
|
+
r: number;
|
|
10
|
+
/** Green (0–255). */
|
|
11
|
+
g: number;
|
|
12
|
+
/** Blue (0–255). */
|
|
13
|
+
b: number;
|
|
14
|
+
}
|
|
15
|
+
/** HSV color, hue degrees + saturation/value 0–1. */
|
|
16
|
+
export interface HsvColor {
|
|
17
|
+
/** Hue in degrees (0–360). */
|
|
18
|
+
h: number;
|
|
19
|
+
/** Saturation (0–1). */
|
|
20
|
+
s: number;
|
|
21
|
+
/** Value / brightness (0–1). */
|
|
22
|
+
v: number;
|
|
23
|
+
}
|
|
24
|
+
/** Props for {@link ColorPicker}. */
|
|
25
|
+
export interface ColorPickerProps {
|
|
26
|
+
/** Current color as a 6-character HEX string with leading `#`. */
|
|
27
|
+
value: string;
|
|
28
|
+
/** Called whenever the user picks a new color. Receives a `#rrggbb` string. */
|
|
29
|
+
onChange: (hex: string) => void;
|
|
30
|
+
/** `data-mol-id` attribute for AI-agent selectors. */
|
|
31
|
+
dataMolId?: string;
|
|
32
|
+
/** Extra classes appended via the ClassMap `cn()` helper. */
|
|
33
|
+
className?: string;
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,2CAA2C;AAC3C,MAAM,WAAW,QAAQ;IACvB,mBAAmB;IACnB,CAAC,EAAE,MAAM,CAAA;IACT,qBAAqB;IACrB,CAAC,EAAE,MAAM,CAAA;IACT,oBAAoB;IACpB,CAAC,EAAE,MAAM,CAAA;CACV;AAED,qDAAqD;AACrD,MAAM,WAAW,QAAQ;IACvB,8BAA8B;IAC9B,CAAC,EAAE,MAAM,CAAA;IACT,wBAAwB;IACxB,CAAC,EAAE,MAAM,CAAA;IACT,gCAAgC;IAChC,CAAC,EAAE,MAAM,CAAA;CACV;AAED,qCAAqC;AACrC,MAAM,WAAW,gBAAgB;IAC/B,kEAAkE;IAClE,KAAK,EAAE,MAAM,CAAA;IACb,+EAA+E;IAC/E,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAA;IAC/B,sDAAsD;IACtD,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,6DAA6D;IAC7D,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB"}
|
package/dist/types.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-color-picker-react",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "HSV slider + RGB sliders + HEX input — controlled color picker for design canvases, photo editors, animation tools",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"color-picker",
|
|
25
|
+
"hsv",
|
|
26
|
+
"rgb",
|
|
27
|
+
"hex",
|
|
28
|
+
"react"
|
|
29
|
+
],
|
|
30
|
+
"license": "Apache-2.0",
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@molecule/app-i18n": "^1.0.0",
|
|
33
|
+
"@molecule/app-react": "^1.0.0",
|
|
34
|
+
"@molecule/app-ui": "^1.0.0",
|
|
35
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
36
|
+
},
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@molecule/app-i18n": "1.0.0",
|
|
39
|
+
"@molecule/app-react": "1.0.0",
|
|
40
|
+
"@molecule/app-ui": "1.0.0",
|
|
41
|
+
"@molecule/app-ui-tailwind": "1.0.0",
|
|
42
|
+
"@testing-library/dom": "10.4.1",
|
|
43
|
+
"@testing-library/react": "16.3.2",
|
|
44
|
+
"@types/node": "26.1.2",
|
|
45
|
+
"@types/react": "19.2.17",
|
|
46
|
+
"@types/react-dom": "19.2.3",
|
|
47
|
+
"jsdom": "30.0.1",
|
|
48
|
+
"react": "19.2.8",
|
|
49
|
+
"react-dom": "19.2.8",
|
|
50
|
+
"typescript": "6.0.3",
|
|
51
|
+
"vitest": "4.1.10"
|
|
52
|
+
}
|
|
53
|
+
}
|