【问题标题】:Reduce brightness of colors in android?降低android中颜色的亮度?
【发布时间】:2014-12-01 13:28:10
【问题描述】:

我正在使用 AChart 引擎。

并在运行时动态设置颜色。

这是我的代码,

private int getRandomColor() {

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

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

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

我的输出:

【问题讨论】:

    标签: java android colors


    【解决方案1】:

    您最好使用 HSB(色相、饱和度、亮度)色彩空间而不是 RGB。您可以改用 getHSBColor() 并硬编码饱和度和亮度以保持它们相同,只是随机化颜色。

    private Color getRandomColor() {
        Random rnd = new Random();
        return Color.getHSBColor(rnd.nextFloat(), 0.5f, 0.5f);
    }
    

    【讨论】:

    • private Color getRandomColor() { Random rnd = new Random(); return Color.getHSB(rnd.nextFloat(), 0.5, 0.5); } 然后你可以编辑这两个 0.5 的值来品尝。
    • 方法 getHSB(float, double, double) 未为 Color 类型定义
    【解决方案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));
    

    【讨论】:

    • @Rich 的回答提供了更灵活的方法来控制所需的参数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多