下面列出了android.graphics.Color#red ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setColor(int color) {
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
int a = Color.alpha(color);
if (!ignoreTextChange) {
ignoreTextChange = true;
colorEditText[0].setText("" + red);
colorEditText[1].setText("" + green);
colorEditText[2].setText("" + blue);
colorEditText[3].setText("" + a);
for (int b = 0; b < 4; b++) {
colorEditText[b].setSelection(colorEditText[b].length());
}
ignoreTextChange = false;
}
alphaGradient = null;
colorGradient = null;
alpha = a / 255.0f;
Color.colorToHSV(color, colorHSV);
invalidate();
}
public boolean pixelsLeft(float threshold)
{
int w = bitmapContext.getWidth();
int h = bitmapContext.getHeight();
int pixelThreshold = (int)(fillablePixelCount * threshold);
int tot = 0;
int pixels[] = new int[w];
for(int i = 0;i < h;i++)
{
bitmapContext.getPixels(pixels, 0, w, 0, i, w, 1);
for(int j = 0;j < w;j++)
{
int px = pixels[j];
if (Color.red(px) > 25)
tot++;
if(tot > pixelThreshold)
return false;
}
}
return true;
}
/**
* 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 rippleColor(int color) {
int alpha = Math.round(Color.alpha(color) * 0.3f);
int red = Color.red(color);
int green = Color.green(color);
int blue = Color.blue(color);
return Color.argb(alpha, red, green, blue);
}
private static int swapColor(int c1, int c2, float p) {
float i = 1 - p;
int r = (int) (Color.red(c1) * i + Color.red(c2) * p);
int g = (int) (Color.green(c1) * i + Color.green(c2) * p);
int b = (int) (Color.blue(c1) * i + Color.blue(c2) * p);
return Color.argb(0xFF, r, g, 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));
}
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 ColorHolder fromARGB(int color) {
return new ColorHolder(
Color.alpha(color),
Color.red(color),
Color.green(color),
Color.blue(color)
);
}
@ColorInt
static int adjustAlpha(@ColorInt 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 getOffsetColor(int color1, int color2, float offset, float alpha) {
int rF = Color.red(color2);
int gF = Color.green(color2);
int bF = Color.blue(color2);
int aF = Color.alpha(color2);
int rS = Color.red(color1);
int gS = Color.green(color1);
int bS = Color.blue(color1);
int aS = Color.alpha(color1);
return Color.argb((int) ((aS + (aF - aS) * offset) * alpha), (int) (rS + (rF - rS) * offset), (int) (gS + (gF - gS) * offset), (int) (bS + (bF - bS) * offset));
}
/**
* Taken from CollapsingToolbarLayout's CollapsingTextHelper class.
*/
public static int blendColors(int color1, int color2, @FloatRange(from = 0.0, to = 1.0) float ratio) {
final float inverseRatio = 1f - ratio;
float a = (Color.alpha(color1) * inverseRatio) + (Color.alpha(color2) * ratio);
float r = (Color.red(color1) * inverseRatio) + (Color.red(color2) * ratio);
float g = (Color.green(color1) * inverseRatio) + (Color.green(color2) * ratio);
float b = (Color.blue(color1) * inverseRatio) + (Color.blue(color2) * ratio);
return Color.argb((int) a, (int) r, (int) g, (int) b);
}
static int darkenColor(double factor, int color) {
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 generateTransparentColor(float percent, int rgb) {
int red = Color.red(rgb);
int green = Color.green(rgb);
int blue = Color.blue(rgb);
int alpha = Color.alpha(rgb);
alpha = (int) (alpha * percent);
return Color.argb(alpha, red, green, blue);
}
/**
* 底片效果处理
* @param bitmap 待操作源图片
* @return 底片效果处理后的图片
*/
public static Bitmap film(final Bitmap bitmap) {
if (bitmap == null) return null;
try {
// ARGB 的最大值
final int MAX_VALUE = 255;
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int pixR = 0;
int pixG = 0;
int pixB = 0;
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos = 0;
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
newR = MAX_VALUE - pixR;
newG = MAX_VALUE - pixG;
newB = MAX_VALUE - pixB;
newR = Math.min(MAX_VALUE, Math.max(0, newR));
newG = Math.min(MAX_VALUE, Math.max(0, newG));
newB = Math.min(MAX_VALUE, Math.max(0, newB));
pixels[pos] = Color.argb(MAX_VALUE, newR, newG, newB);
}
}
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "film");
}
return null;
}
/**
* 模糊效果处理
* @return 模糊效果处理后的图片
*/
public Bitmap blur(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
int pixColor = 0;
int newR = 0;
int newG = 0;
int newB = 0;
int newColor = 0;
int[][] colors = new int[9][3];
for (int i = 1, length = width - 1; i < length; i++) {
for (int k = 1, len = height - 1; k < len; k++) {
for (int m = 0; m < 9; m++) {
int s = 0;
int p = 0;
switch (m) {
case 0:
s = i - 1;
p = k - 1;
break;
case 1:
s = i;
p = k - 1;
break;
case 2:
s = i + 1;
p = k - 1;
break;
case 3:
s = i + 1;
p = k;
break;
case 4:
s = i + 1;
p = k + 1;
break;
case 5:
s = i;
p = k + 1;
break;
case 6:
s = i - 1;
p = k + 1;
break;
case 7:
s = i - 1;
p = k;
break;
case 8:
s = i;
p = k;
}
pixColor = bitmap.getPixel(s, p);
colors[m][0] = Color.red(pixColor);
colors[m][1] = Color.green(pixColor);
colors[m][2] = Color.blue(pixColor);
}
for (int m = 0; m < 9; m++) {
newR += colors[m][0];
newG += colors[m][1];
newB += colors[m][2];
}
newR = (int) (newR / 9F);
newG = (int) (newG / 9F);
newB = (int) (newB / 9F);
newR = Math.min(255, Math.max(0, newR));
newG = Math.min(255, Math.max(0, newG));
newB = Math.min(255, Math.max(0, newB));
newColor = Color.argb(255, newR, newG, newB);
newBitmap.setPixel(i, k, newColor);
newR = 0;
newG = 0;
newB = 0;
}
}
return newBitmap;
}
/**
* 浮雕效果处理
*
* @param bitmap 原图
* @return 浮雕效果处理后的图片
*/
public static Bitmap emboss(Bitmap bitmap) {
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[] pixels = new int[width * height];
bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
int pos = 0;
for (int i = 1, length = height - 1; i < length; i++) {
for (int k = 1, len = width - 1; k < len; k++) {
pos = i * width + k;
pixColor = pixels[pos];
pixR = Color.red(pixColor);
pixG = Color.green(pixColor);
pixB = Color.blue(pixColor);
pixColor = pixels[pos + 1];
newR = Color.red(pixColor) - pixR + 127;
newG = Color.green(pixColor) - pixG + 127;
newB = Color.blue(pixColor) - pixB + 127;
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[pos] = Color.argb(255, newR, newG, newB);
}
}
newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return newBitmap;
}
/**
* Add one particle to the vertexArray (native memory)
*
* @param startTime - particle born time.
* @param duration - particle duration time.
* @param fromSize - particle from size.
* @param toSize - particle to size.
* @param fromAngle - particle from angle.
* @param toAngle - particle to angle.
* @param position - particle position.
* @param direction - particle direction.
* @param fromColor - particle from color.
* @param toColor - particle to color.
* @param textureIndex - particle texture index.
*/
public void addParticle(float startTime,
float duration,
float fromSize,
float toSize,
float fromAngle,
float toAngle,
Geometry.Point position,
Geometry.Vector direction,
int fromColor,
int toColor,
int textureIndex) {
final int particleOffset = mNextParticle * TOTAL_COMPONENT_COUNT;
int currentOffset = particleOffset;
mNextParticle++;
if (mCurrentParticleCount < mMaxParticleCount) {
mCurrentParticleCount++;
}
if (mNextParticle == mMaxParticleCount) {
mNextParticle = 0;
}
mParticles[currentOffset++] = startTime;
mParticles[currentOffset++] = duration;
mParticles[currentOffset++] = fromSize;
mParticles[currentOffset++] = toSize;
mParticles[currentOffset++] = fromAngle;
mParticles[currentOffset++] = toAngle;
mParticles[currentOffset++] = position.x;
mParticles[currentOffset++] = position.y;
mParticles[currentOffset++] = position.z;
mParticles[currentOffset++] = direction.x;
mParticles[currentOffset++] = direction.y;
mParticles[currentOffset++] = direction.z;
mParticles[currentOffset++] = Color.red(fromColor) / 255f;
mParticles[currentOffset++] = Color.green(fromColor) / 255f;
mParticles[currentOffset++] = Color.blue(fromColor) / 255f;
mParticles[currentOffset++] = Color.alpha(fromColor) / 255f;
mParticles[currentOffset++] = Color.red(toColor) / 255f;
mParticles[currentOffset++] = Color.green(toColor) / 255f;
mParticles[currentOffset++] = Color.blue(toColor) / 255f;
mParticles[currentOffset++] = Color.alpha(toColor) / 255f;
mParticles[currentOffset++] = textureIndex;
// Refresh only requested part of the native memory (one particle at a time)
mVertexArray.updateBuffer(mParticles, particleOffset, TOTAL_COMPONENT_COUNT);
}
public static final Bitmap sketch(Bitmap src) {
int type = 6;
int threshold = 130;
int width = src.getWidth();
int height = src.getHeight();
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
int A, R, G, B;
int sumR, sumG, sumB;
int[][] pixels = new int[3][3];
for(int y = 0; y < height - 2; ++y) {
for(int x = 0; x < width - 2; ++x) {
// get pixel matrix
for(int i = 0; i < 3; ++i) {
for(int j = 0; j < 3; ++j) {
pixels[i][j] = src.getPixel(x + i, y + j);
}
}
// get alpha of center pixel
A = Color.alpha(pixels[1][1]);
// init color sum
sumR = sumG = sumB = 0;
sumR = (type*Color.red(pixels[1][1])) - Color.red(pixels[0][0]) - Color.red(pixels[0][2]) - Color.red(pixels[2][0]) - Color.red(pixels[2][2]);
sumG = (type*Color.green(pixels[1][1])) - Color.green(pixels[0][0]) - Color.green(pixels[0][2]) - Color.green(pixels[2][0]) - Color.green(pixels[2][2]);
sumB = (type*Color.blue(pixels[1][1])) - Color.blue(pixels[0][0]) - Color.blue(pixels[0][2]) - Color.blue(pixels[2][0]) - Color.blue(pixels[2][2]);
// get final Red
R = (int)(sumR + threshold);
if(R < 0) { R = 0; }
else if(R > 255) { R = 255; }
// get final Green
G = (int)(sumG + threshold);
if(G < 0) { G = 0; }
else if(G > 255) { G = 255; }
// get final Blue
B = (int)(sumB + threshold);
if(B < 0) { B = 0; }
else if(B > 255) { B = 255; }
result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
}
}
src.recycle();
src = null;
return result;
}
/**
* This method uses HSL to determine in a human eyesight terms if a color is light or not.
* See: http://en.wikipedia.org/wiki/HSL_and_HSV. The threshold values are from ITU Rec. 709
* http://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
*
*
* @param color A color value
* @param factor A factor of lightness measured between 0-1.0
* @return Whether or not the color is considered light
*/
public static boolean isLightColor(final int color, final float factor) {
final int r = Color.red(color);
final int g = Color.green(color);
final int b = Color.blue(color);
final double threshold = 0.21 * r + 0.72 * g + 0.07 * b;
return threshold > (RGB_TOTAL_COLORS * factor);
}