Android 中的颜色混合

发布于 2024-11-08 19:15:41 字数 219 浏览 5 评论 0原文

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

我想实现这些颜色的颜色混合:这样每种颜色有五个按钮。

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

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

编辑:

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

我该如何设置?

I am working on application in which i have five colors:Red,Green,Blue,Yellow,purple

I want to implement color mixing from those colors:such that like there are five button for each color.

User touch whichever color button this color mix with previously drawn color.

I have not any clue how to add two color codes and get third color.

EDitED:

I have to also set this color to imageview's bitmap

how can i set this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

老旧海报 2024-11-15 19:15:41

自 2015 年 4 月起,您可以使用 blendARGB 方法

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

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

Since April 2015 you can use blendARGB method from v4 support library:

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

Ratio value has to be 0.5 to achive even mix.

未蓝澄海的烟 2024-11-15 19:15:41

另一种答案:

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

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

An alternative answer:

You can mix the bits in the hexs:

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;
}
蓝眼泪 2024-11-15 19:15:41

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

SlidingTabStrip has really useful method for blending colors, looks great when used with 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);
}
热鲨 2024-11-15 19:15:41

如果颜色在 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 mix color in RGB

If colors are in RGB space, it is pretty simple (but the result is sometimes not that satisfying):

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

If you want to use other color spaces, search Wikipedia and find HSL color space. You also have some libraries to do that for you.

Then you will have to read this question: Calculation of a mixed color in RGB

时光清浅 2024-11-15 19:15:41

如果您想混合两种颜色(前景和背景),此示例可能很有用。基于 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% 时背景颜色才可见

This example may by useful if you want blend two colors (foreground and background). Based on Orlando Leite answare and wikipedia http://en.wikipedia.org/wiki/Alpha_compositing, proper way to blend two colors with alpha is:

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

In this case alpha channel is used to compute percentage RGB share in blending. Background color may be visible only i foreground alpha is smaller than 100%

橘味果▽酱 2024-11-15 19:15:41

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

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

int color = Color.BLACK;

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

每个值必须在 0 到 255 之间,因此当您将两种颜色混合在一起时,您应该将该值除以二,以确保最终结果在相同的范围内间隔,或者应用另一种算法,记住每个颜色分量对于像素的亮度具有不同的权重。

In Android you can work with Colors using the Color class.

With this class, you can access the Red, Green and Blue components of a color, so that you can then perform operations with them and apply color algorithms. You can extract the color components from a color int in this way:

int color = Color.BLACK;

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

Each value must be between 0 and 255, so when you mix two colors together, you should either divide the value by two, to make sure the final result is within the same interval, or apply another algorithm bearing in mind the fact that each color component has a different weight for the luminance of a pixel.

岁月无声 2024-11-15 19:15:41

在前面的答案中,“amount2”计算错误。应该是:

final double amount2 = ap2 / ap;

所以,整个代码将如下所示:

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 = ap2 / 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;
}

In previous answer "amount2" is calculated wrongly. Should be:

final double amount2 = ap2 / ap;

So, the whole code will look like:

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 = ap2 / 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文