Reputation: 4081
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.
Upvotes: 38
Views: 97177
Reputation: 4676
It seems the value being quoted and the format desired are mismatched. The value is hexadecimal, while RGB would read 255, 0, 255
and the integer is a composite color representation. Since it is unclear what is being accomplished, here are all three variations:
If you have an integer for the composite color, then most color endpoints will accept it unmodified. This would be something like setBackgroundColor(colorInt)
If you have the hexadecimal value, then Color.parseColor(#colorHex)
would convert it to a color object.
Likewise, Color.rgb(redInt, greenInt, blueInt)
would convert the red, green, and blue values to a color object.
If you need to restore the composite integer into a color object, that is even simpler with Color.valueOf(colorInt)
Upvotes: 0
Reputation: 82938
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());
Upvotes: 55
Reputation: 2227
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.
Upvotes: 87
Reputation: 35224
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));
}
Upvotes: 6
Reputation: 417
RGB uses hexa decimal number format,. if you have integer value, convert it to hexa,.
Upvotes: 0