Example usage for android.graphics Color alpha

List of usage examples for android.graphics Color alpha

Introduction

In this page you can find the example usage for android.graphics Color alpha.

Prototype

@IntRange(from = 0, to = 255)
public static int alpha(int color) 

Source Link

Document

Return the alpha component of a color int.

Usage

From source file:com.jaredrummler.android.colorpicker.ColorPickerDialog.java

private void setupTransparency() {
    int progress = 255 - Color.alpha(color);
    transparencySeekBar.setMax(255);/* w ww. ja  v a  2s . c o  m*/
    transparencySeekBar.setProgress(progress);
    int percentage = (int) ((double) progress * 100 / 255);
    transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
    transparencySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int percentage = (int) ((double) progress * 100 / 255);
            transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
            int alpha = 255 - progress;
            // update items in GridView:
            for (int i = 0; i < adapter.colors.length; i++) {
                int color = adapter.colors[i];
                int red = Color.red(color);
                int green = Color.green(color);
                int blue = Color.blue(color);
                adapter.colors[i] = Color.argb(alpha, red, green, blue);
            }
            adapter.notifyDataSetChanged();
            // update shades:
            for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                ColorPanelView cpv = (ColorPanelView) layout.findViewById(R.id.cpv_color_panel_view);
                ImageView iv = (ImageView) layout.findViewById(R.id.cpv_color_image_view);
                if (layout.getTag() == null) {
                    // save the original border color
                    layout.setTag(cpv.getBorderColor());
                }
                int color = cpv.getColor();
                color = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
                if (alpha <= ALPHA_THRESHOLD) {
                    cpv.setBorderColor(color | 0xFF000000);
                } else {
                    cpv.setBorderColor((int) layout.getTag());
                }
                if (cpv.getTag() != null && (Boolean) cpv.getTag()) {
                    // The alpha changed on the selected shaded color. Update the checkmark color filter.
                    if (alpha <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        if (ColorUtils.calculateLuminance(color) >= 0.65) {
                            iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                        } else {
                            iv.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                        }
                    }
                }
                cpv.setColor(color);
            }
            // update color:
            int red = Color.red(color);
            int green = Color.green(color);
            int blue = Color.blue(color);
            color = Color.argb(alpha, red, green, blue);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

From source file:com.jrummyapps.android.colorpicker.ColorPickerDialog.java

private void setupTransparency() {
    int progress = 255 - Color.alpha(color);
    transparencySeekBar.setMax(255);// w  w  w. j av a  2  s  .  co  m
    transparencySeekBar.setProgress(progress);
    int percentage = (int) ((double) progress * 100 / 255);
    transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
    transparencySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            int percentage = (int) ((double) progress * 100 / 255);
            transparencyPercText.setText(String.format(Locale.ENGLISH, "%d%%", percentage));
            int alpha = 255 - progress;
            // update items in GridView:
            for (int i = 0; i < adapter.colors.length; i++) {
                int color = adapter.colors[i];
                int red = Color.red(color);
                int green = Color.green(color);
                int blue = Color.blue(color);
                adapter.colors[i] = Color.argb(alpha, red, green, blue);
            }
            adapter.notifyDataSetChanged();
            // update shades:
            for (int i = 0; i < shadesLayout.getChildCount(); i++) {
                FrameLayout layout = (FrameLayout) shadesLayout.getChildAt(i);
                ColorPanelView cpv = layout.findViewById(R.id.cpv_color_panel_view);
                ImageView iv = layout.findViewById(R.id.cpv_color_image_view);
                if (layout.getTag() == null) {
                    // save the original border color
                    layout.setTag(cpv.getBorderColor());
                }
                int color = cpv.getColor();
                color = Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color));
                if (alpha <= ALPHA_THRESHOLD) {
                    cpv.setBorderColor(color | 0xFF000000);
                } else {
                    cpv.setBorderColor((int) layout.getTag());
                }
                if (cpv.getTag() != null && (Boolean) cpv.getTag()) {
                    // The alpha changed on the selected shaded color. Update the checkmark color filter.
                    if (alpha <= ALPHA_THRESHOLD) {
                        iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                    } else {
                        if (ColorUtils.calculateLuminance(color) >= 0.65) {
                            iv.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_IN);
                        } else {
                            iv.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
                        }
                    }
                }
                cpv.setColor(color);
            }
            // update color:
            int red = Color.red(color);
            int green = Color.green(color);
            int blue = Color.blue(color);
            color = Color.argb(alpha, red, green, blue);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

