Last active
February 18, 2023 12:21
-
-
Save Dmitryure/6181719169a1ef02b1324e4600d376be to your computer and use it in GitHub Desktop.
This file contains hidden or 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 toColour = arr => { | |
| return ( | |
| "#" + | |
| arr | |
| .map(el => { | |
| if (el < 10) { | |
| return "0" + Number(el.toFixed(0)).toString(16); | |
| } | |
| const value = Number(el.toFixed(0)).toString(16); | |
| return value.length < 2 ? "0" + value : value; | |
| }) | |
| .join("") | |
| ); | |
| }; | |
| const move = (a, b, step) => { | |
| if (a < b) { | |
| return a + Math.floor(step); | |
| } | |
| if (a === b) { | |
| return a; | |
| } | |
| return a - Math.floor(step); | |
| }; | |
| export const generateGradient = (from, to, amount) => { | |
| const [r1, g1, b1, r2, g2, b2] = (from + to).match(/.{1,2}/g).map(el => parseInt(el, 16)); | |
| return Array.from(Array(amount)).map((el, index, array) => { | |
| if (index === 0) { | |
| return toColour([r1, g1, b1]); | |
| } | |
| if (index === array.length - 1) { | |
| return toColour([r2, g2, b2]); | |
| } | |
| return toColour([ | |
| move(r1, r2, (Math.abs(r1 - r2) / amount) * index), | |
| move(g1, g2, (Math.abs(g1 - g2) / amount) * index), | |
| move(b1, b2, (Math.abs(b1 - b2) / amount) * index) | |
| ]); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment