Last active
November 19, 2020 11:30
-
-
Save apaleslimghost/e8ac42426a689f0f67f3698ca61197cc to your computer and use it in GitHub Desktop.
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
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