【问题标题】:android color between two colors, based on percentage?两种颜色之间的android颜色,基于百分比?
【发布时间】:2011-05-23 19:34:00
【问题描述】:

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

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%就可以了。

我试过这个:

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% 左右的颜色更亮,因为我在灰色背景上使用它们。我该如何实现?

谢谢!

更新 我尝试像 cmets 中建议的那样转换为 YUV。但我仍然有同样的问题,即 50% 时天黑了。 在这个解决方案中,我现在有 float y = ave(...);,而只是采用float y = c0.y,它会好一点,但在

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 并比较亮度值?
  • 是的,我将其编辑为问题...那里是同样的问题

标签: java android colors


【解决方案1】:

您可以尝试使用来自 android API 的 ArgbEvaluator 类:http://developer.android.com/reference/android/animation/ArgbEvaluator.html

new ArgbEvaluator().evaluate(0.75, 0x00ff00, 0xff0000);

请注意,Alpha 通道计算中存在错误 (http://code.google.com/p/android/issues/detail?id=36158),因此您应该使用没有 Alpha 值的值。

【讨论】:

  • 这个答案+1。将结果转换为 int:int color = (Integer) new ArgbEvaluator().evaluate(0.75, 0x00ff00, 0xff0000);
  • 请注意,该类需要 API 级别 11+!
  • 要为此使用颜色资源,请使用 ContextCompat.getColor(context, R.color.my_color) 而不仅仅是 R.color.my_color int
  • 我想知道为什么他们让 ArgbEvaluator 使用对象而不是整数。动画期间如此多的演员和包围是不必要的开销。
  • 导入android.animation.ArgbEvaluator;
【解决方案2】:

我的 0.02 美元,我找到了这个答案并编写了正确的解决方案。 (感谢 Alnitak 提供 HSV 提示!)

对于复制+粘贴:

  private float interpolate(float a, float b, float proportion) {
    return (a + ((b - a) * proportion));
  }

  /** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
  private int interpolateColor(int a, int b, float proportion) {
    float[] hsva = new float[3];
    float[] hsvb = new float[3];
    Color.colorToHSV(a, hsva);
    Color.colorToHSV(b, hsvb);
    for (int i = 0; i < 3; i++) {
      hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
    }
    return Color.HSVToColor(hsvb);
  }

【讨论】:

  • 应该注意,这不会处理 Alpha 通道。来自Color.colorToHSV docs:“color - 要转换的 argb 颜色。忽略 alpha 分量。”
  • 我在这里更新了您处理 alpha 的答案:stackoverflow.com/a/38536770/1650674
  • 几乎,在蓝色和红色之间插值时我得到黄色但想要粉红色。
【解决方案3】:

作为更新的解决方案,您可以使用来自Android supportAndroidX API 的ColorUtils#blendARGB

val startColor = ContextCompat.getColor(context, R.color.white)
val endColor = ContextCompat.getColor(context, R.color.yellow)
ColorUtils.blendARGB(startColor, endColor, 0.75)

【讨论】:

  • 短而清晰的作品就像一个魅力!
【解决方案4】:

这样的插值最好在 HSL or HSV 颜色空间(而不是 YUV)中完成。

中间颜色看起来“浑浊”的原因是,如果你只是线性地增加红色 (#ff0000),同时减少绿色 (#00ff00),中间颜色最终会变成 #808000而不是#ffff00

相反,找到与您的起始颜色对应的 HSL(或 HSV),并为结束颜色找到相同的颜色。在那个色彩空间中插值,然后将每个点再次转换回 RGB。

由于完全饱和的红色和绿色的SL(或V)值相同,因此只有H(色调)变量会发生变化,从而产生 的正确效果颜色的光谱

【讨论】:

    【解决方案5】:

    我只是想更新 Mark Renouf's answer 来处理 Alpha 通道:

    private float interpolate(float a, float b, float proportion) {
        return (a + ((b - a) * proportion));
    }
    
    /**
     * Returns an interpolated color, between <code>a</code> and <code>b</code>
     * proportion = 0, results in color a
     * proportion = 1, results in color b
     */
    private int interpolateColor(int a, int b, float proportion) {
    
        if (proportion > 1 || proportion < 0) {
            throw new IllegalArgumentException("proportion must be [0 - 1]");
        }
        float[] hsva = new float[3];
        float[] hsvb = new float[3];
        float[] hsv_output = new float[3];
    
        Color.colorToHSV(a, hsva);
        Color.colorToHSV(b, hsvb);
        for (int i = 0; i < 3; i++) {
            hsv_output[i] = interpolate(hsva[i], hsvb[i], proportion);
        }
    
        int alpha_a = Color.alpha(a);
        int alpha_b = Color.alpha(b);
        float alpha_output = interpolate(alpha_a, alpha_b, proportion);
    
        return Color.HSVToColor((int) alpha_output, hsv_output);
    }
    

    【讨论】:

      【解决方案6】:

      这个帖子的一些解决方案对我不起作用,所以我创建了另一个解决方案。也许它对某人有用。

      /**
       * Get the color between two given colors
       * @param colorStart int color start of degradate
       * @param colorEnd int color end of degradate
       * @param percent int percent to apply (0 to 100)
       * @return int color of degradate for given percent
       */
      public static int getColorOfDegradate(int colorStart, int colorEnd, int percent){
          return Color.rgb(
                  getColorOfDegradateCalculation(Color.red(colorStart), Color.red(colorEnd), percent),
                  getColorOfDegradateCalculation(Color.green(colorStart), Color.green(colorEnd), percent),
                  getColorOfDegradateCalculation(Color.blue(colorStart), Color.blue(colorEnd), percent)
          );
      }
      
      private static int getColorOfDegradateCalculation(int colorStart, int colorEnd, int percent){
          return ((Math.min(colorStart, colorEnd)*(100-percent)) + (Math.max(colorStart, colorEnd)*percent)) / 100;
      }
      

      【讨论】:

      • 太棒了!完美运行
      【解决方案7】:

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

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

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

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

      【讨论】:

        【解决方案8】:

        这里有两种插值方法:

        private static float interpolate(final float a, final float b, final float proportion) {
            return a + (b - a) * proportion;
        }
        
        /** Returns an interpoloated color, between <code>a</code> and <code>b</code> */
        public static int interpolateColorHsv(final int a, final int b, final float proportion) {
            final float[] hsva = new float[3];
            final float[] hsvb = new float[3];
            Color.colorToHSV(a, hsva);
            Color.colorToHSV(b, hsvb);
            for (int i = 0; i < 3; ++i) {
                hsvb[i] = interpolate(hsva[i], hsvb[i], proportion);
            }
            return Color.HSVToColor(hsvb);
        }
        
        public static int interpolateRGB(final int colorA, final int colorB, final float bAmount) {
            final float aAmount = 1.0f - bAmount;
            final int red = (int) (Color.red(colorA) * aAmount + Color.red(colorB) * bAmount);
            final int green = (int) (Color.green(colorA) * aAmount + Color.green(colorB) * bAmount);
            final int blue = (int) (Color.blue(colorA) * aAmount + Color.blue(colorB) * bAmount);
            return Color.rgb(red, green, blue);
        }
        

        【讨论】:

          【解决方案9】:

          这是一个伪代码函数,它在两种颜色之间进行线性插值(staying in RGB space)。为了清楚起见,我在这里使用了一个名为 Color 的类而不是整数。

          bAmount 介于 0 和 1 之间(用于插值)

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-08-02
            • 1970-01-01
            • 2012-11-27
            • 2016-08-03
            • 1970-01-01
            • 1970-01-01
            • 2019-12-07
            • 2013-07-06
            相关资源
            最近更新 更多