正态分布的随机颜色偏差
我想根据原始颜色和正态分布生成随机颜色,这样我就可以创建一个随机纹理,告诉脚本“好吧,这是您应该使用的一般颜色,但如果您现在差异很大然后,那就好了”。
我正在用 JavaScript 编写,使用 Box-Muller 变换。但是,如果我对每个 R、G 和 B 值独立使用正态分布,则会得到不需要的尖峰。例如,如果我的基色是浅灰色,我会得到非常红、非常绿或非常蓝的像素。到目前为止,这是我的脚本(“0, 255”是最小值和最大值):
function randomHex(hex, deviation){
R = HexToR(hex);
G = HexToG(hex);
B = HexToB(hex);
R = randomNormal(R, deviation, 0, 255);
G = randomNormal(G, deviation, 0, 255);
B = randomNormal(B, deviation, 0, 255);
return RGBtoHex(R,G,B);
}
我认为我正在寻找的是以某种方式在 RGB 颜色的 3D 空间中定义一个点,然后选择一个(均匀分布的)随机点一个以定义点为中心、半径按正态分布随机生成的球体。
无论如何,这就是理论。我只是不知道如何在 JavaScript 中实现这一点。
我将不胜感激任何指点!
I'd like to generate a random color, based on an original color and a normal distribution, so I can create a random texture telling the script "Okay, this is the general color you should be using, but if you differ greatly every now and then, that's fine".
I'm writing in JavaScript, using the Box-Muller transform. But if I use the normal distribution independently on every R, G and B value, I get undesired spikes. If my base color is a light grey, for example, I get pixels that are very red, very green or very blue. This is my script so far ("0, 255" being min and max):
function randomHex(hex, deviation){
R = HexToR(hex);
G = HexToG(hex);
B = HexToB(hex);
R = randomNormal(R, deviation, 0, 255);
G = randomNormal(G, deviation, 0, 255);
B = randomNormal(B, deviation, 0, 255);
return RGBtoHex(R,G,B);
}
I think what I'm looking for is to somehow define a point in the 3D space of RGB color and then select a (uniformly distributed) random point on a sphere who's center is the defined point and who's radius is randomly generated with normal distribution.
That's the theory, anyway. I just don't have a clue how to make that work in JavaScript.
I'd be thankful for any pointers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试使用 HSV(或者可能是 HSL)而不是 RGB。您需要一个函数来转换回 RGB(请参阅此处了解例子)。在本例中,H 是色调,您的值是颜色环上的某个位置。 S和V分别控制饱和度和明度/亮度。我认为这会给你更多的控制权来获得你想要的发行版。有关维基百科的更多信息,请参见此处。
I'd try using HSV (or perhaps HSL) instead of RGB. You'll need a function to convert back to RGB (see here for an example). In this case, H is the hue, and your value is some position on a circle of colors. S and V control saturation and value/brightness respectively. I think this will give you more control to get the distributions you're after. More info on wikipedia here too.