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:Main.java

/**
 * Returns true if we should add padding to this icon. We use a heuristic that if the pixels in
 * all four corners of the icon are not transparent, we assume the icon is square and maximally
 * sized, i.e. in need of padding. Otherwise, no padding is added.
 *///www.ja v  a2s  .co  m
private static boolean shouldPadIcon(Bitmap icon) {
    int maxX = icon.getWidth() - 1;
    int maxY = icon.getHeight() - 1;

    if ((Color.alpha(icon.getPixel(0, 0)) != 0) && (Color.alpha(icon.getPixel(maxX, maxY)) != 0)
            && (Color.alpha(icon.getPixel(0, maxY)) != 0) && (Color.alpha(icon.getPixel(maxX, 0)) != 0)) {
        return true;
    }
    return false;
}

From source file:Main.java

public static Bitmap doGray(Bitmap src) {
    final double GS_RED = 0.299;
    final double GS_GREEN = 0.587;
    final double GS_BLUE = 0.114;

    Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
    int A, R, G, B, pixel;
    int w = src.getWidth();
    int h = src.getHeight();
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            pixel = src.getPixel(x, y);/*from  w  w  w.j  a  v  a 2s  . com*/

            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            R = G = B = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);
            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

/**
 * Method returning color with modified alpha proportional to given values.
 *
 * @param color     color to modify/*from  w  w  w.ja  va  2s  . c  o m*/
 * @param fullValue total value
 * @param partValue part of fullValue. When partValue equals fullValue, the alpha is 255.
 * @return color with alpha relative to partValue/fullValue ratio.
 */
public static int getColorWithProportionalAlpha(int color, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(color) * progress), Color.red(color), Color.green(color),
            Color.blue(color));
}

From source file:Main.java

/**
 * @param activity Activity that should get the task description update.
 * @param title Title of the activity.//www . j  ava2 s.  c o m
 * @param icon Icon of the activity.
 * @param color Color of the activity. It must be a fully opaque color.
 */
public static void setTaskDescription(Activity activity, String title, Bitmap icon, int color) {
    // TaskDescription requires an opaque color.
    assert Color.alpha(color) == 255;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        ActivityManager.TaskDescription description = new ActivityManager.TaskDescription(title, icon, color);
        activity.setTaskDescription(description);
    }
}

From source file:Main.java

public static Bitmap handleImagePixelsOldPhoto(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0;
    int r, g, b, a, r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];//from www. j  a va  2 s. c  om
        a = Color.alpha(color);
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);

        r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

        if (r1 > 255) {
            r1 = 255;
        }
        if (g1 > 255) {
            g1 = 255;
        }
        if (b1 > 255) {
            b1 = 255;
        }

        newPx[i] = Color.argb(a, r1, g1, b1);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap doSherpiaEffect(Bitmap src, int depth, double red, double green, double blue) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
    final double GS_RED = 0.3;
    final double GS_GREEN = 0.59;
    final double GS_BLUE = 0.11;
    int A, R, G, B;
    int pixel;// w  ww.  j  av a  2s .c  om

    for (int x = 0; x < width; ++x) {
        for (int y = 0; y < height; ++y) {
            pixel = src.getPixel(x, y);
            A = Color.alpha(pixel);
            R = Color.red(pixel);
            G = Color.green(pixel);
            B = Color.blue(pixel);
            B = G = R = (int) (GS_RED * R + GS_GREEN * G + GS_BLUE * B);

            R += (depth * red);
            if (R > 255) {
                R = 255;
            }

            G += (depth * green);
            if (G > 255) {
                G = 255;
            }

            B += (depth * blue);
            if (B > 255) {
                B = 255;
            }

            bmOut.setPixel(x, y, Color.argb(A, R, G, B));
        }
    }
    return bmOut;
}

From source file:Main.java

/**
 * 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.
 *///  w  w  w  .  jav a 2 s.c o  m
public 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;
}

From source file:Main.java

public static Bitmap handleImagePixelsRelief(Bitmap bm) {
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color = 0, colorBefore = 0;
    int a, r, g, b;
    int r1, g1, b1;

    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];

    bm.getPixels(oldPx, 0, bm.getWidth(), 0, 0, width, height);
    for (int i = 1; i < width * height; i++) {
        colorBefore = oldPx[i - 1];//from   w ww  .j a va 2s .  c  o  m
        a = Color.alpha(colorBefore);
        r = Color.red(colorBefore);
        g = Color.green(colorBefore);
        b = Color.blue(colorBefore);

        color = oldPx[i];
        r1 = Color.red(color);
        g1 = Color.green(color);
        b1 = Color.blue(color);

        r = (r - r1 + 127);
        g = (g - g1 + 127);
        b = (b - b1 + 127);
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;
}

From source file:Main.java

public static Bitmap handleImageOldPhoto(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;/*from w  w w .j av a 2 s .c o  m*/
    int r, g, b, a;
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];
    bm.getPixels(oldPx, 0, width, 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = (int) (0.393 * r + 0.769 * g + 0.189 * b);
        g = (int) (0.349 * r + 0.686 * g + 0.168 * b);
        b = (int) (0.272 * r + 0.534 * g + 0.131 * b);
        if (r > 255) {
            r = 255;
        }
        if (g > 255) {
            g = 255;
        }
        if (b > 255) {
            b = 255;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;

}

From source file:Main.java

public static Bitmap handleImageNegative(Bitmap bm) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    int color;//from  w w w  .j a v a 2  s. co  m
    int r, g, b, a;
    Bitmap bmp = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    int[] oldPx = new int[width * height];
    int[] newPx = new int[width * height];
    bm.getPixels(oldPx, 0, width, 0, 0, width, height);
    for (int i = 0; i < width * height; i++) {
        color = oldPx[i];
        r = Color.red(color);
        g = Color.green(color);
        b = Color.blue(color);
        a = Color.alpha(color);

        r = 255 - r;
        g = 255 - g;
        b = 255 - b;
        if (r > 255) {
            r = 255;
        } else if (r < 0) {
            r = 0;
        }
        if (g > 255) {
            g = 255;
        } else if (g < 0) {
            g = 0;
        }
        if (b > 255) {
            b = 255;
        } else if (b < 0) {
            b = 0;
        }
        newPx[i] = Color.argb(a, r, g, b);
    }
    bmp.setPixels(newPx, 0, width, 0, 0, width, height);
    return bmp;

}