有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java降低安卓中颜色的亮度?

我正在与AChart Engine合作

并在运行时动态设置颜色

这是我的代码,

private int getRandomColor() {

    Random rnd = new Random(); 
    int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));   
    return color;
 }

我的问题是我也(随机)得到了亮度颜色

那么如何避免只使用亮度颜色呢

我的输出:

enter image description here


共 (2) 个答案

  1. # 1 楼答案

    使用HSB(色调、饱和度、亮度)颜色空间可能比使用RGB更好。你可以用getHSBColor()代替,并对饱和度和亮度进行硬编码,使它们保持一致,只需随机化颜色即可

    private Color getRandomColor() {
        Random rnd = new Random();
        return Color.getHSBColor(rnd.nextFloat(), 0.5f, 0.5f);
    }
    
  2. # 2 楼答案

    如果我理解正确,你想避免鲜艳的颜色,你必须改变这一行:

    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));