【问题标题】:Color mixing in androidandroid中的颜色混合
【发布时间】:2011-08-29 12:50:39
【问题描述】:

我正在开发有五种颜色的应用程序:红色、绿色、蓝色、黄色、紫色

我想从这些颜色中实现颜色混合:比如每种颜色有五个按钮。

用户触摸该颜色与先前绘制的颜色混合的任何颜色按钮。

我不知道如何添加两种颜色代码并获得第三种颜色。

编辑:

我还必须将此颜色设置为 imageview 的位图

如何设置?

【问题讨论】:

    标签: android colors mixing


    【解决方案1】:

    如果颜色在 RGB 空间中,则非常简单(但结果有时并不那么令人满意):

    public int mixColors(int col1, int col2) {
        int r1, g1, b1, r2, g2, b2;
    
        r1 = Color.red(col1);
        g1 = Color.green(col1);
        b1 = Color.blue(col1);
    
        r2 = Color.red(col2);
        g2 = Color.green(col2);
        b2 = Color.blue(col2);
    
        int r3 = (r1 + r2)/2;
        int g3 = (g1 + g2)/2;
        int b3 = (b1 + b2)/2;
    
        return Color.rgb(r3, g3, b3);
    }
    

    如果您想使用其他颜色空间,请搜索 Wikipedia 并找到 HSL 颜色空间。你也有一些图书馆可以为你做这件事。

    那么你将不得不阅读这个问题:Calculation of a mixed color in RGB

    【讨论】:

    • 谢谢回复 bt 我有 5 种颜色,不仅是 rgb。
    • RGB 是每种颜色的组成部分。所以你有5倍的RGB。您需要结合 Luis Miguel Serrano 和我的答案。
    • 感谢回复。但是我需要更多的色彩空间,在五种混合上使用上述功能它将保持恒定的颜色。对于更多的色彩空间混合,应该有什么更好的解决方案?
    • @MarvinLabs 您知道您的答案以及链接的答案对于混合 RGB 颜色非常不利(您自己说),那么为什么要包含它?重心插值为 RGB 提供了更有意义的结果(尽管它是一个糟糕的颜色系统)。使用三种颜色,一些黄色、棕色和粉红色,你会得到这个:i.imgur.com/thLiR.png。可以扩展到n个点。
    【解决方案2】:

    在 Android 中,您可以使用 Color 类来处理颜色。

    通过这个类,您可以访问颜色的红色、绿色和蓝色分量,然后您可以对它们执行操作并应用颜色算法。您可以通过这种方式从颜色 int 中提取颜色分量:

    int color = Color.BLACK;
    
    int red, green, blue;
    red = Color.red(color);
    green = Color.green(color);
    blue = Color.blue(color);
    

    每个值必须介于 0 和 255 之间,因此当您将两种颜色混合在一起时,您应该将该值除以 2,以确保最终结果在相同的区间内,或者应用另一种算法来牢记这一事实每个颜色分量对于一个像素的亮度都有不同的权重。

    【讨论】:

    • 发现 Color.red、Color.green、Color.blue 等的奖励积分。应该用这些。尽管接受的答案 (mixTwoColors) 提供了一个完整的实现,但可以使用 Color.xxx 方法使其更加时尚。
    【解决方案3】:

    另一种答案:

    您可以混合十六进制中的位:

    public static int mixTwoColors( int color1, int color2, float amount )
    {
        final byte ALPHA_CHANNEL = 24;
        final byte RED_CHANNEL   = 16;
        final byte GREEN_CHANNEL =  8;
        final byte BLUE_CHANNEL  =  0;
    
        final float inverseAmount = 1.0f - amount;
    
        int a = ((int)(((float)(color1 >> ALPHA_CHANNEL & 0xff )*amount) +
                       ((float)(color2 >> ALPHA_CHANNEL & 0xff )*inverseAmount))) & 0xff;
        int r = ((int)(((float)(color1 >> RED_CHANNEL & 0xff )*amount) +
                       ((float)(color2 >> RED_CHANNEL & 0xff )*inverseAmount))) & 0xff;
        int g = ((int)(((float)(color1 >> GREEN_CHANNEL & 0xff )*amount) +
                       ((float)(color2 >> GREEN_CHANNEL & 0xff )*inverseAmount))) & 0xff;
        int b = ((int)(((float)(color1 & 0xff )*amount) +
                       ((float)(color2 & 0xff )*inverseAmount))) & 0xff;
    
        return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
    }
    

    【讨论】:

    • 什么是amount
    • 您想要的一种颜色与另一种颜色混合的量。例如,您想要 color1 的 0.3 和 color2 的 0.7,您使用 mixTwoColors( ..., ..., 0.3 )。
    【解决方案4】:

    SlidingTabStrip 具有非常有用的混合颜色方法,与 ViewPager 一起使用时看起来很棒:

    private static int blendColors(int color1, int color2, float ratio) {
        final float inverseRation = 1f - ratio;
        float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
        float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
        float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
        return Color.rgb((int) r, (int) g, (int) b);
    }
    

    【讨论】:

    • 什么是ratio
    【解决方案5】:

    【讨论】:

    • 太好了,我用它来着色已经完全亮度的颜色,通过将颜色与白色混合。效果很好。
    • Android 开发人员对 API 的奇怪选择。不必要地需要一个实例。不必要地采用 Object 参数,这些参数会立即转换为 Integer。由于装箱和拆箱分配,不应在实时绘图或动画代码中使用。所以你知道。 (感谢原始发帖人;不感谢编写此暴行的 Android 开发人员)。最好只刷一下此帖子中的其他代码片段。
    【解决方案6】:

    如果您想混合两种颜色(前景和背景),此示例可能很有用。基于 Orlando Leite answare 和维基百科http://en.wikipedia.org/wiki/Alpha_compositing,将两种颜色与 alpha 混合的正确方法是:

    public static int MergeColors(int backgroundColor, int foregroundColor) {
        final byte ALPHA_CHANNEL = 24;
        final byte RED_CHANNEL   = 16;
        final byte GREEN_CHANNEL =  8;
        final byte BLUE_CHANNEL  =  0;
    
        final double ap1 = (double)(backgroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
        final double ap2 = (double)(foregroundColor >> ALPHA_CHANNEL & 0xff) / 255d;
        final double ap = ap2 + (ap1 * (1 - ap2));
    
        final double amount1 = (ap1 * (1 - ap2)) / ap;
        final double amount2 = amount1 / ap;
    
        int a = ((int)(ap * 255d)) & 0xff;
    
        int r = ((int)(((float)(backgroundColor >> RED_CHANNEL & 0xff )*amount1) +
                ((float)(foregroundColor >> RED_CHANNEL & 0xff )*amount2))) & 0xff;
        int g = ((int)(((float)(backgroundColor >> GREEN_CHANNEL & 0xff )*amount1) +
                ((float)(foregroundColor >> GREEN_CHANNEL & 0xff )*amount2))) & 0xff;
        int b = ((int)(((float)(backgroundColor & 0xff )*amount1) +
                ((float)(foregroundColor & 0xff )*amount2))) & 0xff;
    
        return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
    }
    

    在这种情况下,Alpha 通道用于计算混合中 RGB 份额的百分比。只有当前景 alpha 小于 100% 时,才能看到背景颜色

    【讨论】:

    • 最佳答案,这就是我想要的!
    【解决方案7】:

    自 2015 年 4 月起,您可以使用 v4 支持库中的 blendARGB method

    int resultColor = ColorUtils.blendARGB(color1, color2, 0.5F);

    比率值必须为 0.5 才能实现均匀混合。

    【讨论】:

    • 也非常适合设置较浅/较深的颜色!
    • 第三个参数是比例(混合颜色时的比例)。例如。如果你想要 color1 的 30% 和 color2 的 70%,然后执行 ColorUtils.blendARGB(***, ***, 0.3F);
    猜你喜欢
    • 1970-01-01
    • 2020-01-01
    • 2012-06-02
    • 1970-01-01
    • 2020-12-25
    • 2014-09-22
    • 2017-09-23
    • 2011-06-21
    • 2011-05-07
    相关资源
    最近更新 更多