Java 降低android中颜色的亮度?
我正在使用AChart引擎 并在运行时动态设置颜色 这是我的代码,Java 降低android中颜色的亮度?,java,android,colors,Java,Android,Colors,我正在使用AChart引擎 并在运行时动态设置颜色 这是我的代码, private int getRandomColor() { Random rnd = new Random(); int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); return color; } 我的问题是我也(随机)得到了亮度颜色。 那么如何避免只使用亮度颜色呢? 我的输出
private int getRandomColor() {
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
return color;
}
我的问题是我也(随机)得到了亮度颜色。
那么如何避免只使用亮度颜色呢?
我的输出:
您最好使用HSB(色调、饱和度、亮度)颜色空间,而不是RGB。你可以改为使用硬编码的饱和度和亮度,以保持它们都一样,只是随机的颜色
private Color getRandomColor() {
Random rnd = new Random();
return Color.getHSBColor(rnd.nextFloat(), 0.5f, 0.5f);
}
如果我对您的理解正确,并且您希望避免鲜艳的颜色,则必须更改此行:
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
对这些行:
int MAX_LEVEL = 128;
int color = Color.argb(255, rnd.nextInt(MAX_LEVEL), rnd.nextInt(MAX_LEVEL), rnd.nextInt(MAX_LEVEL));
这将导致只生成较暗的颜色
如果你只想要浅色,就这样做:
int BASE_LEVEL = 128;
int MAX_LEVEL = 128;
int color = Color.argb(255, BASE_LEVEL + rnd.nextInt(MAX_LEVEL), BASE_LEVEL + rnd.nextInt(MAX_LEVEL), BASE_LEVEL + rnd.nextInt(MAX_LEVEL));
@Rich的答案提供了更灵活的方法来控制所需的参数。
private Color getRandomColor(){Random rnd=new Random();return Color.getHSB(rnd.nextFloat(),0.5,0.5);}
然后您可以编辑两个0.5值来尝试。类型Color的方法getHSB(float,double,double)未定义