Pioneer
RandomColor.h
Go to the documentation of this file.
1 
23 #pragma once
24 
25 #ifndef _RANDOM_COLOR_H_
26 #define _RANDOM_COLOR_H_
27 
28 // Andrew Copland (2016/03/10 )This is a port of a port! Done under the MIT license (see ^^^Above^^^).
29 // Originally pointed out to me by RobN in a forum thread (https://forum.pioneerspacesim.net/viewtopic.php?f=3&t=221)
30 // I was deterred by A) not knowing any javascript, B) the more I saw of it the more I hated it.
31 // Then I noticed the C# port by Nathan Jones (https://github.com/nathanpjones/randomColorSharped) which was a much more logical starting point.
32 // I have included a snippet of his readme below about how he himself came to port it.
33 
34 //randomColorSharped
35 //==================
36 //
37 // This is a port to c# (.NET 4.0) of [randomColor](http://llllll.li/randomColor/), David Merfield's javascript random color generator.
38 // This was ported by [Nathan Jones](http://www.nathanpjones.com/) so that users of the .NET family of languages could enjoy these attractive colors.
39 //
40 // I saw this project linked on [Scott Hanselman's](http://www.hanselman.com/) excellent [Newsletter of Wonderful Things](http://www.hanselman.com/newsletter/)
41 // around the same time a coworker was creating an ad hoc visualization app. As we watched the data appear on screen, we had to squint as some of the colors
42 // were very difficult to make out against the background. This should make things easier to see and will hopefully help others as well.
43 
44 #include "Color.h"
45 #include "Random.h"
46 
47 #include <cstdlib>
48 #include <map>
49 #include <vector>
50 
51 namespace RandomColorGenerator {
52  // Represents a range using an upper and lower value.
53  class Range {
54  public:
55  int Lower;
56  int Upper;
57 
58  Range() {}
59  Range(int lower, int upper)
60  {
61  Lower = lower;
62  Upper = upper;
63  }
64 
65  // Gets the lower range for an index of 0 and the upper for an index of 1.
66  const int &operator[](const size_t index) const
67  {
68  if (index == 0)
69  return Lower;
70  else
71  return Upper;
72  }
73  };
74 
75  enum ColorScheme {
76  // Select randomly from among the other color schemes.
78  // Generates only grayscale colors.
80  // Generates only red colors.
82  // Generates only orange colors.
84  // Generates only yellow colors.
86  // Generates only green colors.
88  // Generates only blue colors.
90  // Generates only purple colors.
92  // Generates only pink colors.
94  };
95 
96  enum Luminosity {
97  // Select randomly from among the other luminosities.
99  // Generate dark colors.
101  // Generate light, pastel colors.
103  // Generate vibrant colors.
105  };
106 
107  class Point {
108  public:
109  Point() :
110  x(0),
111  y(0) {}
112  Point(int nx, int ny) :
113  x(nx),
114  y(ny) {}
115  int x, y;
116  };
117 
118  // Generates random numbers.
119  class RandomColor {
120  private:
121  class DefinedColor {
122  public:
123  DefinedColor() {}
124  Range HueRange;
125  std::vector<Point> LowerBounds;
126  Range SaturationRange;
127  Range BrightnessRange;
128  };
129 
130  static std::map<ColorScheme, DefinedColor> ColorDictionary;
131 
132  public:
133  RandomColor();
134 
138  static Color GetColor(ColorScheme scheme, Luminosity luminosity);
139 
144  static std::vector<Color> GetColors(Random &rand, ColorScheme scheme, Luminosity luminosity, int count);
145 
146  static int PickHue(ColorScheme scheme);
147 
148  private:
149  static int PickSaturation(int hue, Luminosity luminosity, ColorScheme scheme);
150 
151  static int PickBrightness(int H, int S, Luminosity luminosity);
152 
153  static int GetMinimumBrightness(int H, int S);
154 
155  static Range GetHueRange(ColorScheme colorInput);
156 
157  static DefinedColor GetColorInfo(int hue);
158 
159  static int RandomWithin(Range range);
160  static int RandomWithin(int lower, int upper);
161 
162  static void DefineColor(ColorScheme scheme, Point hueRange, const Point *lowerBounds, const size_t lbCount);
163 
164  static void LoadColorBounds();
165 
166  public:
167  // Converts hue, saturation, and lightness to a color.
168  static Color HsvToColor(int hue, int saturation, double value);
169  };
170 } // namespace RandomColorGenerator
171 
172 #endif // _RANDOM_COLOR_H_
Definition: RandomColor.h:107
Point()
Definition: RandomColor.h:109
int y
Definition: RandomColor.h:115
int x
Definition: RandomColor.h:115
Point(int nx, int ny)
Definition: RandomColor.h:112
Definition: RandomColor.h:119
static Color HsvToColor(int hue, int saturation, double value)
Converts hue, saturation, and lightness to a color.
Definition: RandomColor.cpp:280
RandomColor()
Definition: RandomColor.cpp:14
static int PickHue(ColorScheme scheme)
Definition: RandomColor.cpp:59
static std::vector< Color > GetColors(Random &rand, ColorScheme scheme, Luminosity luminosity, int count)
Definition: RandomColor.cpp:46
static Color GetColor(ColorScheme scheme, Luminosity luminosity)
Definition: RandomColor.cpp:24
Definition: RandomColor.h:53
const int & operator[](const size_t index) const
Definition: RandomColor.h:66
Range()
Definition: RandomColor.h:58
int Upper
Definition: RandomColor.h:56
int Lower
Definition: RandomColor.h:55
Range(int lower, int upper)
Definition: RandomColor.h:59
Definition: Random.h:27
Definition: RandomColor.cpp:9
Luminosity
Definition: RandomColor.h:96
@ LUMINOSITY_RANDOM
Definition: RandomColor.h:98
@ LUMINOSITY_LIGHT
Definition: RandomColor.h:102
@ LUMINOSITY_DARK
Definition: RandomColor.h:100
@ LUMINOSITY_BRIGHT
Definition: RandomColor.h:104
ColorScheme
Definition: RandomColor.h:75
@ SCHEME_BLUE
Definition: RandomColor.h:89
@ SCHEME_MONOCHROME
Definition: RandomColor.h:79
@ SCHEME_ORANGE
Definition: RandomColor.h:83
@ SCHEME_GREEN
Definition: RandomColor.h:87
@ SCHEME_RED
Definition: RandomColor.h:81
@ SCHEME_RANDOM
Definition: RandomColor.h:77
@ SCHEME_YELLOW
Definition: RandomColor.h:85
@ SCHEME_PINK
Definition: RandomColor.h:93
@ SCHEME_PURPLE
Definition: RandomColor.h:91
Definition: Color.h:66
Definition: msvc_bug.cpp:29