java hex color to int code example

Example 1: java rgb color to int

//If your RGB values are in form of a float percentage between 
//0 and 1 consider the following method:

public int getIntFromColor(float Red, float Green, float Blue){
    int R = Math.round(255 * Red);
    int G = Math.round(255 * Green);
    int B = Math.round(255 * Blue);

    R = (R << 16) & 0x00FF0000;
    G = (G << 8) & 0x0000FF00;
    B = B & 0x000000FF;

    return 0xFF000000 | R | G | B;
}

Example 2: chnage hex color to int color in android

String white = "#ffffff";
int whiteInt = Color.parseColor(white);

Tags:

Misc Example