react-native-playful-avatar
Version:
Make Playful Avatar for your React Native apps
37 lines (31 loc) • 944 B
text/typescript
export function getColorVariation(hex: string, lum: number) {
hex = checkIfIsHexColor(hex) ? hex : getRandomColor();
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '');
if (hex.length < 6) {
hex = (hex[0] as string) + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
lum = lum || 0;
// convert to decimal and change luminosity
let rgb = '#',
c,
i;
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i * 2, 2), 16);
c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16);
rgb += ('00' + c).substr(c.length);
}
return rgb;
}
export const checkIfIsHexColor = (color: string) => {
const Reg_Exp = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i;
return Reg_Exp.test(color);
};
export function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}