Created
March 21, 2020 05:46
-
-
Save alexcmgit/c756094ff70d87b59571dcf599b6f325 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
function generateRandomColorPalette(userColor = {}, userVariant = {}) { | |
const { color: defaultColor } = generateRandomColor(); | |
const defaultVariant = { vR: 15, vG: 15, vB: 15, vA: 200 }; | |
const color = Object.assign({}, defaultColor, userColor); | |
const variant = Object.assign({}, defaultVariant, userVariant); | |
const { r, g, b, a } = color; | |
const { vR, vG, vB, vA } = variant; | |
const palette = { | |
r: getVariant({ variant: vR, num: r }), | |
g: getVariant({ variant: vG, num: g }), | |
b: getVariant({ variant: vB, num: b }), | |
a: getVariant({ variant: vA, num: a * 1000, max: 1000, min: 0 }).map(item => item / 1000), | |
} | |
function getVariant(userConfig = {}) { | |
const defaultConfig = { | |
min: 0, | |
max: 255, | |
num: randomIntFromInterval(0, 255), | |
variant: 15 | |
} | |
const config = Object.assign({}, defaultConfig, userConfig); | |
let { num, min, max, variant } = config; | |
if (num + variant >= max) { | |
num = [num - variant, max]; | |
} else if (num - variant < min) { | |
num = [min, num + variant]; | |
} else { | |
num = [num - variant, num + variant] | |
} | |
return num; | |
} | |
return palette; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment