gradio-pr-bot commited on
Commit
7526cc5
·
verified ·
1 Parent(s): 6d13704

Upload folder using huggingface_hub

Browse files
6.5.0/colorpicker/Example.svelte ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: string | null;
3
+ export let type: "gallery" | "table";
4
+ export let selected = false;
5
+ </script>
6
+
7
+ <div
8
+ style="background-color: {value ? value : 'black'}"
9
+ class:table={type === "table"}
10
+ class:gallery={type === "gallery"}
11
+ class:selected
12
+ />
13
+
14
+ <style>
15
+ div {
16
+ width: var(--size-10);
17
+ height: var(--size-10);
18
+ }
19
+ .table {
20
+ margin: 0 auto;
21
+ }
22
+ </style>
6.5.0/colorpicker/Index.svelte ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script context="module" lang="ts">
4
+ export { default as BaseColorPicker } from "./shared/Colorpicker.svelte";
5
+ export { default as BaseExample } from "./Example.svelte";
6
+ </script>
7
+
8
+ <script lang="ts">
9
+ import { Gradio } from "@gradio/utils";
10
+ import Colorpicker from "./shared/Colorpicker.svelte";
11
+ import { Block } from "@gradio/atoms";
12
+ import { StatusTracker } from "@gradio/statustracker";
13
+ import type { ColorPickerProps, ColorPickerEvents } from "./types";
14
+
15
+ let props = $props();
16
+ const gradio = new Gradio<ColorPickerEvents, ColorPickerProps>(props, {
17
+ value: "#000000"
18
+ });
19
+ let old_value = $state(gradio.props.value);
20
+ let label = $derived(
21
+ gradio.shared.label || gradio.i18n("color_picker.color_picker")
22
+ );
23
+
24
+ $effect(() => {
25
+ if (old_value !== gradio.props.value) {
26
+ old_value = gradio.props.value;
27
+ gradio.dispatch("change");
28
+ }
29
+ });
30
+ </script>
31
+
32
+ <Block
33
+ visible={gradio.shared.visible}
34
+ elem_id={gradio.shared.elem_id}
35
+ elem_classes={gradio.shared.elem_classes}
36
+ container={gradio.shared.container}
37
+ scale={gradio.shared.scale}
38
+ min_width={gradio.shared.min_width}
39
+ >
40
+ <StatusTracker
41
+ autoscroll={gradio.shared.autoscroll}
42
+ i18n={gradio.i18n}
43
+ {...gradio.shared.loading_status}
44
+ on_clear_status={() =>
45
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
46
+ />
47
+
48
+ <Colorpicker
49
+ bind:value={gradio.props.value}
50
+ {label}
51
+ info={gradio.props.info}
52
+ show_label={gradio.shared.show_label}
53
+ disabled={!gradio.shared.interactive}
54
+ on_input={() => gradio.dispatch("input")}
55
+ on_submit={() => gradio.dispatch("submit")}
56
+ on_blur={() => gradio.dispatch("blur")}
57
+ on_focus={() => gradio.dispatch("focus")}
58
+ />
59
+ </Block>
6.5.0/colorpicker/package.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/colorpicker",
3
+ "version": "0.5.5",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "main_changeset": true,
10
+ "main": "./Index.svelte",
11
+ "exports": {
12
+ ".": {
13
+ "gradio": "./Index.svelte",
14
+ "svelte": "./dist/Index.svelte",
15
+ "types": "./dist/Index.svelte.d.ts"
16
+ },
17
+ "./example": {
18
+ "gradio": "./Example.svelte",
19
+ "svelte": "./dist/Example.svelte",
20
+ "types": "./dist/Example.svelte.d.ts"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "dependencies": {
25
+ "@gradio/atoms": "workspace:^",
26
+ "@gradio/statustracker": "workspace:^",
27
+ "@gradio/utils": "workspace:^",
28
+ "@gradio/icons": "workspace:^",
29
+ "tinycolor2": "^1.6.0",
30
+ "@types/tinycolor2": "^1.4.6"
31
+ },
32
+ "devDependencies": {
33
+ "@gradio/preview": "workspace:^"
34
+ },
35
+ "peerDependencies": {
36
+ "svelte": "^5.48.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/gradio-app/gradio.git",
41
+ "directory": "js/colorpicker"
42
+ }
43
+ }
6.5.0/colorpicker/shared/Colorpicker.svelte ADDED
@@ -0,0 +1,410 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, tick } from "svelte";
3
+ import tinycolor from "tinycolor2";
4
+ import { BlockTitle } from "@gradio/atoms";
5
+ import { click_outside } from "./events";
6
+ import { Eyedropper } from "@gradio/icons";
7
+ import { hsva_to_rgba, format_color } from "./utils";
8
+
9
+ let {
10
+ value = $bindable(),
11
+ label,
12
+ info,
13
+ disabled,
14
+ show_label,
15
+ on_input = () => {},
16
+ on_submit = () => {},
17
+ on_blur = () => {},
18
+ on_focus = () => {}
19
+ }: {
20
+ value: string;
21
+ label: string;
22
+ info?: string;
23
+ disabled: boolean;
24
+ show_label: boolean;
25
+ on_input?: () => void;
26
+ on_submit?: () => void;
27
+ on_blur?: () => void;
28
+ on_focus?: () => void;
29
+ } = $props();
30
+
31
+ let dialog_open = $state(false);
32
+ let current_mode: "hex" | "rgb" | "hsl" = $state("hex");
33
+
34
+ let eyedropper_supported = false;
35
+
36
+ let sl_wrap: HTMLDivElement;
37
+ let hue_wrap: HTMLDivElement;
38
+
39
+ let sl_marker_pos = [0, 0];
40
+ let sl_rect: DOMRect | null = null;
41
+ let sl_moving = false;
42
+ let sl = [0, 0];
43
+
44
+ let hue = 0;
45
+ let hue_marker_pos = 0;
46
+ let hue_rect: DOMRect | null = null;
47
+ let hue_moving = false;
48
+
49
+ function handle_hue_down(
50
+ event: MouseEvent & { currentTarget: HTMLDivElement }
51
+ ): void {
52
+ hue_rect = event.currentTarget.getBoundingClientRect();
53
+ hue_moving = true;
54
+ update_hue_from_mouse(event.clientX);
55
+ }
56
+
57
+ function update_hue_from_mouse(x: number): void {
58
+ if (!hue_rect) return;
59
+ const _x = Math.max(0, Math.min(x - hue_rect.left, hue_rect.width)); // Get the x-coordinate relative to the box
60
+ hue_marker_pos = _x;
61
+ const _hue = (_x / hue_rect.width) * 360; // Scale the x position to a hue value (0-360)
62
+
63
+ hue = _hue;
64
+
65
+ value = hsva_to_rgba({ h: _hue, s: sl[0], v: sl[1], a: 1 });
66
+ on_input();
67
+ }
68
+
69
+ function update_color_from_mouse(x: number, y: number): void {
70
+ if (!sl_rect) return;
71
+ const _x = Math.max(0, Math.min(x - sl_rect.left, sl_rect.width));
72
+ const _y = Math.max(0, Math.min(y - sl_rect.top, sl_rect.height));
73
+ sl_marker_pos = [_x, _y];
74
+ const _hsva = {
75
+ h: hue * 1,
76
+ s: _x / sl_rect.width,
77
+ v: 1 - _y / sl_rect.height,
78
+ a: 1
79
+ };
80
+
81
+ sl = [_hsva.s, _hsva.v];
82
+
83
+ value = hsva_to_rgba(_hsva);
84
+ on_input();
85
+ }
86
+
87
+ function handle_sl_down(
88
+ event: MouseEvent & { currentTarget: HTMLDivElement }
89
+ ): void {
90
+ sl_moving = true;
91
+ sl_rect = event.currentTarget.getBoundingClientRect();
92
+ update_color_from_mouse(event.clientX, event.clientY);
93
+ }
94
+
95
+ function handle_move(event: MouseEvent): void {
96
+ if (sl_moving) update_color_from_mouse(event.clientX, event.clientY);
97
+ if (hue_moving) update_hue_from_mouse(event.clientX);
98
+ }
99
+
100
+ function handle_end(): void {
101
+ sl_moving = false;
102
+ hue_moving = false;
103
+ }
104
+
105
+ async function update_mouse_from_color(color: string): Promise<void> {
106
+ if (sl_moving || hue_moving) return;
107
+ await tick();
108
+ if (!color) return;
109
+
110
+ if (!sl_rect && sl_wrap) {
111
+ sl_rect = sl_wrap.getBoundingClientRect();
112
+ }
113
+
114
+ if (!hue_rect && hue_wrap) {
115
+ hue_rect = hue_wrap.getBoundingClientRect();
116
+ }
117
+
118
+ // Exit if we still don't have valid rectangles
119
+ if (!sl_rect || !hue_rect) return;
120
+
121
+ const hsva = tinycolor(color).toHsv();
122
+ const _x = hsva.s * sl_rect.width;
123
+ const _y = (1 - hsva.v) * sl_rect.height;
124
+ sl_marker_pos = [_x, _y];
125
+ sl = [hsva.s, hsva.v];
126
+ hue = hsva.h;
127
+ hue_marker_pos = (hsva.h / 360) * hue_rect.width;
128
+ }
129
+
130
+ function request_eyedropper(): void {
131
+ // @ts-ignore
132
+ const eyeDropper = new EyeDropper();
133
+
134
+ eyeDropper.open().then((result: { sRGBHex: string }) => {
135
+ value = result.sRGBHex;
136
+ });
137
+ on_input();
138
+ }
139
+
140
+ const modes = [
141
+ ["Hex", "hex"],
142
+ ["RGB", "rgb"],
143
+ ["HSL", "hsl"]
144
+ ] as const;
145
+
146
+ let color_string = $derived.by(() => format_color(value, current_mode));
147
+
148
+ onMount(async () => {
149
+ // @ts-ignore
150
+ eyedropper_supported = window !== undefined && !!window.EyeDropper;
151
+ });
152
+
153
+ function handle_click_outside(): void {
154
+ dialog_open = false;
155
+ }
156
+
157
+ $effect(() => {
158
+ update_mouse_from_color(value);
159
+ });
160
+ </script>
161
+
162
+ <BlockTitle {show_label} {info}>{label}</BlockTitle>
163
+ <button
164
+ class="dialog-button"
165
+ style:background={value}
166
+ {disabled}
167
+ on:click={() => {
168
+ update_mouse_from_color(value);
169
+ dialog_open = !dialog_open;
170
+ }}
171
+ />
172
+
173
+ <svelte:window on:mousemove={handle_move} on:mouseup={handle_end} />
174
+
175
+ {#if dialog_open}
176
+ <div
177
+ class="color-picker"
178
+ on:focus
179
+ on:blur
180
+ use:click_outside={handle_click_outside}
181
+ >
182
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
183
+ <div
184
+ class="color-gradient"
185
+ on:mousedown={handle_sl_down}
186
+ style="--hue:{hue}"
187
+ bind:this={sl_wrap}
188
+ >
189
+ <div
190
+ class="marker"
191
+ style:transform="translate({sl_marker_pos[0]}px,{sl_marker_pos[1]}px)"
192
+ style:background={value}
193
+ />
194
+ </div>
195
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
196
+ <div class="hue-slider" on:mousedown={handle_hue_down} bind:this={hue_wrap}>
197
+ <div
198
+ class="marker"
199
+ style:background={"hsl(" + hue + ", 100%, 50%)"}
200
+ style:transform="translateX({hue_marker_pos}px)"
201
+ />
202
+ </div>
203
+
204
+ <div class="input">
205
+ <button class="swatch" style:background={value}></button>
206
+ <div>
207
+ <div class="input-wrap">
208
+ <input
209
+ type="text"
210
+ bind:value={color_string}
211
+ on:change={(e) => {
212
+ value = e.currentTarget.value;
213
+ }}
214
+ />
215
+ <button class="eyedropper" on:click={request_eyedropper}>
216
+ {#if eyedropper_supported}
217
+ <Eyedropper />
218
+ {/if}
219
+ </button>
220
+ </div>
221
+
222
+ <div class="buttons">
223
+ {#each modes as [label, value]}
224
+ <button
225
+ class="button"
226
+ class:active={current_mode === value}
227
+ on:click={() => {
228
+ current_mode = value;
229
+ }}>{label}</button
230
+ >
231
+ {/each}
232
+ </div>
233
+ </div>
234
+ </div>
235
+ </div>
236
+ {/if}
237
+
238
+ <style>
239
+ .dialog-button {
240
+ display: block;
241
+ width: var(--size-10);
242
+ height: var(--size-5);
243
+ border: var(--block-border-width) solid var(--block-border-color);
244
+ }
245
+
246
+ .dialog-button:disabled {
247
+ cursor: not-allowed;
248
+ }
249
+
250
+ .input {
251
+ display: flex;
252
+ align-items: center;
253
+ padding: 0 10px 15px;
254
+ }
255
+
256
+ .input input {
257
+ height: 30px;
258
+ width: 100%;
259
+ flex-shrink: 1;
260
+ border-bottom-left-radius: 0;
261
+ border: 1px solid var(--block-border-color);
262
+ letter-spacing: -0.05rem;
263
+ border-left: none;
264
+ border-right: none;
265
+ font-family: var(--font-mono);
266
+ font-size: var(--scale-000);
267
+ padding-left: 15px;
268
+ padding-right: 0;
269
+ background-color: var(--background-fill-secondary);
270
+ color: var(--block-label-text-color);
271
+ }
272
+
273
+ .swatch {
274
+ width: 50px;
275
+ height: 50px;
276
+ border-top-left-radius: 15px;
277
+ border-bottom-left-radius: 15px;
278
+ flex-shrink: 0;
279
+ border: 1px solid var(--block-border-color);
280
+ }
281
+
282
+ .color-picker {
283
+ width: 230px;
284
+ background: var(--background-fill-secondary);
285
+ border: 1px solid var(--block-border-color);
286
+ border-radius: var(--block-radius);
287
+ margin: var(--spacing-sm) 0;
288
+ }
289
+
290
+ .buttons {
291
+ height: 20px;
292
+ display: flex;
293
+ justify-content: stretch;
294
+ gap: 0px;
295
+ }
296
+
297
+ .buttons button {
298
+ display: flex;
299
+ align-items: center;
300
+ justify-content: center;
301
+ border: 1px solid var(--block-border-color);
302
+ background: var(--background-fill-secondary);
303
+ padding: 3px 6px;
304
+ font-size: var(--scale-000);
305
+ cursor: pointer;
306
+ border-right: none;
307
+ width: 100%;
308
+ border-top: none;
309
+ }
310
+
311
+ .buttons button:first-child {
312
+ border-left: none;
313
+ }
314
+
315
+ .buttons button:last-child {
316
+ border-bottom-right-radius: 15px;
317
+ border-right: 1px solid var(--block-border-color);
318
+ }
319
+
320
+ .buttons button:hover {
321
+ background: var(--background-fill-secondary-hover);
322
+ font-weight: var(--weight-bold);
323
+ }
324
+
325
+ .buttons button.active {
326
+ background: var(--background-fill-secondary);
327
+ font-weight: var(--weight-bold);
328
+ }
329
+
330
+ .input-wrap {
331
+ display: flex;
332
+ }
333
+
334
+ .color-gradient {
335
+ position: relative;
336
+ --hue: white;
337
+ background:
338
+ linear-gradient(rgba(0, 0, 0, 0), #000),
339
+ linear-gradient(90deg, #fff, hsl(var(--hue), 100%, 50%));
340
+ width: 100%;
341
+ height: 150px;
342
+ border-radius: var(--radius-sm) var(--radius-sm) 0 0;
343
+ }
344
+
345
+ .hue-slider {
346
+ position: relative;
347
+ width: 90%;
348
+ margin: 10px auto;
349
+ height: 10px;
350
+ border-radius: 5px;
351
+ background: linear-gradient(
352
+ to right,
353
+ hsl(0, 100%, 50%) 0%,
354
+ #ff0 17%,
355
+ lime 33%,
356
+ cyan 50%,
357
+ blue 67%,
358
+ magenta 83%,
359
+ red 100%
360
+ );
361
+ }
362
+
363
+ .swatch {
364
+ width: 50px;
365
+ height: 50px;
366
+ border-top-left-radius: 15px;
367
+ border-bottom-left-radius: 15px;
368
+ flex-shrink: 0;
369
+ border: 1px solid var(--block-border-color);
370
+ }
371
+
372
+ .eyedropper {
373
+ display: flex;
374
+ align-items: center;
375
+ justify-content: center;
376
+ width: 25px;
377
+ height: 30px;
378
+ border-top-right-radius: 15px;
379
+ border: 1px solid var(--block-border-color);
380
+ border-left: none;
381
+ background: var(--background-fill-secondary);
382
+ height: 30px;
383
+ padding: 7px 7px 5px 0px;
384
+ cursor: pointer;
385
+ }
386
+
387
+ .marker {
388
+ position: absolute;
389
+ width: 14px;
390
+ height: 14px;
391
+ border-radius: 50%;
392
+ border: 2px solid white;
393
+ top: -2px;
394
+ left: -7px;
395
+ box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
396
+ pointer-events: none;
397
+ }
398
+
399
+ input {
400
+ width: 100%;
401
+ height: 30px;
402
+ border: 1px solid var(--block-border-color);
403
+ border-radius: var(--radius-sm);
404
+ padding: 0 var(--size-2);
405
+ font-family: var(--font-mono);
406
+ font-size: var(--scale-000);
407
+ color: var(--block-label-text-color);
408
+ background-color: var(--background-fill-primary);
409
+ }
410
+ </style>
6.5.0/colorpicker/shared/events.ts ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * Svelte action to handle clicks outside of a DOM node
3
+ * @param node DOM node to check the click is outside of
4
+ * @param callback callback function to call if click is outside
5
+ * @returns svelte action return object with destroy method to remove event listener
6
+ */
7
+ export function click_outside(
8
+ node: Node,
9
+ callback: (arg: MouseEvent) => void
10
+ ): any {
11
+ const handle_click = (event: MouseEvent): void => {
12
+ if (
13
+ node &&
14
+ !node.contains(event.target as Node) &&
15
+ !event.defaultPrevented
16
+ ) {
17
+ callback(event);
18
+ }
19
+ };
20
+
21
+ document.addEventListener("mousedown", handle_click, true);
22
+
23
+ return {
24
+ destroy() {
25
+ document.removeEventListener("mousedown", handle_click, true);
26
+ }
27
+ };
28
+ }
6.5.0/colorpicker/shared/utils.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tinycolor from "tinycolor2";
2
+
3
+ export function hsva_to_rgba(hsva: {
4
+ h: number;
5
+ s: number;
6
+ v: number;
7
+ a: number;
8
+ }): string {
9
+ const saturation = hsva.s;
10
+ const value = hsva.v;
11
+ let chroma = saturation * value;
12
+ const hue_by_60 = hsva.h / 60;
13
+ let x = chroma * (1 - Math.abs((hue_by_60 % 2) - 1));
14
+ const m = value - chroma;
15
+
16
+ chroma = chroma + m;
17
+ x = x + m;
18
+
19
+ const index = Math.floor(hue_by_60) % 6;
20
+ const red = [chroma, x, m, m, x, chroma][index];
21
+ const green = [x, chroma, chroma, x, m, m][index];
22
+ const blue = [m, m, x, chroma, chroma, x][index];
23
+
24
+ return `rgba(${red * 255}, ${green * 255}, ${blue * 255}, ${hsva.a})`;
25
+ }
26
+
27
+ export function format_color(
28
+ color: string,
29
+ mode: "hex" | "rgb" | "hsl"
30
+ ): string {
31
+ if (mode === "hex") {
32
+ return tinycolor(color).toHexString();
33
+ } else if (mode === "rgb") {
34
+ return tinycolor(color).toRgbString();
35
+ }
36
+ return tinycolor(color).toHslString();
37
+ }
6.5.0/colorpicker/types.ts ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export interface ColorPickerProps {
2
+ value: string;
3
+ info: string;
4
+ }
5
+
6
+ export interface ColorPickerEvents {
7
+ change: never;
8
+ input: never;
9
+ submit: never;
10
+ focus: never;
11
+ blur: never;
12
+ clear_status: any;
13
+ }