Skip to content

Instantly share code, notes, and snippets.

@kistaaa
Created February 27, 2023 20:20
Show Gist options
  • Save kistaaa/2e5914f222c419efc69a1608e78447bd to your computer and use it in GitHub Desktop.
Save kistaaa/2e5914f222c419efc69a1608e78447bd to your computer and use it in GitHub Desktop.
Svelte Random Color Generator
<script>
const hex = (d) => Number(d).toString(16).padStart(2, '0')
const rnd = () => Math.floor(Math.random() * 256)
let color = hex(rnd()) + hex(rnd()) + hex(rnd())
const gen = () => color = hex(rnd()) + hex(rnd()) + hex(rnd())
</script>
<style>
.color{display:inline-block; padding:24px; border-radius:50px; vertical-align: middle;}
.text{display:inline-block; font-size:24px; vertical-align: middle;}
button{display:block; margin-top:24px}
</style>
<h1>Random Color Generator</h1>
<span class="color" style="background:#{color}"></span>
<span class="text" >#{color}</span>
<button on:click={gen}>Random!</button>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment