Java Code Examples for android.graphics.Color#alpha()

The following examples show how to use android.graphics.Color#alpha() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ColorPickerView.java    From LyricHere with Apache License 2.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 *
 * @param color    The color that should be selected.
 * @param callback If you want to get a callback to
 *                 your OnColorChangedListener.
 */
public void setColor(int color, boolean callback) {

    int alpha = Color.alpha(color);

    float[] hsv = new float[3];

    Color.colorToHSV(color, hsv);

    mAlpha = alpha;
    mHue = hsv[0];
    mSat = hsv[1];
    mVal = hsv[2];

    if (callback && mListener != null) {
        mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
    }

    invalidate();
}
 
Example 2
Source File: LedMatrix.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
/**
 * Draw the given color to the LED matrix.
 * @param color Color to draw
 * @throws IOException
 */
public void draw(int color) throws IOException {
    mBuffer[0] = 0;
    float a = Color.alpha(color) / 255.f;
    byte r = (byte)((int)(Color.red(color)*a)>>3);
    byte g = (byte)((int)(Color.green(color)*a)>>3);
    byte b = (byte)((int)(Color.blue(color)*a)>>3);
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            mBuffer[1+x+WIDTH*0+3*WIDTH*y] = r;
            mBuffer[1+x+WIDTH*1+3*WIDTH*y] = g;
            mBuffer[1+x+WIDTH*2+3*WIDTH*y] = b;
        }
    }
    mDevice.write(mBuffer, mBuffer.length);
}
 
Example 3
Source File: GreyScaleCalculus.java    From Alexei with Apache License 2.0 6 votes vote down vote up
@Override
protected Bitmap theCalculation(Bitmap image) throws Exception {

    Bitmap bitmap = image.copy(image.getConfig(), true);

    for(int i = 0; i < bitmap.getWidth(); i++)
        for(int j = 0; j < bitmap.getHeight(); j++) {
            int pixel = image.getPixel(i,j);

            int alpha = Color.alpha(pixel);
            int red = Color.red(pixel);
            int green = Color.green(pixel);
            int blue = Color.blue(pixel);

            red = green = blue = (int) (RED_FACTOR * red + GREEN_FACTOR * green + BLUE_FACTOR * blue);

            bitmap.setPixel(i,j,Color.argb(alpha,red,green,blue));
        }

    return bitmap;
}
 
Example 4
Source File: ColorPickerView.java    From ColorPicker with Apache License 2.0 6 votes vote down vote up
/**
 * Set the color this view should show.
 *
 * @param color The color that should be selected. #argb
 * @param callback If you want to get a callback to your OnColorChangedListener.
 */
public void setColor(int color, boolean callback) {

  int alpha = Color.alpha(color);
  int red = Color.red(color);
  int blue = Color.blue(color);
  int green = Color.green(color);

  float[] hsv = new float[3];

  Color.RGBToHSV(red, green, blue, hsv);

  this.alpha = alpha;
  hue = hsv[0];
  sat = hsv[1];
  val = hsv[2];

  if (callback && onColorChangedListener != null) {
    onColorChangedListener.onColorChanged(Color.HSVToColor(this.alpha, new float[] { hue, sat, val }));
  }

  invalidate();
}
 
Example 5
Source File: AlmostRippleDrawable.java    From sealrtc-android with MIT License 5 votes vote down vote up
private static int getModulatedAlphaColor(int alphaValue, int originalColor) {
    int alpha = Color.alpha(originalColor);
    int scale = alphaValue + (alphaValue >> 7);
    alpha = alpha * scale >> 8;
    return Color.argb(
            alpha,
            Color.red(originalColor),
            Color.green(originalColor),
            Color.blue(originalColor));
}
 
