Androide Farbe zwischen zwei Farben, basierend auf Prozent?

Ich würde gerne berechnen, die je nach Farbe einen prozentualen Wert:

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

Wie kann ich berechnen, dass die Letzte, was? Wäre es OK, wenn gelb angezeigt wird, bei 50%.

Habe ich versucht, dieses:

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

Gut das funktioniert, aber ich würde gerne die Farben um die 50% mehr lightend wie verwende ich Sie auf einem grauen hintergrund.. wie kann ich erreichen, dass?

Dank!

UPDATE
Ich habe versucht, zu konvertieren, YUV, wie es wurde vorgeschlagen, in den Kommentaren. Aber ich habe immer noch das gleiche problem, dass bei 50% ist es zu dunkel.
Weitere in diese Lösung habe ich bei <5% jetzt weiß als Farbe. Wenn ich nicht berechnen kann float y = ave(...);, aber nehmen Sie nur float y = c0.y es ist ein wenig besser, aber at <20% habe ich dann cyan Farbe ... ich bin nicht so viel in Farbe-Formate :-/
Vielleicht mache ich etwas falsch bei der Berechnung? Die Konstanten sind entnommen aus 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));
    }
}

InformationsquelleAutor der Frage Stuck | 2010-12-11

Schreibe einen Kommentar