A tiny script for generating attractive colors

Related tags

Color randomColor
Overview

Random Color

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.

Demo

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

var color = randomColor(); // a hex code for an attractive color

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

npm install randomcolor
var randomColor = require('randomcolor'); // import the script
var color = randomColor(); // a hex code for an attractive color

Options

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.

Examples

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

// Returns an array of ten green colors
randomColor({
   count: 10,
   hue: 'green'
});

// Returns a hex code for a light blue
randomColor({
   luminosity: 'light',
   hue: 'blue'
});

// Returns a hex code for a 'truly random' color
randomColor({
   luminosity: 'random',
   hue: 'random'
});

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

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

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

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

To do

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

  1. Use this on the demo http://jsfiddle.net/dpLp318f/ https://www.vis4.net/blog/posts/avoid-equidistant-hsv-colors/ https://www.vis4.net/blog/posts/mastering-multi-hued-color-scales/
  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
  1. Think about how to return seeds.
  2. Think about adding feature to return complementary & contrasting colors, as well as color triads, tetrads etc. Other libraries might already do this better though.
  3. 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.
  4. Add a nice visualization of the 'attractive color space' to the demo https://www.youtube.com/watch?v=x0-qoXOCOow
  5. Add a feature to return random colors close to a provided hex

In use

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

