gradio-pr-bot commited on
Commit
9da93dc
·
verified ·
1 Parent(s): eb51b24

Upload folder using huggingface_hub

Browse files
6.9.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.9.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.9.0/colorpicker/package.json ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/colorpicker",
3
+ "version": "0.5.7",
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.9.0/colorpicker/shared/Colorpicker.svelte ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ onfocus={on_focus}
168
+ onblur={on_blur}
169
+ onclick={() => {
170
+ update_mouse_from_color(value);
171
+ dialog_open = !dialog_open;
172
+ }}
173
+ />
174
+
175
+ <svelte:window onmousemove={handle_move} onmouseup={handle_end} />
176
+
177
+ {#if dialog_open}
178
+ <div class="color-picker" use:click_outside={handle_click_outside}>
179
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
180
+ <div
181
+ class="color-gradient"
182
+ onmousedown={handle_sl_down}
183
+ style="--hue:{hue}"
184
+ bind:this={sl_wrap}
185
+ >
186
+ <div
187
+ class="marker"
188
+ style:transform="translate({sl_marker_pos[0]}px,{sl_marker_pos[1]}px)"
189
+ style:background={value}
190
+ />
191
+ </div>
192
+ <!-- svelte-ignore a11y-no-static-element-interactions -->
193
+ <div class="hue-slider" onmousedown={handle_hue_down} bind:this={hue_wrap}>
194
+ <div
195
+ class="marker"
196
+ style:background={"hsl(" + hue + ", 100%, 50%)"}
197
+ style:transform="translateX({hue_marker_pos}px)"
198
+ />
199
+ </div>
200
+
201
+ <div class="input">
202
+ <button class="swatch" style:background={value}></button>
203
+ <div>
204
+ <div class="input-wrap">
205
+ <input
206
+ type="text"
207
+ bind:value={color_string}
208
+ onchange={(e) => {
209
+ value = e.currentTarget.value;
210
+ }}
211
+ onkeydown={(e) => {
212
+ if (e.key === "Enter") {
213
+ on_submit();
214
+ }
215
+ }}
216
+ />
217
+ <button class="eyedropper" onclick={request_eyedropper}>
218
+ {#if eyedropper_supported}
219
+ <Eyedropper />
220
+ {/if}
221
+ </button>
222
+ </div>
223
+
224
+ <div class="buttons">
225
+ {#each modes as [label, value]}
226
+ <button
227
+ class="button"
228
+ class:active={current_mode === value}
229
+ onclick={() => {
230
+ current_mode = value;
231
+ }}>{label}</button
232
+ >
233
+ {/each}
234
+ </div>
235
+ </div>
236
+ </div>
237
+ </div>
238
+ {/if}
239
+
240
+ <style>
241
+ .dialog-button {
242
+ display: block;
243
+ width: var(--size-10);
244
+ height: var(--size-5);
245
+ border: var(--block-border-width) solid var(--block-border-color);
246
+ }
247
+
248
+ .dialog-button:disabled {
249
+ cursor: not-allowed;
250
+ }
251
+
252
+ .input {
253
+ display: flex;
254
+ align-items: center;
255
+ padding: 0 10px 15px;
256
+ }
257
+
258
+ .input input {
259
+ height: 30px;
260
+ width: 100%;
261
+ flex-shrink: 1;
262
+ border-bottom-left-radius: 0;
263
+ border: 1px solid var(--block-border-color);
264
+ letter-spacing: -0.05rem;
265
+ border-left: none;
266
+ border-right: none;
267
+ font-family: var(--font-mono);
268
+ font-size: var(--scale-000);
269
+ padding-left: 15px;
270
+ padding-right: 0;
271
+ background-color: var(--background-fill-secondary);
272
+ color: var(--block-label-text-color);
273
+ }
274
+
275
+ .swatch {
276
+ width: 50px;
277
+ height: 50px;
278
+ border-top-left-radius: 15px;
279
+ border-bottom-left-radius: 15px;
280
+ flex-shrink: 0;
281
+ border: 1px solid var(--block-border-color);
282
+ }
283
+
284
+ .color-picker {
285
+ width: 230px;
286
+ background: var(--background-fill-secondary);
287
+ border: 1px solid var(--block-border-color);
288
+ border-radius: var(--block-radius);
289
+ margin: var(--spacing-sm) 0;
290
+ }
291
+
292
+ .buttons {
293
+ height: 20px;
294
+ display: flex;
295
+ justify-content: stretch;
296
+ gap: 0px;
297
+ }
298
+
299
+ .buttons button {
300
+ display: flex;
301
+ align-items: center;
302
+ justify-content: center;
303
+ border: 1px solid var(--block-border-color);
304
+ background: var(--background-fill-secondary);
305
+ padding: 3px 6px;
306
+ font-size: var(--scale-000);
307
+ cursor: pointer;
308
+ border-right: none;
309
+ width: 100%;
310
+ border-top: none;
311
+ }
312
+
313
+ .buttons button:first-child {
314
+ border-left: none;
315
+ }
316
+
317
+ .buttons button:last-child {
318
+ border-bottom-right-radius: 15px;
319
+ border-right: 1px solid var(--block-border-color);
320
+ }
321
+
322
+ .buttons button:hover {
323
+ background: var(--background-fill-secondary-hover);
324
+ font-weight: var(--weight-bold);
325
+ }
326
+
327
+ .buttons button.active {
328
+ background: var(--background-fill-secondary);
329
+ font-weight: var(--weight-bold);
330
+ }
331
+
332
+ .input-wrap {
333
+ display: flex;
334
+ }
335
+
336
+ .color-gradient {
337
+ position: relative;
338
+ --hue: white;
339
+ background:
340
+ linear-gradient(rgba(0, 0, 0, 0), #000),
341
+ linear-gradient(90deg, #fff, hsl(var(--hue), 100%, 50%));
342
+ width: 100%;
343
+ height: 150px;
344
+ border-radius: var(--radius-sm) var(--radius-sm) 0 0;
345
+ }
346
+
347
+ .hue-slider {
348
+ position: relative;
349
+ width: 90%;
350
+ margin: 10px auto;
351
+ height: 10px;
352
+ border-radius: 5px;
353
+ background: linear-gradient(
354
+ to right,
355
+ hsl(0, 100%, 50%) 0%,
356
+ #ff0 17%,
357
+ lime 33%,
358
+ cyan 50%,
359
+ blue 67%,
360
+ magenta 83%,
361
+ red 100%
362
+ );
363
+ }
364
+
365
+ .swatch {
366
+ width: 50px;
367
+ height: 50px;
368
+ border-top-left-radius: 15px;
369
+ border-bottom-left-radius: 15px;
370
+ flex-shrink: 0;
371
+ border: 1px solid var(--block-border-color);
372
+ }
373
+
374
+ .eyedropper {
375
+ display: flex;
376
+ align-items: center;
377
+ justify-content: center;
378
+ width: 25px;
379
+ height: 30px;
380
+ border-top-right-radius: 15px;
381
+ border: 1px solid var(--block-border-color);
382
+ border-left: none;
383
+ background: var(--background-fill-secondary);
384
+ height: 30px;
385
+ padding: 7px 7px 5px 0px;
386
+ cursor: pointer;
387
+ }
388
+
389
+ .marker {
390
+ position: absolute;
391
+ width: 14px;
392
+ height: 14px;
393
+ border-radius: 50%;
394
+ border: 2px solid white;
395
+ top: -2px;
396
+ left: -7px;
397
+ box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
398
+ pointer-events: none;
399
+ }
400
+
401
+ input {
402
+ width: 100%;
403
+ height: 30px;
404
+ border: 1px solid var(--block-border-color);
405
+ border-radius: var(--radius-sm);
406
+ padding: 0 var(--size-2);
407
+ font-family: var(--font-mono);
408
+ font-size: var(--scale-000);
409
+ color: var(--block-label-text-color);
410
+ background-color: var(--background-fill-primary);
411
+ }
412
+ </style>
6.9.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.9.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.9.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
+ }