randomcolor

repository·master·Indexed 27 days ago

https://github.com/davidmerfield/randomcolor

A tiny script for generating attractive random colors in both browser and Node.js environments. Version 0.6.2 supports customization of hue (via names, numbers, or hex), luminosity (bright, light, dark), and output formats including hex, rgb, rgba, hsl, hsla, and arrays. It allows generating multiple colors using the count option and repeatable results via a seed.

Tokens
1.1K
Snippets
3
Records
8
Agent score
41%

What's inside randomcolor

  1. Use randomColor() to generate colors

    master

    Import the library and call randomColor() to generate a color string. By default, it returns a hex code for an attractive color.

    In Node.js:

    var randomColor = require('randomcolor');
    var color = randomColor();

    In the Browser:

    var color = randomColor();
    var randomColor = require('randomcolor'); // import the script
    var color = randomColor(); // a hex code for an attractive color
  2. Set hue using names, numbers, or hex

    master

    The hue option allows you to restrict the color range. You can provide:

    • A number: An integer between 0 and 359.
    • A color name: Supported names include 'red', 'orange', 'yellow', 'green', 'blue', 'purple', 'pink', and 'monochrome'.
    • A hex string: A standard hex color (e.g., '#00ff00').

    When using count with a specific hue, the library attempts to pick distinct colors within that hue range to avoid duplicates.

  3. Generate multiple colors or specific formats

    master

    Use the options object to generate arrays of colors or specific color models like RGB or HSL.

    // Returns an array of ten green colors
    randomColor({
       count: 10,
       hue: 'green'
    });
    
    // Returns a bright color in RGB
    randomColor({
       luminosity: 'bright',
       format: 'rgb'
    });
    
    // Returns a dark RGB color with specified alpha
    randomColor({
       luminosity: 'dark',
       format: 'rgba',
       alpha: 0.5
    });
    // Returns an array of ten green colors
    randomColor({
       count: 10,
       hue: 'green'
    });
    
    // Returns a bright color in RGB
    randomColor({
       luminosity: 'bright',
       format: 'rgb' // e.g. 'rgb(225,200,20)'
    });
    
    // Returns a dark RGB color with specified alpha
    randomColor({
       luminosity: 'dark',
       format: 'rgba',
       alpha: 0.5 // e.g. 'rgba(9, 1, 107, 0.5)',
    });
  4. Configure randomColor options

    master

    Pass an options object to randomColor() to customize the output.

    OptionTypeDescription
    huestringControls hue. Supported names: red, orange, yellow, green, blue, purple, pink, monochrome. You can also pass a hex string (e.g., #00FFFF) to extract its hue.
    luminositystringControls brightness. Values: bright, light, dark.
    countintegerThe number of colors to generate.
    seedinteger or stringProvides a seed so the function returns the same color every time.
    formatstringThe output format. Values: rgb, rgba, rgbArray, hsl, hsla, hslArray, hex (default).
    alphanumberA decimal between 0 and 1. Only used with rgba or hsla formats. Defaults to a random value.
  5. Generate random colors with randomColor()

    master
    The randomColor(options) function is the primary entry point for generating random colors. It can return a single color string or an array of color strings based on the options provided. It supports various color formats, luminosity settings, and seeding for repeatable results.
  6. Use different color formats

    master

    The format option determines the string representation of the generated color. Supported formats include:

    FormatDescription
    hsvArrayReturns [H, S, B] array
    hslArrayReturns [H, S, L] array
    hslReturns hsl(H, S%, L%) string
    hslaReturns hsla(H, S%, L%, A) string (uses alpha option or random)
    rgbArrayReturns [R, G, B] array
    rgbReturns rgb(R, G, B) string
    rgbaReturns rgba(R, G, B, A) string (uses alpha option or random)
    (default)Returns #RRGGBB hex string