两种颜色之间的android颜色的基础上,比例是多少? [英] android color between two colors, based on percentage?

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

问题描述

我想计算的颜色取决于百分比值:

 浮动比例= X /总;
INT的颜色;
如果(百分比&GT = 0.95){
  颜色= Color.GREEN;
}否则如果(百分比&其中; = 0.5){
  颜色= Color.RED;
} 其他 {
  //颜色=的getColor(Color.Green,Color.RED,百分比);
}
 

我怎么能计算出最后一件事?如果黄色出现在50%这将是美好的。

我尝试这样做:

 私人诠释的getColor(INT C0,C1 INT,浮点P){
    int的一个= AVE(Color.alpha(C0),Color.alpha(C1)中,p);
    INT R = AVE(Color.red(C0),Color.red(C1)中,p);
    INT克= AVE(Color.green(C0),Color.green(C1)中,p);
    INT B = AVE(Color.blue(C0),Color.blue(C1)中,p);
    返回Color.argb(A,R,G,B);
}
私人诠释AVE(INT SRC,诠释DST,持股量P){
    返回SRC + java.lang.Math.round(P *(DST  -  SRC));
}
 

嗯,这工作,但我喜欢的颜色,在50%左右,更加lightend因为我用他们在灰色的背景。我怎么能做到呢?

谢谢!

更新 我试图转换为YUV像有人建议的意见。但我仍然有同样的问题,在50%是暗。 另外在此解决方案我在小于5%,现在白的颜色。如果我没有计算浮法Y = AVE(...); ,但只取浮动Y = c0.y 这是好一点,但小于20%,我有那么青色......我没有那么多为颜色格式: - / 也许我做一些错误的计算?这些常量来源于维基百科

 公共类ColorUtils {

    私有静态类YUV {
        公众持股量ÿ;
        公众持股量U;
        公众持股量伏;

        公共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;
        }
    }

    公共静态INT的getColor(INT COLOR0,INT颜色1,浮法P){
        YUV C0 =新的YUV(COLOR0);
        YUV C1 =新的YUV(颜色1);
        浮动Y = AVE(c0.y,c1.y,P);
        浮U = AVE(c0.u,c1.u,P);
        浮ν= 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);

        返回Color.rgb(R,G,B);
    }

    私有静态浮动AVE(SRC浮动,浮动DST,持股量P){
        返回SRC + Math.round(P *(DST  -  SRC));
    }
}
 

解决方案

好了2个小时转换为YUV,HSV等PP后......我放弃了。我现在做的只是这样的:

 公共类ColorUtils {
    私有静态诠释FIRST_COLOR = Color.GREEN;
    私有静态诠释SECOND_COLOR = Color.YELLOW;
    私有静态诠释THIRD_COLOR = Color.RED;

    公共静态INT的getColor(浮点P){
        INT C0;
        INT C1;
        如果(P&其中; = 0.5F){
            P * = 2;
            C0 = FIRST_COLOR;
            C1 = SECOND_COLOR;
        } 其他 {
            p值=(对 -  0.5F)* 2;
            C0 = SECOND_COLOR;
            C1 = THIRD_COLOR;
        }
        int的一个= AVE(Color.alpha(C0),Color.alpha(C1)中,p);
        INT R = AVE(Color.red(C0),Color.red(C1)中,p);
        INT克= AVE(Color.green(C0),Color.green(C1)中,p);
        INT B = AVE(Color.blue(C0),Color.blue(C1)中,p);
        返回Color.argb(A,R,G,B);
    }

    私有静态诠释AVE(INT SRC,诠释DST,持股量P){
        返回SRC + java.lang.Math.round(P *(DST  -  SRC));
    }
}
 

通过明确地使用黄色为中间色,产生的颜色是亮: - )

反正..如果有人有一个很好的解决方案,其他的,我会的AP preciate吧。

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

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

I tried this:

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

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?

Thanks!

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

解决方案

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天全站免登陆