From source file:com.google.appinventor.components.runtime.GoogleMap.java

@SimpleFunction(description = "Set the property of an existing circle. Properties include: "
        + "\"alpha\"(number, value ranging from 0~255), \"color\" (nimber, hue value ranging 0~360), "
        + "\"radius\"(number in meters)")
public void UpdateCircle(int circleId, String propertyName, Object value) {
    Log.i(TAG, "inputs: " + circleId + "," + propertyName + ", " + value);
    float[] hsv = new float[3];
    Object circle = getCircleIfExisted(circleId); // if it's null, getCircleIfExisted will show error msg
    Circle updateCircle = null; // the real circle content that gets updated

    if (circle != null) {
        if (circle instanceof DraggableCircle) {
            updateCircle = ((DraggableCircle) circle).getCircle();

        }/*from  www.ja  v  a 2 s  .com*/
        if (circle instanceof Circle) {
            updateCircle = (Circle) circle;

        }
        try {

            Float val = Float.parseFloat(value.toString());
            if (propertyName.equals("alpha")) {

                int color = updateCircle.getFillColor();
                Color.colorToHSV(color, hsv);
                Integer alphaVal = val.intValue();
                //Color.HSVToColor(mAlpha, new float[] {mColorHue, 1, 1});//default to red, medium level hue color
                int newColor = Color.HSVToColor(alphaVal, hsv);
                updateCircle.setFillColor(newColor);
            }

            if (propertyName.equals("color")) {
                int alpha = Color.alpha(updateCircle.getFillColor());

                int newColor = Color.HSVToColor(alpha, new float[] { val, 1, 1 });
                updateCircle.setFillColor(newColor);
            }

            if (propertyName.equals("radius")) {
                // need to cast value to float
                Float radius = val;
                updateCircle.setRadius(radius);
                // if it's a draggableCircle, then we need to remove the previous marker and get new radius marker
                if (circle instanceof DraggableCircle) {
                    // remove previous radius marker
                    Marker centerMarker = ((DraggableCircle) circle).getCenterMarker();
                    Marker oldMarker = ((DraggableCircle) circle).getRadiusMarker();
                    oldMarker.remove();
                    Marker newMarker = mMap.addMarker(new MarkerOptions()
                            .position(toRadiusLatLng(centerMarker.getPosition(), radius)).draggable(true)
                            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                    ((DraggableCircle) circle).setRadiusMarker(newMarker);
                    // create a new draggabble circle

                }

            }

        } catch (NumberFormatException e) { //can't parse the string
            form.dispatchErrorOccurredEvent(this, "UpdateCircle", ErrorMessages.ERROR_GOOGLE_MAP_INVALID_INPUT,
                    value.toString());
        }

    } else {
        // the circle doesn't exist
        form.dispatchErrorOccurredEvent(this, "UpdateCircle", ErrorMessages.ERROR_GOOGLE_MAP_CIRCLE_NOT_EXIST,
                circleId);
    }
}

From source file:info.papdt.blacklight.support.Utility.java

/**
 *
 * @param from color value in the form 0xAARRGGBB.
 * @param to color value in the form 0xAARRGGBB.
 *//* www. ja  va 2  s.  c o m*/
public static int getGradientColor(int from, int to, float factor) {
    int r = calculateGradient(Color.red(from), Color.red(to), factor); // It's so annoying without lambda.
    int g = calculateGradient(Color.green(from), Color.green(to), factor);
    int b = calculateGradient(Color.blue(from), Color.blue(to), factor);
    int a = calculateGradient(Color.alpha(from), Color.alpha(to), factor);

    return Color.argb(a, r, g, b);
}

From source file:am.widget.basetabstrip.BaseTabStrip.java

/**
 * ???//w w w  . java2 s  .  co m
 *
 * @param normalColor   
 * @param selectedColor 
 * @param offset        ??
 * @return ??
 */
@SuppressWarnings("unused")
protected int getColor(int normalColor, int selectedColor, float offset) {
    int normalAlpha = Color.alpha(normalColor);
    int normalRed = Color.red(normalColor);
    int normalGreen = Color.green(normalColor);
    int normalBlue = Color.blue(normalColor);
    int selectedAlpha = Color.alpha(selectedColor);
    int selectedRed = Color.red(selectedColor);
    int selectedGreen = Color.green(selectedColor);
    int selectedBlue = Color.blue(selectedColor);
    int a = (int) Math.ceil((selectedAlpha - normalAlpha) * offset);
    int r = (int) Math.ceil((selectedRed - normalRed) * offset);
    int g = (int) Math.ceil((selectedGreen - normalGreen) * offset);
    int b = (int) Math.ceil((selectedBlue - normalBlue) * offset);
    return Color.argb(normalAlpha + a, normalRed + r, normalGreen + g, normalBlue + b);
}

From source file:com.appeaser.sublimepickerlibrary.timepicker.RadialTimePickerView.java

private int getMultipliedAlpha(int argb, int alpha) {
    return (int) (Color.alpha(argb) * (alpha / 255.0) + 0.5);
}

From source file:com.doubleTwist.drawerlib.ADrawerLayout.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void adjustScrollStates(float x, float y) {
    dx = Math.round(x);/*from www.ja v  a 2  s.  c  o  m*/
    dy = Math.round(y);

    if (mContent != null) {
        dxx = Math.round(dx * mContentParalaxFactorX);
        dyy = Math.round(dy * mContentParalaxFactorY);
        setDXYCompat(mContent, dxx, dyy);

        animateLeft = mAnimateScrolling[0] && mDrawerState.mActiveDrawer == LEFT_DRAWER;
        animateTop = mAnimateScrolling[1] && mDrawerState.mActiveDrawer == TOP_DRAWER;
        animateRight = mAnimateScrolling[2] && mDrawerState.mActiveDrawer == RIGHT_DRAWER;
        animateBottom = mAnimateScrolling[3] && mDrawerState.mActiveDrawer == BOTTOM_DRAWER;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            if (animateLeft)
                aparams = mAnimationParams[0];
            else if (animateTop)
                aparams = mAnimationParams[1];
            else if (animateRight)
                aparams = mAnimationParams[2];
            else if (animateBottom)
                aparams = mAnimationParams[3];
            else
                aparams = mNoAnimationParameters;

            animScale = 1 - aparams.mScale * getScrollFraction();
            animAlpha = 1 - aparams.mAlpha * getScrollFraction();
            animRotX = -90.f * aparams.mRotX * getScrollFraction();
            animRotY = -90.f * aparams.mRotY * getScrollFraction();
            if (mContent.getScaleX() != animScale)
                mContent.setScaleX(animScale);
            if (mContent.getScaleY() != animScale)
                mContent.setScaleY(animScale);
            if (mContent.getAlpha() != animAlpha)
                mContent.setAlpha(animAlpha);
            if (mContent.getRotationY() != animRotY)
                mContent.setRotationY(animRotY);
            if (mContent.getRotationX() != animRotX)
                mContent.setRotationX(animRotX);
        }
    }

    if (mContentDimmer != null) {
        dimLeft = mDimContent[0] && mDrawerState.mActiveDrawer == LEFT_DRAWER;
        dimTop = mDimContent[1] && mDrawerState.mActiveDrawer == TOP_DRAWER;
        dimRight = mDimContent[2] && mDrawerState.mActiveDrawer == RIGHT_DRAWER;
        dimBottom = mDimContent[3] && mDrawerState.mActiveDrawer == BOTTOM_DRAWER;

        // dxx = Math.round(dx);
        // if( mDrawerState.mActiveDrawer == LEFT_DRAWER ) dxx += mPeekSize.left;
        // if( mDrawerState.mActiveDrawer == RIGHT_DRAWER ) dxx -= mPeekSize.right;
        // dyy = Math.round(dy);
        // if( mDrawerState.mActiveDrawer == TOP_DRAWER ) dyy += mPeekSize.top;
        // if( mDrawerState.mActiveDrawer == BOTTOM_DRAWER ) dyy -= mPeekSize.bottom;

        if (isClosed() || dimLeft || dimTop || dimRight || dimBottom) {
            // dxx = Math.round(dx * mContentParalaxFactorX);
            // dyy = Math.round(dy * mContentParalaxFactorY);
            dxx = dx;
            dyy = dy;
            if (mPeekSize != null) {
                if (dimLeft)
                    dxx += mPeekSize.left;
                if (dimRight)
                    dxx -= mPeekSize.right;
                if (dimTop)
                    dyy += mPeekSize.top;
                if (dimBottom)
                    dyy -= mPeekSize.bottom;
            }
            setDXYCompat(mContentDimmer, dxx, dyy);

            alpha = getScrollFraction();
            alpha = Math.min(1.f, Math.max(0.f, alpha));
            mContentDimmer.setBackgroundColor(Color.argb(Math.round(alpha * Color.alpha(mDimmingColor)),
                    Color.red(mDimmingColor), Color.green(mDimmingColor), Color.blue(mDimmingColor)));
        }
    }

    if (mLeft != null) {
        dxx = -mLeft.getMeasuredWidth() + Math.max(0, dx);
        dyy = dy;
        setDXYCompat(mLeft, dxx, dyy);
    }

    if (mRight != null) {
        dxx = mRight.getMeasuredWidth() + Math.min(0, dx);
        dyy = dy;
        setDXYCompat(mRight, dxx, dyy);
    }

    if (mTop != null) {
        dxx = dx;
        dyy = -mTop.getMeasuredHeight() + Math.max(0, dy);
        setDXYCompat(mTop, dxx, dyy);
    }

    if (mBottom != null) {
        dxx = dx;
        dyy = mBottom.getMeasuredHeight() + Math.min(0, dy);
        setDXYCompat(mBottom, dxx, dyy);
    }
}

