Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: generate random nice saturated colors (selected palette)

Tags:

javascript

i saw a lot of Js example but none fit my needs:

I'd like to have a script to generate colros, but if I use the common solution

color =  '#'+Math.floor(Math.random()*16777215).toString(16);

it generates pastel colors, where I'd like better more saturated colors...or more ambitious, how can i select the palette form which i pick the colors?

how can I "restrict" the randomness?

like image 919
Francesco Avatar asked Jun 09 '26 05:06

Francesco


1 Answers

For fully saturated colours use the HSL colour space with H from 0-360, S at (or slightly below) 100% and L at 50%.

If you know the browser has CSS3 support you may be able to use that directly. Failing that, use an HSL to RGB algorithm (20ish lines of code) readily available via Google to convert into hex format.

The formulae for conversion from HSL to RGB are also on the Wikipedia page on HSL and are easily translated into Javascript (and most other languages too!)

like image 110
Alnitak Avatar answered Jun 10 '26 20:06

Alnitak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!