Comments
  • Dedicate this to Public Domain

    Dedicate this to Public Domain

    I would like to dedicate this project to the public domain and change the license on this repo from MIT to the more permissive CC0. If you are one of the 14 contributors, do you consent to this change in license? If so, please leave a comment on this issue saying 'yes' or 'lgtm' or however you typically express consent. If not, just comment 'no' and I'll close this issue.

    Sorry for the bother.

    • [x] @davidmerfield
    • [x] @kkirsche
    • [x] @mistic100
    • [x] @DanielRosenwasser
    • [x] @koide
    • [x] @riley
    • [x] @daviesgeek
    • [x] @bilalq
    • [x] @onevcat
    • [x] @cgmartin
    • [x] @lijunle
    • [x] @afc163
    • [x] @kevinwuhoo
    • [x] @Zepx
    • [x] @yageek
    opened by davidmerfield 18
  • Maximize color difference

    Maximize color difference

    This looks exactly like a lib that I'd soon need!

    I see there's an option for providing a seed for having deterministic results, but is there any way of specifying "I want 5 colors (with these settings) that are as different from each other as possible"? So that if I ask for 3 colors, I won't get 3 different shades of red.

    enhancement 
    opened by jareware 11
  • Specific hue hex colors not working

    Specific hue hex colors not working

    When i pass the hexadecimal #fec300 value to the hue key, i get the following error:

    var randomColor = randomColor({ hue: '#fec300' });

    Screenshot 2019-04-29 at 14 56 20

    I had the same result when testing with: #fec200, #fec400, #fec500 and #fec600

    bug 
    opened by HannesDeMets 8
  • is there a typings for this to be used in Angular 2?

    is there a typings for this to be used in Angular 2?

    I have converted this into an angular service for my own purposes but it would be great if there was a typings file for your tool.

    Here's a link to my service: https://gist.github.com/tylerlindell/130586743d41fede94c464f0b66a7126

    it only generates one random color at a time but it does the job for what I need right now

    question 
    opened by tylerlindell 7
  • Output UMD and ES module builds using rollup

    Output UMD and ES module builds using rollup

    This introduces rollup as a dev-dependency which allows us to author the original file using ES6 import/export syntax, which Rollup can read and subsequently output as a UMD file. The file with ES6 import syntax can subsequently be consumed by bundler tools like Rollup and WebPack to create more compact files within those tools.

    This should change nothing for users who already consume the UMD version - but it should improve the code output for those that use tools such as Webpack and Rollup.

    Diff with whitespace removed

    opened by keithamus 6
  • Maximise color difference

    Maximise color difference

    • I changed the randomWithin method to generate hues that are evenly distributed.
    • I added the method getRealHueRange that return the range where a given hue fits in.
    • I changed the pickHue method that return hue within a range that is not already taken.
    opened by m-bourkha 6
  • Change seeds so they work the first time they are passed in

    Change seeds so they work the first time they are passed in

    Bug Report

    Currently the way randomColor works, a seed will work the very first time it's passed in, but if the seed changes, it uses the last seed that was passed in to generate the color(s), and then it sets the new seed, causing the wrong output.

    Here's an example:

    randomColor({seed: 1, count: 10})
    // Output:
    ['#8bd343', '#a31f44', '#f23aa5', '#25bfd1', '#b9dd5d', '#b2ffd3', '#017984', '#c4214f', '#fcd928', '#55dbd2']
    
    
    randomColor({seed: 2, count: 10}) // Note the seed has changed from 1 to 2
    // Notice that this is the same array as when it was seeded with "1"
    ['#8bd343', '#a31f44', '#f23aa5', '#25bfd1', '#b9dd5d', '#b2ffd3', '#017984', '#c4214f', '#fcd928', '#55dbd2']
    
    randomColor({seed: 2, count: 10}) // Note the seed is still 2
    // This is now the correct array of colors
    ['#7ed65e', '#d8d82f', '#46e2a1', '#77eaea', '#d062fc', '#8e6af2', '#604ce0', '#e5d177', '#9df2e2', '#efcb8d']
    

    As you can see, when you run the generator with one seed (first run), change the seed, run it again (second run), it generates the wrong array of colors. Then when you run it for the third time (with the same seed as the second run), it generates the correct array of colors.

    Solution

    This pull request modifies the behavior of the seed option so that the seed is reset at the end of each run.

    I've also written a test that fails if you use the original version of randomColor (before my changes), so that you can run it (via npm run test) and easily see the bug.

    Misc

    I've also bumped the version number in package.json, as well as added a version number to the bower.json file. I also created a new tag for the 0.4.1 release (not sure if this will move over when my PR is merged in though…)

    opened by daviesgeek 6
  • Empty Options must be passed

    Empty Options must be passed

    I get the following error when I call randomColor() :

    [...]/node_modules/randomcolor/randomColor.js:36 if (options.seed && !seed) seed = options.seed; ^ TypeError: Cannot read property 'seed' of undefined

    The simple fix: randomColor({})

    opened by livemixlove 5
  • Light colors not light enough ?

    Light colors not light enough ?

    Hello. Thanks for your lib !

    I'm using it for generating colors in a bookmark minisite (open source)

    capture d ecran 2017-03-17 a 15 56 02

    As you can see, random colors generated with luminosity "light" are not always contrasting well with a black text for the "blue" hue (it's even worse with purple, so I don't use it).

    I'm expecting "light" colors to contrast well with a dark color. Could this be an improvement ? Or am I assuming wrong ?

    opened by Offirmo 4
  • Exporting functions like HSVtoHSL, HSVtoRGB, HSVtoHex?

    Exporting functions like HSVtoHSL, HSVtoRGB, HSVtoHex?

    I find them quite useful but for now I just copy them directly into my source code https://github.com/fritx/markppt/commit/ae7df02b5577cb6ce41dde6da60345365243b40b#diff-5f48f4b7cd5fa5aa6e97a127e55158deR271

    opened by fritx 4
  • Suggestion: If asked to return x colours, return x colours with sufficient contrast between them

    Suggestion: If asked to return x colours, return x colours with sufficient contrast between them

    I ran into the problem that I needed 5 colours but 2 of the returned colours were so close to each other that they were indistinguishable. It would be great if in that case the 5 colours would be random but sufficiently distinct from each other.

    opened by logolab 3
  • "Truly Random" isn't actually random?

    randomColor({hue: 'random',luminosity: 'random',count: 54});
    

    More of a pet peeve than an severe issue: On the demo page, you have the above code for Truly Random colors, but it produces dark colors more often than not, so it appears biased and/or constrained to certain saturations and luminosities (which means its not truly random).

    Comparison of randomColor on the left, to fully random RGB values (on the right).

    image

    bug 
    opened by bryc 1
  • Does the color '#c411a6' break randomColor?

    Does the color '#c411a6' break randomColor?

    I'm seeing that this code breaks the generation or randomColor, but I find this esoterical so it's likely something else

      console.log(randomColor({
        count: 5,
        luminosity: 'light',
        hue: '#c411a6',
      }))
    

    I do see it in a codesandbox

    import "./styles.css";
    import randomColor from 'randomcolor'
    
    console.log(randomColor({
      count: 5,
      luminosity: 'light',
      hue: '#ffffff',
    }))
    
    console.log(randomColor({
      count: 5,
      luminosity: 'light',
      hue: '#c411a6',
    }))
    
    document.getElementById("app").innerHTML = `
    <h1>Hello Vanilla!</h1>
    <div>
      We use the same configuration as Parcel to bundle this sandbox, you can find more
      info about Parcel 
      <a href="https://parceljs.org" target="_blank" rel="noopener noreferrer">here</a>.
    </div>
    `;
    

    The hue value is from a previous randomly randomColor-generated array

    image

    opened by kuworking 3
  • Adding TypeScript Definition

    Adding TypeScript Definition

    Hello!

    Created a d.ts for consumption in typescript projects. options and returns are based on documentation. I've also had a look at source code but couldn't analyze the code fully due to time constraints.

    Let me know if anything needs changing.

    Regards.

    opened by arafatzahan 1
  • Is there a way to control the

    Is there a way to control the "distribution" of generated colors?

    Right now I'm using

    let colors = randomColor({
        seed: 0, // Just a fixed seed so that generated colors are deterministic.
        count: count,
        luminosity: "bright",
    });
    

    With count = 2, the colors I get is somewhat similiar to each other (blue-green ish).

    image

    I would like to get colors that are very different visually. What those colors are does not really matter.

    Currently I can't find a way to control this behavii. Or is there some magic seeds that can work better?

    opened by laike9m 1
