I have a random colour generator that works as expected, however I am wanting to use white text on top of whatever colour I generate and of course, this is no good when the colour generated is light as the contrast makes the text unreadable.
const generateColour = () => '#' + Math.random().toString(16).slice(-6);
I’m wondering if there is any way in which my current code can be altered to mean that I only receive colours dark enough to be of a accessible contrast ratio?
2
Answers
Use HSL, either directly in CSS or convert to RGB. Select S & L fixed ranges so colors won’t be too light.
Use HSL colours so you can control the hue, saturation and luminance independently.
It will be easier to spot the differences if, after running this snippet, you use the full page link to see all the results on one page.