Last active
September 2, 2020 00:49
-
-
Save alanrsoares/4385ee84d48e44d99839837b608a1fc7 to your computer and use it in GitHub Desktop.
HSL color generator with bilinear interpolation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HSLGenerator { | |
constructor(hueLength = 100, options = { lightness: 0.6, saturation: 1 }) { | |
this.hueIncrement = 360 / Math.sqrt(hueLength ** 2 * 2); | |
this.saturation = options.saturation * 100; | |
this.lightness = options.lightness * 100; | |
this.cache = {}; | |
} | |
getColor(y, x) { | |
const cacheKey = [y, x].join("-"); | |
if (cacheKey in this.cache) { | |
return this.cache[cacheKey]; | |
} | |
const hue = Math.floor(Math.sqrt(y ** 2 + x ** 2) * this.hueIncrement); | |
const hslColor = `hsl(${hue},${this.saturation}%,${this.lightness}%)`; | |
this.cache[cacheKey] = hslColor; | |
return hslColor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment