Convert integer color value to RGB

74,368

Solution 1

Use this

String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

We know lenght of color value in HEX is 6. So you see 6 here. %06X matches the result coming from (0xFFFFFF & intColor) and if length is less than 6, it makes result with 6 by appending ZERO to left side of result. And you see #, so this # char gets appended to result and finally you get a HEX COLOR value.


Update since API 26

Since API 26, new methods Color.valueOf(....) has been introduced to convert colors for similar reason. you can use it like

// sRGB
Color opaqueRed = Color.valueOf(0xffff0000); // from a color int
Color translucentRed = Color.valueOf(1.0f, 0.0f, 0.0f, 0.5f);

// Wide gamut color
ColorSpace sRgb = ColorSpace.get(ColorSpace.Named.SRGB);
@ColorLong long p3 = Color.pack(1.0f, 1.0f, 0.0f, 1.0f, sRgb);
Color opaqueYellow = Color.valueOf(p3); // from a color long

// CIE L*a*b* color space
ColorSpace lab = ColorSpace.get(Named.CIE_LAB);
Color green = Color.valueOf(100.0f, -128.0f, 128.0f, 1.0f, lab);

mView.setBackgroundColor(opaqueRed.toArgb());
mView2.setBackgroundColor(green.toArgb());
mView3.setBackgroundColor(translucentRed.toArgb());
mView4.setBackgroundColor(opaqueYellow.toArgb());

Solution 2

What I found to be the simplest and best solution for me was to directly use the Color class as follows:

int red = Color.red(intColor);
int green = Color.green(intColor);
int blue = Color.blue(intColor);
int alpha = Color.alpha(intColor);

This way I could already deal with the integer values without having to handle strings. If on the other hand the string representing the rgb color is what you need, Pankaj Kumar's answer is the best. I hope this is useful to someone.

Solution 3

Since SDK 26 you can just use

Color c = Color.valueOf(colorInt);

apart from that it does not seem to possible to create a Color instance from arbitrary argb. The underlying code uses a private constructor:

/**
 * Creates a new <code>Color</code> instance from an ARGB color int.
 * The resulting color is in the {@link ColorSpace.Named#SRGB sRGB}
 * color space.
 *
 * @param color The ARGB color int to create a <code>Color</code> from
 * @return A non-null instance of {@link Color}
 */
@NonNull
public static Color valueOf(@ColorInt int color) {
    float r = ((color >> 16) & 0xff) / 255.0f;
    float g = ((color >>  8) & 0xff) / 255.0f;
    float b = ((color      ) & 0xff) / 255.0f;
    float a = ((color >> 24) & 0xff) / 255.0f;
    return new Color(r, g, b, a, ColorSpace.get(ColorSpace.Named.SRGB));
}

Solution 4

RGB uses hexa decimal number format,. if you have integer value, convert it to hexa,.

Share:
74,368
Marek
Author by

Marek

Helped? :) BTC tips 1Nq3SS8QA7gCaSLxkmfkThZbXUsbLBRsZJ

Updated on February 12, 2022

Comments

  • Marek
    Marek about 2 years

    I am trying to modify a third party software. I want to use a color which is returned by some methods (which I cant modifiy) as an integer. However, I would like to use RGB format, like #FF00FF. How can I make a conversion?

    Here is an HTML example http://www.shodor.org/stella2java/rgbint.html I would like to archive same thing in Java, on Android.

  • Pankaj Kumar
    Pankaj Kumar almost 11 years
    Are you asking about (0xFFFFFF & intColor)? If yes this will converts the int value to hex.
  • Marek
    Marek almost 11 years
    I am more interested in "#%06X". It is a regular expression, which I totally don't understand. Could you please explain it?
  • Pankaj Kumar
    Pankaj Kumar almost 11 years
    Actually what this does is :- We know lenght of color value in HEX is 6. So you see 6 here. %06X matches the result coming from (0xFFFFFF & intColor) and if length is less than 6, it makes result with 6 by appending ZERO to left side of result. And you see #, so this # char gets appended to result and finally you get a HEX COLOR value. Am I clear now?
  • Daryl Bennett
    Daryl Bennett over 7 years
    The syntax may have changed. Unless there's a static Color class that I'm unaware of: new Color(intColor).getRed();
  • Francesco D.M.
    Francesco D.M. over 7 years
    I have no idea...I haven't been working on Android for a pretty long time now. If you are certain about this you could edit my answer with this info, perhaps even with the version at which this change happened
  • arlomedia
    arlomedia almost 7 years
    These are static methods of the Color class, so the syntax shown here is correct (and is working for me in Android 7).
  • TheIT
    TheIT over 5 years
    If you have an 8 byte color value such as 0xAARRGGBB, you can use String.format("#%06X", (0x00FFFFFF & intColor)) instead to get the 0xRRBBGG value
  • Pankaj Kumar
    Pankaj Kumar over 5 years
    @TheIT Thank you for valuable information. It will definitely help many.
  • nspo
    nspo over 3 years
    To get integer R, G, and B values. simply remove the division (/ 255.0f) and save as int.
  • Francesco D.M.
    Francesco D.M. about 2 years
    @AbandonedCart I highly doubt is was introduced in Android O as this answer predates that version. I just checked and it looks like it is there from API 1: - developer.android.com/reference/android/graphics/Color#red(i‌​nt) - developer.android.com/reference/android/graphics/Color
  • Francesco D.M.
    Francesco D.M. about 2 years
    @AbandonedCart perhaps I am getting things wrong but to me it looks like I am using the static methods that are available since API 1 and to which I provided the links in my previous comment. What you are linking and that is available since API 26 are very similar but accept long values instead of int
  • Abandoned Cart
    Abandoned Cart about 2 years
    @FrancescoD.M. I am not entirely sure I understand your intentions, since my original reply was neither addressing you or disputing your answer. Quite the opposite, it was disputing a claim that it wasn't valid. I guess some prefer to argue for the sake of argument? I'll remove the comment to spare you the time.
  • Francesco D.M.
    Francesco D.M. about 2 years
    @AbandonedCart I might have understood things wrong but I'm definitely not arguing just for the sake of it :) I thought you were saying that the methods used in my answer were available only from api 26 onwards. If that was not the case then I'm sorry for wasting your time but please understand it was just a mistake.