From source file:com.upsideideas.zdrawerlayout.ADrawerLayout.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
protected void adjustScrollStates(float x, float y) {
    dx = Math.round(x);/*  ww w .j a  va  2  s .  co  m*/
    dy = Math.round(y);

    if (mContent != null) {
        dxx = Math.round(dx * mContentParalaxFactorX);
        dyy = Math.round(dy * mContentParalaxFactorY);
        setDXYCompat(mContent, dxx, dyy);

        animateLeft = mAnimateScrolling[0] && mDrawerState.mActiveDrawer == LEFT_DRAWER;
        animateTop = mAnimateScrolling[1] && mDrawerState.mActiveDrawer == TOP_DRAWER;
        animateRight = mAnimateScrolling[2] && mDrawerState.mActiveDrawer == RIGHT_DRAWER;
        animateBottom = mAnimateScrolling[3] && mDrawerState.mActiveDrawer == BOTTOM_DRAWER;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            if (animateLeft)
                aparams = mAnimationParams[0];
            else if (animateTop)
                aparams = mAnimationParams[1];
            else if (animateRight)
                aparams = mAnimationParams[2];
            else if (animateBottom)
                aparams = mAnimationParams[3];
            else
                aparams = mNoAnimationParameters;

            animScale = 1 - aparams.mScale * getScrollFraction();
            animAlpha = 1 - aparams.mAlpha * getScrollFraction();
            animRotX = -90.f * aparams.mRotX * getScrollFraction();
            animRotY = -90.f * aparams.mRotY * getScrollFraction();
            if (mContent.getScaleX() != animScale)
                mContent.setScaleX(animScale);
            if (mContent.getScaleY() != animScale)
                mContent.setScaleY(animScale);
            if (mContent.getAlpha() != animAlpha)
                mContent.setAlpha(animAlpha);
            if (mContent.getRotationY() != animRotY)
                mContent.setRotationY(animRotY);
            if (mContent.getRotationX() != animRotX)
                mContent.setRotationX(animRotX);
        }
    }

    if (mContentDimmer != null) {
        dimLeft = mDimContent[0] && mDrawerState.mActiveDrawer == LEFT_DRAWER;
        dimTop = mDimContent[1] && mDrawerState.mActiveDrawer == TOP_DRAWER;
        dimRight = mDimContent[2] && mDrawerState.mActiveDrawer == RIGHT_DRAWER;
        dimBottom = mDimContent[3] && mDrawerState.mActiveDrawer == BOTTOM_DRAWER;

        // dxx = Math.round(dx);
        // if( mDrawerState.mActiveDrawer == LEFT_DRAWER ) dxx += mPeekSize.left;
        // if( mDrawerState.mActiveDrawer == RIGHT_DRAWER ) dxx -= mPeekSize.right;
        // dyy = Math.round(dy);
        // if( mDrawerState.mActiveDrawer == TOP_DRAWER ) dyy += mPeekSize.top;
        // if( mDrawerState.mActiveDrawer == BOTTOM_DRAWER ) dyy -= mPeekSize.bottom;

        if (isClosed() || dimLeft || dimTop || dimRight || dimBottom) {
            // dxx = Math.round(dx * mContentParalaxFactorX);
            // dyy = Math.round(dy * mContentParalaxFactorY);
            dxx = dx;
            dyy = dy;
            if (mPeekSize != null) {
                if (dimLeft)
                    dxx += mPeekSize.left;
                if (dimRight)
                    dxx -= mPeekSize.right;
                if (dimTop)
                    dyy += mPeekSize.top;
                if (dimBottom)
                    dyy -= mPeekSize.bottom;
            }
            setDXYCompat(mContentDimmer, dxx, dyy);

            alpha = getScrollFraction();
            alpha = Math.min(1.f, Math.max(0.f, alpha));
            mContentDimmer.setBackgroundColor(Color.argb(Math.round(alpha * Color.alpha(mDimmingColor)),
                    Color.red(mDimmingColor), Color.green(mDimmingColor), Color.blue(mDimmingColor)));
        }
    }

    if (mLeft != null) {
        dxx = -mLeft.getMeasuredWidth() + Math.max(0, dx);
        dyy = dy;
        setDXYCompat(mLeft, dxx, dyy);
    }

    if (mRight != null) {
        dxx = mRight.getMeasuredWidth() + Math.min(0, dx);
        dyy = dy;
        setDXYCompat(mRight, dxx, dyy);
    }

    if (mTop != null) {
        dxx = dx;
        dyy = -mTop.getMeasuredHeight() + Math.max(0, dy);
        setDXYCompat(mTop, dxx, dyy);
    }

    if (mBottom != null) {
        dxx = dx;
        dyy = mBottom.getMeasuredHeight() + Math.min(0, dy);
        setDXYCompat(mBottom, dxx, dyy);
    }
}

