Kurt Wagner
Kurt Wagner

Reputation: 3315

How to convert returned int value from ColorDrawable's getColor() to RGB?

So I have the following snippet of code that retrieves an activity's background color. However, getColor returns an int value, and there doesn't seem to be a way to modify this to return a more standard format to work with and modify.

    setContentView(R.layout.activity_test);

    View root = ((ViewGroup) findViewById(android.R.id.content)).getChildAt(0);

    int color = Color.TRANSPARENT;
    Drawable background = root.getBackground();
    if (background instanceof ColorDrawable)
        color = ((ColorDrawable) background).getColor();

In this particular case, the activity's background color is initially defined to be android:background="#0099cc" in the XML file which getColor() returns as -16737844. However, I want to change it by doing stuff like incrementally changing the color's rgb values over time [i.e. rgb(initialRVal+1,initialGVal+1,initialBVal+1)]. This requires me to somehow convert -16737844 to a set of RGB values, but there doesn't seem to be a means to do so.

Upvotes: 1

Views: 1802

Answers (1)

Gintas_
Gintas_

Reputation: 5030

You want to convert hexadecimal color value to RGB.

int red = (color >> 16) & 0xFF;
int green = (color >> 8) & 0xFF;
int blue = (color >> 0) & 0xFF;

Upvotes: 5

Related Questions