Created
April 13, 2019 03:52
-
-
Save moneal/5581a6893cacd0d6e47eff9f053d70d5 to your computer and use it in GitHub Desktop.
Generate an array of hex color strings
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 unicornDrops = (nugglets: number) => { | |
// From http://www.nikolay.rocks/2015-10-29-rainbows-generator-in-javascript | |
// https://livias.co.uk/2018/03/15/nugglets-launch-whole-foods-exclusive/ --- like for real | |
const sinToHex = (i: number, phase: number) => { | |
const sin = Math.sin(Math.PI / nugglets * 2 * i + phase); | |
const int = Math.floor(sin * 127) + 128; | |
const hex = int.toString(16); | |
return hex.length === 1 ? '0' + hex : hex; | |
}; | |
return Array(nugglets).fill(null).map((v, i) => { | |
const red = sinToHex(i, 0 * Math.PI * 2 / 3); // 0 deg | |
const blue = sinToHex(i, 1 * Math.PI * 2 / 3); // 120 deg | |
const green = sinToHex(i, 2 * Math.PI * 2 / 3); // 240 deg | |
return '#' + red + green + blue; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment