Coder Social home page Coder Social logo

randomcolor-cpp's Introduction

randomColor-cpp

This is a C++ random color generator inspired by and based on David Merfield's [randomColor] (https://github.com/davidmerfield/randomColor) (but not an exact port). The generator uses the HSB color space and predefined color map to produce colors.

The code was compiled with mingw 5.1.0. It requires support of C++11, particularly initializer lists, mutexes and lambdas.

example

Differences from the original project

  • Added upper bound for brightness to support colors like brown.
  • Light/dark/bright shades are calculated in a slightly different way and with relative values, so that produced colors always remain within the color map.
  • Added "Normal" luminosity option to produce colors which are not too light or dark. This option is set by default.
  • Added intermediate color ranges like RedOrange, Cyan, Magenta, etc.
  • Added option to specify a set of possible colors, one of which should be generated: generate({Red, Orange, Yellow}, ...). The probability of each color depends on its hue range size. There are predefined sets like AnyRed, AnyBlue, etc.
  • Added option to generate a color within custom hue range, like generate({100, 150}, ...).
  • The color map may contain hue ranges which intersect each other. When the generator needs to find a range containing some hue, it selects the narrowest one.
  • The color map is changed.
  • Several instances of the color generator can be created, each with its own seed. The seed can be changed at any time.
  • A generated color is always returned in 0xRRGGBB format.
  • The internal random generator is std::default_random_engine.
  • There is no option to generate an array of colors in one call.

Example

RandomColor randomColor;

// Random color with normal luminosity
int c1 = randomColor.generate();

// Random light red color
int c2 = randomColor.generate(RandomColor::Red, RandomColor::Light);

// Random dark color with hue within [150; 160]
int c3 = randomColor.generate({150, 160}, RandomColor::Dark);

// Random yellow or orange color with normal luminosity
int c4 = randomColor.generate({RandomColor::Yellow, RandomColor::Orange});

Known issues

Colors which are naturally bright (like yellow, pink) or dark (like red, blue) can become too bright/dark with corresponding luminosity. The generator selects all colors in the same way, not considering this "natural brightness". This could be solved by including additional parameter in the color map or by making this map more complex.

The generator can produce similar colors in a row. Distinct hue values are not always enough to generate distinct colors, but this could be a simple solution.

Adjacent hue ranges can produce similar colors. One could try to add unused gaps between them, e.g. by defining ranges like [11, 18] and [24, 41], where [19, 23] is not used. But these gaps should also be present in the color map, otherwise the generator would use RandomHue parameters for them.

The color map can be improved (or configured for a particular purpose).

Credits

Thanks to David Merfield, the author of the original randomColor.

Also thanks to the authors of http://www.workwithcolor.com, which contains a lot of useful information (their color ranges definition helped to create the color map).

And thanks to the authors of wikipedia articles like that:

randomcolor-cpp's People

Contributors

artureganyan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.