如何动态计算颜色列表?

如何动态计算颜色列表?,第1张

如何动态计算颜色列表?

我的解决方案的另一个版本,范围:

List<int> getUniqueColors(int amount) {    final int lowerLimit = 0x10;    final int upperLimit = 0xE0;        final int colorStep = (upperLimit-lowerLimit)/Math.pow(amount,1f/3);    final List<int> colors = new ArrayList<int>(amount);    for (int R = lowerLimit;R < upperLimit; R+=colorStep)        for (int G = lowerLimit;G < upperLimit; G+=colorStep) for (int B = lowerLimit;B < upperLimit; B+=colorStep) {     if (colors.size() >= amount) { //The calculated step is not very precise, so this safeguard is appropriate         return colors;     } else {         int color = (R<<16)+(G<<8)+(B);         colors.add(color);     }     }    return colors;}

这是一个更高级的功能,因为它会生成彼此之间尽可能不同的颜色(类似于@aiiobe所做的事情)。

通常,我们将范围划分为红色,绿色和蓝色三个子范围,计算我们需要迭代多少步(通过应用pow(range,1f / 3))并对其进行迭代。

例如,给定数字3,它将生成

0x0000B1, 0x00B100, 0x00B1B1
。对于10号,它将是:
0x000076, 0x0000EC,0x007600, 0x007676, 0x0076EC, 0x00EC00, 0x00EC76, 0x00ECEC, 0x760000,0x760076



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5489284.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存