Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3500228
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:42:29+00:00 2026-05-18T12:42:29+00:00

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

  • 0

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));
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T12:42:29+00:00Added an answer on May 18, 2026 at 12:42 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to calculate total order amount in the previous month. I got
I would like to calculate my total order amount in the previous week. I
Good morning, I have mysql queries where I would like to calculate percentage of
I would like to calculate, in PHP, the percentage of a number. For example:
I would like to calculate the similarity between users, which is reciprocal. similarity[:user1][:user2] ==
I would like to calculate a timestamp based on the input date. I would
I would like to calculate a density function of a distribution whose characteristics function
Lets say I would like to calculate percents and do: double n = ...;
I have a matrix mat and would like to calculate the mean of the
I'm developing an AI sandbox and I would like to calculate what every living

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.