Android: Color To Int conversion

This might be a stupid question but I'm surprised that Paint class has no setColor(Color c) method. I want to do the following:

public void setColor(Color color) {
/* ... */
Paint p = new Paint();
p.setColor(color); // set color takes only int as a paramter :(
/* ... */
}

So any easy way to convert Color to int?


Any color parse into int simplest two way here:

1) Get System Color

int redColorValue = Color.RED;

2) Any Color Hex Code as a String Argument

int greenColorValue = Color.parseColor("#00ff00")

MUST REMEMBER in above code Color class must be android.graphics...!


All the methods and variables in Color are static. You can not instantiate a Color object.

Official Color Docs

The Color class defines methods for creating and converting color ints.

Colors are represented as packed ints, made up of 4 bytes: alpha, red, green, blue.

The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components.

The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue.

Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution.

Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF


R.color.black or some color are obviously integers. It needs a RGB value. You can give your own like #FF123454 which represents various primary colors