@aphexcms/plugin-color-picker 0.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/README.md +148 -0
- package/dist/ColorInput.svelte +59 -0
- package/dist/ColorInput.svelte.d.ts +5 -0
- package/dist/ColorInput.svelte.d.ts.map +1 -0
- package/dist/ColorPicker.svelte +518 -0
- package/dist/ColorPicker.svelte.d.ts +13 -0
- package/dist/ColorPicker.svelte.d.ts.map +1 -0
- package/dist/app.d.ts +17 -0
- package/dist/color.d.ts +44 -0
- package/dist/color.d.ts.map +1 -0
- package/dist/color.js +148 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +3 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +37 -0
- package/dist/schema.d.ts +60 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +63 -0
- package/package.json +60 -0
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# @aphexcms/plugin-color-picker
|
|
2
|
+
|
|
3
|
+
A color field for AphexCMS — a swatch + popover picker (hex / RGB / HSL, optional
|
|
4
|
+
alpha). Color is a plugin, not a built-in field type: the engine ships the primitives,
|
|
5
|
+
color composes on top of them.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @aphexcms/plugin-color-picker
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Register it once in your client-safe plugin registry (`src/lib/plugins.ts`):
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { colorPickerPlugin } from '@aphexcms/plugin-color-picker';
|
|
17
|
+
|
|
18
|
+
export const plugins = [colorPickerPlugin()];
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
That single registration does three things: adds the picker widget, registers the
|
|
22
|
+
`type: 'color'` field type (so it's type-safe with no extra import), and installs the
|
|
23
|
+
transform that expands it.
|
|
24
|
+
|
|
25
|
+
Those land on **both planes**, so `plugins.ts` has to reach both: `aphex.config.ts`
|
|
26
|
+
imports it (the engine needs the transform) and the admin passes it to
|
|
27
|
+
`<AdminApp {plugins} />` (the browser needs the widget). Scaffolded projects are already
|
|
28
|
+
wired this way, so there's nothing further to do — but in a hand-rolled setup,
|
|
29
|
+
registering on only one side leaves you with either a `color` field the engine can't
|
|
30
|
+
expand, or an expanded field with no picker.
|
|
31
|
+
|
|
32
|
+
## Two ways to store a color
|
|
33
|
+
|
|
34
|
+
The plugin supports two storage shapes. Pick per field by the shape you want to read.
|
|
35
|
+
|
|
36
|
+
### Rich object — `type: 'color'`
|
|
37
|
+
|
|
38
|
+
Stores the full `{ hex, alpha, rgb, hsl, hsv }` object (the same model as Sanity's
|
|
39
|
+
`@sanity/color-input`), so you can read the color in any format without converting.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
// In a schema — no import needed; registering the plugin makes `type: 'color'`
|
|
43
|
+
// available and fully type-safe (autocomplete, typos caught).
|
|
44
|
+
{ name: 'brand', type: 'color', title: 'Brand color' }
|
|
45
|
+
|
|
46
|
+
// Allow an alpha channel:
|
|
47
|
+
{ name: 'overlay', type: 'color', title: 'Overlay', alpha: true }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Stored value:
|
|
51
|
+
|
|
52
|
+
```json
|
|
53
|
+
{
|
|
54
|
+
"hex": "#9D2F2F",
|
|
55
|
+
"alpha": 1,
|
|
56
|
+
"rgb": { "r": 157, "g": 47, "b": 47, "a": 1 },
|
|
57
|
+
"hsl": { "h": 0, "s": 53.9, "l": 40, "a": 1 },
|
|
58
|
+
"hsv": { "h": 0, "s": 70.1, "v": 61.6, "a": 1 }
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Read `.hex` for a CSS value:
|
|
63
|
+
|
|
64
|
+
```svelte
|
|
65
|
+
<div style:--accent={settings.brand?.hex}>…</div>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`type: 'color'` generates a fully-typed nested interface in `generated-types.ts` — not
|
|
69
|
+
`unknown` — because the plugin's schema-transform runs during type generation too (see
|
|
70
|
+
[How `type: 'color'` works](#how-type-color-works)).
|
|
71
|
+
|
|
72
|
+
### Plain string — `type: 'string', input: 'color'`
|
|
73
|
+
|
|
74
|
+
Stores just a CSS color string (`"#9D2F2F"`). Simplest to consume — it drops straight
|
|
75
|
+
into CSS — ideal for theme tokens where you never need the other formats.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
{ name: 'accent', type: 'string', input: 'color' }
|
|
79
|
+
|
|
80
|
+
// Works per-item in arrays too:
|
|
81
|
+
{ name: 'palette', type: 'array', of: [{ type: 'string', input: 'color' }] }
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Hex-only (no rgb/hsl formats in the picker):
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
{ name: 'accent', type: 'string', input: 'color', inputOptions: { hexOnly: true } }
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### The `color()` helper (alternative to `type: 'color'`)
|
|
91
|
+
|
|
92
|
+
Equivalent to `type: 'color'`, for when you'd rather import a builder than rely on the
|
|
93
|
+
ambient type (e.g. to avoid depending on plugin registration order). It accepts the same
|
|
94
|
+
field properties the literal does — `access`, `validation`, `group` (including multiple)
|
|
95
|
+
— so neither way is more expressive. Import from the server-safe `/schema` entry:
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
import { color } from '@aphexcms/plugin-color-picker/schema';
|
|
99
|
+
|
|
100
|
+
// in a schema's fields:
|
|
101
|
+
color({ name: 'brand', title: 'Brand color', group: 'design', alpha: true });
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Which should I use?
|
|
105
|
+
|
|
106
|
+
| Want | Use |
|
|
107
|
+
| ----------------------------------------------------- | ---------------------------------------------- |
|
|
108
|
+
| A CSS value that drops straight into `style`/tokens | `type: 'string', input: 'color'` → `"#…"` |
|
|
109
|
+
| The color in multiple formats (hex + rgb + hsl + hsv) | `type: 'color'` → the object, read `.hex` etc. |
|
|
110
|
+
| A builder instead of the ambient type | `color({ … })` |
|
|
111
|
+
|
|
112
|
+
## Type generation
|
|
113
|
+
|
|
114
|
+
`type: 'color'` only desugars into its object shape during codegen if the type generator
|
|
115
|
+
is told about your plugins. Pass your plugin registry as the third argument:
|
|
116
|
+
|
|
117
|
+
```jsonc
|
|
118
|
+
// package.json
|
|
119
|
+
"generate:types": "aphex generate:types ./src/lib/schemaTypes/index.ts ./src/lib/generated-types.ts ./src/lib/plugins.ts"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The `aphex()` Vite plugin does this automatically in dev (it passes `src/lib/plugins.ts`
|
|
123
|
+
by default; override with the `typegen.plugins` option). Without the plugins argument,
|
|
124
|
+
`type: 'color'` fields generate as `unknown`.
|
|
125
|
+
|
|
126
|
+
## How `type: 'color'` works
|
|
127
|
+
|
|
128
|
+
Color is **not** a new storage primitive — the engine only knows built-in field types.
|
|
129
|
+
`type: 'color'` is sugar in two halves:
|
|
130
|
+
|
|
131
|
+
- **Compile-time:** the plugin augments core's `FieldTypeMap` (via `declare module`), so
|
|
132
|
+
`{ type: 'color' }` is a first-class, type-safe field wherever the plugin is imported.
|
|
133
|
+
- **Runtime + codegen:** the plugin's `aphex/schema/transform` part rewrites every
|
|
134
|
+
`{ type: 'color' }` into the rich `object` field **before** the engine, admin, and type
|
|
135
|
+
generator see it. So the stored data is a plain object — portable and interpretable by
|
|
136
|
+
core alone, even if the plugin is later removed.
|
|
137
|
+
|
|
138
|
+
This is why `type: 'color'` reads like a native type while keeping full type safety and
|
|
139
|
+
Aphex's "documents are portable" guarantee.
|
|
140
|
+
|
|
141
|
+
## Exports
|
|
142
|
+
|
|
143
|
+
| Import | From | What |
|
|
144
|
+
| -------------------------- | -------------------------------------- | -------------------------------------- |
|
|
145
|
+
| `colorPickerPlugin()` | `@aphexcms/plugin-color-picker` | The plugin (register in `plugins.ts`) |
|
|
146
|
+
| `ColorPicker` | `@aphexcms/plugin-color-picker` | The standalone picker Svelte component |
|
|
147
|
+
| `color()` | `@aphexcms/plugin-color-picker/schema` | Rich-color field builder |
|
|
148
|
+
| `ColorValue`, `ColorField` | `@aphexcms/plugin-color-picker/schema` | Types |
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { FieldComponentProps } from '@aphexcms/cms-core';
|
|
3
|
+
import ColorPicker from './ColorPicker.svelte';
|
|
4
|
+
import { parseColorToValue, colorValueToCss } from './color.js';
|
|
5
|
+
|
|
6
|
+
// Registered as `aphex/field/component` for `input: 'color'`, so it receives the
|
|
7
|
+
// plugin field-component contract — not the editor's internals.
|
|
8
|
+
let {
|
|
9
|
+
field,
|
|
10
|
+
value,
|
|
11
|
+
onUpdate,
|
|
12
|
+
validationClasses,
|
|
13
|
+
readonly = false
|
|
14
|
+
}: FieldComponentProps = $props();
|
|
15
|
+
|
|
16
|
+
// Storage shape follows the field type: an `object` field stores the rich
|
|
17
|
+
// { hex, alpha, rgb, hsl, hsv } ColorValue (Sanity-style); a `string` field
|
|
18
|
+
// stores a plain hex/CSS string (drops straight into CSS). One widget, both.
|
|
19
|
+
const isObject = $derived(field.type === 'object');
|
|
20
|
+
|
|
21
|
+
// Per-widget config lives in the field's `inputOptions` bag (typed on BaseField,
|
|
22
|
+
// so any custom input can carry config). `alpha` enables an 8-digit hex channel.
|
|
23
|
+
const allowOpacity = $derived(field.inputOptions?.alpha === true);
|
|
24
|
+
type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'oklch';
|
|
25
|
+
const formats = $derived<ColorFormat[] | undefined>(
|
|
26
|
+
// Object mode canonicalises via hex/rgb/hsl parsing (no oklch); string mode
|
|
27
|
+
// honours `hexOnly`.
|
|
28
|
+
isObject ? ['hex', 'rgb', 'hsl'] : field.inputOptions?.hexOnly === true ? ['hex'] : undefined
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// Local mirror the picker binds to (always a CSS string). External changes flow in;
|
|
32
|
+
// picker changes flow out through onChange → onUpdate, converted to the field's
|
|
33
|
+
// storage shape. No ping-pong: onChange sets `picked`, the sync effect then sees
|
|
34
|
+
// them equal and does nothing.
|
|
35
|
+
let picked = $state('');
|
|
36
|
+
$effect(() => {
|
|
37
|
+
const next = isObject ? colorValueToCss(value) : typeof value === 'string' ? value : '';
|
|
38
|
+
if (next !== picked) picked = next;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
function emit(css: string) {
|
|
42
|
+
if (isObject) {
|
|
43
|
+
// Store the rich object; empty/unparseable clears the field.
|
|
44
|
+
onUpdate(css ? parseColorToValue(css) : undefined);
|
|
45
|
+
} else {
|
|
46
|
+
onUpdate(css);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
</script>
|
|
50
|
+
|
|
51
|
+
<div class:pointer-events-none={readonly} class:opacity-50={readonly} aria-disabled={readonly}>
|
|
52
|
+
<ColorPicker
|
|
53
|
+
bind:value={picked}
|
|
54
|
+
{allowOpacity}
|
|
55
|
+
{formats}
|
|
56
|
+
class={validationClasses}
|
|
57
|
+
onChange={emit}
|
|
58
|
+
/>
|
|
59
|
+
</div>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { FieldComponentProps } from '@aphexcms/cms-core';
|
|
2
|
+
declare const ColorInput: import("svelte").Component<FieldComponentProps, {}, "">;
|
|
3
|
+
type ColorInput = ReturnType<typeof ColorInput>;
|
|
4
|
+
export default ColorInput;
|
|
5
|
+
//# sourceMappingURL=ColorInput.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ColorInput.svelte.d.ts","sourceRoot":"","sources":["../src/lib/ColorInput.svelte.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AA4D9D,QAAA,MAAM,UAAU,yDAAwC,CAAC;AACzD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,UAAU,CAAC,CAAC;AAChD,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { cn } from '@aphexcms/ui/utils';
|
|
3
|
+
import { Button } from '@aphexcms/ui/shadcn/button';
|
|
4
|
+
import { ChevronDown } from '@lucide/svelte';
|
|
5
|
+
import * as Popover from '@aphexcms/ui/shadcn/popover';
|
|
6
|
+
import * as Command from '@aphexcms/ui/shadcn/command';
|
|
7
|
+
import * as ButtonGroup from '@aphexcms/ui/shadcn/button-group';
|
|
8
|
+
import { Input } from '@aphexcms/ui/shadcn/input';
|
|
9
|
+
|
|
10
|
+
type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'oklch';
|
|
11
|
+
|
|
12
|
+
let {
|
|
13
|
+
value = $bindable('#000000'),
|
|
14
|
+
class: className,
|
|
15
|
+
allowOpacity = false,
|
|
16
|
+
defaultFormat = 'hex',
|
|
17
|
+
formats = ['hex', 'rgb', 'hsl', 'oklch'],
|
|
18
|
+
onChange
|
|
19
|
+
}: {
|
|
20
|
+
value?: string;
|
|
21
|
+
class?: string;
|
|
22
|
+
allowOpacity?: boolean;
|
|
23
|
+
defaultFormat?: ColorFormat;
|
|
24
|
+
formats?: ColorFormat[];
|
|
25
|
+
onChange?: (value: string) => void;
|
|
26
|
+
} = $props();
|
|
27
|
+
|
|
28
|
+
let h = $state(0);
|
|
29
|
+
let s = $state(0);
|
|
30
|
+
let v = $state(0);
|
|
31
|
+
let a = $state(1);
|
|
32
|
+
// Intentional: seed from the initial prop value.
|
|
33
|
+
// svelte-ignore state_referenced_locally
|
|
34
|
+
let activeFormat = $state<ColorFormat>(defaultFormat);
|
|
35
|
+
let isDragging = $state(false);
|
|
36
|
+
|
|
37
|
+
// Static full-hue spectrum for the hue slider track.
|
|
38
|
+
const hueGradient =
|
|
39
|
+
'linear-gradient(to right, #f00 0%, #ff0 17%, #0f0 33%, #0ff 50%, #00f 67%, #f0f 83%, #f00 100%)';
|
|
40
|
+
|
|
41
|
+
let sbRef: HTMLDivElement | undefined = $state();
|
|
42
|
+
let hueRef: HTMLDivElement | undefined = $state();
|
|
43
|
+
let alphaRef: HTMLDivElement | undefined = $state();
|
|
44
|
+
let formatOpen = $state(false);
|
|
45
|
+
|
|
46
|
+
$effect(() => {
|
|
47
|
+
if (!isDragging) {
|
|
48
|
+
const parsed = parseColor(value);
|
|
49
|
+
if (parsed) {
|
|
50
|
+
const currentStr = formatOutput(h, s, v, a, activeFormat);
|
|
51
|
+
const parsedStr = formatOutput(parsed.h, parsed.s, parsed.v, parsed.a, activeFormat);
|
|
52
|
+
|
|
53
|
+
if (currentStr !== parsedStr) {
|
|
54
|
+
h = parsed.h;
|
|
55
|
+
s = parsed.s;
|
|
56
|
+
v = parsed.v;
|
|
57
|
+
a = parsed.a;
|
|
58
|
+
if (value.startsWith('rgb')) activeFormat = 'rgb';
|
|
59
|
+
else if (value.startsWith('hsl')) activeFormat = 'hsl';
|
|
60
|
+
else if (value.startsWith('oklch')) activeFormat = 'oklch';
|
|
61
|
+
else activeFormat = 'hex';
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
function updateExternal() {
|
|
68
|
+
value = formatOutput(h, s, v, a, activeFormat);
|
|
69
|
+
onChange?.(value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setFormat(fmt: ColorFormat) {
|
|
73
|
+
activeFormat = fmt;
|
|
74
|
+
updateExternal();
|
|
75
|
+
formatOpen = false;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseColor(str: string) {
|
|
79
|
+
str = str.trim().toLowerCase();
|
|
80
|
+
if (str.startsWith('#')) {
|
|
81
|
+
const hex = str.replace('#', '');
|
|
82
|
+
let r = 0,
|
|
83
|
+
g = 0,
|
|
84
|
+
b = 0,
|
|
85
|
+
alpha = 1;
|
|
86
|
+
if (hex.length === 3) {
|
|
87
|
+
r = parseInt(hex.slice(0, 1).repeat(2), 16);
|
|
88
|
+
g = parseInt(hex.slice(1, 2).repeat(2), 16);
|
|
89
|
+
b = parseInt(hex.slice(2, 3).repeat(2), 16);
|
|
90
|
+
} else if (hex.length === 6) {
|
|
91
|
+
r = parseInt(hex.substring(0, 2), 16);
|
|
92
|
+
g = parseInt(hex.substring(2, 4), 16);
|
|
93
|
+
b = parseInt(hex.substring(4, 6), 16);
|
|
94
|
+
} else if (hex.length === 8) {
|
|
95
|
+
r = parseInt(hex.substring(0, 2), 16);
|
|
96
|
+
g = parseInt(hex.substring(2, 4), 16);
|
|
97
|
+
b = parseInt(hex.substring(4, 6), 16);
|
|
98
|
+
alpha = parseInt(hex.substring(6, 8), 16) / 255;
|
|
99
|
+
} else {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
return { ...rgbToHsv(r, g, b), a: alpha };
|
|
103
|
+
}
|
|
104
|
+
if (str.startsWith('rgb')) {
|
|
105
|
+
const values = str.match(/[\d.]+/g)?.map(Number);
|
|
106
|
+
if (values && values.length >= 3) {
|
|
107
|
+
const [r = 0, g = 0, b = 0, alpha = 1] = values;
|
|
108
|
+
return { ...rgbToHsv(r, g, b), a: alpha };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (str.startsWith('hsl')) {
|
|
112
|
+
const values = str.match(/[\d.]+/g)?.map(Number);
|
|
113
|
+
if (values && values.length >= 3) {
|
|
114
|
+
const [hue = 0, sat = 0, light = 0, alpha = 1] = values;
|
|
115
|
+
const sNorm = sat / 100,
|
|
116
|
+
lNorm = light / 100;
|
|
117
|
+
const vNorm = lNorm + sNorm * Math.min(lNorm, 1 - lNorm);
|
|
118
|
+
const sHsv = vNorm === 0 ? 0 : 2 * (1 - lNorm / vNorm);
|
|
119
|
+
return { h: hue, s: sHsv * 100, v: vNorm * 100, a: alpha };
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
if (str.startsWith('oklch')) {
|
|
123
|
+
const values = str
|
|
124
|
+
.match(/[\d.%]+/g)
|
|
125
|
+
?.map((s) => (s.includes('%') ? parseFloat(s) / 100 : parseFloat(s)));
|
|
126
|
+
if (values && values.length >= 3) {
|
|
127
|
+
const [l = 0, c = 0, hue = 0, alpha = 1] = values;
|
|
128
|
+
const rgb = oklchToRgb(l, c, hue);
|
|
129
|
+
return { ...rgbToHsv(rgb.r, rgb.g, rgb.b), a: alpha };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function formatOutput(h: number, s: number, v: number, a: number, format: ColorFormat): string {
|
|
136
|
+
if (format === 'hex') return hsvToHex(h, s, v, a);
|
|
137
|
+
if (format === 'rgb') return hsvToRgbString(h, s, v, a);
|
|
138
|
+
if (format === 'hsl') return hsvToHslString(h, s, v, a);
|
|
139
|
+
if (format === 'oklch') return hsvToOklchString(h, s, v, a);
|
|
140
|
+
return '';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function rgbToHsv(r: number, g: number, b: number) {
|
|
144
|
+
r /= 255;
|
|
145
|
+
g /= 255;
|
|
146
|
+
b /= 255;
|
|
147
|
+
const max = Math.max(r, g, b),
|
|
148
|
+
min = Math.min(r, g, b);
|
|
149
|
+
let h = 0,
|
|
150
|
+
s = 0,
|
|
151
|
+
v = max;
|
|
152
|
+
const d = max - min;
|
|
153
|
+
s = max === 0 ? 0 : d / max;
|
|
154
|
+
if (max !== min) {
|
|
155
|
+
switch (max) {
|
|
156
|
+
case r:
|
|
157
|
+
h = (g - b) / d + (g < b ? 6 : 0);
|
|
158
|
+
break;
|
|
159
|
+
case g:
|
|
160
|
+
h = (b - r) / d + 2;
|
|
161
|
+
break;
|
|
162
|
+
case b:
|
|
163
|
+
h = (r - g) / d + 4;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
h /= 6;
|
|
167
|
+
}
|
|
168
|
+
return { h: h * 360, s: s * 100, v: v * 100 };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function hsvToRgb(h: number, s: number, v: number) {
|
|
172
|
+
let sNorm = s / 100,
|
|
173
|
+
vNorm = v / 100;
|
|
174
|
+
let r = 0,
|
|
175
|
+
g = 0,
|
|
176
|
+
b = 0;
|
|
177
|
+
const i = Math.floor(h / 60),
|
|
178
|
+
f = h / 60 - i,
|
|
179
|
+
p = vNorm * (1 - sNorm),
|
|
180
|
+
q = vNorm * (1 - f * sNorm),
|
|
181
|
+
t = vNorm * (1 - (1 - f) * sNorm);
|
|
182
|
+
switch (i % 6) {
|
|
183
|
+
case 0:
|
|
184
|
+
r = vNorm;
|
|
185
|
+
g = t;
|
|
186
|
+
b = p;
|
|
187
|
+
break;
|
|
188
|
+
case 1:
|
|
189
|
+
r = q;
|
|
190
|
+
g = vNorm;
|
|
191
|
+
b = p;
|
|
192
|
+
break;
|
|
193
|
+
case 2:
|
|
194
|
+
r = p;
|
|
195
|
+
g = vNorm;
|
|
196
|
+
b = t;
|
|
197
|
+
break;
|
|
198
|
+
case 3:
|
|
199
|
+
r = p;
|
|
200
|
+
g = q;
|
|
201
|
+
b = vNorm;
|
|
202
|
+
break;
|
|
203
|
+
case 4:
|
|
204
|
+
r = t;
|
|
205
|
+
g = p;
|
|
206
|
+
b = vNorm;
|
|
207
|
+
break;
|
|
208
|
+
case 5:
|
|
209
|
+
r = vNorm;
|
|
210
|
+
g = p;
|
|
211
|
+
b = q;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
return { r: Math.round(r * 255), g: Math.round(g * 255), b: Math.round(b * 255) };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function hsvToOklchString(h: number, s: number, v: number, a: number) {
|
|
218
|
+
const rgb = hsvToRgb(h, s, v);
|
|
219
|
+
const oklch = rgbToOklch(rgb.r, rgb.g, rgb.b);
|
|
220
|
+
const L = (oklch.l * 100).toFixed(1) + '%';
|
|
221
|
+
const C = oklch.c.toFixed(3);
|
|
222
|
+
const H = (oklch.h || 0).toFixed(1);
|
|
223
|
+
|
|
224
|
+
if (allowOpacity && a < 1) return `oklch(${L} ${C} ${H} / ${parseFloat(a.toFixed(2))})`;
|
|
225
|
+
return `oklch(${L} ${C} ${H})`;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function rgbToOklch(r: number, g: number, b: number) {
|
|
229
|
+
r /= 255;
|
|
230
|
+
g /= 255;
|
|
231
|
+
b /= 255;
|
|
232
|
+
r = r > 0.04045 ? Math.pow((r + 0.055) / 1.055, 2.4) : r / 12.92;
|
|
233
|
+
g = g > 0.04045 ? Math.pow((g + 0.055) / 1.055, 2.4) : g / 12.92;
|
|
234
|
+
b = b > 0.04045 ? Math.pow((b + 0.055) / 1.055, 2.4) : b / 12.92;
|
|
235
|
+
|
|
236
|
+
const l = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
|
|
237
|
+
const m = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b;
|
|
238
|
+
const s = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b;
|
|
239
|
+
|
|
240
|
+
const l_ = Math.cbrt(l),
|
|
241
|
+
m_ = Math.cbrt(m),
|
|
242
|
+
s_ = Math.cbrt(s);
|
|
243
|
+
|
|
244
|
+
const L = 0.2104542553 * l_ + 0.793617785 * m_ - 0.0040720468 * s_;
|
|
245
|
+
const A = 1.9779984951 * l_ - 2.428592205 * m_ + 0.4505937099 * s_;
|
|
246
|
+
const B = 0.0259040371 * l_ + 0.7827717662 * m_ - 0.808675766 * s_;
|
|
247
|
+
|
|
248
|
+
const C = Math.sqrt(A * A + B * B);
|
|
249
|
+
let H = (Math.atan2(B, A) * 180) / Math.PI;
|
|
250
|
+
if (H < 0) H += 360;
|
|
251
|
+
|
|
252
|
+
return { l: L, c: C, h: H };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function oklchToRgb(l: number, c: number, h: number) {
|
|
256
|
+
const hRad = h * (Math.PI / 180);
|
|
257
|
+
const A = c * Math.cos(hRad);
|
|
258
|
+
const B = c * Math.sin(hRad);
|
|
259
|
+
const L = l;
|
|
260
|
+
|
|
261
|
+
const l_ = L + 0.3963377774 * A + 0.2158037573 * B;
|
|
262
|
+
const m_ = L - 0.1055613458 * A - 0.0638541728 * B;
|
|
263
|
+
const s_ = L - 0.0894841775 * A - 1.291485548 * B;
|
|
264
|
+
|
|
265
|
+
const lLin = l_ * l_ * l_;
|
|
266
|
+
const mLin = m_ * m_ * m_;
|
|
267
|
+
const sLin = s_ * s_ * s_;
|
|
268
|
+
|
|
269
|
+
let r = +4.0767416621 * lLin - 3.3077115913 * mLin + 0.2309699292 * sLin;
|
|
270
|
+
let g = -1.2684380046 * lLin + 2.6097574011 * mLin - 0.3413193965 * sLin;
|
|
271
|
+
let b = -0.0041960863 * lLin - 0.7034186147 * mLin + 1.707614701 * sLin;
|
|
272
|
+
|
|
273
|
+
r = r >= 0.0031308 ? 1.055 * Math.pow(r, 1.0 / 2.4) - 0.055 : 12.92 * r;
|
|
274
|
+
g = g >= 0.0031308 ? 1.055 * Math.pow(g, 1.0 / 2.4) - 0.055 : 12.92 * g;
|
|
275
|
+
b = b >= 0.0031308 ? 1.055 * Math.pow(b, 1.0 / 2.4) - 0.055 : 12.92 * b;
|
|
276
|
+
|
|
277
|
+
r = Math.min(Math.max(0, r), 1) * 255;
|
|
278
|
+
g = Math.min(Math.max(0, g), 1) * 255;
|
|
279
|
+
b = Math.min(Math.max(0, b), 1) * 255;
|
|
280
|
+
return { r, g, b };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function hsvToHex(h: number, s: number, v: number, a: number) {
|
|
284
|
+
const { r, g, b } = hsvToRgb(h, s, v);
|
|
285
|
+
const toHex = (x: number) => {
|
|
286
|
+
const hex = x.toString(16);
|
|
287
|
+
return hex.length === 1 ? '0' + hex : hex;
|
|
288
|
+
};
|
|
289
|
+
let hex = `#${toHex(r)}${toHex(g)}${toHex(b)}`;
|
|
290
|
+
if (allowOpacity && a < 1) hex += toHex(Math.round(a * 255));
|
|
291
|
+
return hex.toUpperCase();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function hsvToRgbString(h: number, s: number, v: number, a: number) {
|
|
295
|
+
const { r, g, b } = hsvToRgb(h, s, v);
|
|
296
|
+
if (allowOpacity && a < 1) return `rgba(${r}, ${g}, ${b}, ${parseFloat(a.toFixed(2))})`;
|
|
297
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function hsvToHslString(h: number, s: number, v: number, a: number) {
|
|
301
|
+
const sNorm = s / 100,
|
|
302
|
+
vNorm = v / 100;
|
|
303
|
+
let l = ((2 - sNorm) * vNorm) / 2;
|
|
304
|
+
let sHsl = l && l < 1 ? (sNorm * vNorm) / (l < 0.5 ? l * 2 : 2 - l * 2) : sNorm;
|
|
305
|
+
if (allowOpacity && a < 1)
|
|
306
|
+
return `hsla(${Math.round(h)}, ${Math.round(sHsl * 100)}%, ${Math.round(l * 100)}%, ${parseFloat(a.toFixed(2))})`;
|
|
307
|
+
return `hsl(${Math.round(h)}, ${Math.round(sHsl * 100)}%, ${Math.round(l * 100)}%)`;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function handleDragStart(e: MouseEvent | TouchEvent, fn: (e: MouseEvent | TouchEvent) => void) {
|
|
311
|
+
isDragging = true;
|
|
312
|
+
fn(e);
|
|
313
|
+
const move = (e: MouseEvent | TouchEvent) => fn(e);
|
|
314
|
+
const stop = () => {
|
|
315
|
+
isDragging = false;
|
|
316
|
+
window.removeEventListener('mousemove', move);
|
|
317
|
+
window.removeEventListener('touchmove', move);
|
|
318
|
+
window.removeEventListener('mouseup', stop);
|
|
319
|
+
window.removeEventListener('touchend', stop);
|
|
320
|
+
};
|
|
321
|
+
window.addEventListener('mousemove', move);
|
|
322
|
+
window.addEventListener('touchmove', move);
|
|
323
|
+
window.addEventListener('mouseup', stop);
|
|
324
|
+
window.addEventListener('touchend', stop);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Pointer coordinates from a mouse or touch event, or null if unavailable. */
|
|
328
|
+
function pointerXY(e: MouseEvent | TouchEvent): { x: number; y: number } | null {
|
|
329
|
+
if ('touches' in e) {
|
|
330
|
+
const touch = e.touches[0];
|
|
331
|
+
return touch ? { x: touch.clientX, y: touch.clientY } : null;
|
|
332
|
+
}
|
|
333
|
+
return { x: e.clientX, y: e.clientY };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
function handleSbChange(e: MouseEvent | TouchEvent) {
|
|
337
|
+
if (!sbRef) return;
|
|
338
|
+
const pt = pointerXY(e);
|
|
339
|
+
if (!pt) return;
|
|
340
|
+
const rect = sbRef.getBoundingClientRect();
|
|
341
|
+
const x = Math.max(0, Math.min(1, (pt.x - rect.left) / rect.width));
|
|
342
|
+
const y = Math.max(0, Math.min(1, (pt.y - rect.top) / rect.height));
|
|
343
|
+
s = x * 100;
|
|
344
|
+
v = (1 - y) * 100;
|
|
345
|
+
updateExternal();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function handleHueChange(e: MouseEvent | TouchEvent) {
|
|
349
|
+
if (!hueRef) return;
|
|
350
|
+
const pt = pointerXY(e);
|
|
351
|
+
if (!pt) return;
|
|
352
|
+
const rect = hueRef.getBoundingClientRect();
|
|
353
|
+
const x = Math.max(0, Math.min(1, (pt.x - rect.left) / rect.width));
|
|
354
|
+
h = x * 360;
|
|
355
|
+
updateExternal();
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function handleAlphaChange(e: MouseEvent | TouchEvent) {
|
|
359
|
+
if (!alphaRef) return;
|
|
360
|
+
const pt = pointerXY(e);
|
|
361
|
+
if (!pt) return;
|
|
362
|
+
const rect = alphaRef.getBoundingClientRect();
|
|
363
|
+
const x = Math.max(0, Math.min(1, (pt.x - rect.left) / rect.width));
|
|
364
|
+
a = Math.round(x * 100) / 100;
|
|
365
|
+
updateExternal();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
function handleAlphaInput(e: Event & { currentTarget: HTMLInputElement }) {
|
|
369
|
+
let val = parseInt(e.currentTarget.value);
|
|
370
|
+
if (isNaN(val)) return;
|
|
371
|
+
val = Math.max(0, Math.min(100, val));
|
|
372
|
+
a = val / 100;
|
|
373
|
+
updateExternal();
|
|
374
|
+
}
|
|
375
|
+
</script>
|
|
376
|
+
|
|
377
|
+
<div
|
|
378
|
+
class={cn('bg-popover flex w-[350px] flex-col gap-3 rounded-lg border p-3 shadow-sm', className)}
|
|
379
|
+
>
|
|
380
|
+
<div
|
|
381
|
+
bind:this={sbRef}
|
|
382
|
+
class="relative h-56 w-full cursor-crosshair touch-none rounded-md shadow-sm"
|
|
383
|
+
style:background-color={`hsl(${h}, 100%, 50%)`}
|
|
384
|
+
role="slider"
|
|
385
|
+
aria-label="Saturation and brightness"
|
|
386
|
+
aria-valuenow={s}
|
|
387
|
+
tabindex="0"
|
|
388
|
+
onmousedown={(e) => handleDragStart(e, handleSbChange)}
|
|
389
|
+
ontouchstart={(e) => handleDragStart(e, handleSbChange)}
|
|
390
|
+
>
|
|
391
|
+
<div class="pointer-events-none absolute inset-0 overflow-hidden rounded-md">
|
|
392
|
+
<div class="absolute inset-0 bg-gradient-to-r from-white to-transparent"></div>
|
|
393
|
+
<div class="absolute inset-0 bg-gradient-to-t from-black to-transparent"></div>
|
|
394
|
+
</div>
|
|
395
|
+
<svg
|
|
396
|
+
class="pointer-events-none absolute inset-0 z-10 h-full w-full"
|
|
397
|
+
style:overflow="visible"
|
|
398
|
+
aria-hidden="true"
|
|
399
|
+
>
|
|
400
|
+
<circle cx={`${s}%`} cy={`${100 - v}%`} r="6" fill="none" stroke="rgba(0, 0, 0, 0.25)" />
|
|
401
|
+
<circle cx={`${s}%`} cy={`${100 - v}%`} r="5" fill="none" stroke="white" stroke-width="2" />
|
|
402
|
+
</svg>
|
|
403
|
+
</div>
|
|
404
|
+
|
|
405
|
+
<div class="flex items-center gap-3">
|
|
406
|
+
<div
|
|
407
|
+
class="relative mt-1 h-8 w-8 shrink-0 overflow-hidden rounded-md border bg-[url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMUlEQVQ4T2NkYGAQYcAP3uCTZhw1gGGYhAGBZIA/nYDCgBDAm9BGDWAAJyRCgLaBCAAgXwixzAS0pgAAAABJRU5ErkJggg==')] shadow-sm"
|
|
408
|
+
>
|
|
409
|
+
<div class="absolute inset-0" style:background-color={hsvToHex(h, s, v, a)}></div>
|
|
410
|
+
</div>
|
|
411
|
+
|
|
412
|
+
<div class="flex flex-1 flex-col justify-center gap-3">
|
|
413
|
+
<div
|
|
414
|
+
bind:this={hueRef}
|
|
415
|
+
class="relative h-3 w-full cursor-pointer touch-none rounded-full shadow-sm ring-1 ring-black/5"
|
|
416
|
+
style:background={hueGradient}
|
|
417
|
+
role="slider"
|
|
418
|
+
aria-label="Hue"
|
|
419
|
+
aria-valuenow={h}
|
|
420
|
+
tabindex="0"
|
|
421
|
+
onmousedown={(e) => handleDragStart(e, handleHueChange)}
|
|
422
|
+
ontouchstart={(e) => handleDragStart(e, handleHueChange)}
|
|
423
|
+
>
|
|
424
|
+
<div
|
|
425
|
+
class="pointer-events-none absolute top-1/2 z-10 h-3 w-3 rounded-full bg-white"
|
|
426
|
+
style:left={`min(max(6px, ${(h / 360) * 100}%), calc(100% - 6px))`}
|
|
427
|
+
style:transform="translate(-50%, -50%)"
|
|
428
|
+
></div>
|
|
429
|
+
</div>
|
|
430
|
+
|
|
431
|
+
{#if allowOpacity}
|
|
432
|
+
<div
|
|
433
|
+
bind:this={alphaRef}
|
|
434
|
+
class="relative h-3 w-full cursor-pointer touch-none rounded-full bg-[url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAMUlEQVQ4T2NkYGAQYcAP3uCTZhw1gGGYhAGBZIA/nYDCgBDAm9BGDWAAJyRCgLaBCAAgXwixzAS0pgAAAABJRU5ErkJggg==')] shadow-sm ring-1 ring-black/5"
|
|
435
|
+
role="slider"
|
|
436
|
+
aria-label="Opacity"
|
|
437
|
+
aria-valuenow={a}
|
|
438
|
+
tabindex="0"
|
|
439
|
+
onmousedown={(e) => handleDragStart(e, handleAlphaChange)}
|
|
440
|
+
ontouchstart={(e) => handleDragStart(e, handleAlphaChange)}
|
|
441
|
+
>
|
|
442
|
+
<div
|
|
443
|
+
class="absolute inset-0 rounded-full"
|
|
444
|
+
style:background={`linear-gradient(to right, transparent, ${hsvToHex(h, s, v, 1)})`}
|
|
445
|
+
></div>
|
|
446
|
+
<div
|
|
447
|
+
class="pointer-events-none absolute top-1/2 z-10 h-3 w-3 rounded-full bg-white"
|
|
448
|
+
style:left={`min(max(6px, ${a * 100}%), calc(100% - 6px))`}
|
|
449
|
+
style:transform="translate(-50%, -50%)"
|
|
450
|
+
></div>
|
|
451
|
+
</div>
|
|
452
|
+
{/if}
|
|
453
|
+
</div>
|
|
454
|
+
</div>
|
|
455
|
+
|
|
456
|
+
<ButtonGroup.Root class="w-full">
|
|
457
|
+
{#if formats.length > 1}
|
|
458
|
+
<Popover.Root bind:open={formatOpen}>
|
|
459
|
+
<Popover.Trigger>
|
|
460
|
+
{#snippet child({ props })}
|
|
461
|
+
<Button
|
|
462
|
+
{...props}
|
|
463
|
+
variant="outline"
|
|
464
|
+
class="h-9 max-w-[5rem] justify-between px-2 text-[10px]"
|
|
465
|
+
>
|
|
466
|
+
{activeFormat.toUpperCase()}
|
|
467
|
+
<ChevronDown class="h-3 w-3 opacity-50" />
|
|
468
|
+
</Button>
|
|
469
|
+
{/snippet}
|
|
470
|
+
</Popover.Trigger>
|
|
471
|
+
<Popover.Content class="w-[4.5rem] p-0" align="start">
|
|
472
|
+
<Command.Root>
|
|
473
|
+
<Command.List>
|
|
474
|
+
<Command.Group>
|
|
475
|
+
{#each formats as fmt}
|
|
476
|
+
<Command.Item
|
|
477
|
+
value={fmt}
|
|
478
|
+
onSelect={() => setFormat(fmt)}
|
|
479
|
+
class="flex h-7 justify-center text-[10px]"
|
|
480
|
+
>
|
|
481
|
+
{fmt.toUpperCase()}
|
|
482
|
+
</Command.Item>
|
|
483
|
+
{/each}
|
|
484
|
+
</Command.Group>
|
|
485
|
+
</Command.List>
|
|
486
|
+
</Command.Root>
|
|
487
|
+
</Popover.Content>
|
|
488
|
+
</Popover.Root>
|
|
489
|
+
{:else}
|
|
490
|
+
<Button variant="outline" class="h-9 max-w-[5rem] justify-between px-2 text-[10px]">
|
|
491
|
+
{activeFormat.toUpperCase()}
|
|
492
|
+
</Button>
|
|
493
|
+
{/if}
|
|
494
|
+
<Input
|
|
495
|
+
class="h-9 flex-1 font-mono text-[10px] uppercase"
|
|
496
|
+
{value}
|
|
497
|
+
oninput={(e) => {
|
|
498
|
+
const parsed = parseColor(e.currentTarget.value);
|
|
499
|
+
if (parsed) {
|
|
500
|
+
h = parsed.h;
|
|
501
|
+
s = parsed.s;
|
|
502
|
+
v = parsed.v;
|
|
503
|
+
a = parsed.a;
|
|
504
|
+
updateExternal();
|
|
505
|
+
}
|
|
506
|
+
}}
|
|
507
|
+
/>
|
|
508
|
+
|
|
509
|
+
{#if allowOpacity}
|
|
510
|
+
<Input
|
|
511
|
+
class="h-9 max-w-[4.2rem] text-right font-mono text-[10px]"
|
|
512
|
+
value={Math.round(a * 100) + '%'}
|
|
513
|
+
oninput={handleAlphaInput}
|
|
514
|
+
maxlength={3}
|
|
515
|
+
/>
|
|
516
|
+
{/if}
|
|
517
|
+
</ButtonGroup.Root>
|
|
518
|
+
</div>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
type ColorFormat = 'hex' | 'rgb' | 'hsl' | 'oklch';
|
|
2
|
+
type $$ComponentProps = {
|
|
3
|
+
value?: string;
|
|
4
|
+
class?: string;
|
|
5
|
+
allowOpacity?: boolean;
|
|
6
|
+
defaultFormat?: ColorFormat;
|
|
7
|
+
formats?: ColorFormat[];
|
|
8
|
+
onChange?: (value: string) => void;
|
|
9
|
+
};
|
|
10
|
+
declare const ColorPicker: import("svelte").Component<$$ComponentProps, {}, "value">;
|
|
11
|
+
type ColorPicker = ReturnType<typeof ColorPicker>;
|
|
12
|
+
export default ColorPicker;
|
|
13
|
+
//# sourceMappingURL=ColorPicker.svelte.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ColorPicker.svelte.d.ts","sourceRoot":"","sources":["../src/lib/ColorPicker.svelte.ts"],"names":[],"mappings":"AAYC,KAAK,WAAW,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,GAAG,OAAO,CAAC;AACnD,KAAK,gBAAgB,GAAI;IACxB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,aAAa,CAAC,EAAE,WAAW,CAAC;IAC5B,OAAO,CAAC,EAAE,WAAW,EAAE,CAAC;IACxB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC,CAAC;AAicH,QAAA,MAAM,WAAW,2DAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Aphex plugins consume `@aphexcms/cms-core` from source, so this package's
|
|
2
|
+
// `svelte-check` follows into cms-core's server files, which rely on the
|
|
3
|
+
// SvelteKit `App.Locals` augmentation a host app provides. Re-declare it here so
|
|
4
|
+
// the plugin type-checks standalone (in an app, the app's own app.d.ts wins).
|
|
5
|
+
import type { CMSInstances, Auth, PreviewPerspective } from '@aphexcms/cms-core/server';
|
|
6
|
+
|
|
7
|
+
declare global {
|
|
8
|
+
namespace App {
|
|
9
|
+
interface Locals {
|
|
10
|
+
aphexCMS: CMSInstances;
|
|
11
|
+
auth?: Auth;
|
|
12
|
+
previewPerspective?: PreviewPerspective;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export {};
|
package/dist/color.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color value model + conversions for the rich (object) color field.
|
|
3
|
+
*
|
|
4
|
+
* The picker can store a color two ways:
|
|
5
|
+
* - `type: 'string'` → a plain hex/CSS string (simple, drops straight into CSS).
|
|
6
|
+
* - `type: 'object'` → this `ColorValue` — hex + alpha + rgb/hsl/hsv, mirroring
|
|
7
|
+
* Sanity's `@sanity/color-input` data model so a color is usable in any format
|
|
8
|
+
* without re-converting at the call site.
|
|
9
|
+
*
|
|
10
|
+
* Plain TS (no DOM), safe to import anywhere.
|
|
11
|
+
*/
|
|
12
|
+
export interface RgbaColor {
|
|
13
|
+
r: number;
|
|
14
|
+
g: number;
|
|
15
|
+
b: number;
|
|
16
|
+
a: number;
|
|
17
|
+
}
|
|
18
|
+
export interface HslaColor {
|
|
19
|
+
h: number;
|
|
20
|
+
s: number;
|
|
21
|
+
l: number;
|
|
22
|
+
a: number;
|
|
23
|
+
}
|
|
24
|
+
export interface HsvaColor {
|
|
25
|
+
h: number;
|
|
26
|
+
s: number;
|
|
27
|
+
v: number;
|
|
28
|
+
a: number;
|
|
29
|
+
}
|
|
30
|
+
/** The stored shape of a rich color field. */
|
|
31
|
+
export interface ColorValue {
|
|
32
|
+
hex: string;
|
|
33
|
+
alpha: number;
|
|
34
|
+
rgb: RgbaColor;
|
|
35
|
+
hsl: HslaColor;
|
|
36
|
+
hsv: HsvaColor;
|
|
37
|
+
}
|
|
38
|
+
/** RGBA → `#RRGGBB` (or `#RRGGBBAA` when alpha < 1). */
|
|
39
|
+
export declare function rgbToHex({ r, g, b, a }: RgbaColor): string;
|
|
40
|
+
/** Build the full `ColorValue` from any CSS color string the picker emits. `null` if unparseable. */
|
|
41
|
+
export declare function parseColorToValue(css: string): ColorValue | null;
|
|
42
|
+
/** The CSS string a picker should bind to for a stored `ColorValue` (hex, 8-digit if alpha < 1). */
|
|
43
|
+
export declare function colorValueToCss(value: unknown): string;
|
|
44
|
+
//# sourceMappingURL=color.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"color.d.ts","sourceRoot":"","sources":["../src/lib/color.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAMH,MAAM,WAAW,SAAS;IACzB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AACD,MAAM,WAAW,SAAS;IACzB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AACD,MAAM,WAAW,SAAS;IACzB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACV;AAED,8CAA8C;AAC9C,MAAM,WAAW,UAAU;IAC1B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,SAAS,CAAC;IACf,GAAG,EAAE,SAAS,CAAC;IACf,GAAG,EAAE,SAAS,CAAC;CACf;AAoED,wDAAwD;AACxD,wBAAgB,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,SAAS,GAAG,MAAM,CAG1D;AA0CD,qGAAqG;AACrG,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,GAAG,IAAI,CAUhE;AAED,oGAAoG;AACpG,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAMtD"}
|
package/dist/color.js
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color value model + conversions for the rich (object) color field.
|
|
3
|
+
*
|
|
4
|
+
* The picker can store a color two ways:
|
|
5
|
+
* - `type: 'string'` → a plain hex/CSS string (simple, drops straight into CSS).
|
|
6
|
+
* - `type: 'object'` → this `ColorValue` — hex + alpha + rgb/hsl/hsv, mirroring
|
|
7
|
+
* Sanity's `@sanity/color-input` data model so a color is usable in any format
|
|
8
|
+
* without re-converting at the call site.
|
|
9
|
+
*
|
|
10
|
+
* Plain TS (no DOM), safe to import anywhere.
|
|
11
|
+
*/
|
|
12
|
+
const clamp = (n, lo, hi) => Math.max(lo, Math.min(hi, n));
|
|
13
|
+
const round = (n, dp = 0) => {
|
|
14
|
+
const f = 10 ** dp;
|
|
15
|
+
return Math.round(n * f) / f;
|
|
16
|
+
};
|
|
17
|
+
function hexToRgb(hex) {
|
|
18
|
+
let h = hex.replace('#', '').trim();
|
|
19
|
+
// Expand shorthand (#rgb / #rgba) by doubling each nibble.
|
|
20
|
+
if (h.length === 3 || h.length === 4)
|
|
21
|
+
h = h
|
|
22
|
+
.split('')
|
|
23
|
+
.map((c) => c + c)
|
|
24
|
+
.join('');
|
|
25
|
+
if (h.length === 6)
|
|
26
|
+
h += 'ff';
|
|
27
|
+
if (h.length !== 8 || /[^0-9a-f]/i.test(h))
|
|
28
|
+
return null;
|
|
29
|
+
return {
|
|
30
|
+
r: parseInt(h.slice(0, 2), 16),
|
|
31
|
+
g: parseInt(h.slice(2, 4), 16),
|
|
32
|
+
b: parseInt(h.slice(4, 6), 16),
|
|
33
|
+
a: round(parseInt(h.slice(6, 8), 16) / 255, 2)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function rgbToHsv({ r, g, b, a }) {
|
|
37
|
+
const rn = r / 255, gn = g / 255, bn = b / 255;
|
|
38
|
+
const max = Math.max(rn, gn, bn), min = Math.min(rn, gn, bn);
|
|
39
|
+
const d = max - min;
|
|
40
|
+
let h = 0;
|
|
41
|
+
if (d !== 0) {
|
|
42
|
+
if (max === rn)
|
|
43
|
+
h = ((gn - bn) / d) % 6;
|
|
44
|
+
else if (max === gn)
|
|
45
|
+
h = (bn - rn) / d + 2;
|
|
46
|
+
else
|
|
47
|
+
h = (rn - gn) / d + 4;
|
|
48
|
+
h *= 60;
|
|
49
|
+
if (h < 0)
|
|
50
|
+
h += 360;
|
|
51
|
+
}
|
|
52
|
+
const s = max === 0 ? 0 : d / max;
|
|
53
|
+
return { h: round(h, 1), s: round(s * 100, 1), v: round(max * 100, 1), a };
|
|
54
|
+
}
|
|
55
|
+
function rgbToHsl({ r, g, b, a }) {
|
|
56
|
+
const rn = r / 255, gn = g / 255, bn = b / 255;
|
|
57
|
+
const max = Math.max(rn, gn, bn), min = Math.min(rn, gn, bn);
|
|
58
|
+
const d = max - min;
|
|
59
|
+
const l = (max + min) / 2;
|
|
60
|
+
let h = 0;
|
|
61
|
+
let s = 0;
|
|
62
|
+
if (d !== 0) {
|
|
63
|
+
s = d / (1 - Math.abs(2 * l - 1));
|
|
64
|
+
if (max === rn)
|
|
65
|
+
h = ((gn - bn) / d) % 6;
|
|
66
|
+
else if (max === gn)
|
|
67
|
+
h = (bn - rn) / d + 2;
|
|
68
|
+
else
|
|
69
|
+
h = (rn - gn) / d + 4;
|
|
70
|
+
h *= 60;
|
|
71
|
+
if (h < 0)
|
|
72
|
+
h += 360;
|
|
73
|
+
}
|
|
74
|
+
return { h: round(h, 1), s: round(s * 100, 1), l: round(l * 100, 1), a };
|
|
75
|
+
}
|
|
76
|
+
const toHex2 = (n) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, '0');
|
|
77
|
+
/** RGBA → `#RRGGBB` (or `#RRGGBBAA` when alpha < 1). */
|
|
78
|
+
export function rgbToHex({ r, g, b, a }) {
|
|
79
|
+
const base = `#${toHex2(r)}${toHex2(g)}${toHex2(b)}`;
|
|
80
|
+
return a < 1 ? `${base}${toHex2(a * 255)}` : base.toUpperCase();
|
|
81
|
+
}
|
|
82
|
+
/** Parse a CSS color string the picker emits (hex / rgb(a) / hsl(a)) into `RgbaColor`. */
|
|
83
|
+
function parseCssToRgb(css) {
|
|
84
|
+
const s = css.trim().toLowerCase();
|
|
85
|
+
if (s.startsWith('#'))
|
|
86
|
+
return hexToRgb(s);
|
|
87
|
+
if (s.startsWith('rgb')) {
|
|
88
|
+
const n = s.match(/[\d.]+/g)?.map(Number);
|
|
89
|
+
if (n && n.length >= 3) {
|
|
90
|
+
const [r = 0, g = 0, b = 0, a = 1] = n;
|
|
91
|
+
return { r, g, b, a };
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (s.startsWith('hsl')) {
|
|
95
|
+
const n = s.match(/[\d.]+/g)?.map(Number);
|
|
96
|
+
if (n && n.length >= 3) {
|
|
97
|
+
const [hh = 0, ss = 0, ll = 0, a = 1] = n;
|
|
98
|
+
const h = hh / 360, sl = ss / 100, l = ll / 100;
|
|
99
|
+
const q = l < 0.5 ? l * (1 + sl) : l + sl - l * sl;
|
|
100
|
+
const p = 2 * l - q;
|
|
101
|
+
const hue = (t) => {
|
|
102
|
+
let tt = t;
|
|
103
|
+
if (tt < 0)
|
|
104
|
+
tt += 1;
|
|
105
|
+
if (tt > 1)
|
|
106
|
+
tt -= 1;
|
|
107
|
+
if (tt < 1 / 6)
|
|
108
|
+
return p + (q - p) * 6 * tt;
|
|
109
|
+
if (tt < 1 / 2)
|
|
110
|
+
return q;
|
|
111
|
+
if (tt < 2 / 3)
|
|
112
|
+
return p + (q - p) * (2 / 3 - tt) * 6;
|
|
113
|
+
return p;
|
|
114
|
+
};
|
|
115
|
+
return {
|
|
116
|
+
r: round(hue(h + 1 / 3) * 255),
|
|
117
|
+
g: round(hue(h) * 255),
|
|
118
|
+
b: round(hue(h - 1 / 3) * 255),
|
|
119
|
+
a
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
/** Build the full `ColorValue` from any CSS color string the picker emits. `null` if unparseable. */
|
|
126
|
+
export function parseColorToValue(css) {
|
|
127
|
+
const rgb = parseCssToRgb(css);
|
|
128
|
+
if (!rgb)
|
|
129
|
+
return null;
|
|
130
|
+
return {
|
|
131
|
+
hex: rgbToHex(rgb),
|
|
132
|
+
alpha: rgb.a,
|
|
133
|
+
rgb,
|
|
134
|
+
hsl: rgbToHsl(rgb),
|
|
135
|
+
hsv: rgbToHsv(rgb)
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
/** The CSS string a picker should bind to for a stored `ColorValue` (hex, 8-digit if alpha < 1). */
|
|
139
|
+
export function colorValueToCss(value) {
|
|
140
|
+
if (!value || typeof value !== 'object')
|
|
141
|
+
return '';
|
|
142
|
+
const v = value;
|
|
143
|
+
if (typeof v.hex === 'string' && v.hex)
|
|
144
|
+
return v.hex;
|
|
145
|
+
if (v.rgb)
|
|
146
|
+
return rgbToHex(v.rgb);
|
|
147
|
+
return '';
|
|
148
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/lib/constants.ts"],"names":[],"mappings":"AAAA;uFACuF;AACvF,eAAO,MAAM,WAAW,UAAU,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { COLOR_INPUT } from './constants.js';
|
|
2
|
+
export type { ColorField } from './schema.js';
|
|
3
|
+
export declare function colorPickerPlugin(): import("@aphexcms/cms-core").CMSPlugin;
|
|
4
|
+
export { default as ColorPicker } from './ColorPicker.svelte';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/lib/index.ts"],"names":[],"mappings":"AA0BA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAG7C,YAAY,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAE9C,wBAAgB,iBAAiB,2CAUhC;AAED,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color plugin — a rich color field widget for AphexCMS.
|
|
3
|
+
*
|
|
4
|
+
* Registers an `aphex/field/component` part for the `color` input. It works over
|
|
5
|
+
* two storage shapes, chosen by the field's `type`:
|
|
6
|
+
* - `{ type: 'string', input: 'color' }` → stores a plain hex/CSS string (drops
|
|
7
|
+
* straight into CSS; ideal for theme tokens).
|
|
8
|
+
* - a rich object storing `{ hex, alpha, rgb, hsl, hsv }` (Sanity's data model) —
|
|
9
|
+
* use the `color()` helper from `@aphexcms/plugin-color-picker/schema` to declare
|
|
10
|
+
* it in one line.
|
|
11
|
+
*
|
|
12
|
+
* Color is intentionally NOT a built-in field type in cms-core — like Sanity, the
|
|
13
|
+
* engine ships the primitives and color is a plugin. Register it once:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* // src/lib/plugins.ts
|
|
17
|
+
* import { colorPickerPlugin } from '@aphexcms/plugin-color-picker';
|
|
18
|
+
* export const plugins = [colorPickerPlugin()];
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
import { definePlugin } from '@aphexcms/cms-core';
|
|
22
|
+
import ColorInput from './ColorInput.svelte';
|
|
23
|
+
import { COLOR_INPUT } from './constants.js';
|
|
24
|
+
import { expandColorTypes } from './schema.js';
|
|
25
|
+
export { COLOR_INPUT } from './constants.js';
|
|
26
|
+
export function colorPickerPlugin() {
|
|
27
|
+
const parts = [
|
|
28
|
+
// The widget for `input: 'color'` — over a `string` field (plain hex) or the
|
|
29
|
+
// rich `object` field the `type: 'color'` sugar / `color()` helper produce.
|
|
30
|
+
{ implements: 'aphex/field/component', input: COLOR_INPUT, component: ColorInput },
|
|
31
|
+
// Desugars authored `{ type: 'color' }` into the rich color object before the
|
|
32
|
+
// engine and type generator see it.
|
|
33
|
+
{ implements: 'aphex/schema/transform', transform: expandColorTypes }
|
|
34
|
+
];
|
|
35
|
+
return definePlugin({ name: '@aphexcms/plugin-color-picker', version: '0.1.0', parts });
|
|
36
|
+
}
|
|
37
|
+
export { default as ColorPicker } from './ColorPicker.svelte';
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema surface for the color field.
|
|
3
|
+
*
|
|
4
|
+
* Two ways to declare one, both storing the same rich `{ hex, alpha, rgb, hsl, hsv }`
|
|
5
|
+
* object:
|
|
6
|
+
* - `{ type: 'color', name: 'brand' }` — the literal. Registering `colorPickerPlugin()`
|
|
7
|
+
* augments core's `FieldTypeMap` (see below) so `type: 'color'` is fully type-safe
|
|
8
|
+
* with no import, and the plugin's schema-transform desugars it into the object.
|
|
9
|
+
* - `color({ name: 'brand' })` — the helper. Equivalent, for when you'd rather import
|
|
10
|
+
* a builder than rely on the ambient type.
|
|
11
|
+
*
|
|
12
|
+
* Either way it's an `object` under the hood (portable, type-generated, validated) —
|
|
13
|
+
* not a bespoke storage primitive.
|
|
14
|
+
*/
|
|
15
|
+
import type { ObjectField, BaseField, SchemaType } from '@aphexcms/cms-core';
|
|
16
|
+
export type { ColorValue, RgbaColor, HslaColor, HsvaColor } from './color.js';
|
|
17
|
+
/** The `type` keyword this plugin desugars (authored as `{ type: 'color' }`). */
|
|
18
|
+
export declare const COLOR_TYPE = "color";
|
|
19
|
+
/**
|
|
20
|
+
* The authored shape of a `{ type: 'color' }` field. The plugin's schema-transform
|
|
21
|
+
* expands it into the rich color `object` before the engine/codegen see it.
|
|
22
|
+
*/
|
|
23
|
+
export interface ColorField extends BaseField {
|
|
24
|
+
type: 'color';
|
|
25
|
+
/** Allow an alpha channel (stored on the object + 8-digit hex). Default: false. */
|
|
26
|
+
alpha?: boolean;
|
|
27
|
+
}
|
|
28
|
+
declare module '@aphexcms/cms-core' {
|
|
29
|
+
interface FieldTypeMap {
|
|
30
|
+
color: ColorField;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Config for the `color()` builder.
|
|
35
|
+
*
|
|
36
|
+
* Extends `BaseField` (minus the keys the builder owns) so the helper can express
|
|
37
|
+
* everything the `{ type: 'color' }` literal can — `access`, `validation`, multiple
|
|
38
|
+
* groups. Otherwise the two ways of declaring a color wouldn't actually be
|
|
39
|
+
* equivalent: the literal preserves them, and the builder couldn't accept them.
|
|
40
|
+
*/
|
|
41
|
+
export interface ColorFieldConfig extends Partial<Omit<BaseField, 'type' | 'name' | 'input' | 'inputOptions'>> {
|
|
42
|
+
name: string;
|
|
43
|
+
/** Allow an alpha channel (stored on the object + 8-digit hex). Default: false. */
|
|
44
|
+
alpha?: boolean;
|
|
45
|
+
}
|
|
46
|
+
/** Build a rich-color `object` field: stores `{ hex, alpha, rgb, hsl, hsv }`, edited by the picker. */
|
|
47
|
+
export declare function color(config: ColorFieldConfig): ObjectField;
|
|
48
|
+
/**
|
|
49
|
+
* Schema-transform: desugar every `{ type: 'color' }` field into the rich color
|
|
50
|
+
* `object`, everywhere in the schema list. Registered as the plugin's
|
|
51
|
+
* `aphex/schema/transform` part so it runs in the engine, admin, and type generator
|
|
52
|
+
* alike — the engine never sees a `color` primitive.
|
|
53
|
+
*
|
|
54
|
+
* `desugarFieldType` owns the walk (nested objects, array members) and preserves
|
|
55
|
+
* whatever the author declared — `access`, `validation`, multiple groups — so this
|
|
56
|
+
* only has to describe the shape. `alpha` is sugar: it becomes `inputOptions.alpha`
|
|
57
|
+
* and must not survive onto the expanded object.
|
|
58
|
+
*/
|
|
59
|
+
export declare function expandColorTypes(schemas: SchemaType[]): SchemaType[];
|
|
60
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/lib/schema.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAI7E,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE9E,iFAAiF;AACjF,eAAO,MAAM,UAAU,UAAU,CAAC;AAElC;;;GAGG;AACH,MAAM,WAAW,UAAW,SAAQ,SAAS;IAC5C,IAAI,EAAE,OAAO,CAAC;IACd,mFAAmF;IACnF,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAKD,OAAO,QAAQ,oBAAoB,CAAC;IACnC,UAAU,YAAY;QACrB,KAAK,EAAE,UAAU,CAAC;KAClB;CACD;AAED;;;;;;;GAOG;AACH,MAAM,WAAW,gBAAiB,SAAQ,OAAO,CAChD,IAAI,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,cAAc,CAAC,CAC3D;IACA,IAAI,EAAE,MAAM,CAAC;IACb,mFAAmF;IACnF,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAID,uGAAuG;AACvG,wBAAgB,KAAK,CAAC,MAAM,EAAE,gBAAgB,GAAG,WAAW,CAkC3D;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,UAAU,EAAE,GAAG,UAAU,EAAE,CAWpE"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { desugarFieldType } from '@aphexcms/cms-core';
|
|
2
|
+
import { COLOR_INPUT } from './constants.js';
|
|
3
|
+
/** The `type` keyword this plugin desugars (authored as `{ type: 'color' }`). */
|
|
4
|
+
export const COLOR_TYPE = 'color';
|
|
5
|
+
const numberSub = (name) => ({ name, type: 'number', title: name });
|
|
6
|
+
/** Build a rich-color `object` field: stores `{ hex, alpha, rgb, hsl, hsv }`, edited by the picker. */
|
|
7
|
+
export function color(config) {
|
|
8
|
+
const { name, title, alpha, ...rest } = config;
|
|
9
|
+
return {
|
|
10
|
+
// `rest` carries whatever else the caller declared (access, validation, group,
|
|
11
|
+
// description, …) so the builder stays equivalent to the `type: 'color'` literal.
|
|
12
|
+
...rest,
|
|
13
|
+
name,
|
|
14
|
+
type: 'object',
|
|
15
|
+
title: title ?? name,
|
|
16
|
+
input: COLOR_INPUT,
|
|
17
|
+
inputOptions: { alpha: alpha === true },
|
|
18
|
+
fields: [
|
|
19
|
+
{ name: 'hex', type: 'string', title: 'Hex' },
|
|
20
|
+
{ name: 'alpha', type: 'number', title: 'Alpha' },
|
|
21
|
+
{
|
|
22
|
+
name: 'rgb',
|
|
23
|
+
type: 'object',
|
|
24
|
+
title: 'RGB',
|
|
25
|
+
fields: ['r', 'g', 'b', 'a'].map(numberSub)
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'hsl',
|
|
29
|
+
type: 'object',
|
|
30
|
+
title: 'HSL',
|
|
31
|
+
fields: ['h', 's', 'l', 'a'].map(numberSub)
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'hsv',
|
|
35
|
+
type: 'object',
|
|
36
|
+
title: 'HSV',
|
|
37
|
+
fields: ['h', 's', 'v', 'a'].map(numberSub)
|
|
38
|
+
}
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Schema-transform: desugar every `{ type: 'color' }` field into the rich color
|
|
44
|
+
* `object`, everywhere in the schema list. Registered as the plugin's
|
|
45
|
+
* `aphex/schema/transform` part so it runs in the engine, admin, and type generator
|
|
46
|
+
* alike — the engine never sees a `color` primitive.
|
|
47
|
+
*
|
|
48
|
+
* `desugarFieldType` owns the walk (nested objects, array members) and preserves
|
|
49
|
+
* whatever the author declared — `access`, `validation`, multiple groups — so this
|
|
50
|
+
* only has to describe the shape. `alpha` is sugar: it becomes `inputOptions.alpha`
|
|
51
|
+
* and must not survive onto the expanded object.
|
|
52
|
+
*/
|
|
53
|
+
export function expandColorTypes(schemas) {
|
|
54
|
+
return desugarFieldType(schemas, {
|
|
55
|
+
type: COLOR_TYPE,
|
|
56
|
+
sugarKeys: ['alpha'],
|
|
57
|
+
build: (f) => color({
|
|
58
|
+
name: f.name,
|
|
59
|
+
title: f.title,
|
|
60
|
+
alpha: f.alpha === true
|
|
61
|
+
})
|
|
62
|
+
});
|
|
63
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aphexcms/plugin-color-picker",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Color picker field widget for AphexCMS — a rich color input (hex, optional alpha) registered as a plugin field component",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "https://github.com/IcelandicIcecream/aphex.git",
|
|
12
|
+
"directory": "plugins/plugin-color-picker"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"svelte": "./dist/index.js",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"svelte": "./dist/index.js",
|
|
22
|
+
"default": "./dist/index.js"
|
|
23
|
+
},
|
|
24
|
+
"./schema": {
|
|
25
|
+
"types": "./dist/schema.d.ts",
|
|
26
|
+
"default": "./dist/schema.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist"
|
|
31
|
+
],
|
|
32
|
+
"scripts": {
|
|
33
|
+
"dev": "tsc --watch --preserveWatchOutput",
|
|
34
|
+
"build": "svelte-package",
|
|
35
|
+
"check": "svelte-check --tsconfig ./tsconfig.json",
|
|
36
|
+
"dev:local": "node ../../scripts/swap-package-paths.js ./package.json src lib",
|
|
37
|
+
"prepack": "node ../../scripts/swap-package-paths.js ./package.json dist lib",
|
|
38
|
+
"postpack": "node ../../scripts/swap-package-paths.js ./package.json src lib"
|
|
39
|
+
},
|
|
40
|
+
"keywords": [
|
|
41
|
+
"aphex",
|
|
42
|
+
"aphexcms",
|
|
43
|
+
"cms",
|
|
44
|
+
"plugin",
|
|
45
|
+
"color",
|
|
46
|
+
"color-picker"
|
|
47
|
+
],
|
|
48
|
+
"devDependencies": {
|
|
49
|
+
"@sveltejs/package": "^2.5.6",
|
|
50
|
+
"svelte": "^5.0.0",
|
|
51
|
+
"svelte-check": "^4.0.0",
|
|
52
|
+
"typescript": "^5.9.3"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"@aphexcms/cms-core": "workspace:^",
|
|
56
|
+
"@aphexcms/ui": "workspace:^",
|
|
57
|
+
"@lucide/svelte": "^0.554.0",
|
|
58
|
+
"svelte": "^5.0.0"
|
|
59
|
+
}
|
|
60
|
+
}
|