|
33 | 33 |
|
34 | 34 | const generateForegroundColorFrom = function (input, percentage = 0.8) {
|
35 | 35 | const result = interpolate([input, isDark(input) ? "white" : "black"], "oklch")(percentage)
|
36 |
| - return colorObjToString(result) |
| 36 | + return formatHex('oklch(' + colorObjToString(result) + ')') |
37 | 37 | }
|
38 | 38 |
|
39 | 39 | const generateDarkenColorFrom = function (input, percentage = 0.07) {
|
40 | 40 | const result = interpolate([input, "black"], "oklch")(percentage)
|
41 |
| - return colorObjToString(result) |
| 41 | + return formatHex('oklch(' + colorObjToString(result) + ')') |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | function changeColorValuesToObject(input) {
|
|
88 | 88 | "error",
|
89 | 89 | ]
|
90 | 90 |
|
| 91 | + $: onlyRequiredColorNames = true |
| 92 | +
|
91 | 93 | $: colors = [
|
92 | 94 | {
|
93 | 95 | name: "primary",
|
|
192 | 194 | ]
|
193 | 195 |
|
194 | 196 | function darken(name, variable, source, percentage = 0.2) {
|
195 |
| - return { |
196 |
| - name: name, |
197 |
| - variable: variable, |
198 |
| - value: generateDarkenColorFrom( |
199 |
| - colors.find((item) => item.name === source).value, |
200 |
| - percentage, |
201 |
| - "oklch" |
202 |
| - ), |
203 |
| - } |
| 197 | + return generateDarkenColorFrom( |
| 198 | + colors.find((item) => item.name === source).value, |
| 199 | + percentage, |
| 200 | + ) |
204 | 201 | }
|
205 | 202 | function contrastMaker(name, variable, source, percentage = 0.8) {
|
206 |
| - return { |
207 |
| - name: name, |
208 |
| - variable: variable, |
209 |
| - value: generateForegroundColorFrom( |
210 |
| - colors.find((item) => item.name === source).value, |
211 |
| - percentage, |
212 |
| - "oklch" |
213 |
| - ), |
214 |
| - } |
| 203 | + return generateForegroundColorFrom( |
| 204 | + colors.find((item) => item.name === source).value, |
| 205 | + percentage, |
| 206 | + ) |
215 | 207 | }
|
216 | 208 |
|
217 |
| - function generateOptionalColors(colors) { |
218 |
| - let optionalColors = [] |
219 |
| - optionalColors.push(darken("base-200", "--b2", "base-100", 0.1)) |
220 |
| - optionalColors.push(darken("base-300", "--b3", "base-100", 0.2)) |
221 |
| - optionalColors.push(contrastMaker("base-content", "--bc", "base-100")) |
222 |
| -
|
223 |
| - optionalColors.push(contrastMaker("primary-content", "--pc", "primary")) |
224 |
| - optionalColors.push(contrastMaker("secondary-content", "--sc", "secondary")) |
225 |
| - optionalColors.push(contrastMaker("accent-content", "--ac", "accent")) |
226 |
| - optionalColors.push(contrastMaker("neutral-content", "--nc", "neutral")) |
227 |
| -
|
228 |
| - optionalColors.push(contrastMaker("info-content", "--inc", "info")) |
229 |
| - optionalColors.push(contrastMaker("success-content", "--suc", "success")) |
230 |
| - optionalColors.push(contrastMaker("warning-content", "--wac", "warning")) |
231 |
| - optionalColors.push(contrastMaker("error-content", "--erc", "error")) |
232 |
| - return optionalColors |
| 209 | + function generateOptionalColors() { |
| 210 | + colors[9].value = darken("base-200", "--b2", "base-100", 0.1) |
| 211 | + colors[10].value = darken("base-300", "--b3", "base-100", 0.2) |
| 212 | + colors[11].value = contrastMaker("base-content", "--bc", "base-100") |
| 213 | +
|
| 214 | + colors[1].value = contrastMaker("primary-content", "--pc", "primary") |
| 215 | + colors[3].value = contrastMaker("secondary-content", "--sc", "secondary") |
| 216 | + colors[5].value = contrastMaker("accent-content", "--ac", "accent") |
| 217 | + colors[7].value = contrastMaker("neutral-content", "--nc", "neutral") |
| 218 | +
|
| 219 | + colors[13].value = contrastMaker("info-content", "--inc", "info") |
| 220 | + colors[15].value = contrastMaker("success-content", "--suc", "success") |
| 221 | + colors[17].value = contrastMaker("warning-content", "--wac", "warning") |
| 222 | + colors[19].value = contrastMaker("error-content", "--erc", "error") |
233 | 223 | }
|
234 | 224 |
|
235 | 225 | function generateColors(newColorToCheck = "transparent") {
|
236 | 226 | if (CSS.supports("color", newColorToCheck)) {
|
| 227 | + if (onlyRequiredColorNames) { |
| 228 | + generateOptionalColors() |
| 229 | + } |
237 | 230 | colors
|
238 |
| - .filter((item) => requiredColorNames.includes(item.name)) |
239 | 231 | .forEach((color) => {
|
240 | 232 | wrapper.style.setProperty(
|
241 | 233 | color.variable,
|
242 | 234 | colorObjToString(toGamut("oklch")(color.value), "oklch")
|
243 | 235 | )
|
244 | 236 | })
|
245 |
| - generateOptionalColors(colors).forEach((color) => { |
246 |
| - wrapper.style.setProperty(color.variable, color.value) |
247 |
| - }) |
248 | 237 | if (browser) {
|
249 | 238 | localStorage.setItem("theme-generator-colors", JSON.stringify(colors))
|
250 | 239 | }
|
|
257 | 246 | if (browser && localStorage.getItem("theme-generator-colors")) {
|
258 | 247 | localStorage.removeItem("theme-generator-colors")
|
259 | 248 | colors = JSON.parse(localStorage.getItem("theme-generator-default-colors"))
|
| 249 | + generateOptionalColors() |
260 | 250 | generateColors()
|
261 | 251 | }
|
262 | 252 | }
|
|
300 | 290 | )
|
301 | 291 | //error
|
302 | 292 | // })
|
| 293 | + generateOptionalColors() |
303 | 294 | generateColors()
|
304 | 295 | }
|
305 | 296 |
|
|
347 | 338 | {#if browser}
|
348 | 339 | <div class="mockup-code not-prose relative">
|
349 | 340 | <div class="absolute right-2 top-2">
|
| 341 | + <button class="btn btn-xs" on:click={() => onlyRequiredColorNames = !onlyRequiredColorNames}> |
| 342 | + <Translate text="Show all" /> |
| 343 | + </button> |
| 344 | + {#if !onlyRequiredColorNames} |
| 345 | + <button class="btn btn-xs" on:click={() => {generateOptionalColors(); generateColors()}}> |
| 346 | + <Translate text="Generate Optional" /> |
| 347 | + </button> |
| 348 | + {/if} |
350 | 349 | <button class="btn btn-xs" on:click={() => randomize()}>
|
351 | 350 | <Translate text="Randomize" />
|
352 | 351 | </button>
|
|
359 | 358 | themes: [
|
360 | 359 | {
|
361 | 360 | mytheme: {`}</code>
|
362 |
| -{#each colors.filter((item) => requiredColorNames.includes(item.name)) as color} |
| 361 | +{#each colors.filter((item) => !onlyRequiredColorNames || requiredColorNames.includes(item.name)) as color} |
363 | 362 | <code> <span
|
364 | 363 | data-tip="Pick →"
|
365 | 364 | class:tooltip={colors.filter((item) =>
|
366 |
| - requiredColorNames.includes(item.name) |
367 |
| - )[0] == color} |
| 365 | + !onlyRequiredColorNames || requiredColorNames.includes(item.name) |
| 366 | + )[0] === color} |
368 | 367 | class="tooltip-open tooltip-accent tooltip-left align-middle"><span
|
369 | 368 | class="inline-block w-1" /><ColorPicker
|
370 | 369 | bind:value={color.value}
|
|
382 | 381 | require('daisyui'),
|
383 | 382 | ],
|
384 | 383 | //...
|
385 |
| - } |
| 384 | + } |
386 | 385 | `}</code></pre>
|
387 | 386 | </div>
|
388 | 387 | {/if}
|
|
0 commit comments