gradio-pr-bot commited on
Commit
55e734e
·
verified ·
1 Parent(s): 2548498

Upload folder using huggingface_hub

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