If you think the Android project helsinki-testbed2-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package fi.testbed2.util;
/*fromwww.java2s.com*//**
* Utility class for color manipulation.
*/publicclass ColorUtil {
/**
* Returns the opacity component of the ARGB color as a decimal format.
*
* @param argb Color in HEX format (either with or without alpha, e.g.
* #CCCCCC or #ffCCCCCC.
* @return Value between 0.0...1.0 where 0.0 is completely transparent
* and 1.0 means no transparency.
*/publicstaticdouble getOpacityFromARGB(String argb) {
int[] components = getARGBComponents(argb);
double opacity = components[0]/255.0;
if (opacity>1.0) {
opacity = 1.0;
} elseif (opacity<0.0) {
opacity = 0.0;
}
return opacity;
}
/**
* Returns the color without alpha channel.
*
* @param argb Color in HEX format (either with or without alpha, e.g.
* #CCCCCC or #ffCCCCCC.
* @return
*/publicstatic String getColorWithoutAlpha(String argb) {
int[] components = getARGBComponents(argb);
return"#"+ getTwoDigitHexString(components[1])+
getTwoDigitHexString(components[2])+
getTwoDigitHexString(components[3]);
}
/**
* Returns the ARGB color with inverted alpha channel.
*
* @param argb Color in HEX format (either with or without alpha, e.g.
* #CCCCCC or #ffCCCCCC.
* @return
*/publicstatic String getColorWithInvertedAplha(String argb) {
int[] components = getARGBComponents(argb);
components[0] = 255-components[0];
return"#"+ getTwoDigitHexString(components[0])+
getTwoDigitHexString(components[1])+
getTwoDigitHexString(components[2])+
getTwoDigitHexString(components[3]);
}
/**
* Return ARGB values in int array
* @param argb Color in HEX format (either with or without alpha, e.g.
* #CCCCCC or #ffCCCCCC.
* @return
*/privatestaticint[] getARGBComponents(String argb) {
if (argb.startsWith("#")) {
argb = argb.replace("#", "");
}
int alpha = -1, red = -1, green = -1, blue = -1;
if (argb.length() == 8) {
alpha = Integer.parseInt(argb.substring(0, 2), 16);
red = Integer.parseInt(argb.substring(2, 4), 16);
green = Integer.parseInt(argb.substring(4, 6), 16);
blue = Integer.parseInt(argb.substring(6, 8), 16);
}
elseif (argb.length() == 6) {
alpha = 255;
red = Integer.parseInt(argb.substring(0, 2), 16);
green = Integer.parseInt(argb.substring(2, 4), 16);
blue = Integer.parseInt(argb.substring(4, 6), 16);
}
int[] components = {alpha, red, green, blue};
return components;
}
privatestatic String getTwoDigitHexString(int integer) {
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(integer));
if (sb.length() < 2) {
sb.insert(0, '0'); // pad with leading zero if needed
}
return sb.toString();
}
}