文章出处:http://blog.csdn.net/shift_wwx
请转载的朋友标明出处~~
前言:对于color 资深的Android 工程师肯定都熟悉,我是记忆力太差了,随记一下。
public static final int BLACK = 0xFF000000; public static final int DKGRAY = 0xFF444444; public static final int GRAY = 0xFF888888; public static final int LTGRAY = 0xFFCCCCCC; public static final int WHITE = 0xFFFFFFFF; public static final int RED = 0xFFFF0000; public static final int GREEN = 0xFF00FF00; public static final int BLUE = 0xFF0000FF; public static final int YELLOW = 0xFFFFFF00; public static final int CYAN = 0xFF00FFFF; public static final int MAGENTA = 0xFFFF00FF; public static final int TRANSPARENT = 0;需要注意的是 transparent,就是alpha 为0,rgb是完全不关心的。
public static int alpha(int color) { return color >>> 24; } /** * Return the red component of a color int. This is the same as saying * (color >> 16) & 0xFF */ public static int red(int color) { return (color >> 16) & 0xFF; } /** * Return the green component of a color int. This is the same as saying * (color >> 8) & 0xFF */ public static int green(int color) { return (color >> 8) & 0xFF; } /** * Return the blue component of a color int. This is the same as saying * color & 0xFF */ public static int blue(int color) { return color & 0xFF; } /** * Return a color-int from red, green, blue components. * The alpha component is implicity 255 (fully opaque). * These component values should be [0..255], but there is no * range check performed, so if they are out of range, the * returned color is undefined. * @param red Red component [0..255] of the color * @param green Green component [0..255] of the color * @param blue Blue component [0..255] of the color */ public static int rgb(int red, int green, int blue) { return (0xFF << 24) | (red << 16) | (green << 8) | blue; } /** * Return a color-int from alpha, red, green, blue components. * These component values should be [0..255], but there is no * range check performed, so if they are out of range, the * returned color is undefined. * @param alpha Alpha component [0..255] of the color * @param red Red component [0..255] of the color * @param green Green component [0..255] of the color * @param blue Blue component [0..255] of the color */ public static int argb(int alpha, int red, int green, int blue) { return (alpha << 24) | (red << 16) | (green << 8) | blue; } /** * Returns the hue component of a color int. * * @return A value between 0.0f and 1.0f * * @hide Pending API council */ public static float hue(int color) { int r = (color >> 16) & 0xFF; int g = (color >> 8) & 0xFF; int b = color & 0xFF; int V = Math.max(b, Math.max(r, g)); int temp = Math.min(b, Math.min(r, g)); float H; if (V == temp) { H = 0; } else { final float vtemp = (float) (V - temp); final float cr = (V - r) / vtemp; final float cg = (V - g) / vtemp; final float cb = (V - b) / vtemp; if (r == V) { H = cb - cg; } else if (g == V) { H = 2 + cr - cb; } else { H = 4 + cg - cr; } H /= 6.f; if (H < 0) { H++; } } return H; } /** * Returns the saturation component of a color int. * * @return A value between 0.0f and 1.0f * * @hide Pending API council */ public static float saturation(int color) { int r = (color >> 16) & 0xFF; int g = (color >> 8) & 0xFF; int b = color & 0xFF; int V = Math.max(b, Math.max(r, g)); int temp = Math.min(b, Math.min(r, g)); float S; if (V == temp) { S = 0; } else { S = (V - temp) / (float) V; } return S; } /** * Returns the brightness component of a color int. * * @return A value between 0.0f and 1.0f * * @hide Pending API council */ public static float brightness(int color) { int r = (color >> 16) & 0xFF; int g = (color >> 8) & 0xFF; int b = color & 0xFF; int V = Math.max(b, Math.max(r, g)); return (V / 255.f); }可以获取 int alpha,int red,int green, int blue;
int color = Color.rgb(100, 200, 255);这个时候alpha 是FF。
int color = Color.argb(100, 100, 200, 255);加了透明色的color。
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="source_unfocus">#1E1E1E</color> <color name="source_focus">#FFFFFF</color> <color name="source_unselect">#5D5D5D</color> <color name="no_signal">#1E1E1E</color> <color name="source_info_ha">#1E1E1E</color> </resources>这样可以在code中用:
int color = getResources().getColor(R.color.source_unfocus);也可以直接在资源的xml中用;
@color/source_unfocus
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_focused="true" android:color="@color/source_focus" /> <item android:state_focused="false" android:color="@color/source_unfocus"/> <item android:state_selected="false" android:color="@color/source_unselect"/> </selector>当然啦,也可以用shape,详细看一下:《 android资源文件之:shape详解 》