I have an <Avatar/>
component and the text inside of it is color white
,
How do I make sure that its background color is dark so the text can be seen since its dynamically generated?
Codesandbox —–> CLICK HERE
CODE:
const colors = useMemo(() => {
const randomColors = persons.map(() => `#${Math.floor(Math.random() * 16777215).toString(16)}`);
return randomColors;
}, [persons]);
2
Answers
The random value for each colour component (red, green, blue) is from 0-255. You need to limit to 0 – 127 to ensure that the generated colours are darker.
padStart(2, '0')
ensures that each component has two digits in hexadecimal formutils.js
Inside your react component.
CODESANDBOX
You’ll need to check the color for its luminance and than adjust it accordingly