下面列出了android.graphics.Color#blue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static int getGradientColor(int startColor, int endColor, float percent) {
int startA = Color.alpha(startColor);
int startR = Color.red(startColor);
int startG = Color.green(startColor);
int startB = Color.blue(startColor);
int endA = Color.alpha(endColor);
int endR = Color.red(endColor);
int endG = Color.green(endColor);
int endB = Color.blue(endColor);
int currentA = (int) (startA * (1 - percent) + endA * percent);
int currentR = (int) (startR * (1 - percent) + endR * percent);
int currentG = (int) (startG * (1 - percent) + endG * percent);
int currentB = (int) (startB * (1 - percent) + endB * percent);
return Color.argb(currentA, currentR, currentG, currentB);
}
/**
* 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 static int adjustAlpha(int color, float factor) {
int alpha = Math.round(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);
}
/**
* 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);
}
private int generateRandomColor() {
Random random = new Random();
int red = random.nextInt(256);
int green = random.nextInt(256);
int blue = random.nextInt(256);
red = (red + Color.red(TINT)) >> 1;
green = (green + Color.green(TINT)) >> 1;
blue = (blue + Color.blue(TINT)) >> 1;
return Color.rgb(red, green, blue);
}
/** Returns darker version of specified color */
private static int darken(int color, float factor) {
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
return Color.argb(a,
Math.max((int) (r * factor), 0),
Math.max((int) (g * factor), 0),
Math.max((int) (b * factor), 0));
}
/**
* Returns true if the text color should be white, given a background color
*
* @param color background color
* @return true if the text should be white, false if the text should be black
*/
public static boolean isWhiteText(@ColorInt final int color) {
final int red = Color.red(color);
final int green = Color.green(color);
final int blue = Color.blue(color);
// https://en.wikipedia.org/wiki/YIQ
// https://24ways.org/2010/calculating-color-contrast/
final int yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000;
return yiq < 192;
}
/**
*
* 为后面的wave设置透明度
* @param waveColor
* @param factor
* @return
*/
private int setBackWaveColor(int waveColor, float factor){
int alpha = Math.round(Color.alpha(waveColor) * factor);
int red = Color.red(waveColor);
int green = Color.green(waveColor);
int blue = Color.blue(waveColor);
return Color.argb(alpha, red, green, blue);
}
public static Float4 toRenderscriptColor(final int color) {
return new Float4(
Color.red(color) / 255.0f,
Color.green(color) / 255.0f,
Color.blue(color) / 255.0f,
Color.alpha(color) / 255.0f
);
}
public static int darken(int color, float factor) {
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
return Color.argb(a,
Math.max((int) (r * factor), 0),
Math.max((int) (g * factor), 0),
Math.max((int) (b * factor), 0));
}
private int adjustAlpha(int color, float factor) {
int alpha = Math.round(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);
}
public static int adjustAlpha(int color, @SuppressWarnings("SameParameterValue") float factor) {
int alpha = Math.round(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);
}
/**
* 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);
}
/** Calculates the contrast between the given color and white, using the algorithm provided by
* the WCAG v2 in http://www.w3.org/TR/WCAG20/#contrast-ratiodef.
*/
private static float getContrastForColor(int color) {
float bgR = Color.red(color) / 255f;
float bgG = Color.green(color) / 255f;
float bgB = Color.blue(color) / 255f;
bgR = (bgR < 0.03928f) ? bgR / 12.92f : (float) Math.pow((bgR + 0.055f) / 1.055f, 2.4f);
bgG = (bgG < 0.03928f) ? bgG / 12.92f : (float) Math.pow((bgG + 0.055f) / 1.055f, 2.4f);
bgB = (bgB < 0.03928f) ? bgB / 12.92f : (float) Math.pow((bgB + 0.055f) / 1.055f, 2.4f);
float bgL = 0.2126f * bgR + 0.7152f * bgG + 0.0722f * bgB;
return Math.abs((1.05f) / (bgL + 0.05f));
}
/**
* 锐化效果处理
*
* @param bitmap 原图
* @return 锐化效果处理后的图片
*/
public static Bitmap sharpen(Bitmap bitmap) {
// 拉普拉斯矩阵
int[] laplacian = new int[]{-1, -1, -1, -1, 9, -1, -1, -1, -1};
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height,
Config.RGB_565);
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int idx = 0;
float alpha = 0.3F;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
idx = 0;
for (int m = -1; m <= 1; m++) {
for (int n = -1; n <= 1; n++) {
pixColor = pixels[(i + n) * width + k + m];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = newR + (int) (pixR * laplacian[idx] * alpha);
newG = newG + (int) (pixG * laplacian[idx] * alpha);
newB = newB + (int) (pixB * laplacian[idx] * alpha);
idx++;
}
}
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
pixels[i * width + k] = Color.argb(255, newR, newG, newB);
newR = 0;
newG = 0;
newB = 0;
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
public static Bitmap sepia(Bitmap src) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
// constant grayscale
final double GS_RED = 0.3;
final double GS_GREEN = 0.59;
final double GS_BLUE = 0.11;
// color information
int A, R, G, B;
int pixel;
// scan through all pixels
for(int x = 0; x < width; ++x) {
for(int y = 0; y < height; ++y) {
// get pixel color
pixel = src.getPixel(x, y);
// get color on each channel
A = Color.alpha(pixel);
R = Color.red(pixel);
G = Color.green(pixel);
B = Color.blue(pixel);
// apply grayscale sample
B = G = R = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
// apply intensity level for sepid-toning on each channel
R += 110;
if(R > 255) { R = 255; }
G += 65;
if(G > 255) { G = 255; }
B += 20;
if(B > 255) { B = 255; }
// set new pixel color to output image
bmOut.setPixel(x, y, Color.argb(A, R, G, B));
}
}
src.recycle();
src = null;
return bmOut;
}
/**
* Default constructor.
*
* @param context
* @param attrs
* @param defStyle
*/
public SegmentedProgressBar(final Context context, final AttributeSet attrs, final int defStyle)
{
super(context, attrs, defStyle);
// extract params (if provided)
final TypedArray args = context.obtainStyledAttributes(attrs, R.styleable.segmentedProgressBar);
try
{
// colors
final int startColor = args.getColor(R.styleable.segmentedProgressBar_startColor, R.color.black);
int endColor = args.getColor(R.styleable.segmentedProgressBar_endColor, -1);
if (endColor == -1)
{
endColor = startColor;
}
final int unfilledColor = args.getColor(R.styleable.segmentedProgressBar_unfilledColor, R.color.gray);
// interpolator
this.mGradientInterpolatorResId = args.getResourceId(R.styleable.segmentedProgressBar_android_interpolator,
-1);
if (this.mGradientInterpolatorResId != -1)
{
this.setInterpolator(context, this.mGradientInterpolatorResId);
}
else
{
this.setInterpolator(new LinearInterpolator());
}
// other settings
this.mSegmentCount = args.getInt(R.styleable.segmentedProgressBar_segmentCount,
SegmentedProgressBar.DEFAULT_SEGMENT_COUNT);
// create colors based on supplied args
this.mColorStartA = Color.alpha(startColor);
this.mColorStartR = Color.red(startColor);
this.mColorStartG = Color.green(startColor);
this.mColorStartB = Color.blue(startColor);
this.mColorEndA = Color.alpha(endColor);
this.mColorEndR = Color.red(endColor);
this.mColorEndG = Color.green(endColor);
this.mColorEndB = Color.blue(endColor);
this.mColorUnfilled = Color.argb(Color.alpha(unfilledColor), Color.red(unfilledColor),
Color.blue(unfilledColor), Color.green(unfilledColor));
// init paints
this.mPaintSegment = new Paint();
this.mPaintSegment.setColor(startColor);
this.mPaintSegment.setStyle(Style.FILL);
this.mPaintSegment.setAntiAlias(true);
}
finally
{
args.recycle();
}
}
/**
* Lightens a color by a given factor.
*
* @param color
* The color to lighten
* @param factor
* The factor to lighten the color. 0 will make the color unchanged. 1 will make the
* color white.
* @return lighter version of the specified color.
*/
public static int lighten(int color, float factor) {
int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255);
int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255);
int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255);
return Color.argb(Color.alpha(color), red, green, blue);
}