Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Last active November 19, 2020 11:30
Show Gist options
  • Save apaleslimghost/e8ac42426a689f0f67f3698ca61197cc to your computer and use it in GitHub Desktop.
Save apaleslimghost/e8ac42426a689f0f67f3698ca61197cc to your computer and use it in GitHub Desktop.
const {hexToHsluv, hsluvToHex} = require('hsluv')
const interpolate = (a1, a2) => x => a1.map((c, i) => c + (a2[i] - c) * x)
const range = steps => Array.from({length: steps + 1}, (_, i) => i / steps)
const stopsToHsluv = (start, end) => {
const hStart = hexToHsluv(start)
const hEnd = hexToHsluv(end)
const throughZero = Math.abs(hStart[0] - hEnd[0]) > 180
if(throughZero) {
if(hStart[0] < hEnd[0]) {
hStart[0] += 360
} else {
hEnd[0] += 360
}
}
return [hStart, hEnd]
}
const gradient = (start, end, steps = 10) => range(steps)
.map(interpolate(...stopsToHsluv(start, end)))
.map(hsluvToHex)
module.exports = gradient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment