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

The following are Jave code examples for showing how to use alpha() of the android.graphics.Color class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: baselibrary-master   File: SmoothCheckBox.java   View Source Code Vote up 6 votes
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);
}
 
Example 2
Project: GitHub   File: ColorUtils.java   View Source Code Vote up 6 votes
/**
 * Returns the contrast ratio between {@code foreground} and {@code background}.
 * {@code background} must be opaque.
 * <p>
 * Formula defined
 * <a href="http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef">here</a>.
 */
public static double calculateContrast(@ColorInt int foreground, @ColorInt int background) {
    if (Color.alpha(background) != 255) {
        throw new IllegalArgumentException("background can not be translucent: #"
                + Integer.toHexString(background));
    }
    if (Color.alpha(foreground) < 255) {
        // If the foreground is translucent, composite the foreground over the background
        foreground = compositeColors(foreground, background);
    }

    final double luminance1 = calculateLuminance(foreground) + 0.05;
    final double luminance2 = calculateLuminance(background) + 0.05;

    // Now return the lighter luminance divided by the darker luminance
    return Math.max(luminance1, luminance2) / Math.min(luminance1, luminance2);
}
 
Example 3
Project: MyCalendar   File: FloatingActionButtonLibrary.java   View Source Code Vote up 6 votes
private Drawable createCircleDrawable(int color, float strokeWidth) {
  int alpha = Color.alpha(color);
  int opaqueColor = opaque(color);

  ShapeDrawable fillDrawable = new ShapeDrawable(new OvalShape());

  final Paint paint = fillDrawable.getPaint();
  paint.setAntiAlias(true);
  paint.setColor(opaqueColor);

  Drawable[] layers = {
      fillDrawable,
      createInnerStrokesDrawable(opaqueColor, strokeWidth)
  };

  LayerDrawable drawable = alpha == 255 || !mStrokeVisible
      ? new LayerDrawable(layers)
      : new TranslucentLayerDrawable(alpha, layers);

  int halfStrokeWidth = (int) (strokeWidth / 2f);
  drawable.setLayerInset(1, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth, halfStrokeWidth);

  return drawable;
}
 
Example 4
Project: PrettyDialog   File: PrettyDialogButton.java   View Source Code Vote up 5 votes
private int getLightenColor(int color){
    double fraction = 0.2;
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);
    red = (int)Math.min(red + (red * fraction), 255);
    green = (int)Math.min(green + (green * fraction), 255);
    blue = (int)Math.min(blue + (blue * fraction), 255);
    int alpha = Color.alpha(color);
    return Color.argb(alpha, red, green, blue);
}
 
Example 5
Project: RoundButton   File: RoundButton.java   View Source Code Vote up 5 votes
private int manipulateColor(int color, @FloatRange(from = 0, to = 2) float factor) {
    int a = Color.alpha(color);
    int r = Math.round(Color.red(color) * factor);
    int g = Math.round(Color.green(color) * factor);
    int b = Math.round(Color.blue(color) * factor);
    return Color.argb(a, Math.min(r, 255), Math.min(g, 255), Math.min(b, 255));
}
 
Example 6
Project: ProgressManager   File: a.java   View Source Code Vote up 5 votes
@Override
protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
                                    boolean first) {
    if (mParent == null) {
        super.onApplyThemeResource(theme, resid, first);
    } else {
        try {
            theme.setTo(mParent.getTheme());
        } catch (Exception e) {
            // Empty
        }
        theme.applyStyle(resid, false);
    }

    // Get the primary color and update the TaskDescription for this activity
    TypedArray a = theme.obtainStyledAttributes(
            com.android.internal.R.styleable.ActivityTaskDescription);
    if (mTaskDescription.getPrimaryColor() == 0) {
        int colorPrimary = a.getColor(
                com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
        if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
            mTaskDescription.setPrimaryColor(colorPrimary);
        }
    }
    // For dev-preview only.
    if (mTaskDescription.getBackgroundColor() == 0) {
        int colorBackground = a.getColor(
                com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
        if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
            mTaskDescription.setBackgroundColor(colorBackground);
        }
    }
    a.recycle();
    setTaskDescription(mTaskDescription);
}
 
Example 7
Project: ProgressManager   File: a.java   View Source Code Vote up 5 votes
@Override
protected void onApplyThemeResource(Resources.Theme theme, @StyleRes int resid,
                                    boolean first) {
    if (mParent == null) {
        super.onApplyThemeResource(theme, resid, first);
    } else {
        try {
            theme.setTo(mParent.getTheme());
        } catch (Exception e) {
            // Empty
        }
        theme.applyStyle(resid, false);
    }

    // Get the primary color and update the TaskDescription for this activity
    TypedArray a = theme.obtainStyledAttributes(
            com.android.internal.R.styleable.ActivityTaskDescription);
    if (mTaskDescription.getPrimaryColor() == 0) {
        int colorPrimary = a.getColor(
                com.android.internal.R.styleable.ActivityTaskDescription_colorPrimary, 0);
        if (colorPrimary != 0 && Color.alpha(colorPrimary) == 0xFF) {
            mTaskDescription.setPrimaryColor(colorPrimary);
        }
    }
    // For dev-preview only.
    if (mTaskDescription.getBackgroundColor() == 0) {
        int colorBackground = a.getColor(
                com.android.internal.R.styleable.ActivityTaskDescription_colorBackground, 0);
        if (colorBackground != 0 && Color.alpha(colorBackground) == 0xFF) {
            mTaskDescription.setBackgroundColor(colorBackground);
        }
    }
    a.recycle();
    setTaskDescription(mTaskDescription);
}
 
Example 8
Project: EmoticonGIFKeyboard   File: EmoticonGifImageView.java   View Source Code Vote up 5 votes
/**
 * Get the dark color.
 *
 * @param color Original color.
 * @return Dark color.
 */
@ColorInt
private static int getDarkColor(final int color) {
    final float factor = 0.6f;

    final int a = Color.alpha(color);
    final int r = Math.round(Color.red(color) * factor);
    final int g = Math.round(Color.green(color) * factor);
    final int b = Math.round(Color.blue(color) * factor);
    return Color.argb(a,
            Math.min(r, 255),
            Math.min(g, 255),
            Math.min(b, 255));
}
 
Example 9
Project: SmartRefreshLayout   File: ColorUtils.java   View Source Code Vote up 5 votes
/**
 * Composite two potentially translucent colors over each other and returns the result.
 */
public static int compositeColors(@ColorInt int foreground, @ColorInt int background) {
    int bgAlpha = Color.alpha(background);
    int fgAlpha = Color.alpha(foreground);
    int a = compositeAlpha(fgAlpha, bgAlpha);

    int r = compositeComponent(Color.red(foreground), fgAlpha,
            Color.red(background), bgAlpha, a);
    int g = compositeComponent(Color.green(foreground), fgAlpha,
            Color.green(background), bgAlpha, a);
    int b = compositeComponent(Color.blue(foreground), fgAlpha,
            Color.blue(background), bgAlpha, a);

    return Color.argb(a, r, g, b);
}
 
Example 10
Project: FireFiles   File: ColorUtils.java   View Source Code Vote up 5 votes
/**
 * 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 11
Project: SimpleUILauncher   File: FocusIndicatorHelper.java   View Source Code Vote up 5 votes
public FocusIndicatorHelper(View container) {
    mContainer = container;

    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    int color = container.getResources().getColor(R.color.focused_background);
    mMaxAlpha = Color.alpha(color);
    mPaint.setColor(0xFF000000 | color);

    setAlpha(0);
    mShift = 0;
}
 
Example 12
Project: MetadataEditor   File: ColorUtil.java   View Source Code Vote up 5 votes
@ColorInt
public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
    if (by == 1f) return color;
    int alpha = Color.alpha(color);
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] *= by; // value component
    return (alpha << 24) + (0x00ffffff & Color.HSVToColor(hsv));
}
 
Example 13
Project: boohee_v5.6   File: ChartUtils.java   View Source Code Vote up 5 votes
public static int darkenColor(int color) {
    hsv = new float[3];
    int alpha = Color.alpha(color);
    Color.colorToHSV(color, hsv);
    hsv[1] = Math.min(hsv[1] * DARKEN_SATURATION, 1.0f);
    hsv[2] = hsv[2] * DARKEN_INTENSITY;
    int tempColor = Color.HSVToColor(hsv);
    return Color.argb(alpha, Color.red(tempColor), Color.green(tempColor), Color.blue(tempColor));
}
 
Example 14
Project: hsv-alpha-color-picker   File: ObservableColor.java   View Source Code Vote up 4 votes
ObservableColor(int color) {
	Color.colorToHSV(color, hsv);
	alpha = Color.alpha(color);
}
 
Example 15
Project: music_player   File: ScrimUtil.java   View Source Code Vote up 4 votes
/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details.
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

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

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
        case Gravity.LEFT:  x0 = 1; x1 = 0; break;
        case Gravity.RIGHT: x0 = 0; x1 = 1; break;
        default:            x0 = 0; x1 = 0; break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
        case Gravity.TOP:    y0 = 1; y1 = 0; break;
        case Gravity.BOTTOM: y0 = 0; y1 = 1; break;
        default:             y0 = 0; y1 = 0; break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            return new LinearGradient(
                    width * x0,
                    height * y0,
                    width * x1,
                    height * y1,
                    stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
 
Example 16
Project: ShadowLibrary-Android   File: ShadowLayout.java   View Source Code Vote up 4 votes
public void setShadowColor(final int shadowColor) {
    mShadowColor = shadowColor;
    mShadowAlpha = Color.alpha(shadowColor);

    resetShadow();
}
 
Example 17
Project: letv   File: ColorUtils.java   View Source Code Vote up 4 votes
public static int compositeColors(@ColorInt int foreground, @ColorInt int background) {
    int bgAlpha = Color.alpha(background);
    int fgAlpha = Color.alpha(foreground);
    int a = compositeAlpha(fgAlpha, bgAlpha);
    return Color.argb(a, compositeComponent(Color.red(foreground), fgAlpha, Color.red(background), bgAlpha, a), compositeComponent(Color.green(foreground), fgAlpha, Color.green(background), bgAlpha, a), compositeComponent(Color.blue(foreground), fgAlpha, Color.blue(background), bgAlpha, a));
}
 
Example 18
Project: XERUNG   File: CheckableDrawable.java   View Source Code Vote up 4 votes
public void setCheckedColor(int checkedColor) {
    this.checkedColor = checkedColor;
    checkedFilter = new LightingColorFilter(0, checkedColor);
    checkedAlpha = Color.alpha(checkedColor);
    invalidateSelf();
}
 
Example 19
Project: AOSP-Kayboard-7.1.2   File: SuggestionStripLayoutHelper.java   View Source Code Vote up 4 votes
private static int applyAlpha(final int color, final float alpha) {
    final int newAlpha = (int)(Color.alpha(color) * alpha);
    return Color.argb(newAlpha, Color.red(color), Color.green(color), Color.blue(color));
}
 
Example 20
Project: GravityBox   File: KeyButtonRipple.java   View Source Code Vote up 4 votes
public void setGlowColor(int color) {
    int rgb = Color.rgb(Color.red(color), Color.green(color), Color.blue(color));
    getRipplePaint().setColor(rgb);
    mGlowAlphaMax = Color.alpha(color) / 255f;
}