Example usage for android.graphics Color argb

List of usage examples for android.graphics Color argb

Introduction

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

Prototype

@ColorInt
public static int argb(float alpha, float red, float green, float blue) 

Source Link

Document

Return a color-int from alpha, red, green, blue float components in the range \([0..1]\).

Usage

From source file:com.pranavpandey.smallapp.theme.DynamicTheme.java

/**
 * Calculate tint based on a given color for better readability.
 *
 * @param color whose tint to be calculated.
 *
 * @return Tint of color.//from   w  w w  .  j  a  v  a  2 s  .  com
 */
public static @ColorInt int getTintColor(@ColorInt int color) {
    int finalColor;
    double tint;
    int a = Color.alpha(color);
    int r = Color.red(color);
    int g = Color.green(color);
    int b = Color.blue(color);
    double colorDarkness = getColorDarkness(color);
    boolean isColorDark;

    if (isColorDark = isColorDark(color)) {
        if (colorDarkness >= 0.5f && colorDarkness < 0.6f) {
            tint = 0.9f;
        } else if (colorDarkness >= 0.6f && colorDarkness < 0.65f) {
            tint = 0.8f;
        } else {
            tint = 0.7f;
        }
        finalColor = Color.argb(a, (int) (r + (tint * (255 - r))), (int) (g + (tint * (255 - g))),
                (int) (b + (tint * (255 - b))));
    } else {
        if (colorDarkness < 0.5f && colorDarkness >= 0.4f) {
            tint = 0.4f;
        } else if (colorDarkness < 0.4f && colorDarkness >= 0.3f) {
            tint = 0.5f;
        } else {
            tint = 0.6f;
        }
        finalColor = Color.argb((int) Math.min(1.33 * a, 255), (int) (r * tint), (int) (g * tint),
                (int) (b * tint));
    }

    if (calculateContrast(color, finalColor) < 0.3f) {
        int newTint = 25;
        if (isColorDark) {
            newTint = 225;
        }
        finalColor = Color.argb(a, newTint, newTint, newTint);
    }
    return finalColor;
}

From source file:com.manuelpeinado.numericpageindicator.demo.MyPagerAdapter.java

@Override
public Object instantiateItem(ViewGroup container, int position) {
    TextView item = new TextView(container.getContext());
    item.setText(Integer.toString(position + 1));
    item.setTextColor(Color.argb(192, 255, 255, 255));
    item.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 50);
    item.setGravity(Gravity.CENTER);//w  w w .  j  a va  2  s.c  om
    item.setBackgroundColor(COLORS[position % COLORS.length]);
    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    container.addView(item, layoutParams);
    return item;
}

From source file:Main.java

/**
 * 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./*from  w  ww  .j a  v  a  2s .  com*/
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {
    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 = (float) Math.max(0, Math.min(1, 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) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    return paintDrawable;
}

From source file:Main.java

/**
 * 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./*from  w  ww.  j  a v  a2  s.c  o m*/
 */
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 = 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;
}

From source file:at.wada811.android.library.demos.view.ViewPagerAdapter.java

@Override
public View instantiateItem(ViewGroup container, int position) {
    View page = mInflater.inflate(R.layout.layout_page, container, false);
    TextView textView = (TextView) page.findViewById(R.id.text);
    int rgb = position * 255;
    int r = (rgb >> 16) & 0xFF;
    int g = (rgb >> 8) & 0xFF;
    int b = rgb & 0xFF;
    r = g * 5 % 256;//ww w  . ja  v a  2  s.co m
    g = b * 15 % 256;
    b = r * 25 % 256;
    int argb = Color.argb(255, r, g, b);
    textView.setText(String.format("#%02X%02X%02X", r, g, b));
    textView.setTextColor(r > 192 || g > 192 || b > 192 ? Color.BLACK : Color.WHITE);
    page.setBackgroundColor(argb);
    container.addView(page);
    return page;
}

From source file:Main.java

