@octanejs/colorful 0.0.1
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 +21 -0
- package/README.md +31 -0
- package/UPSTREAM.md +56 -0
- package/package.json +63 -0
- package/src/components/HexAlphaColorPicker.tsrx +14 -0
- package/src/components/HexColorInput.tsrx +26 -0
- package/src/components/HexColorPicker.tsrx +14 -0
- package/src/components/HslColorPicker.tsrx +14 -0
- package/src/components/HslStringColorPicker.tsrx +14 -0
- package/src/components/HslaColorPicker.tsrx +14 -0
- package/src/components/HslaStringColorPicker.tsrx +14 -0
- package/src/components/HsvColorPicker.tsrx +14 -0
- package/src/components/HsvStringColorPicker.tsrx +14 -0
- package/src/components/HsvaColorPicker.tsrx +14 -0
- package/src/components/HsvaStringColorPicker.tsrx +14 -0
- package/src/components/RgbColorPicker.tsrx +14 -0
- package/src/components/RgbStringColorPicker.tsrx +14 -0
- package/src/components/RgbaColorPicker.tsrx +14 -0
- package/src/components/RgbaStringColorPicker.tsrx +14 -0
- package/src/components/common/Alpha.tsrx +43 -0
- package/src/components/common/AlphaColorPicker.tsrx +75 -0
- package/src/components/common/ColorInput.tsrx +59 -0
- package/src/components/common/ColorPicker.tsrx +73 -0
- package/src/components/common/Hue.tsrx +41 -0
- package/src/components/common/Interactive.tsrx +167 -0
- package/src/components/common/Pointer.tsrx +16 -0
- package/src/components/common/Saturation.tsrx +46 -0
- package/src/css/styles.css +108 -0
- package/src/css/styles.ts +107 -0
- package/src/hooks/useColorManipulation.tsrx +55 -0
- package/src/hooks/useEventCallback.tsrx +12 -0
- package/src/hooks/useStyleSheet.tsrx +28 -0
- package/src/index.ts +18 -0
- package/src/types.ts +61 -0
- package/src/utils/clamp.ts +6 -0
- package/src/utils/compare.ts +32 -0
- package/src/utils/convert.ts +209 -0
- package/src/utils/format.ts +1 -0
- package/src/utils/nonce.ts +13 -0
- package/src/utils/round.ts +3 -0
- package/src/utils/validate.ts +13 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Octane } from 'octane/jsx-runtime';
|
|
2
|
+
|
|
3
|
+
export interface RgbColor {
|
|
4
|
+
r: number;
|
|
5
|
+
g: number;
|
|
6
|
+
b: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface RgbaColor extends RgbColor {
|
|
10
|
+
a: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HslColor {
|
|
14
|
+
h: number;
|
|
15
|
+
s: number;
|
|
16
|
+
l: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface HslaColor extends HslColor {
|
|
20
|
+
a: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface HsvColor {
|
|
24
|
+
h: number;
|
|
25
|
+
s: number;
|
|
26
|
+
v: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface HsvaColor extends HsvColor {
|
|
30
|
+
a: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type ObjectColor = RgbColor | HslColor | HsvColor | RgbaColor | HslaColor | HsvaColor;
|
|
34
|
+
export type AnyColor = string | ObjectColor;
|
|
35
|
+
|
|
36
|
+
export interface ColorModel<T extends AnyColor> {
|
|
37
|
+
defaultColor: T;
|
|
38
|
+
toHsva: (defaultColor: T) => HsvaColor;
|
|
39
|
+
fromHsva: (hsva: HsvaColor) => T;
|
|
40
|
+
equal: (first: T, second: T) => boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
type ColorPickerHTMLAttributes = Omit<
|
|
44
|
+
Octane.JSX.IntrinsicElements['div'],
|
|
45
|
+
'color' | 'onChange' | 'onChangeCapture'
|
|
46
|
+
>;
|
|
47
|
+
|
|
48
|
+
export interface ColorPickerBaseProps<T extends AnyColor> extends ColorPickerHTMLAttributes {
|
|
49
|
+
color: T;
|
|
50
|
+
onChange: (newColor: T) => void;
|
|
51
|
+
onChangeEnd?: (newColor: T) => void;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// OCTANE DIVERGENCE[react-colorful-native-event-attributes][types:react-colorful-adapted]: residual host handlers use Octane native DOM event attributes instead of React synthetic event types.
|
|
55
|
+
// OCTANE DIVERGENCE[react-colorful-native-event-attributes][differential:react-colorful-controlled]: same event-model adaptation observed by the controlled differential against react-colorful@5.8.0.
|
|
56
|
+
type ColorInputHTMLAttributes = Omit<Octane.JSX.IntrinsicElements['input'], 'onChange' | 'value'>;
|
|
57
|
+
|
|
58
|
+
export interface ColorInputBaseProps extends ColorInputHTMLAttributes {
|
|
59
|
+
color?: string;
|
|
60
|
+
onChange?: (newColor: string) => void;
|
|
61
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Clamps a value between an upper and lower bound.
|
|
2
|
+
// We use ternary operators because it makes the minified code
|
|
3
|
+
// 2 times shorter then `Math.min(Math.max(a,b),c)`
|
|
4
|
+
export const clamp = (number: number, min = 0, max = 1): number => {
|
|
5
|
+
return number > max ? max : number < min ? min : number;
|
|
6
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { hexToRgba } from './convert';
|
|
2
|
+
import { ObjectColor } from '../types';
|
|
3
|
+
|
|
4
|
+
export const equalColorObjects = (first: ObjectColor, second: ObjectColor): boolean => {
|
|
5
|
+
if (first === second) return true;
|
|
6
|
+
|
|
7
|
+
for (const prop in first) {
|
|
8
|
+
// The following allows for a type-safe calling of this function (first & second have to be HSL, HSV, or RGB)
|
|
9
|
+
// with type-unsafe iterating over object keys. TS does not allow this without an index (`[key: string]: number`)
|
|
10
|
+
// on an object to define how iteration is normally done. To ensure extra keys are not allowed on our types,
|
|
11
|
+
// we must cast our object to unknown (as RGB demands `r` be a key, while `Record<string, x>` does not care if
|
|
12
|
+
// there is or not), and then as a type TS can iterate over.
|
|
13
|
+
if (
|
|
14
|
+
(first as unknown as Record<string, number>)[prop] !==
|
|
15
|
+
(second as unknown as Record<string, number>)[prop]
|
|
16
|
+
)
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return true;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const equalColorString = (first: string, second: string): boolean => {
|
|
24
|
+
return first.replace(/\s/g, '') === second.replace(/\s/g, '');
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const equalHex = (first: string, second: string): boolean => {
|
|
28
|
+
if (first.toLowerCase() === second.toLowerCase()) return true;
|
|
29
|
+
|
|
30
|
+
// To compare colors like `#FFF` and `ffffff` we convert them into RGB objects
|
|
31
|
+
return equalColorObjects(hexToRgba(first), hexToRgba(second));
|
|
32
|
+
};
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { round } from './round';
|
|
2
|
+
import { RgbaColor, RgbColor, HslaColor, HslColor, HsvaColor, HsvColor } from '../types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Valid CSS <angle> units.
|
|
6
|
+
* https://developer.mozilla.org/en-US/docs/Web/CSS/angle
|
|
7
|
+
*/
|
|
8
|
+
const angleUnits: Record<string, number> = {
|
|
9
|
+
grad: 360 / 400,
|
|
10
|
+
turn: 360,
|
|
11
|
+
rad: 360 / (Math.PI * 2),
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const hexToHsva = (hex: string): HsvaColor => rgbaToHsva(hexToRgba(hex));
|
|
15
|
+
|
|
16
|
+
export const hexToRgba = (hex: string): RgbaColor => {
|
|
17
|
+
if (hex[0] === '#') hex = hex.substring(1);
|
|
18
|
+
|
|
19
|
+
if (hex.length < 6) {
|
|
20
|
+
return {
|
|
21
|
+
r: parseInt(hex[0] + hex[0], 16),
|
|
22
|
+
g: parseInt(hex[1] + hex[1], 16),
|
|
23
|
+
b: parseInt(hex[2] + hex[2], 16),
|
|
24
|
+
a: hex.length === 4 ? round(parseInt(hex[3] + hex[3], 16) / 255, 2) : 1,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
r: parseInt(hex.substring(0, 2), 16),
|
|
30
|
+
g: parseInt(hex.substring(2, 4), 16),
|
|
31
|
+
b: parseInt(hex.substring(4, 6), 16),
|
|
32
|
+
a: hex.length === 8 ? round(parseInt(hex.substring(6, 8), 16) / 255, 2) : 1,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const parseHue = (value: string, unit = 'deg'): number => {
|
|
37
|
+
return Number(value) * (angleUnits[unit] || 1);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const hslaStringToHsva = (hslString: string): HsvaColor => {
|
|
41
|
+
const matcher =
|
|
42
|
+
/hsla?\(?\s*(-?\d*\.?\d+)(deg|rad|grad|turn)?[,\s]+(-?\d*\.?\d+)%?[,\s]+(-?\d*\.?\d+)%?,?\s*[/\s]*(-?\d*\.?\d+)?(%)?\s*\)?/i;
|
|
43
|
+
const match = matcher.exec(hslString);
|
|
44
|
+
|
|
45
|
+
if (!match) return { h: 0, s: 0, v: 0, a: 1 };
|
|
46
|
+
|
|
47
|
+
return hslaToHsva({
|
|
48
|
+
h: parseHue(match[1], match[2]),
|
|
49
|
+
s: Number(match[3]),
|
|
50
|
+
l: Number(match[4]),
|
|
51
|
+
a: match[5] === undefined ? 1 : Number(match[5]) / (match[6] ? 100 : 1),
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const hslStringToHsva = hslaStringToHsva;
|
|
56
|
+
|
|
57
|
+
export const hslaToHsva = ({ h, s, l, a }: HslaColor): HsvaColor => {
|
|
58
|
+
s *= (l < 50 ? l : 100 - l) / 100;
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
h: h,
|
|
62
|
+
s: s > 0 ? ((2 * s) / (l + s)) * 100 : 0,
|
|
63
|
+
v: l + s,
|
|
64
|
+
a,
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const hsvaToHex = (hsva: HsvaColor): string => rgbaToHex(hsvaToRgba(hsva));
|
|
69
|
+
|
|
70
|
+
export const hsvaToHsla = ({ h, s, v, a }: HsvaColor): HslaColor => {
|
|
71
|
+
const hh = ((200 - s) * v) / 100;
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
h: round(h),
|
|
75
|
+
s: round(hh > 0 && hh < 200 ? ((s * v) / 100 / (hh <= 100 ? hh : 200 - hh)) * 100 : 0),
|
|
76
|
+
l: round(hh / 2),
|
|
77
|
+
a: round(a, 2),
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const hsvaToHslString = (hsva: HsvaColor): string => {
|
|
82
|
+
const { h, s, l } = hsvaToHsla(hsva);
|
|
83
|
+
return `hsl(${h}, ${s}%, ${l}%)`;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const hsvaToHsvString = (hsva: HsvaColor): string => {
|
|
87
|
+
const { h, s, v } = roundHsva(hsva);
|
|
88
|
+
return `hsv(${h}, ${s}%, ${v}%)`;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export const hsvaToHsvaString = (hsva: HsvaColor): string => {
|
|
92
|
+
const { h, s, v, a } = roundHsva(hsva);
|
|
93
|
+
return `hsva(${h}, ${s}%, ${v}%, ${a})`;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const hsvaToHslaString = (hsva: HsvaColor): string => {
|
|
97
|
+
const { h, s, l, a } = hsvaToHsla(hsva);
|
|
98
|
+
return `hsla(${h}, ${s}%, ${l}%, ${a})`;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
export const hsvaToRgba = ({ h, s, v, a }: HsvaColor): RgbaColor => {
|
|
102
|
+
h = (h / 360) * 6;
|
|
103
|
+
s = s / 100;
|
|
104
|
+
v = v / 100;
|
|
105
|
+
|
|
106
|
+
const hh = Math.floor(h),
|
|
107
|
+
b = v * (1 - s),
|
|
108
|
+
c = v * (1 - (h - hh) * s),
|
|
109
|
+
d = v * (1 - (1 - h + hh) * s),
|
|
110
|
+
module = hh % 6;
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
r: round([v, c, b, b, d, v][module] * 255),
|
|
114
|
+
g: round([d, v, v, c, b, b][module] * 255),
|
|
115
|
+
b: round([b, b, d, v, v, c][module] * 255),
|
|
116
|
+
a: round(a, 2),
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const hsvaToRgbString = (hsva: HsvaColor): string => {
|
|
121
|
+
const { r, g, b } = hsvaToRgba(hsva);
|
|
122
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export const hsvaToRgbaString = (hsva: HsvaColor): string => {
|
|
126
|
+
const { r, g, b, a } = hsvaToRgba(hsva);
|
|
127
|
+
return `rgba(${r}, ${g}, ${b}, ${a})`;
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const hsvaStringToHsva = (hsvString: string): HsvaColor => {
|
|
131
|
+
const matcher =
|
|
132
|
+
/hsva?\(?\s*(-?\d*\.?\d+)(deg|rad|grad|turn)?[,\s]+(-?\d*\.?\d+)%?[,\s]+(-?\d*\.?\d+)%?,?\s*[/\s]*(-?\d*\.?\d+)?(%)?\s*\)?/i;
|
|
133
|
+
const match = matcher.exec(hsvString);
|
|
134
|
+
|
|
135
|
+
if (!match) return { h: 0, s: 0, v: 0, a: 1 };
|
|
136
|
+
|
|
137
|
+
return roundHsva({
|
|
138
|
+
h: parseHue(match[1], match[2]),
|
|
139
|
+
s: Number(match[3]),
|
|
140
|
+
v: Number(match[4]),
|
|
141
|
+
a: match[5] === undefined ? 1 : Number(match[5]) / (match[6] ? 100 : 1),
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
export const hsvStringToHsva = hsvaStringToHsva;
|
|
146
|
+
|
|
147
|
+
export const rgbaStringToHsva = (rgbaString: string): HsvaColor => {
|
|
148
|
+
const matcher =
|
|
149
|
+
/rgba?\(?\s*(-?\d*\.?\d+)(%)?[,\s]+(-?\d*\.?\d+)(%)?[,\s]+(-?\d*\.?\d+)(%)?,?\s*[/\s]*(-?\d*\.?\d+)?(%)?\s*\)?/i;
|
|
150
|
+
const match = matcher.exec(rgbaString);
|
|
151
|
+
|
|
152
|
+
if (!match) return { h: 0, s: 0, v: 0, a: 1 };
|
|
153
|
+
|
|
154
|
+
return rgbaToHsva({
|
|
155
|
+
r: Number(match[1]) / (match[2] ? 100 / 255 : 1),
|
|
156
|
+
g: Number(match[3]) / (match[4] ? 100 / 255 : 1),
|
|
157
|
+
b: Number(match[5]) / (match[6] ? 100 / 255 : 1),
|
|
158
|
+
a: match[7] === undefined ? 1 : Number(match[7]) / (match[8] ? 100 : 1),
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export const rgbStringToHsva = rgbaStringToHsva;
|
|
163
|
+
|
|
164
|
+
const format = (number: number) => {
|
|
165
|
+
const hex = number.toString(16);
|
|
166
|
+
return hex.length < 2 ? '0' + hex : hex;
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
export const rgbaToHex = ({ r, g, b, a }: RgbaColor): string => {
|
|
170
|
+
const alphaHex = a < 1 ? format(round(a * 255)) : '';
|
|
171
|
+
return '#' + format(r) + format(g) + format(b) + alphaHex;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
export const rgbaToHsva = ({ r, g, b, a }: RgbaColor): HsvaColor => {
|
|
175
|
+
const max = Math.max(r, g, b);
|
|
176
|
+
const delta = max - Math.min(r, g, b);
|
|
177
|
+
|
|
178
|
+
// prettier-ignore
|
|
179
|
+
const hh = delta
|
|
180
|
+
? max === r
|
|
181
|
+
? (g - b) / delta
|
|
182
|
+
: max === g
|
|
183
|
+
? 2 + (b - r) / delta
|
|
184
|
+
: 4 + (r - g) / delta
|
|
185
|
+
: 0;
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
h: round(60 * (hh < 0 ? hh + 6 : hh)),
|
|
189
|
+
s: round(max ? (delta / max) * 100 : 0),
|
|
190
|
+
v: round((max / 255) * 100),
|
|
191
|
+
a,
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
export const roundHsva = (hsva: HsvaColor): HsvaColor => ({
|
|
196
|
+
h: round(hsva.h),
|
|
197
|
+
s: round(hsva.s),
|
|
198
|
+
v: round(hsva.v),
|
|
199
|
+
a: round(hsva.a, 2),
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
export const rgbaToRgb = ({ r, g, b }: RgbaColor): RgbColor => ({ r, g, b });
|
|
203
|
+
|
|
204
|
+
export const hslaToHsl = ({ h, s, l }: HslaColor): HslColor => ({ h, s, l });
|
|
205
|
+
|
|
206
|
+
export const hsvaToHsv = (hsva: HsvaColor): HsvColor => {
|
|
207
|
+
const { h, s, v } = roundHsva(hsva);
|
|
208
|
+
return { h, s, v };
|
|
209
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const formatClassName = (names: unknown[]): string => names.filter(Boolean).join(' ');
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
declare const __webpack_nonce__: string | undefined;
|
|
2
|
+
|
|
3
|
+
let nonce: string | undefined;
|
|
4
|
+
|
|
5
|
+
export const getNonce = (): string | undefined => {
|
|
6
|
+
if (nonce) return nonce;
|
|
7
|
+
if (typeof __webpack_nonce__ !== 'undefined') return __webpack_nonce__;
|
|
8
|
+
return undefined;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const setNonce = (hash: string): void => {
|
|
12
|
+
nonce = hash;
|
|
13
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const matcher = /^#?([0-9A-F]{3,8})$/i;
|
|
2
|
+
|
|
3
|
+
export const validHex = (value: string, alpha?: boolean): boolean => {
|
|
4
|
+
const match = matcher.exec(value);
|
|
5
|
+
const length = match ? match[1].length : 0;
|
|
6
|
+
|
|
7
|
+
return (
|
|
8
|
+
length === 3 || // '#rgb' format
|
|
9
|
+
length === 6 || // '#rrggbb' format
|
|
10
|
+
(!!alpha && length === 4) || // '#rgba' format
|
|
11
|
+
(!!alpha && length === 8) // '#rrggbbaa' format
|
|
12
|
+
);
|
|
13
|
+
};
|