Alex Rivera | Logout

Color mixing in android

Asked 2011-05-20T09:48:04.167
23

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?

Edit
Report

1 Answer

7

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

answered 2011-05-20T09:52:26.247

Your Answer