下面列出了android.graphics.Color#green ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void updateColor(String hex) throws JSONException, UnknownHostException {
int c = Color.parseColor(hex);
float alpha = (float) (Color.alpha(c) / 255.0); //html uses normalized values
int r = Color.red(c);
int g = Color.green(c);
int b = Color.blue(c);
JSONObject values = new JSONObject()
.put("type", "background")
.put("a", alpha)
.put("r", r)
.put("g", g)
.put("b", b);
JSONObject msg = new JSONObject()
.put("type", "widget")
.put("action", "add")
.put("values", values);
//TODO change to events
//CustomWebsocketServer.getInstance(getContext()).send(msg);
}
private int calculateTextColor(int color) {
rgb[0] = Color.red(color);
rgb[1] = Color.green(color);
rgb[2] = Color.blue(color);
for (int i = 0; i < rgb.length; i++) {
double c = rgb[i];
c = c / 255.0;
if (c <= 0.03928) {
c = c / 12.92;
} else {
c = Math.pow((c + 0.055) / 1.055, 2.4);
}
rgbRes[i] = c;
}
double luminance = 0.2126 * rgbRes[0] + 0.7152 * rgbRes[1] + 0.0722 * rgbRes[2];
if (luminance > 0.179) {
return Color.BLACK;
} else {
return Color.WHITE;
}
}
/**
* 是否是深色的颜色
*
* @param color
* @return
*/
public static boolean isColorDark(@ColorInt int color) {
double darkness =
1
- (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color))
/ 255;
return darkness >= 0.382;
}
/**
* Let the hosting fragment or activity implement this interface
* to receive results from the dialog
*
* @param dialogTag the tag passed to {@link SimpleDialog#show}
* @param which result type, one of {@link #BUTTON_POSITIVE}, {@link #BUTTON_NEGATIVE},
* {@link #BUTTON_NEUTRAL} or {@link #CANCELED}
* @param extras the extras passed to {@link SimpleDialog#extra(Bundle)}
* @return true if the result was handled, false otherwise
*/
@Override
public boolean onResult(@NonNull String dialogTag, int which, @NonNull Bundle extras) {
// handle results as usual
if (COLOR_FRAGMENT.equals(dialogTag) && which == BUTTON_POSITIVE) {
@ColorInt int color = extras.getInt(SimpleColorDialog.COLOR);
// Sets action bar colors
if (getSupportActionBar() != null) {
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xFF000000 | color));
boolean dark = Color.red(color) * 0.299 + Color.green(color) * 0.587 + Color.blue(color) * 0.114 < 180;
SpannableString s = new SpannableString(getSupportActionBar().getTitle());
s.setSpan(new ForegroundColorSpan(dark ? Color.WHITE : Color.BLACK), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.75;
getWindow().setStatusBarColor(Color.HSVToColor(hsv));
}
return true;
}
return false;
}
public static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
public static int disableColor(int color) {
int alpha = Color.alpha(color);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb((int) (alpha * 0.2f), red, green, blue);
}
public static int getAverageColor(int color1, int color2) {
int r1 = Color.red(color1);
int r2 = Color.red(color2);
int g1 = Color.green(color1);
int g2 = Color.green(color2);
int b1 = Color.blue(color1);
int b2 = Color.blue(color2);
return Color.argb(255, (r1 / 2 + r2 / 2), (g1 / 2 + g2 / 2), (b1 / 2 + b2 / 2));
}
/**
* Blend {@code color1} and {@code color2} using the given ratio.
*
* @param ratio of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend,
* 0.0 will return {@code color2}.
*/
private static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
/**
* Adjust alpha of a color according to the given parameter.
*
* @param color The color whose alpha to be adjusted.
* @param factor Factor in float by which adjust the alpha.
*
* @return The color with adjusted alpha.
*/
public static @ColorInt int adjustAlpha(@ColorInt int color, float factor) {
int alpha = Math.min(255, (int) (Color.alpha(color) * factor));
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
static float[] red() {
return new float[]{
Color.red(Color.RED) / 255f,
Color.green(Color.RED) / 255f,
Color.blue(Color.RED) / 255f,
1.0f
};
}
/**
* Blend {@code color1} and {@code color2} using the given ratio.
*
* @param ratio of which to blend. 1.0 will return {@code color1}, 0.5 will give an even blend,
* 0.0 will return {@code color2}.
*/
private static int blendColors(int color1, int color2, float ratio) {
final float inverseRation = 1f - ratio;
float r = (Color.red(color1) * ratio) + (Color.red(color2) * inverseRation);
float g = (Color.green(color1) * ratio) + (Color.green(color2) * inverseRation);
float b = (Color.blue(color1) * ratio) + (Color.blue(color2) * inverseRation);
return Color.rgb((int) r, (int) g, (int) b);
}
public int darken(int color, double fraction) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
red = darkenColor(red, fraction);
green = darkenColor(green, fraction);
blue = darkenColor(blue, fraction);
int alpha = Color.alpha(color);
return Color.argb(alpha, red, green, blue);
}
/**
* convert a color to hex string
*
* @param color
* @return
*/
public static Integer getHexColor(Integer color) {
if (color != null) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return (red / 16) * 1048576 + (red % 16) * 65536 + (green / 16) * 4096 + (green % 16) * 256 + (blue / 16) * 16 + (blue % 16);
}
return 0;
}
public void setBackgroundColor(final Integer color) {
if (color == null) {
return;
}
float red = (float) Color.red(color) / 255f;
float green = (float) Color.green(color) / 255f;
float blue = (float) Color.blue(color) / 255f;
float alpha = (float) Color.alpha(color) / 255f;
this.backgroundColor = new float[] {red, green, blue, alpha};
this.tryInitScene();
}
private int getColorWithAlpha(int alpha, int color) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
/**
* Try to guess if the color on top of the given {@code colorOnBottomInt}
* should be light or dark. Returns true if top color should be light
*/
public boolean shouldColorOnTopBeLight(@ColorInt final int colorOnBottomInt) {
return 186 > (((0.299 * Color.red(colorOnBottomInt))
+ ((0.587 * Color.green(colorOnBottomInt))
+ (0.114 * Color.blue(colorOnBottomInt)))));
}
private boolean isLightColor(int color) {
return (1 - (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255) < 0.5;
}
public static boolean isColorDark(int color) {
return (0.299 * Color.red(color) + 0.587 * Color.green(color) + 0.114 * Color.blue(color)) / 255 < 0.5;
}
/**
* 从颜色找出角度
*
* @param color
* @return
*/
private float fromColor2Degree(int color) {
float degree = 0;
int diff = 360 / (mCircleColors.length - 1);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int[] mColor = { b, g, r };
// 把最大的,置0xFF,最小的,置0
int min = findMin(b, g, r);
int max = findMax(b, g, r);
int temp = (0xff << (max * 8)) +( 0xff << (8 * 3));
if (max == min) {//证明RGB相等;
return 90;// 九十度
}
int mid = 3 - max - min;
int start = 0;
int end = 0;
for (int i = 0; i < mCircleColors.length - 2; i++) {
if (mCircleColors[i] - temp == 0)
start = i;
if (mCircleColors[i] - temp == (0xff << (mid * 8)))
end = i;
}
float percent = (float) mColor[mid] / (float) 0xff;
int degreeDiff = (int) (percent * diff);
if (start < end) {
degree = start * diff;
degree += degreeDiff;
} else {
degree = start * diff;
degree -= degreeDiff;
}
degree += 90;
if (degree > 360)
degree -= 360;
return degree;
}
public static boolean isColorDark(@ColorInt int color) {
double brightness = Color.red(color) * 0.299 +
Color.green(color) * 0.587 +
Color.blue(color) * 0.114;
return brightness < 180;
}