Example 6
Source File: FabView.java    From fabuless with Apache License 2.0 5 votes vote down vote up
protected static int transformColor(int fromColor, int toColor, float factor) {
	final float defactor = 1f - factor;
	final int alpha = (int) (defactor * Color.alpha(fromColor) + factor * Color.alpha(toColor));
	final int red = (int) (defactor * Color.red(fromColor) + factor * Color.red(toColor));
	final int green = (int) (defactor * Color.green(fromColor) + factor * Color.green(toColor));
	final int blue = (int) (defactor * Color.blue(fromColor) + factor * Color.blue(toColor));
	return Color.argb(alpha, red, green, blue);
}
 
Example 7
Source File: ColorUtils.java    From materialup with Apache License 2.0 5 votes vote down vote up
/**
 * Blend {@code color1} and {@code color2} using the given ratio.
 *
 * @param ratio of which to blend. 0.0 will return {@code color1}, 0.5 will give an even blend,
 *              1.0 will return {@code color2}.
 */
public static
@ColorInt
int blendColors(@ColorInt int color1,
                @ColorInt int color2,
                @FloatRange(from = 0f, to = 1f) 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);
}
 
Example 8
Source File: ColorPickerDialogPreference.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
private int shadeColor(@ColorInt int color, double percent) {
    String hex = String.format("#%06X", (0xFFFFFF & color));
    long f = Long.parseLong(hex.substring(1), 16);
    double t = percent < 0 ? 0 : 255;
    double p = percent < 0 ? percent * -1 : percent;
    long R = f >> 16;
    long G = f >> 8 & 0x00FF;
    long B = f & 0x0000FF;
    int alpha = Color.alpha(color);
    int red = (int) (Math.round((t - R) * p) + R);
    int green = (int) (Math.round((t - G) * p) + G);
    int blue = (int) (Math.round((t - B) * p) + B);
    return Color.argb(alpha, red, green, blue);
}
 
Example 9
Source File: ColorUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
/**
 * 获得颜色 ARGB 各个纬度的分量
 *
 * @param color 颜色
 * @return      ARGB 各个分量组成的数组
 */
public static int[] getColorARGB(@ColorInt int color) {
    int[] argb = new int[4];
    argb[0] = Color.alpha(color);
    argb[1] = Color.red(color);
    argb[2] = Color.green(color);
    argb[3] = Color.blue(color);
    return argb;
}
 
Example 10
Source File: RoundRectDrawable.java    From holoaccent with Apache License 2.0 5 votes vote down vote up
private Paint initBorderPaint(DisplayMetrics displayMetrics, float borderWidthDp, int borderColor) {
	if (Color.alpha(borderColor) == 0)
		return null;
	if (borderWidthDp <= 0f)
		return null;
	
       float borderWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, borderWidthDp, displayMetrics);
	Paint result = new Paint();
	result.setColor(borderColor);
	result.setStyle(Paint.Style.STROKE);
	result.setStrokeWidth(borderWidth);
	result.setAntiAlias(true);
	return result;
}
 
Example 11
Source File: MarkerDrawable.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static int blendColors(int color1, int color2, float factor) {
    final float inverseFactor = 1f - factor;
    float a = (Color.alpha(color1) * factor) + (Color.alpha(color2) * inverseFactor);
    float r = (Color.red(color1) * factor) + (Color.red(color2) * inverseFactor);
    float g = (Color.green(color1) * factor) + (Color.green(color2) * inverseFactor);
    float b = (Color.blue(color1) * factor) + (Color.blue(color2) * inverseFactor);
    return Color.argb((int) a, (int) r, (int) g, (int) b);
}
 
