I am using this code to generate random colors (which is working fine):
{
$r = rand(128,255);
$g = rand(128,255);
$b = rand(128,255);
$color = dechex($r) . dechex($g) . dechex($b);
return "#".$color;
}
I am just wondering if is there a way/combination to generate only the Bright colors?
Thank you
Your original code doesn’t work as you’d expect – if a low number is generated you might get
#1ffff(1 being the low red value) – which is invalid. It is much more stable to use this:Since
rgb(123,45,67)is perfectly valid colour specification.Along similar lines, you can generate random numbers for hsl:
This will generate fully saturated, normal lightness colours of any hue. However, note that only recent browers support HSL so you may be better off coerting to RGB if browser support is a concern.