android color between two colors, based on percentage?

后端 未结 9 1833
悲哀的现实
悲哀的现实 2020-11-29 23:17

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

float percentage = x/total;
int color;
if (percentage >= 0.95) {
  color = Color.GREE         


        
9条回答
  •  盖世英雄少女心
    2020-11-29 23:54

    Here is a pseudocode function that interpolates linearly between 2 colors (staying in RGB space). I'm using a class called Color here instead of ints for clarity.

    bAmount is between 0 and 1 (for interpolation)

    Color interpolate(Color colorA, Color colorB, float bAmount) {
        Color colorOut;
        float aAmount = 1.0 - bAmount;
        colorOut.r =  colorA.r * aAmount + colorB.r * bAmount;
        colorOut.g =  colorA.g * aAmount + colorB.g * bAmount;
        colorOut.b =  colorA.b * aAmount + colorB.b * bAmount;
        return colorOut;
    }
    

提交回复
热议问题