How to convert hex to rgb using Java?
I guess this should do it:
/**
*
* @param colorStr e.g. "#FFFFFF"
* @return
*/
public static Color hex2Rgb(String colorStr) {
return new Color(
Integer.valueOf( colorStr.substring( 1, 3 ), 16 ),
Integer.valueOf( colorStr.substring( 3, 5 ), 16 ),
Integer.valueOf( colorStr.substring( 5, 7 ), 16 ) );
}
java : convert Hex color #RRGGBB to rgb r g b?
You can try:
int r= Integer.valueOf( colorStr.substring( 1, 3 ), 16 );
int g= Integer.valueOf( colorStr.substring( 3, 5 ), 16 );
int b= Integer.valueOf( colorStr.substring( 5, 7 ), 16 );
processing hex string to color
What I did was simply recreated the color like this:
int c = Integer.parseInt(obj.getString("color"), 16);
c = color(red(c), green(c), blue(c));
convert from hex color to RGB
Color has the static methods red/blue/green and alpha
int color = Color.parseColor("#6F00AC");
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int alpha = Color.alpha(color);
they return respectively the red, blue, green, alpha component of a color int.
EDIT:
Your code is almost correct, (you don't need to divide by 255)
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
How to Convert colors from Hex to RGB
Edit:
This was accepted as the answer but have a look at @johann's answer too, it will leave you and those reading your code less confused.
If you want to learn about the underlying representation of colours, you could start here.
Original answer:
getColor()
gives you the colour as an ARGB int
.
int color = getResources().getColor(R.color.ItemColor1);
float red = (color >> 16) & 0xFF;
float green = (color >> 8) & 0xFF;
float blue = (color) & 0xFF;
float alpha = (color >> 24) & 0xFF;
Convert a RGB Color Value to a Hexadecimal String
You can use
String hex = String.format("#%02x%02x%02x", r, g, b);
Use capital X's if you want your resulting hex-digits to be capitalized (#FFFFFF
vs. #ffffff
).
Converting hex color to RGB and vice-versa
Real answer: Depends on what kind of hexadecimal color value you are looking for (e.g. 565, 555, 888, 8888, etc), the amount of alpha bits, the actual color distribution (rgb vs bgr...) and a ton of other variables.
Here's a generic algorithm for most RGB values using C++ templates (straight from ScummVM).
template<class T>
uint32 RGBToColor(uint8 r, uint8 g, uint8 b) {
return T::kAlphaMask |
(((r << T::kRedShift) >> (8 - T::kRedBits)) & T::kRedMask) |
(((g << T::kGreenShift) >> (8 - T::kGreenBits)) & T::kGreenMask) |
(((b << T::kBlueShift) >> (8 - T::kBlueBits)) & T::kBlueMask);
}
Here's a sample color struct for 565 (the standard format for 16 bit colors):
template<>
struct ColorMasks<565> {
enum {
highBits = 0xF7DEF7DE,
lowBits = 0x08210821,
qhighBits = 0xE79CE79C,
qlowBits = 0x18631863,
kBytesPerPixel = 2,
kAlphaBits = 0,
kRedBits = 5,
kGreenBits = 6,
kBlueBits = 5,
kAlphaShift = kRedBits+kGreenBits+kBlueBits,
kRedShift = kGreenBits+kBlueBits,
kGreenShift = kBlueBits,
kBlueShift = 0,
kAlphaMask = ((1 << kAlphaBits) - 1) << kAlphaShift,
kRedMask = ((1 << kRedBits) - 1) << kRedShift,
kGreenMask = ((1 << kGreenBits) - 1) << kGreenShift,
kBlueMask = ((1 << kBlueBits) - 1) << kBlueShift,
kRedBlueMask = kRedMask | kBlueMask
};
};
RGB to hex and hex to RGB
Note: both versions of rgbToHex
expect integer values for r
, g
and b
, so you'll need to do your own rounding if you have non-integer values.
The following will do to the RGB to hex conversion and add any required zero padding:
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
function rgbToHex(r, g, b) {
return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b);
}
alert(rgbToHex(0, 51, 255)); // #0033ff
HEX to RGB converter, Android Studio
Might I suggest:
int color = Color.parseColor("#123456");
Additionally, you might try:
public static int[] getRGB(final int hex) {
int r = (hex & 0xFF0000) >> 16;
int g = (hex & 0xFF00) >> 8;
int b = (hex & 0xFF);
return new int[] {r, g, b};
}
int hex = 0x123456;
getRGB(hex);
Or, if you need it from a string:
public static int[] getRGB(final String rgb)
{
int r = Integer.parseInt(rgb.substring(0, 2), 16); // 16 for hex
int g = Integer.parseInt(rgb.substring(2, 4), 16); // 16 for hex
int b = Integer.parseInt(rgb.substring(4, 6), 16); // 16 for hex
return new int[] {r, g, b};
}
getRGB("123456");
Related Topics
Difference Between an Application Server and a Servlet Container
Can a Class Have No Constructor
Log4J2 - Assigning File Appender Filename at Runtime
Compile Error: Package Javax.Servlet Does Not Exist
Make Hibernate Ignore Instance Variables That Are Not Mapped
Spring Session-Scoped Beans (Controllers) and References to Services, in Terms of Serialization
When Should an Illegalargumentexception Be Thrown
Jtable Getselectedrow Does Not Return the Selected Row Index
What's the Difference Between a Resource, Uri, Url, Path and File in Java
How to Simulate a Real Mouse Click Using Java
Java Threads and Number of Cores
File Path Windows Format to Java Format
What Is the Optimal Capacity and Load Factor for a Fixed-Size Hashmap
When Not to Use the Static Keyword in Java
In Java, When I Call Outputstream.Close() Do I Always Need to Call Outputstream.Flush() Before
How to Improve the Performance of G.Drawimage() Method for Resizing Images
What Is the Most Recommended Way to Store Time in Postgresql Using Java