File size: 16,161 Bytes
99347a0
 
 
b1c4191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99347a0
 
b1c4191
dcdd362
b1c4191
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa18eeb
 
 
 
b1c4191
fa18eeb
 
 
b1c4191
fa18eeb
 
 
 
d4c4975
 
fa18eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8a73ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa18eeb
d94013d
79e2259
7a9fbe6
 
 
 
 
d94013d
 
3798fc3
 
 
 
 
 
 
 
 
 
 
d4c4975
 
 
3798fc3
 
 
d4c4975
3798fc3
 
d4c4975
 
3798fc3
 
d4c4975
 
 
3798fc3
 
 
 
 
 
 
 
d94013d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa18eeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99347a0
d94013d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
function gradioCustomJS() {
	console.log("gradioCustomJS Started")

	// MARK: berechne Helligkeit der Akzentfarbe
	function berechneHelligkeit(rgb) {
		const match = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)
		if (!match) {
			throw new Error("Ungültiges Farbformat")
		}

		const r = parseInt(match[1]) / 255
		const g = parseInt(match[2]) / 255
		const b = parseInt(match[3]) / 255

		const rLin = r <= 0.03928 ? r / 12.92 : Math.pow((r + 0.055) / 1.055, 2.4)
		const gLin = g <= 0.03928 ? g / 12.92 : Math.pow((g + 0.055) / 1.055, 2.4)
		const bLin = b <= 0.03928 ? b / 12.92 : Math.pow((b + 0.055) / 1.055, 2.4)

		const luminanz = 0.2126 * rLin + 0.7152 * gLin + 0.0722 * bLin

		return luminanz
	}

	// MARK: Textfarbe bestimmen
	function anpasseTextfarbe(farbe) {
		const luminanz = berechneHelligkeit(farbe)
		const textFarbe = luminanz > 0.4 ? "var(--neutral-950)" : "var(--neutral-50)"
		console.log("Luminanz: " + luminanz + " Text-Farbe: " + textFarbe)

		return textFarbe
	}

	const body = document.querySelector("body")
	body.className = "dark"

	let usedColor = "rgb(250, 179, 135)"
	// Catppuccin colors
	body.style.setProperty("--cat-rosewater", "rgb(245, 224, 220)")
	body.style.setProperty("--cat-flamingo", "rgb(242, 205, 205)")
	body.style.setProperty("--cat-pink", "rgb(245, 194, 231)")
	body.style.setProperty("--cat-mauve", "rgb(203, 166, 247)")
	body.style.setProperty("--cat-red", "rgb(243, 139, 168)")
	body.style.setProperty("--cat-maroon", "rgb(235, 160, 172)")
	body.style.setProperty("--cat-peach", "rgb(250, 179, 135)")
	body.style.setProperty("--cat-yellow", "rgb(249, 226, 175)")
	body.style.setProperty("--cat-green", "rgb(166, 227, 161)")
	body.style.setProperty("--cat-teal", "rgb(148, 226, 213)")
	body.style.setProperty("--cat-sky", "rgb(137, 220, 235)")
	body.style.setProperty("--cat-sapphire", "rgb(116, 199, 236)")
	body.style.setProperty("--cat-blue", "rgb(137, 180, 250)")

	body.style.setProperty("--primary-600", usedColor)
	body.style.setProperty("--primary-50", "color-mix(in srgb, var(--primary-600) 5%, white)")
	body.style.setProperty("--primary-100", "color-mix(in srgb, var(--primary-600) 10%, white)")
	body.style.setProperty("--primary-200", "color-mix(in srgb, var(--primary-600) 20%, white)")
	body.style.setProperty("--primary-300", "color-mix(in srgb, var(--primary-600) 60%, white)")
	body.style.setProperty("--primary-400", "color-mix(in srgb, var(--primary-600) 70%, white)")
	body.style.setProperty("--primary-500", "color-mix(in srgb, var(--primary-600) 80%, white)")
	body.style.setProperty("--primary-700", "color-mix(in srgb, var(--primary-600) 80%, black)")
	body.style.setProperty("--primary-800", "color-mix(in srgb, var(--primary-600) 65%, black)")
	body.style.setProperty("--primary-900", "color-mix(in srgb, var(--primary-600) 40%, black)")
	body.style.setProperty("--primary-950", "color-mix(in srgb, var(--primary-600) 30%, black)")

	body.style.setProperty("--button-primary-background-fill", "var(--primary-600)")
	body.style.setProperty("--button-primary-background-fill-hover", "var(--primary-500)")
	body.style.setProperty("--blur-value", "0px")
	body.style.setProperty("--text-color-by-luminance", anpasseTextfarbe(usedColor))

	// MARK: Selectors & Elements
	const gradioApp = document.querySelector("gradio-app")
	const gradioContainer = document.querySelector("body > gradio-app > div.gradio-container")
	const dominantColorField = document.querySelector("#dominant_image_color > label > textarea")
	const outputImageElem = document.querySelector("#output_image > div.image-container.svelte-1p15vfy > button > div > img")
	const alertModalElem = document.createElement("div")
	const alertModalElemP = document.createElement("p")
	const alertModalElemI = document.createElement("i")
	alertModalElemI.className = "fas fa-exclamation-circle"
	const alertModalElemSpan = document.createElement("span")
	alertModalElemSpan.id = "alertModalText"
	const alertModalElemButton = document.createElement("button")
	alertModalElemButton.className = "lg primary run-btn svelte-cmf5ev"
	alertModalElemButton.id = "alertModalBtn"
	alertModalElemButton.textContent = "Ok"
	alertModalElemP.id = "alertModalP"
	alertModalElemP.append(alertModalElemI, alertModalElemSpan)
	alertModalElem.id = "alertModal"
	alertModalElem.style.display = "none"
	alertModalElem.append(alertModalElemP, alertModalElemButton)
	//alertModalElem.innerHTML = '<p></p>'
	gradioApp.appendChild(alertModalElem)

	alertModalElemButton.addEventListener("click", () => {
		oldText = alertModalElemSpan.textContent
		alertModalElemButton.disabled = true
		if (alertModalElemButton.textContent == "Noch mal?") {
			alertModalElemSpan.innerHTML = 'Na gut, noch mal. Der "Ok" ... Mooment, der "Noch mal?" Button ändert nur diesen Text. 😉<br/>In 8 Sekunden wird wieder die ursprüngliche Meldung gezeigt. Cool oder?'
			setTimeout(() => {
				alertModalElemSpan.textContent = oldText
				alertModalElemButton.disabled = false
				alertModalElemButton.textContent = "Noch mal?"
			}, 8000)
		} else if (alertModalElemButton.textContent == "Ok") {
			alertModalElemSpan.innerHTML = 'Der "Ok" Button ändert nur diesen Text. 🫢<br/>In 4 Sekunden wird wieder die ursprüngliche Meldung gezeigt. Cool oder?'
			setTimeout(() => {
				alertModalElemSpan.textContent = oldText
				alertModalElemButton.disabled = false
				alertModalElemButton.textContent = "Noch mal?"
			}, 4000)
		}
	})

	const prompt_input = document.querySelector("#prompt_input")
	prompt_input.setAttribute("autocomplete", "off")
	prompt_input.setAttribute("autocorrect", "off")
	prompt_input.setAttribute("autocapitalize", "off")
	prompt_input.setAttribute("spellcheck", "false")

	const switch_width_height = document.querySelector("#switch_width_height")
	const random_prompt_btn = document.querySelector("#random_prompt_btn")

	// MARK: DOM Change Detection
	function onDomChange(callback) {
		const observer = new MutationObserver(callback)
		observer.observe(document.querySelector("#dominant_image_color"), {
			childList: true,
			subtree: true,
		})
	}

	onDomChange(() => {
		console.log("changed: " + dominantColorField.value)
		if (dominantColorField.value.match(/^rgb\((\d{1,3}),(\s\d{1,3}),(\s\d{1,3})\)$/) && outputImageElem.src) {
			outputImageElem.style.opacity = "0"
			document.querySelector("body > gradio-app > div").classList.add("fade-bg")
			document.querySelector("gradio-app").style.opacity = "0"
			setTimeout(() => {
				usedColor = dominantColorField.value
				body.style.setProperty("--primary-600", usedColor)
				body.style.setProperty("--text-color-by-luminance", anpasseTextfarbe(usedColor))
				gradioApp.classList.add("has-bg-image")
				body.style.setProperty("--bg-image-path", `url("${outputImageElem.src}")`)
			}, 400)
			setTimeout(() => {
				gradioApp.style.opacity = "1"
				outputImageElem.style.opacity = "1"
				dominantColorField.value = ""
			}, 800)
			setTimeout(() => {
				//document.querySelector("body > gradio-app > div").classList.remove("fade-bg")
			}, 2000)
		}
	})

	// MARK: SVGs
	random_prompt_btn.innerHTML =
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><g data-name="Ebene_1"><path fill="none" d="M0 48V0h48v48z"/><path d="M42.15 9.85C41.58 9.28 40.9 9 40.12 9H7.88c-.78 0-1.46.28-2.03.85S5 11.1 5 11.88v24.23c0 .78.28 1.46.85 2.03s1.25.85 2.03.85h32.23c.78 0 1.46-.28 2.03-.85s.85-1.25.85-2.03V11.88c0-.78-.28-1.46-.85-2.03ZM13.66 30.78c-.68 0-1.24-.55-1.24-1.24s.55-1.24 1.24-1.24 1.24.55 1.24 1.24-.55 1.24-1.24 1.24Zm2.8-7.78-1.3 1.8c-.32.45-.41.76-.48 1.23-.04.26-.13.48-.28.64-.17.19-.42.29-.73.29s-.56-.12-.73-.31c-.16-.18-.25-.42-.25-.73 0-.68.12-1.09.65-1.8l1.48-2c.33-.45.58-.87.58-1.47 0-1.04-.75-1.83-1.83-1.83-.52 0-.99.15-1.36.52-.29.29-.43.65-.55 1.12-.06.22-.16.47-.27.58-.15.17-.39.29-.7.29-.26 0-.49-.1-.66-.29-.18-.2-.23-.42-.23-.71 0-.67.35-1.58 1.03-2.22.68-.65 1.62-1.03 2.73-1.03 2.11 0 3.79 1.4 3.79 3.57 0 1-.35 1.62-.89 2.36Zm7.28 7.78c-.68 0-1.24-.55-1.24-1.24s.55-1.24 1.24-1.24 1.24.55 1.24 1.24-.55 1.24-1.24 1.24Zm2.8-7.78-1.3 1.8c-.32.45-.41.76-.48 1.23-.04.26-.13.48-.28.64-.17.19-.42.29-.73.29s-.56-.12-.73-.31c-.16-.18-.25-.42-.25-.73 0-.68.12-1.09.65-1.8l1.48-2c.33-.45.58-.87.58-1.47 0-1.04-.75-1.83-1.83-1.83-.52 0-.99.15-1.36.52-.29.29-.43.65-.55 1.12-.06.22-.16.47-.27.58-.15.17-.39.29-.7.29-.26 0-.49-.1-.66-.29-.18-.2-.23-.42-.23-.71 0-.67.35-1.58 1.03-2.22.68-.65 1.62-1.03 2.73-1.03 2.11 0 3.79 1.4 3.79 3.57 0 1-.35 1.62-.89 2.36Zm7.28 7.78c-.68 0-1.24-.55-1.24-1.24s.55-1.24 1.24-1.24 1.24.55 1.24 1.24-.55 1.24-1.24 1.24Zm2.8-7.78-1.3 1.8c-.32.45-.41.76-.48 1.23-.04.26-.13.48-.28.64-.17.19-.42.29-.73.29s-.56-.12-.73-.31c-.16-.18-.25-.42-.25-.73 0-.68.12-1.09.65-1.8l1.48-2c.33-.45.58-.87.58-1.47 0-1.04-.75-1.83-1.83-1.83-.52 0-.99.15-1.36.52-.29.29-.43.65-.55 1.12-.06.22-.16.47-.27.58-.15.17-.39.29-.7.29-.26 0-.49-.1-.66-.29-.18-.2-.23-.42-.23-.71 0-.67.35-1.58 1.03-2.22.68-.65 1.62-1.03 2.73-1.03 2.11 0 3.79 1.4 3.79 3.57 0 1-.35 1.62-.89 2.36Z"/></g></svg>'
	const enhance_prompt_btn = document.querySelector("#enhance_prompt_btn")
	enhance_prompt_btn.innerHTML =
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><g data-name="Ebene_1"><path fill="none" d="M0 48V0h48v48z"/><path d="m33.91 29.59 9.37-8.07h.38c.4.05.72.21.95.49.23.28.35.59.35.91 0 .18-.05.37-.14.55-.09.18-.22.35-.37.5l-7.55 6.48 2.31 10s.04.1.04.17v.14c0 .41-.14.73-.42.98-.28.24-.6.37-.97.37-.12 0-.24-.01-.37-.04s-.25-.07-.37-.13l-.4-.27-2.8-12.09Zm-4.95-15.31-3-7.1.16-.33c.1-.27.26-.47.5-.63.24-.15.49-.23.75-.23.24 0 .49.07.74.22s.43.36.55.65l3.25 7.67-2.95-.26ZM12.1 37.29l8.69-5.23 8.69 5.29-2.3-9.9 7.65-6.64-10.09-.89-3.95-9.31-3.95 9.25-10.09.89 7.65 6.66-2.3 9.88Zm-3.09 1.65 2.44-10.56-8.16-7.13c-.2-.18-.35-.38-.46-.59-.11-.22-.16-.42-.16-.63 0-.41.14-.79.43-1.14.29-.34.67-.53 1.14-.55l10.8-.96 4.18-9.96c.13-.34.34-.59.64-.76.3-.17.6-.25.91-.25s.62.08.93.25c.31.17.53.42.66.76l4.18 9.96 10.8.96c.48.02.86.21 1.15.55.29.34.44.72.44 1.13 0 .22-.06.44-.18.65-.12.21-.27.4-.47.58l-8.16 7.13 2.47 10.56c.03.09.05.21.05.36 0 .48-.17.89-.51 1.22-.34.33-.73.49-1.16.49-.09 0-.4-.09-.93-.27l-9.26-5.59-9.26 5.59c-.14.08-.29.14-.46.19-.16.05-.31.08-.44.08-.5 0-.94-.21-1.31-.62-.37-.41-.48-.9-.32-1.45Z"/></g></svg>'
	const run_btn = document.querySelector("#run_btn")
	run_btn.innerHTML =
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><g data-name="Ebene_1"><path fill="none" d="M0 48V0h48v48z"/><path d="M29.27 43.84V33.13l-6.01-5.57-1.89 8.38c-.1.4-.31.7-.64.92-.33.21-.69.28-1.08.22L9.48 34.99c-.28-.06-.51-.21-.69-.46s-.24-.54-.19-.86c.05-.3.21-.53.48-.68s.54-.2.82-.15l8.99 1.79 3.86-19.69-5.88 2.67v5.52c0 .33-.11.6-.33.82-.22.22-.5.33-.83.33-.33 0-.61-.11-.82-.33-.22-.22-.33-.5-.33-.82V17c0-.3.08-.58.25-.82.17-.24.38-.43.65-.55l7.42-3.07c.94-.41 1.62-.67 2.04-.78.42-.11.83-.17 1.22-.17.6 0 1.14.13 1.61.4.47.27.88.66 1.21 1.17l2.12 3.37c.79 1.27 1.85 2.4 3.16 3.39 1.31.99 2.81 1.62 4.5 1.91.33.06.61.2.83.42.23.22.34.49.34.8s-.11.6-.34.82c-.23.22-.49.31-.81.27-1.84-.22-3.58-.87-5.2-1.96-1.62-1.09-3.12-2.68-4.49-4.79l-2.09 9.07 4.11 3.88c.16.14.28.31.36.49.08.19.13.38.13.57v12.42c0 .33-.11.6-.34.82-.23.22-.5.33-.82.33s-.61-.11-.82-.33c-.22-.22-.33-.5-.33-.82Zm-.06-34.83c-.97 0-1.8-.34-2.48-1.02-.68-.68-1.03-1.51-1.03-2.48s.34-1.8 1.02-2.48C27.4 2.35 28.23 2 29.2 2s1.8.34 2.48 1.02c.68.68 1.03 1.51 1.03 2.48s-.34 1.8-1.02 2.48c-.68.68-1.51 1.03-2.48 1.03Z"/></g></svg>'

	switch_width_height.innerHTML =
		'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><g data-name="Ebene_1"><path fill="none" d="M0 48V0h48v48z"/><path d="M33.75 22.5V6.01c0-1.84-1.5-3.34-3.34-3.34H5.01c-1.84 0-3.34 1.5-3.34 3.34V22.5c0 1.84 1.5 3.34 3.34 3.34h25.4c1.84 0 3.34-1.5 3.34-3.34Zm-29.41 0V6.01c0-.37.3-.67.67-.67h25.4c.37 0 .67.3.67.67V22.5c0 .37-.3.67-.67.67H5.01a.67.67 0 0 1-.67-.67Z"/><path d="M39.65 15.1c-.41-.72-1.33-.97-2.05-.56s-.97 1.33-.56 2.05C38.32 18.84 39 21.4 39 24c0 8.27-6.73 15-15 15-5.2 0-9.91-2.61-12.68-7H16c.83 0 1.5-.67 1.5-1.5S16.83 29 16 29H7.5c-.83 0-1.5.67-1.5 1.5V39c0 .83.67 1.5 1.5 1.5S9 39.83 9 39v-5.06C12.35 39 17.9 42 24 42c9.93 0 18-8.07 18-18 0-3.12-.81-6.2-2.35-8.9Z"/></g></svg>'

	switch_width_height.addEventListener("click", () => {
		switch_width_height.querySelector("svg").classList.toggle("portrait")
		switch_width_height.querySelector("svg").classList.toggle("landscape")
		// imageRatioBtns.forEach((_) => {
		// 	const ratio = _.querySelector("input").value
		// 	const [a, b] = ratio.split(":")
		// 	_.dataset.testid = `${b}:${a}-radio-label`
		// 	_.querySelector("input").value = `${b}:${a}`
		// 	_.querySelector("span").textContent = `${b}:${a}`
		// })

		// const imageRatioBtnsLabel = document.querySelector("#image_ratio_buttons > span")
		// imageRatioBtnsLabel.textContent = imageRatioBtnsLabel.textContent === "Querformat" ? "Hochformat" : "Querformat"
		// const new_hight = width_selector.value
		// const new_width = hight_selector.value
		// width_selector.value = new_width
		// hight_selector.value = new_hight
	})

	//document.querySelector("#image_ratio_buttons > div.wrap.svelte-1kzox3m > label")
	// imageRatioBtns.forEach((_) => {
	// 	_.querySelector("input").addEventListener("click", (e) => {
	// 		let set_width
	// 		let set_height
	// 		const clicked_on = e.target
	// 		console.log("Clicked Btn:", clicked_on.value)

	// 		const ratio = e.target.value
	// 		const [a, b] = ratio.split(":")

	// 		if (parseInt(a) > parseInt(b)) {
	// 			console.log("a: " + a + " > " + "b: " + b)
	// 			set_width = 1024
	// 			set_height = Math.round(set_width * (b / a))
	// 		} else if (parseInt(a) < parseInt(b)) {
	// 			console.log("a: " + a + " < " + "b: " + b)
	// 			set_height = 1024
	// 			set_width = Math.round(set_height * (a / b))
	// 		} else if (parseInt(a) == parseInt(b)) {
	// 			set_height = 1024
	// 			set_width = 1024
	// 		}

	// 		width_selector.value = set_width
	// 		hight_selector.value = set_height
	// 	})
	// })

	// MARK: Element-Ready function
	function elementReady(selector) {
		return new Promise((resolve, reject) => {
			const el = document.querySelector(selector)
			if (el) {
				resolve(el)
			}

			new MutationObserver((mutationRecords, observer) => {
				Array.from(document.querySelectorAll(selector)).forEach((element) => {
					resolve(element)
					observer.disconnect()
				})
			}).observe(document.documentElement, {
				childList: true,
				subtree: true,
			})
		})
	}

	// MARK: Mobile Check
	function istMobile() {
		// Überprüfen, ob das Gerät ein Touchscreen hat
		if ("ontouchstart" in window || (navigator.maxTouchPoints && window.innerWidth < 768)) {
			gradioContainer.classList.add("blur-container")
			body.style.setProperty("--blur-value", "12px")
			gradioApp.style.height = "calc(100vh - 120px)"
			alertModalElem.style.display = ""
			alertModalElemSpan.textContent = "Diese Seite ist nicht für mobile Geräte optimiert. Bitte besuche diese Seite von einem Desktop-Computer aus."
		} else if (window.innerWidth < 1024) {
			gradioContainer.classList.add("blur-container")
			body.style.setProperty("--blur-value", "12px")
			gradioApp.style.height = "calc(100vh - 120px)"
			alertModalElem.style.display = ""
			alertModalElemSpan.textContent = "Bildschirm Auflösung oder Fensterbreite zu gering. Bitte besuche diese Seite von einem Desktop-Computer aus."
		} else {
			gradioContainer.classList.remove("blur-container")
			body.style.setProperty("--blur-value", "0px")
			gradioApp.style.height = ""
			alertModalElem.style.display = "none"
			alertModalElemSpan.textContent = ""
		}
	}

	// MARK: Event Listeners
	window.addEventListener("resize", () => {
		console.log("Event Window resize.")
		istMobile()
	})

	elementReady("body > gradio-app > div.gradio-container").then((element) => {
		console.log("Element exist: " + element)
		istMobile()
	})

	return "Custom Gradio JS"
}