I've been reading the questions here about how to generate random colors in JS/JQuery. But I want to generate only darken colors avoiding the black, yellow and gray range colors, in order to be able to draw areas in google maps with different colors.
Thanks
According to duplicate taken from comment of jj__?
Where's the problem? Just generate 3 random numbers, maybe in the range from, let's say 30 to 150, so you will only get relatively low values for R, G and B, and block "black-like" colors. Then you would just have to check if these three values fall into the range of your defined blacklisted color ranges like yellow or gray.
That was to generate a random hsl color. But if you want to darken an existing rgb color the best thing to do is to convert it to hsl and then setting its lightness value according to your needs. Have fun with these functions:
Here,
s
andl
ranges from0
to1
,r
,g
andb
from0
to255
andh
from0
to359
.First define the RGB ranges you do want, and once you have those defines just generate random values within those ranges. Or define the ranges you don't want and keep looping till you find a colour that fits those criteria, a lot less efficient, but quite a bit easier to program if efficiency is not an issue.
For simplicity of implementation and thoroughness, I can't recommend this script highly enough:
David Merfeld's randomColor
Choose between hex or rgb, add in some alpha, specify light or dark palettes, avoid repeats, etc.