From source file:com.mikepenz.iconics.IconicsDrawable.java

/**
 * Ensures that the icon paint and alpha is consistent with icon state, invalidates icon if
 * any changes were made//from  w w w.ja v  a2  s.  c  o m
 */
private void updateIconColor() {
    boolean invalidate = false;

    int color = mIconColor.getColorForState(getState(), mIconColor.getDefaultColor());
    int red = Color.red(color);
    int green = Color.green(color);
    int blue = Color.blue(color);

    int iconColor = Color.rgb(red, green, blue);
    if (iconColor != mIconPaint.getColor()) {
        mIconPaint.setColor(iconColor);
        invalidate = true;
    }

    int alpha = Color.alpha(color);
    if (alpha != 255 && alpha != mAlpha) {
        setAlpha(alpha);
    } else if (invalidate) {
        invalidateSelf();
    }
}

From source file:com.mixiaoxiao.support.widget.SmoothSwitch.java

private int convertColorOfPostion(float position, int color0, int color1) {
    int a0 = Color.alpha(color0);
    int r0 = Color.red(color0);
    int g0 = Color.green(color0);
    int b0 = Color.blue(color0);

    int a1 = Color.alpha(color1);
    int r1 = Color.red(color1);
    int g1 = Color.green(color1);
    int b1 = Color.blue(color1);
    int a = (int) (a0 + (a1 - a0) * position);
    int r = (int) (r0 + (r1 - r0) * position);
    int g = (int) (g0 + (g1 - g0) * position);
    int b = (int) (b0 + (b1 - b0) * position);
    return Color.argb(a, r, g, b);
}