Example 12
Source File: TintImageView.java    From actor-platform with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void onDraw(Canvas canvas) {

    long animationTime = SystemClock.uptimeMillis() - animationStart;
    if (animationTime > TINT_ANIMATION_TIME) {
        currentTintColor = destTintColor;
    } else {
        float alpha = animationTime / (float) TINT_ANIMATION_TIME;
        int sR = Color.red(startTintColor);
        int sG = Color.green(startTintColor);
        int sB = Color.blue(startTintColor);
        int sA = Color.alpha(startTintColor);

        int dR = Color.red(destTintColor);
        int dG = Color.green(destTintColor);
        int dB = Color.blue(destTintColor);
        int dA = Color.alpha(destTintColor);

        int r = (int) (sR * (1 - alpha) + dR * (alpha));
        int g = (int) (sG * (1 - alpha) + dG * (alpha));
        int b = (int) (sB * (1 - alpha) + dB * (alpha));
        int a = (int) (sA * (1 - alpha) + dA * (alpha));

        currentTintColor = Color.argb(a, r, g, b);

        invalidate();
    }

    if (baseBitmap != null) {

        PorterDuffColorFilter filter = new PorterDuffColorFilter(currentTintColor, PorterDuff.Mode.SRC_IN);
        PAINT.setColorFilter(filter);

        int x = (getWidth() - baseBitmap.getWidth()) / 2;
        int y = (getHeight() - baseBitmap.getHeight()) / 2;

        canvas.drawBitmap(baseBitmap, x, y, PAINT);
    }
}
 
Example 13
Source File: ColorUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
/**
 * Finds the minimum alpha value which can be applied to {@code foreground} so that is has a
 * contrast value of at least {@code minContrastRatio} when compared to background.
 *
 * @return the alpha value in the range 0-255.
 */
private static int findMinimumAlpha(int foreground, int background, double minContrastRatio) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException("background can not be translucent");
    }

    // First lets check that a fully opaque foreground has sufficient contrast
    int testForeground = modifyAlpha(foreground, 255);
    double testRatio = calculateContrast(testForeground, background);
    if (testRatio < minContrastRatio) {
        // Fully opaque foreground does not have sufficient contrast, return error
        return -1;
    }

    // Binary search to find a value with the minimum value which provides sufficient contrast
    int numIterations = 0;
    int minAlpha = 0;
    int maxAlpha = 255;

    while (numIterations <= MIN_ALPHA_SEARCH_MAX_ITERATIONS &&
            (maxAlpha - minAlpha) > MIN_ALPHA_SEARCH_PRECISION) {
        final int testAlpha = (minAlpha + maxAlpha) / 2;

        testForeground = modifyAlpha(foreground, testAlpha);
        testRatio = calculateContrast(testForeground, background);

        if (testRatio < minContrastRatio) {
            minAlpha = testAlpha;
        } else {
            maxAlpha = testAlpha;
        }

        numIterations++;
    }

    // Conservatively return the max of the range of possible alphas, which is known to pass.
    return maxAlpha;
}
 
Example 14
Source File: TaskSnapshotSurface.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void drawNavigationBarBackground(Canvas c) {
    final Rect navigationBarRect = new Rect();
    getNavigationBarRect(c.getWidth(), c.getHeight(), mStableInsets, mContentInsets,
            navigationBarRect);
    final boolean visible = isNavigationBarColorViewVisible();
    if (visible && Color.alpha(mNavigationBarColor) != 0 && !navigationBarRect.isEmpty()) {
        c.drawRect(navigationBarRect, mNavigationBarPaint);
    }
}
 
Example 15
Source File: LedMatrix.java    From contrib-drivers with Apache License 2.0 5 votes vote down vote up
/**
 * Draw the given bitmap to the LED matrix.
 * @param bitmap Bitmap to draw
 * @throws IOException
 */
public void draw(Bitmap bitmap) throws IOException {
    Bitmap dest = Bitmap.createScaledBitmap(bitmap, 8, 8, true);
    mBuffer[0] = 0;
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            int p = bitmap.getPixel(x, y);
            float a = Color.alpha(p) / 255.f;
            mBuffer[1+x+WIDTH*0+3*WIDTH*y] = (byte)((int)(Color.red(p)*a)>>3);
            mBuffer[1+x+WIDTH*1+3*WIDTH*y] = (byte)((int)(Color.green(p)*a)>>3);
            mBuffer[1+x+WIDTH*2+3*WIDTH*y] = (byte)((int)(Color.blue(p)*a)>>3);
        }
    }
    mDevice.write(mBuffer, mBuffer.length);
}
 