public static Bitmap createFramedImage(Drawable imageDrawable, int borderThickness) {
    int size = Math.min(imageDrawable.getMinimumWidth(), imageDrawable.getMinimumHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);/*from   ww  w .  j  a v  a  2  s . c om*/
    canvas.drawRect(outerRect, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    // Save the layer to apply the paint
    canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
    imageDrawable.draw(canvas);
    canvas.restore();

    // FRAMING THE PHOTO
    float border = size / 15f;

    // 1. Create offscreen bitmap link: http://www.youtube.com/watch?feature=player_detailpage&v=jF6Ad4GYjRU#t=1035s
    Bitmap framedOutput = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas framedCanvas = new Canvas(framedOutput);
    // End of Step 1

    // Start - TODO IMPORTANT - this section shouldn't be included in the final code
    // It's needed here to differentiate step 2 (red) with the background color of the activity
    // It's should be commented out after the codes includes step 3 onwards
    // Paint squaredPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // squaredPaint.setColor(Color.BLUE);
    // framedCanvas.drawRoundRect(outerRect, 0f, 0f, squaredPaint);
    // End

    // 2. Draw an opaque rounded rectangle link:
    RectF innerRect = new RectF(border, border, size - border, size - border);

    Paint innerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    innerPaint.setColor(Color.RED);
    //      framedCanvas.drawRoundRect(innerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(innerRect, innerPaint);

    // 3. Set the Power Duff mode link:
    Paint outerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    outerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));

    // 4. Draw a translucent rounded rectangle link:
    outerPaint.setColor(Color.argb(255, 255, 255, 255));
    //      framedCanvas.drawRoundRect(outerRect, cornerRadius, cornerRadius, outerPaint);
    framedCanvas.drawRect(outerRect, outerPaint);

    // 5. Draw the frame on top of original bitmap
    canvas.drawBitmap(framedOutput, 0f, 0f, null);

    return output;
}

From source file:com.yoloo.android.util.ScrimUtil.java

/**
 * 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./* w  ww  . j  ava2 s .  co  m*/
 */
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];

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

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtils.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.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        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;
}

From source file:babbq.com.searchplace.util.ColorUtils.java

/**
 * 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}.
 *//*from w w  w.j  a v a2 s.c  o  m*/
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);
}

From source file:com.jaredrummler.materialspinner.MaterialSpinner.java

/**
 * Darkens a color by a given factor./*from ww  w .  j a va2  s . c o m*/
 *
 * @param color
 *     the color to darken
 * @param factor
 *     The factor to darken the color.
 * @return darker version of specified color.
 */
@ColorInt
private static int darker(@ColorInt int color, @FloatRange(from = 0.0, to = 1.0) float factor) {
    return Color.argb(Color.alpha(color), Math.max((int) (Color.red(color) * factor), 0),
            Math.max((int) (Color.green(color) * factor), 0), Math.max((int) (Color.blue(color) * factor), 0));
}

From source file:com.manuelpeinado.numericpageindicator.demo.StylingProgrammaticallyActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(new MyPagerAdapter());
    pageIndicator = (NumericPageIndicator) findViewById(R.id.pageIndicator);
    pageIndicator.setViewPager(viewPager);

    pageIndicator.setTextTemplate("Page: #i");
    pageIndicator.setStartButtonText("|<");
    pageIndicator.setPreviousButtonText("<");
    pageIndicator.setNextButtonText(">");
    pageIndicator.setEndButtonText(">|");
    pageIndicator.setTextColor(Color.argb(128, 255, 255, 255));
    pageIndicator.setPageNumberTextColor(Color.argb(192, 255, 255, 255));
    pageIndicator.setPageNumberTextBold(false);
    final float scale = getResources().getDisplayMetrics().density;
    pageIndicator.setTextSize((int) (12 * scale + 0.5f));
    pageIndicator.setTopPadding((int) (7 * scale + 0.5f));
    pageIndicator.setBottomPadding((int) (7 * scale + 0.5f));
    pageIndicator.setBackgroundColor(Color.rgb(64, 96, 64));
    pageIndicator.setPressedButtonColor(Color.argb(128, 255, 255, 255));
    pageIndicator.setShowChangePageButtons(true);
}