两种颜色之间的android颜色,基于百分比? [英] android color between two colors, based on percentage?

查看:21
本文介绍了两种颜色之间的android颜色,基于百分比?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据百分比值计算颜色:

I would like to calculate the color depending on a percentage value:

float percentage = x/total;
int color;
if (percentage >= 0.95) {
  color = Color.GREEN;
} else if (percentage <= 0.5) {
  color = Color.RED;
} else {
  // color = getColor(Color.Green, Color.RED, percentage);
}

我如何计算最后一件事?如果黄色出现在 50% 就可以了.

How can I calculate that last thing? It would be OK if yellow appears at 50%.

我试过了:

private int getColor(int c0, int c1, float p) {
    int a = ave(Color.alpha(c0), Color.alpha(c1), p);
    int r = ave(Color.red(c0), Color.red(c1), p);
    int g = ave(Color.green(c0), Color.green(c1), p);
    int b = ave(Color.blue(c0), Color.blue(c1), p);
    return Color.argb(a, r, g, b);
}
private int ave(int src, int dst, float p) {
    return src + java.lang.Math.round(p * (dst - src));
}

这是可行的,但我希望 50% 左右的颜色更淡,因为我在灰色背景上使用它们..我该如何实现?

Well this works, but I would like the colors at around 50% being more lightend as I use them on a grey background.. how can I accomplish that?

谢谢!

更新我尝试像评论中建议的那样转换为 YUV.但我仍然有同样的问题,在 50% 时它会变黑.在这个解决方案中,我现在有 <5% 的白色作为颜色.如果我不计算 float y = ave(...);,而只计算 float y = c0.y 它会好一点,但 <20%然后我有青色......我不太喜欢颜色格式:-/也许我在计算中做错了什么?常量取自维基百科

UPDATE I tried to convert to YUV like it was suggested in the comments. But I still have the same problem that at 50% it's to dark. Additional in this solution I have at <5% now white as color. If I do not calculate float y = ave(...);, but just take float y = c0.y it's a little better, but at <20% I have then cyan color ... I'm not so much into color-formats :-/ Maybe I'm doing something wrong in the calculation? The constants are taken from Wikipedia

public class ColorUtils {

    private static class Yuv {
        public float y;
        public float u;
        public float v;

        public Yuv(int c) {
            int r = Color.red(c);
            int g = Color.green(c);
            int b = Color.blue(c);
            this.y = 0.299f * r + 0.587f * g + 0.114f * b;
            this.u = (b - y) * 0.493f;
            this.v = (r - y) * 0.877f;
        }
    }

    public static int getColor(int color0, int color1, float p) {
        Yuv c0 = new Yuv(color0);
        Yuv c1 = new Yuv(color1);
        float y = ave(c0.y, c1.y, p);
        float u = ave(c0.u, c1.u, p);
        float v = ave(c0.v, c1.v, p);

        int b = (int) (y + u / 0.493f);
        int r = (int) (y + v / 0.877f);
        int g = (int) (1.7f * y - 0.509f * r - 0.194f * b);

        return Color.rgb(r, g, b);
    }

    private static float ave(float src, float dst, float p) {
        return src + Math.round(p * (dst - src));
    }
}

推荐答案

好的,在转换为 yuv、hsv 等 pp 2 小时后...我放弃了.我现在就是这样做的:

Ok, after 2 hours of converting to yuv, hsv, etc pp... I give up. I now do it just like this:

public class ColorUtils {
    private static int FIRST_COLOR = Color.GREEN;
    private static int SECOND_COLOR = Color.YELLOW;
    private static int THIRD_COLOR = Color.RED;

    public static int getColor(float p) {
        int c0;
        int c1;
        if (p <= 0.5f) {
            p *= 2;
            c0 = FIRST_COLOR;
            c1 = SECOND_COLOR;
        } else {
            p = (p - 0.5f) * 2;
            c0 = SECOND_COLOR;
            c1 = THIRD_COLOR;
        }
        int a = ave(Color.alpha(c0), Color.alpha(c1), p);
        int r = ave(Color.red(c0), Color.red(c1), p);
        int g = ave(Color.green(c0), Color.green(c1), p);
        int b = ave(Color.blue(c0), Color.blue(c1), p);
        return Color.argb(a, r, g, b);
    }

    private static int ave(int src, int dst, float p) {
        return src + java.lang.Math.round(p * (dst - src));
    }
}

通过明确使用黄色作为中间色,生成的颜色更亮:-)

By explicity using yellow as the middle color, the generated colors are brighter :-)

无论如何..如果有人有其他好的解决方案,我将不胜感激.

Anyway.. if someone has a good other solution, I would appreciate it.

这篇关于两种颜色之间的android颜色,基于百分比?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