Example 16
Source File: SubtitlePainter.java    From no-player with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("PMD.NPathComplexity")  // TODO break this method up
private void drawTextLayout(Canvas canvas) {
    StaticLayout layout = textLayout;
    if (layout == null) {
        // Nothing to draw.
        return;
    }

    int saveCount = canvas.save();
    canvas.translate(textLeft, textTop);

    if (Color.alpha(windowColor) > 0) {
        paint.setColor(windowColor);
        canvas.drawRect(-textPaddingX, 0, layout.getWidth() + textPaddingX, layout.getHeight(),
                paint);
    }

    if (Color.alpha(backgroundColor) > 0) {
        paint.setColor(backgroundColor);
        float previousBottom = layout.getLineTop(0);
        int lineCount = layout.getLineCount();
        for (int i = 0; i < lineCount; i++) {
            lineBounds.left = layout.getLineLeft(i) - textPaddingX;
            lineBounds.right = layout.getLineRight(i) + textPaddingX;
            lineBounds.top = previousBottom;
            lineBounds.bottom = layout.getLineBottom(i);
            previousBottom = lineBounds.bottom;
            canvas.drawRoundRect(lineBounds, cornerRadius, cornerRadius, paint);
        }
    }

    if (edgeType == CaptionStyleCompat.EDGE_TYPE_OUTLINE) {
        textPaint.setStrokeJoin(Join.ROUND);
        textPaint.setStrokeWidth(outlineWidth);
        textPaint.setColor(edgeColor);
        textPaint.setStyle(Style.FILL_AND_STROKE);
        layout.draw(canvas);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_DROP_SHADOW) {
        textPaint.setShadowLayer(shadowRadius, shadowOffset, shadowOffset, edgeColor);
    } else if (edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED
            || edgeType == CaptionStyleCompat.EDGE_TYPE_DEPRESSED) {
        boolean raised = edgeType == CaptionStyleCompat.EDGE_TYPE_RAISED;
        int colorUp = raised ? Color.WHITE : edgeColor;
        int colorDown = raised ? edgeColor : Color.WHITE;
        float offset = shadowRadius / 2;
        textPaint.setColor(foregroundColor);
        textPaint.setStyle(Style.FILL);
        textPaint.setShadowLayer(shadowRadius, -offset, -offset, colorUp);
        layout.draw(canvas);
        textPaint.setShadowLayer(shadowRadius, offset, offset, colorDown);
    }

    textPaint.setColor(foregroundColor);
    textPaint.setStyle(Style.FILL);
    layout.draw(canvas);
    textPaint.setShadowLayer(0, 0, 0, 0);

    canvas.restoreToCount(saveCount);
}
 
Example 17
Source File: VectorDrawableCompat.java    From VectorChildFinder with Apache License 2.0 4 votes vote down vote up
static int applyAlpha(int color, float alpha) {
    int alphaBytes = Color.alpha(color);
    color &= 0x00FFFFFF;
    color |= ((int) (alphaBytes * alpha)) << 24;
    return color;
}
 
Example 18
Source File: ScrollBarHelper.java    From ticdesign with Apache License 2.0 4 votes vote down vote up
private void setColorWithOpacity(Paint paint, int color, float opacity) {
    int targetAlpha = (int) (Color.alpha(color) * opacity);
    paint.setColor(color);
    paint.setAlpha(targetAlpha);
}
 
Example 19
Source File: ObservableColor.java    From hsv-alpha-color-picker-android with Apache License 2.0 4 votes vote down vote up
public void updateColor(int color, ColorObserver sender) {
	Color.colorToHSV(color, hsv);
	alpha = Color.alpha(color);
	notifyOtherObservers(sender);
}
 
Example 20
Source File: ColorPickerPreference.java    From AcDisplay with GNU General Public License v2.0 2 votes vote down vote up
/**
 * @return {@code true} if you should generate random colors instead
 * of this one, {@code false} otherwise.
 * @see #getColor(int)
 */
public static boolean isRandomEnabled(int color) {
    return Color.alpha(color) == RANDOM_COLOR_ALPHA_MASK;
}