<p>I'm looking for a simple algorithm to generate a large number of random, aesthetically pleasing colors. So no crazy neon colors, colors reminiscent of feces, etc. </p>

<p>I've found solutions to this problem but they rely on alternative color palettes than RGB.
I would rather just use straight RGB than mapping back and forth. These other solutions also can at most generate only 32 or so pleasing random colors. </p>

<p>Any ideas would be great.</p>

[{'answer_id': 43081, 'author': 'helloandre', 'author_id': 50, 'author_profile': 'https://Stackoverflow.com/users/50', 'pm_score': 0, 'selected': False, 'text': '<p>you could have them be within a certain brightness. that would control the ammount of "neon" colors a bit. for instance, if the "brightness"</p>\n\n<pre><code>brightness = sqrt(R^2+G^2+B^2)\n</code></pre>\n\n<p>was within a certain high bound, it would have a washed out, light color to it. Conversely, if it was within a certain low bound, it would be darker. This would eliminate any crazy, standout colors and if you chose a bound really high or really low, they would all be fairly close to either white or black.</p>\n'}, {'answer_id': 43150, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 0, 'selected': False, 'text': "<p>It's going to be hard to get what you want algorithmically - people have been studying color theory for a long time, and they don't even know all the rules.</p>\n\n<p>However, there are <em>some</em> rules which you can use to cull bad color combinations (ie, there are rules for clashing colors, and choosing complementary colors).</p>\n\n<p>I'd recommend you visit your library's art section and check out books on color theory to gain a better understanding of what is a good color before you try to make one - it appears you might not even know why certain combinations work and others don't.</p>\n\n<p>-Adam</p>\n"}, {'answer_id': 43235, 'author': 'David Crow', 'author_id': 2783, 'author_profile': 'https://Stackoverflow.com/users/2783', 'pm_score': 10, 'selected': True, 'text': '<p>You could average the RGB values of random colors with those of a constant color:</p>\n\n<p><em>(example in Java)</em></p>\n\n<pre><code>public Color generateRandomColor(Color mix) {\n    Random random = new Random();\n    int red = random.nextInt(256);\n    int green = random.nextInt(256);\n    int blue = random.nextInt(256);\n\n    // mix the color\n    if (mix != null) {\n        red = (red + mix.getRed()) / 2;\n        green = (green + mix.getGreen()) / 2;\n        blue = (blue + mix.getBlue()) / 2;\n    }\n\n    Color color = new Color(red, green, blue);\n    return color;\n}\n</code></pre>\n\n<p><br/>\nMixing random colors with white (255, 255, 255) creates neutral pastels by increasing the lightness while keeping the hue of the original color. These randomly generated pastels usually go well together, especially in large numbers.</p>\n\n<p>Here are some pastel colors generated using the above method:</p>\n\n<p><img src="https://i.stack.imgur.com/8jKGx.jpg" alt="First"></p>\n\n<p><br/>\nYou could also mix the random color with a constant pastel, which results in a tinted set of neutral colors. For example, using a light blue creates colors like these:</p>\n\n<p><img src="https://i.stack.imgur.com/zI406.jpg" alt="Second"></p>\n\n<p><br/>\nGoing further, you could add heuristics to your generator that take into account complementary colors or levels of shading, but it all depends on the impression you want to achieve with your random colors.</p>\n\n<p>Some additional resources:</p>\n\n<ul>\n<li><a href="http://en.wikipedia.org/wiki/Color_theory" rel="noreferrer">http://en.wikipedia.org/wiki/Color_theory</a></li>\n<li><a href="http://en.wikipedia.org/wiki/Complementary_color" rel="noreferrer">http://en.wikipedia.org/wiki/Complementary_color</a></li>\n</ul>\n'}, {'answer_id': 962777, 'author': 'Nick Johnson', 'author_id': 12030, 'author_profile': 'https://Stackoverflow.com/users/12030', 'pm_score': 4, 'selected': False, 'text': '<p>Converting to another palette is a far superior way to do this. There\'s a reason they do that: other palettes are \'perceptual\' - that is, they put similar seeming colors close together, and adjusting one variable changes the color in a predictable manner. None of that is true for RGB, where there\'s no obvious relationship between colors that "go well together".</p>\n'}, {'answer_id': 5008241, 'author': 'Fgblanch', 'author_id': 76778, 'author_profile': 'https://Stackoverflow.com/users/76778', 'pm_score': 6, 'selected': False, 'text': '<p>I would use a color wheel and given a random position you could add the golden angle (137,5 degrees)</p>\n\n<p><a href="http://en.wikipedia.org/wiki/Golden_angle" rel="noreferrer">http://en.wikipedia.org/wiki/Golden_angle</a></p>\n\n<p>in order to get different colours each time that do not overlap. </p>\n\n<p>Adjusting the brightness for the color wheel you could get also different bright/dark color combinations.</p>\n\n<p>I\'ve found this blog post that explains really well the problem and the solution using the golden ratio.</p>\n\n<p><a href="http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/" rel="noreferrer">http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/</a></p>\n\n<p><strong>UPDATE:</strong> I\'ve just found this other approach:</p>\n\n<p>It\'s called RYB(red, yellow, blue) method and it\'s described in this paper:</p>\n\n<p><a href="http://threekings.tk/mirror/ryb_TR.pdf" rel="noreferrer">http://threekings.tk/mirror/ryb_TR.pdf</a></p>\n\n<p>as "Paint Inspired Color Compositing".</p>\n\n<p>The algorithm generates the colors and each new color is chosen to maximize its euclidian distance to the previously selected ones.</p>\n\n<p>Here you can find a a good implementation in javascript:</p>\n\n<p><a href="http://afriggeri.github.com/RYB/" rel="noreferrer">http://afriggeri.github.com/RYB/</a></p>\n\n<p><strong>UPDATE 2:</strong> </p>\n\n<p>The Sciences Po Medialb have just released a tool called "I want Hue" that generate color palettes for data scientists. Using different color spaces and generating the palettes by using k-means clustering or force vectors ( repulsion graphs) The results from those methods are very good, they show the theory and an implementation in their web page. </p>\n\n<p><a href="http://tools.medialab.sciences-po.fr/iwanthue/index.php" rel="noreferrer">http://tools.medialab.sciences-po.fr/iwanthue/index.php</a></p>\n'}, {'answer_id': 12266311, 'author': 'motobói', 'author_id': 25612, 'author_profile': 'https://Stackoverflow.com/users/25612', 'pm_score': 5, 'selected': False, 'text': '<p>In javascript:</p>\n\n<pre><code>function pastelColors(){\n    var r = (Math.round(Math.random()* 127) + 127).toString(16);\n    var g = (Math.round(Math.random()* 127) + 127).toString(16);\n    var b = (Math.round(Math.random()* 127) + 127).toString(16);\n    return \'#\' + r + g + b;\n}\n</code></pre>\n\n<p>Saw the idea here: <a href="http://blog.functionalfun.net/2008/07/random-pastel-colour-generator.html" rel="noreferrer">http://blog.functionalfun.net/2008/07/random-pastel-colour-generator.html</a></p>\n'}, {'answer_id': 14810261, 'author': 'ChilledFlame', 'author_id': 2060983, 'author_profile': 'https://Stackoverflow.com/users/2060983', 'pm_score': 2, 'selected': False, 'text': '<pre><code>function fnGetRandomColour(iDarkLuma, iLightLuma) \n{       \n  for (var i=0;i&lt;20;i++)\n  {\n    var sColour = (\'ffffff\' + Math.floor(Math.random() * 0xFFFFFF).toString(16)).substr(-6);\n\n    var rgb = parseInt(sColour, 16);   // convert rrggbb to decimal\n    var r = (rgb &gt;&gt; 16) &amp; 0xff;  // extract red\n    var g = (rgb &gt;&gt;  8) &amp; 0xff;  // extract green\n    var b = (rgb &gt;&gt;  0) &amp; 0xff;  // extract blue\n\n    var iLuma = 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709\n\n\n    if (iLuma &gt; iDarkLuma &amp;&amp; iLuma &lt; iLightLuma) return sColour;\n  }\n  return sColour;\n} \n</code></pre>\n\n<p>For pastel, pass in higher luma dark/light integers - ie fnGetRandomColour(120, 250)</p>\n\n<p>Credits: all credits to \n  <a href="http://paulirish.com/2009/random-hex-color-code-snippets/" rel="nofollow">http://paulirish.com/2009/random-hex-color-code-snippets/</a>\n  stackoverflow.com/questions/12043187/how-to-check-if-hex-color-is-too-black</p>\n'}, {'answer_id': 18269541, 'author': 'LifeInTheTrees', 'author_id': 2040877, 'author_profile': 'https://Stackoverflow.com/users/2040877', 'pm_score': 3, 'selected': False, 'text': "<p>An answer that shouldn't be overlooked, because it's simple and presents advantages, is sampling of real life photos and paintings. sample as many random pixels as you want random colors on thumbnails of modern art pics, cezanne, van gogh, monnet, photos... the advantage is that you can get colors by theme and that they are organic colors. just put 20 - 30 pics in a folder and random sample a random pic every time.</p>\n\n<p>Conversion to HSV values is a widespread code algorithm for psychologically based palette. hsv is easier to randomize.</p>\n"}, {'answer_id': 21361663, 'author': 'm1ch4ls', 'author_id': 3024975, 'author_profile': 'https://Stackoverflow.com/users/3024975', 'pm_score': 2, 'selected': False, 'text': '<p>Here is quick and dirty color generator in C# (using \'RYB approach\' described in this <a href="http://threekings.tk/mirror/ryb_TR.pdf" rel="nofollow">article</a>). It\'s a rewrite from <a href="http://afriggeri.github.com/RYB/" rel="nofollow">JavaScript</a>.</p>\n\n<p><strong>Use:</strong></p>\n\n<pre><code>List&lt;Color&gt; ColorPalette = ColorGenerator.Generate(30).ToList();\n</code></pre>\n\n<p>First two colors tend to be white and a shade of black. I often skip them like this (using Linq):</p>\n\n<pre><code>List&lt;Color&gt; ColorsPalette = ColorGenerator\n            .Generate(30)\n            .Skip(2) // skip white and black\n            .ToList(); \n</code></pre>\n\n<p><strong>Implementation:</strong></p>\n\n<pre><code>public static class ColorGenerator\n{\n\n    // RYB color space\n    private static class RYB\n    {\n        private static readonly double[] White = { 1, 1, 1 };\n        private static readonly double[] Red = { 1, 0, 0 };\n        private static readonly double[] Yellow = { 1, 1, 0 };\n        private static readonly double[] Blue = { 0.163, 0.373, 0.6 };\n        private static readonly double[] Violet = { 0.5, 0, 0.5 };\n        private static readonly double[] Green = { 0, 0.66, 0.2 };\n        private static readonly double[] Orange = { 1, 0.5, 0 };\n        private static readonly double[] Black = { 0.2, 0.094, 0.0 };\n\n        public static double[] ToRgb(double r, double y, double b)\n        {\n            var rgb = new double[3];\n            for (int i = 0; i &lt; 3; i++)\n            {\n                rgb[i] = White[i]  * (1.0 - r) * (1.0 - b) * (1.0 - y) +\n                         Red[i]    * r         * (1.0 - b) * (1.0 - y) +\n                         Blue[i]   * (1.0 - r) * b         * (1.0 - y) +\n                         Violet[i] * r         * b         * (1.0 - y) +\n                         Yellow[i] * (1.0 - r) * (1.0 - b) *        y +\n                         Orange[i] * r         * (1.0 - b) *        y +\n                         Green[i]  * (1.0 - r) * b         *        y +\n                         Black[i]  * r         * b         *        y;\n            }\n\n            return rgb;\n        }\n    }\n\n    private class Points : IEnumerable&lt;double[]&gt;\n    {\n        private readonly int pointsCount;\n        private double[] picked;\n        private int pickedCount;\n\n        private readonly List&lt;double[]&gt; points = new List&lt;double[]&gt;();\n\n        public Points(int count)\n        {\n            pointsCount = count;\n        }\n\n        private void Generate()\n        {\n            points.Clear();\n            var numBase = (int)Math.Ceiling(Math.Pow(pointsCount, 1.0 / 3.0));\n            var ceil = (int)Math.Pow(numBase, 3.0);\n            for (int i = 0; i &lt; ceil; i++)\n            {\n                points.Add(new[]\n                {\n                    Math.Floor(i/(double)(numBase*numBase))/ (numBase - 1.0),\n                    Math.Floor((i/(double)numBase) % numBase)/ (numBase - 1.0),\n                    Math.Floor((double)(i % numBase))/ (numBase - 1.0),\n                });\n            }\n        }\n\n        private double Distance(double[] p1)\n        {\n            double distance = 0;\n            for (int i = 0; i &lt; 3; i++)\n            {\n                distance += Math.Pow(p1[i] - picked[i], 2.0);\n            }\n\n            return distance;\n        }\n\n        private double[] Pick()\n        {\n            if (picked == null)\n            {\n                picked = points[0];\n                points.RemoveAt(0);\n                pickedCount = 1;\n                return picked;\n            }\n\n            var d1 = Distance(points[0]);\n            int i1 = 0, i2 = 0;\n            foreach (var point in points)\n            {\n                var d2 = Distance(point);\n                if (d1 &lt; d2)\n                {\n                    i1 = i2;\n                    d1 = d2;\n                }\n\n                i2 += 1;\n            }\n\n            var pick = points[i1];\n            points.RemoveAt(i1);\n\n            for (int i = 0; i &lt; 3; i++)\n            {\n                picked[i] = (pickedCount * picked[i] + pick[i]) / (pickedCount + 1.0);\n            }\n\n            pickedCount += 1;\n            return pick;\n        }\n\n        public IEnumerator&lt;double[]&gt; GetEnumerator()\n        {\n            Generate();\n            for (int i = 0; i &lt; pointsCount; i++)\n            {\n                yield return Pick();\n            }\n        }\n\n        IEnumerator IEnumerable.GetEnumerator()\n        {\n            return GetEnumerator();\n        }\n    }\n\n    public static IEnumerable&lt;Color&gt; Generate(int numOfColors)\n    {\n        var points = new Points(numOfColors);\n\n        foreach (var point in points)\n        {\n            var rgb = RYB.ToRgb(point[0], point[1], point[2]);\n            yield return Color.FromArgb(\n                (int)Math.Floor(255 * rgb[0]),\n                (int)Math.Floor(255 * rgb[1]),\n                (int)Math.Floor(255 * rgb[2]));\n        }\n    }\n}\n</code></pre>\n'}, {'answer_id': 22363008, 'author': 'Petr Bugyík', 'author_id': 2875783, 'author_profile': 'https://Stackoverflow.com/users/2875783', 'pm_score': 2, 'selected': False, 'text': '<p>In php:\n</p>\n\n<pre><code>function pastelColors() {\n    $r = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);\n    $g = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);\n    $b = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);\n\n    return "#" . $r . $g . $b;\n}\n</code></pre>\n\n<p>source: <a href="https://stackoverflow.com/a/12266311/2875783">https://stackoverflow.com/a/12266311/2875783</a></p>\n'}, {'answer_id': 22743973, 'author': 'LifeInTheTrees', 'author_id': 2040877, 'author_profile': 'https://Stackoverflow.com/users/2040877', 'pm_score': 0, 'selected': False, 'text': "<p>I'd strongly recommend using a CG HSVtoRGB shader function, they are awesome... it gives you natural color control like a painter instead of control like a crt monitor, which you arent presumably!</p>\n\n<p>This is a way to make 1 float value. i.e. Grey, into 1000 ds of combinations of color and brightness and saturation etc:</p>\n\n<pre><code>int rand = a global color randomizer that you can control by script/ by a crossfader etc.\nfloat h = perlin(grey,23.3*rand)\nfloat s = perlin(grey,54,4*rand)\nfloat v = perlin(grey,12.6*rand)\n\nReturn float4 HSVtoRGB(h,s,v);\n</code></pre>\n\n<p>result is AWESOME COLOR RANDOMIZATION! it's not natural but it uses natural color gradients and it looks organic and controlleably irridescent / pastel parameters.</p>\n\n<p>For perlin, you can use this function, it is a fast zig zag version of perlin.</p>\n\n<pre><code>function  zig ( xx : float ): float{    //lfo nz -1,1\n    xx= xx+32;\n    var x0 = Mathf.Floor(xx);\n    var x1 = x0+1;\n    var v0 = (Mathf.Sin (x0*.014686)*31718.927)%1;\n    var v1 = (Mathf.Sin  (x1*.014686)*31718.927)%1;\n    return Mathf.Lerp( v0 , v1 , (xx)%1 )*2-1;\n}\n</code></pre>\n"}, {'answer_id': 28500955, 'author': 'Dave_L', 'author_id': 1163569, 'author_profile': 'https://Stackoverflow.com/users/1163569', 'pm_score': 2, 'selected': False, 'text': "<p>David Crow's method in an R two-liner:</p>\n\n<pre><code>GetRandomColours &lt;- function(num.of.colours, color.to.mix=c(1,1,1)) {\n  return(rgb((matrix(runif(num.of.colours*3), nrow=num.of.colours)*color.to.mix)/2))\n}\n</code></pre>\n"}, {'answer_id': 31594284, 'author': 'Ephraim', 'author_id': 599402, 'author_profile': 'https://Stackoverflow.com/users/599402', 'pm_score': 0, 'selected': False, 'text': '<p>Here is something I wrote for a site I made. It will auto-generate a random flat background-color for any div with the class <code>.flat-color-gen</code>. Jquery is only required for the purposes of adding css to the page; it\'s not required for the main part of this, which is the <code>generateFlatColorWithOrder()</code> method.</p>\n\n<p><a href="https://jsfiddle.net/ephraimrothschild/kdprqauf/3/" rel="nofollow">JsFiddle Link</a></p>\n\n<pre class="lang-js prettyprint-override"><code>(function($) {\n    function generateFlatColorWithOrder(num, rr, rg, rb) {\n        var colorBase = 256;\n        var red = 0;\n        var green = 0;\n        var blue = 0;\n        num = Math.round(num);\n        num = num + 1;\n        if (num != null) {\n\n            red = (num*rr) % 256;\n            green = (num*rg) % 256;\n            blue = (num*rb) % 256;\n        }\n        var redString = Math.round((red + colorBase) / 2).toString();\n        var greenString = Math.round((green + colorBase) / 2).toString();\n        var blueString = Math.round((blue + colorBase) / 2).toString();\n        return "rgb("+redString+", "+greenString+", "+blueString+")";\n        //return \'#\' + redString + greenString + blueString;\n    }\n\n    function generateRandomFlatColor() {\n        return generateFlatColorWithOrder(Math.round(Math.random()*127));\n    }\n\n    var rr = Math.round(Math.random()*1000);\n    var rg = Math.round(Math.random()*1000);\n    var rb = Math.round(Math.random()*1000);\n    console.log("random red: "+ rr);\n    console.log("random green: "+ rg);\n    console.log("random blue: "+ rb);\n    console.log("----------------------------------------------------");\n    $(\'.flat-color-gen\').each(function(i, obj) {\n        console.log(generateFlatColorWithOrder(i));\n        $(this).css("background-color",generateFlatColorWithOrder(i, rr, rg, rb).toString());\n    });\n})(window.jQuery);\n</code></pre>\n'}, {'answer_id': 31629356, 'author': 'Sceptic', 'author_id': 3340062, 'author_profile': 'https://Stackoverflow.com/users/3340062', 'pm_score': 1, 'selected': False, 'text': "<h2>JavaScript adaptation of David Crow's original answer, IE and Nodejs specific code included.</h2>\n\n<pre><code>generateRandomComplementaryColor = function(r, g, b){\n    //--- JavaScript code\n    var red = Math.floor((Math.random() * 256));\n    var green = Math.floor((Math.random() * 256));\n    var blue = Math.floor((Math.random() * 256));\n    //---\n\n    //--- Extra check for Internet Explorers, its Math.random is not random enough.\n    if(!/MSIE 9/i.test(navigator.userAgent) &amp;&amp; !/MSIE 10/i.test(navigator.userAgent) &amp;&amp; !/rv:11.0/i.test(navigator.userAgent)){\n        red = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));\n        green = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));\n        blue = Math.floor((('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]) * 256));\n    };\n    //---\n\n    //--- nodejs code\n    /*\n    crypto = Npm.require('crypto');\n    red = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);\n    green = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);\n    blue = Math.floor((parseInt(crypto.randomBytes(8).toString('hex'), 16)) * 1.0e-19 * 256);\n    */\n    //---\n\n    red = (red + r)/2;\n    green = (green + g)/2;\n    blue = (blue + b)/2;\n\n    return 'rgb(' + Math.floor(red) + ', ' + Math.floor(green) + ', ' + Math.floor(blue) + ')';\n}\n</code></pre>\n\n<p><strong>Run the function using:</strong></p>\n\n<pre><code>generateRandomComplementaryColor(240, 240, 240);\n</code></pre>\n"}, {'answer_id': 32124551, 'author': 'InternalFX', 'author_id': 654518, 'author_profile': 'https://Stackoverflow.com/users/654518', 'pm_score': 2, 'selected': False, 'text': '<p>Use <a href="https://github.com/internalfx/distinct-colors" rel="nofollow">distinct-colors</a>.</p>\n\n<p>Written in javascript.</p>\n\n<p>It generates a palette of <em>visually</em> distinct colors.</p>\n\n<p>distinct-colors is highly configurable:</p>\n\n<ul>\n<li>Choose how many colors are in the palette</li>\n<li>Restrict the hue to a specific range</li>\n<li>Restrict the chroma (saturation) to a specific range</li>\n<li>Restrict the lightness to a specific range</li>\n<li>Configure general quality of the palette</li>\n</ul>\n'}, {'answer_id': 41068169, 'author': 'Moriarty', 'author_id': 273101, 'author_profile': 'https://Stackoverflow.com/users/273101', 'pm_score': 3, 'selected': False, 'text': '<p>I\'ve had success using <a href="http://devmag.org.za/2012/07/29/how-to-choose-colours-procedurally-algorithms/" rel="noreferrer">TriadMixing</a> and <a href="https://en.wikipedia.org/wiki/Color_difference#CIE94" rel="noreferrer">CIE94</a> to avoid similar colors. The following image uses input colors red, yellow, and white. See <a href="https://github.com/jamesmoriarty/scorched_earth" rel="noreferrer">here</a>.</p>\n\n<p><a href="https://i.stack.imgur.com/kOZiV.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/kOZiV.jpg" alt="TriadMixing + CIE94"></a></p>\n'}]
['https://Stackoverflow.com/questions/43044', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3415/']