Skip to content

Instantly share code, notes, and snippets.

@Namaskar-1F64F
Last active August 8, 2023 16:04
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Namaskar-1F64F/c851789c4df7d528c6c08ec60a9c90f8 to your computer and use it in GitHub Desktop.
Gradient generator
type GradientGenerator = {
generate: (seed: string) => string
}
const colors = ['c051e9', '578ce6', '8cacf5', 'd8fb9f']
// Simple PRNG using Mulberry32 algorithm
function mulberry32(seed: number) {
return function () {
let temp = (seed += 0x6d2b79f5)
temp = Math.imul(temp ^ (temp >>> 15), temp | 1)
temp ^= temp + Math.imul(temp ^ (temp >>> 7), temp | 61)
return ((temp ^ (temp >>> 14)) >>> 0) / 4294967296
}
}
const GradientGenerator: GradientGenerator = {
generate(seed: string) {
// Convert seed string to seed number
let seedNumber = 0
for (let i = 0; i < seed.length; i++) {
seedNumber += seed.charCodeAt(i)
}
const rng = mulberry32(seedNumber)
const gradients = []
const numGradients = Math.floor(rng() * 9) + 2
for (let i = 0; i < numGradients; i++) {
const x = Math.floor(rng() * 201) - 50
const y = Math.floor(rng() * 201) - 50
const start = Math.floor(rng() * 101)
const end = start + Math.floor(rng() * (101 - start))
const color = colors[Math.floor(rng() * colors.length)]
gradients.push(
`radial-gradient(at ${x}% ${y}%, #${color} ${start}%, transparent ${end}%)`
)
}
return gradients.join()
},
}
console.log(`
Usage looks like this in a css style:
backgroundImage: GradientGenerator.generate(id || ''),
backgroundImage: ${GradientGenerator.generate('meow')}
`)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment