我正在寻找一种简单的算法来生成大量随机的、美观的颜色。所以没有疯狂的霓虹灯颜色,让人联想到粪便的颜色等等。

我已经找到了这个问题的解决方案,但它们依赖于替代调色板而不是RGB。 我宁愿直接使用RGB,而不是来回映射。这些其他解决方案也最多只能生成32种左右令人愉悦的随机颜色。

任何想法都很好。


当前回答

在php中:

function pastelColors() {
    $r = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);
    $g = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);
    $b = dechex(round(((float) rand() / (float) getrandmax()) * 127) + 127);

    return "#" . $r . $g . $b;
}

来源:https://stackoverflow.com/a/12266311/2875783

其他回答

你可以让它们在一定的亮度范围内。这样可以稍微控制“霓虹灯”颜色的数量。例如,如果“亮度”

brightness = sqrt(R^2+G^2+B^2)

在一定的高度范围内,它就会被洗掉,变成浅色。相反,如果它在某个下限内,它就会更暗。这将消除任何疯狂的、突出的颜色,如果你选择一个非常高或非常低的边界,它们都将非常接近白色或黑色。

我会使用色轮并在任意位置加上黄金角(137.5度)

http://en.wikipedia.org/wiki/Golden_angle

为了每次都能得到不重叠的不同颜色。

调整颜色轮的亮度,你也可以得到不同的亮/暗颜色组合。

我发现这篇博客文章很好地解释了这个问题和使用黄金比例的解决方案。

http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/

更新:我刚刚发现了另一种方法:

它被称为RYB(红黄蓝)方法,本文对其进行了描述:

http://threekings.tk/mirror/ryb_TR.pdf

作为“油漆灵感的颜色合成”。

算法生成颜色,并选择每个新颜色以最大化其与先前选择的颜色的欧几里得距离。

在这里你可以找到一个很好的javascript实现:

http://afriggeri.github.com/RYB/

更新2:

巴黎政治学院刚刚发布了一个名为“I want Hue”的工具,可以为数据科学家生成调色板。使用不同的颜色空间,并通过使用k-means聚类或力向量(斥力图)生成调色板,这些方法的结果非常好,他们在他们的网页上展示了理论和实现。

http://tools.medialab.sciences-po.fr/iwanthue/index.php

David Crow在R双行代码中的方法:

GetRandomColours <- function(num.of.colours, color.to.mix=c(1,1,1)) {
  return(rgb((matrix(runif(num.of.colours*3), nrow=num.of.colours)*color.to.mix)/2))
}

从算法上很难得到你想要的东西——人们研究颜色理论已经很长时间了,他们甚至不知道所有的规则。

然而,有一些规则可以用来剔除糟糕的颜色组合(例如,有冲突的颜色和选择互补的颜色的规则)。

我建议你去图书馆的艺术区看看关于色彩理论的书籍,在你尝试一个好颜色之前更好地理解什么是好颜色——看起来你甚至不知道为什么某些组合有效,而另一些则不行。

亚当

下面是c#中的一个快速而肮脏的颜色生成器(使用本文中描述的“RYB方法”)。这是JavaScript重写的。

Use:

List<Color> ColorPalette = ColorGenerator.Generate(30).ToList();

前两种颜色往往是白色和暗色的黑色。我经常这样跳过它们(使用Linq):

List<Color> ColorsPalette = ColorGenerator
            .Generate(30)
            .Skip(2) // skip white and black
            .ToList(); 

实现:

public static class ColorGenerator
{

    // RYB color space
    private static class RYB
    {
        private static readonly double[] White = { 1, 1, 1 };
        private static readonly double[] Red = { 1, 0, 0 };
        private static readonly double[] Yellow = { 1, 1, 0 };
        private static readonly double[] Blue = { 0.163, 0.373, 0.6 };
        private static readonly double[] Violet = { 0.5, 0, 0.5 };
        private static readonly double[] Green = { 0, 0.66, 0.2 };
        private static readonly double[] Orange = { 1, 0.5, 0 };
        private static readonly double[] Black = { 0.2, 0.094, 0.0 };

        public static double[] ToRgb(double r, double y, double b)
        {
            var rgb = new double[3];
            for (int i = 0; i < 3; i++)
            {
                rgb[i] = White[i]  * (1.0 - r) * (1.0 - b) * (1.0 - y) +
                         Red[i]    * r         * (1.0 - b) * (1.0 - y) +
                         Blue[i]   * (1.0 - r) * b         * (1.0 - y) +
                         Violet[i] * r         * b         * (1.0 - y) +
                         Yellow[i] * (1.0 - r) * (1.0 - b) *        y +
                         Orange[i] * r         * (1.0 - b) *        y +
                         Green[i]  * (1.0 - r) * b         *        y +
                         Black[i]  * r         * b         *        y;
            }

            return rgb;
        }
    }

    private class Points : IEnumerable<double[]>
    {
        private readonly int pointsCount;
        private double[] picked;
        private int pickedCount;

        private readonly List<double[]> points = new List<double[]>();

        public Points(int count)
        {
            pointsCount = count;
        }

        private void Generate()
        {
            points.Clear();
            var numBase = (int)Math.Ceiling(Math.Pow(pointsCount, 1.0 / 3.0));
            var ceil = (int)Math.Pow(numBase, 3.0);
            for (int i = 0; i < ceil; i++)
            {
                points.Add(new[]
                {
                    Math.Floor(i/(double)(numBase*numBase))/ (numBase - 1.0),
                    Math.Floor((i/(double)numBase) % numBase)/ (numBase - 1.0),
                    Math.Floor((double)(i % numBase))/ (numBase - 1.0),
                });
            }
        }

        private double Distance(double[] p1)
        {
            double distance = 0;
            for (int i = 0; i < 3; i++)
            {
                distance += Math.Pow(p1[i] - picked[i], 2.0);
            }

            return distance;
        }

        private double[] Pick()
        {
            if (picked == null)
            {
                picked = points[0];
                points.RemoveAt(0);
                pickedCount = 1;
                return picked;
            }

            var d1 = Distance(points[0]);
            int i1 = 0, i2 = 0;
            foreach (var point in points)
            {
                var d2 = Distance(point);
                if (d1 < d2)
                {
                    i1 = i2;
                    d1 = d2;
                }

                i2 += 1;
            }

            var pick = points[i1];
            points.RemoveAt(i1);

            for (int i = 0; i < 3; i++)
            {
                picked[i] = (pickedCount * picked[i] + pick[i]) / (pickedCount + 1.0);
            }

            pickedCount += 1;
            return pick;
        }

        public IEnumerator<double[]> GetEnumerator()
        {
            Generate();
            for (int i = 0; i < pointsCount; i++)
            {
                yield return Pick();
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public static IEnumerable<Color> Generate(int numOfColors)
    {
        var points = new Points(numOfColors);

        foreach (var point in points)
        {
            var rgb = RYB.ToRgb(point[0], point[1], point[2]);
            yield return Color.FromArgb(
                (int)Math.Floor(255 * rgb[0]),
                (int)Math.Floor(255 * rgb[1]),
                (int)Math.Floor(255 * rgb[2]));
        }
    }
}