Skip to content

Instantly share code, notes, and snippets.

@alanrsoares
Last active September 2, 2020 00:49
Show Gist options
  • Save alanrsoares/4385ee84d48e44d99839837b608a1fc7 to your computer and use it in GitHub Desktop.
Save alanrsoares/4385ee84d48e44d99839837b608a1fc7 to your computer and use it in GitHub Desktop.
HSL color generator with bilinear interpolation
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