Random Color

A tiny script for generating attractive colors


A tiny script for generating attractive random colors. See the demo for an explanation and some samples. randomColor has been ported to C#, C++, Go, Haskell, Kotlin, Mathematica, PHP, Python, Swift, Perl6, Objective-C, Java, R, Reason, Dart, Ruby and Rust.

To use randomColor in the browser, download the latest minified version of randomColor and include it on your page. Then call the script:

  1. ``` js
  2. var color = randomColor(); // a hex code for an attractive color
  3. ```

To use randomColor on the server with node.js, install randomColor from npm then call the script:

  1. ``` sh
  2. npm install randomcolor
  3. ```
  1. ``` js
  2. var randomColor = require('randomcolor'); // import the script
  3. var color = randomColor(); // a hex code for an attractive color
  4. ```


You can pass an options object to influence the type of color it produces. The options object accepts the following properties:

hue – Controls the hue of the generated color. You can pass a string representing a color name: red , orange , yellow , green , blue , purple , pink and monochrome are currently supported. If you pass a  hexidecimal color string such as #00FFFF , randomColor will extract its hue value and use that to generate colors.

luminosity – Controls the luminosity of the generated color. You can specify a string containing bright , light or dark .

count – An integer which specifies the number of colors to generate.

seed - An integer or string which when passed will cause randomColor to return the same color each time.

format – A string which specifies the format of the generated color. Possible values are rgb , rgba , rgbArray , hsl , hsla , hslArray and hex (default).

alpha – A decimal between 0 and 1. Only relevant when using a format with an alpha channel ( rgba and hsla ). Defaults to a random value.


  1. ``` js

  2. // Returns a hex code for an attractive color
  3. randomColor();

  4. // Returns an array of ten green colors
  5. randomColor({
  6.    count: 10,
  7.    hue: 'green'
  8. });

  9. // Returns a hex code for a light blue
  10. randomColor({
  11.    luminosity: 'light',
  12.    hue: 'blue'
  13. });

  14. // Returns a hex code for a 'truly random' color
  15. randomColor({
  16.    luminosity: 'random',
  17.    hue: 'random'
  18. });

  19. // Returns a bright color in RGB
  20. randomColor({
  21.    luminosity: 'bright',
  22.    format: 'rgb' // e.g. 'rgb(225,200,20)'
  23. });

  24. // Returns a dark RGB color with random alpha
  25. randomColor({
  26.    luminosity: 'dark',
  27.    format: 'rgba' // e.g. 'rgba(9, 1, 107, 0.6482447960879654)'
  28. });

  29. // Returns a dark RGB color with specified alpha
  30. randomColor({
  31.    luminosity: 'dark',
  32.    format: 'rgba',
  33.    alpha: 0.5 // e.g. 'rgba(9, 1, 107, 0.5)',
  34. });

  35. // Returns a light HSL color with random alpha
  36. randomColor({
  37.    luminosity: 'light',
  38.    format: 'hsla' // e.g. 'hsla(27, 88.99%, 81.83%, 0.6450211517512798)'
  39. });

  40. ```

To do

More generally, it might be a good idea to consider using other color models.

1. Use this on the demo
2. When returning an array of multiple colors, try to make each color as distinct as possible. Make sure each color is distinct to its neighbours.
3. Improve attractive dark color generation, currently they're a touch murky.
4. Improve the color dictionary
  - definition of hue values and attractive luminosity bounds
  - think about how to store hue aliases (e.g. fuschia) or subranges (e.g teal)
  - think about making a little point and click tool for defining your own luminosity bounds and customizing the color dictionary
5. Think about how to return seeds.
6. Think about adding feature to return complementary & contrasting colors, as well as color triads, tetrads etc. Other libraries might already do this better though.
7. Think about adding a scheme options which would return a dominant color, as well as secondary, tertiary color and primary and secondary text colors. This might be a bad idea.
8. Add a nice visualization of the 'attractive color space' to the demo https://www.youtube.com/watch?v=x0-qoXOCOow
9. Add a feature to return random colors close to a provided hex

In use

Initial.js used it to generate Gmail-style avatars.
ng2-Logger used it to make logs a little more colorful.
vue-randomcolor adds attractive random colors to Vue.js.
sudoku-solver.ch uses it for coloring constraints on the puzzle
farbvelo.elastiq.ch uses it to generate color harmonies.

Did you use randomColor.js for something? Tell me about it.