Releases(0.6.1)
Owner
David Merfield
David Merfield
Smarter defaults for colors on the web.

colors.css 3.0.0 Better default colors for the web. A collection of skin classes for faster prototyping and nicer looking sites. Stats 903 85 85 bytes

murmurs 9.2k Jan 9, 2023
JavaScript Library for creating random pleasing colors and color schemes

#PleaseJS www.checkman.io/please Please.js is a polite companion that wants to help you make your projects beautiful. It uses HSV color space to creat

Jordan Checkman 2.3k Dec 22, 2022
Extract prominent colors from an image. JS port of Android's Palette.

Vibrant.js Extract prominent colors from an image. Vibrant.js is a javascript port of the awesome Palette class in the Android support library. ⚠️ THI

Jari Zwarts 4.6k Dec 25, 2022
Generate colors based on a desired contrast ratio

Generate colors based on a desired contrast ratio

Adobe, Inc. 1.4k Jan 3, 2023
get colors in your node.js console

colors.js This is a fork of Marak Squires' original colors.js project Please check out the roadmap for upcoming features and releases. Please open Iss

Josh 5 Jan 11, 2022
Node Terminal Colors. Fast!

Tcol npm package to add colors to your terminal Installation # npm npm install tcol # pnpm pnpm install tcol Usage import { c } from "tcol"; console.

Posandu 4 May 13, 2022
Get colors in your node.js console

@colors/colors ("colors.js") Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback. get color and st

David Hyde 89 Dec 30, 2022
A simple Discord bot that will listen for HEX, RGB(a), and HSL(a) colors in a message, and provide a small image of that color.

Pigment For the teams of designers and developers out there - Pigment will listen for messages containing a HEX, RGB(a), or HSL(a) color, and provide

Conrad Crawford 17 Dec 8, 2022
A tiny script and component intended to be used with Astro for generating images with eleventy-img.

Astro + eleventy-img A tiny script and component intended to be used with Astro for generating images with eleventy-img. It also supports creating blu

Erika 36 Dec 16, 2022
colors.js - get colors in your node.js console

colors.js Please check out the roadmap for upcoming features and releases. Please open Issues to provide feedback, and check the develop branch for th

cronlabspl 4 Feb 9, 2022
chakra-radix-colors provides radix-ui color palettes, automatic dark mode, and acessible colors to chakra-ui applications

chakra-radix-colors chakra-radix-colors provides radix-ui color palettes, automatic dark mode, and acessible colors to chakra-ui applications. About C

null 11 Dec 30, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Attractive JavaScript charts for jQuery

flot About flot is a JavaScript plotting library for engineering and scientific applications derived from Flot: http://www.flotcharts.org/ Take a look

Flot 5.9k Dec 22, 2022
Animated images that are superficially attractive and entertaining but intellectually undemanding. Cool as all hell though!

MMM-EyeCandy Animated images that are superficially attractive and entertaining but intellectually undemanding. Add some EyeCandy to your mirror. Some

Mykle 36 Dec 28, 2022
A collection of preloaded and essential files that makes the website more attractive, smooth and user friendly

Web-Docs A collection of preloaded and essential files that makes the website more attractive, smooth and user friendly How to use: git clone https://

MAINAK CHAUDHURI 23 Dec 17, 2022
An attractive music player using HTML and CSS

Music Player I have made an attractive music player using HTML and CSS. This project was a part of my Coding Ninja Course (Introduction to Web Technol

DHRUV 5 Feb 17, 2022
A tiny utility library to generate mesh gradient based on 4 RGB colors, built with vanilla js.

MeshGradient.js mesh-gradient.js is tiny utility library to generate mesh gradient based on 4 RGB colors, built with vanilla js. Installation! npm ins

Anup Aglawe 7 Jan 4, 2023
A tiny isomorphic fast function for generating a cryptographically random hex string.

ZeptoID A tiny isomorphic fast function for generating a cryptographically random hex string. Accoding to this calculator one would have to generate i

Fabio Spampinato 9 Oct 24, 2022