Last active
January 26, 2020 21:21
-
-
Save bhargavms/5c435fa95a20b8d194e696226e77306c to your computer and use it in GitHub Desktop.
A utility class for bitmap related operations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Matrix; | |
import android.graphics.Paint; | |
import android.util.Log; | |
import com.crashlytics.android.Crashlytics; | |
import com.koushikdutta.ion.Ion; | |
import name.space.to.ColorUtils; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.concurrent.ExecutionException; | |
/** | |
* A Utility class for bitmap related functions. | |
*/ | |
@SuppressWarnings({"TryFinallyCanBeTryWithResources", "unused"}) | |
public class BitmapUtils { | |
private static final String TAG = BitmapUtils.class.getSimpleName(); | |
// Decodes image and scales it to reduce memory consumption | |
public static Bitmap decodeFile(InputStream inputStream, float resizedHeight, float resizedWidth) { | |
if (resizedHeight == 0 || resizedWidth == 0) { | |
return BitmapFactory.decodeStream(inputStream); | |
} | |
// Decode image size | |
BitmapFactory.Options o = new BitmapFactory.Options(); | |
o.inJustDecodeBounds = true; | |
BitmapFactory.decodeStream(inputStream, null, o); | |
// Find the correct scale value. It should be the power of 2. | |
int scale = 1; | |
while (o.outWidth / scale / 2 >= resizedWidth && | |
o.outHeight / scale / 2 >= resizedHeight) { | |
scale *= 2; | |
} | |
// Decode with inSampleSize | |
BitmapFactory.Options o2 = new BitmapFactory.Options(); | |
o2.inSampleSize = scale; | |
return BitmapFactory.decodeStream(inputStream, null, o2); | |
} | |
public static Bitmap decodeFile(File f, float resizedHeight, float resizedWidth) { | |
Bitmap bitmap = null; | |
if (f == null) { | |
return null; | |
} | |
long fileSize = f.length(); | |
Log.d(TAG, "File size = " + fileSize); | |
try { | |
FileInputStream fileInputStream = new FileInputStream(f); | |
try { | |
bitmap = decodeFile(fileInputStream, resizedHeight, resizedWidth); | |
} finally { | |
fileInputStream.close(); | |
} | |
} catch (IOException ioEx) { | |
Log.e(TAG, ioEx.getMessage(), ioEx); | |
} | |
return bitmap; | |
} | |
/** | |
* Uses http url connection, this function is error prone, all error possibilities are | |
* not handled here, recommend using an http library for this functionality. | |
* (Also if you can think of any improvements please post them) | |
*/ | |
public static Bitmap getBitmap(String url) { | |
InputStream is; | |
try { | |
Bitmap bitmap; | |
URL imageUrl = new URL(url); | |
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); | |
conn.setConnectTimeout(70000); | |
conn.setReadTimeout(70000); | |
conn.setInstanceFollowRedirects(true); | |
conn.connect(); | |
int status = conn.getResponseCode(); | |
if (status == HttpURLConnection.HTTP_OK) { | |
is = conn.getInputStream(); | |
} else { | |
is = conn.getErrorStream(); | |
BufferedReader br = new BufferedReader(new InputStreamReader(is)); | |
StringBuilder sb = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
sb.append(line).append("\n"); | |
} | |
br.close(); | |
Log.e("API_RESPONSE", sb.toString()); | |
return null; | |
} | |
bitmap = BitmapFactory.decodeStream(is); | |
conn.disconnect(); | |
is.close(); | |
return bitmap; | |
} catch (Throwable ex) { | |
ex.printStackTrace(); | |
//noinspection StatementWithEmptyBody | |
if (ex instanceof OutOfMemoryError) { | |
Crashlytics.log(Log.ERROR, TAG, "Out of memory while getting bitmap using http url connection lib"); | |
Crashlytics.logException(ex); | |
} | |
return null; | |
} | |
} | |
public static Bitmap getResizedBitmap(Bitmap bm, float newHeight, float newWidth) { | |
float width = bm.getWidth(); | |
float height = bm.getHeight(); | |
float scaleWidth = (newWidth) / width; | |
float scaleHeight = (newHeight) / height; | |
// create a matrix for the manipulation | |
Matrix matrix = new Matrix(); | |
// resize the bit map | |
matrix.postScale(scaleWidth, scaleHeight); | |
// recreate the new Bitmap | |
return Bitmap.createBitmap(bm, 0, 0, (int) width, (int) height, matrix, false); | |
} | |
public static Bitmap getBitmapWith(int color, String text, int textColor, int drawableWidth, int drawableHeight) { | |
Bitmap bm = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); | |
Canvas c = new Canvas(bm); | |
// Draw background | |
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | |
| Paint.LINEAR_TEXT_FLAG); | |
paint.setStyle(Paint.Style.FILL); | |
paint.setColor(color); | |
c.drawPaint(paint); | |
Paint textPaint = new Paint(); | |
textPaint.setStyle(Paint.Style.FILL); | |
textPaint.setTextAlign(Paint.Align.CENTER); | |
textPaint.setColor(textColor); | |
textPaint.setTextSize(c.getWidth() / 2); | |
c.drawText(text.toUpperCase(), c.getWidth() / 2, (c.getHeight() / 2 + textPaint.getTextSize() / 4), textPaint); | |
return bm; | |
} | |
public static Bitmap getBitmapWithRandomColor(char c, int width, int height) { | |
return getBitmapWith(ColorUtils.getRandomMaterialColor(ColorUtils.MaterialColorIntensity.INTENSITY_700), String.valueOf(c), Color.WHITE, width, height); | |
} | |
/** | |
* Stack Blur v1.0 from | |
* http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html | |
* Java Author: Mario Klingemann <mario at quasimondo.com> | |
* http://incubator.quasimondo.com | |
* <p/> | |
* created Feburary 29, 2004 | |
* Android port : Yahel Bouaziz <yahel at kayenko.com> | |
* http://www.kayenko.com | |
* ported april 5th, 2012 | |
* <p/> | |
* This is a compromise between Gaussian Blur and Box blur | |
* It creates much better looking blurs than Box Blur, but is | |
* 7x faster than my Gaussian Blur implementation. | |
* <p/> | |
* I called it Stack Blur because this describes best how this | |
* filter works internally: it creates a kind of moving stack | |
* of colors whilst scanning through the image. Thereby it | |
* just has to add one new block of color to the right side | |
* of the stack and remove the leftmost color. The remaining | |
* colors on the topmost layer of the stack are either added on | |
* or reduced by one, depending on if they are on the right or | |
* on the left side of the stack. | |
* <p/> | |
* If you are using this algorithm in your code please add | |
* the following line: | |
* Stack Blur Algorithm by Mario Klingemann <[email protected]> | |
* | |
* @param sentBitmap bitmap to blur. | |
* @param scale the float to scale the bitmap into before blurring it, lower values means better | |
* performance.(should be between 0-1) | |
* @param radius must be > 1. | |
* The bigger the radius, the blurrier the final image. The algorithm mixes the | |
* pixels in a circle "radius" wide.The final color of the pixel being modified | |
* is the average of all the pixel in the "radius" circle. | |
* @return the blurred bitmap. | |
* @author Mario Klingemann <[email protected]> | |
*/ | |
public static Bitmap fastBlur(Bitmap sentBitmap, float scale, int radius) { | |
int width = Math.round(sentBitmap.getWidth() * scale); | |
int height = Math.round(sentBitmap.getHeight() * scale); | |
sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false); | |
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true); | |
if (radius < 1) { | |
return (null); | |
} | |
int w = bitmap.getWidth(); | |
int h = bitmap.getHeight(); | |
int[] pix = new int[w * h]; | |
Log.e("pix", w + " " + h + " " + pix.length); | |
bitmap.getPixels(pix, 0, w, 0, 0, w, h); | |
int wm = w - 1; | |
int hm = h - 1; | |
int wh = w * h; | |
int div = radius + radius + 1; | |
int r[] = new int[wh]; | |
int g[] = new int[wh]; | |
int b[] = new int[wh]; | |
int rsum, gsum, bsum, x, y, i, p, yp, yi, yw; | |
int vmin[] = new int[Math.max(w, h)]; | |
int divsum = (div + 1) >> 1; | |
divsum *= divsum; | |
int dv[] = new int[256 * divsum]; | |
for (i = 0; i < 256 * divsum; i++) { | |
dv[i] = (i / divsum); | |
} | |
yw = yi = 0; | |
int[][] stack = new int[div][3]; | |
int stackpointer; | |
int stackstart; | |
int[] sir; | |
int rbs; | |
int r1 = radius + 1; | |
int routsum, goutsum, boutsum; | |
int rinsum, ginsum, binsum; | |
for (y = 0; y < h; y++) { | |
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; | |
for (i = -radius; i <= radius; i++) { | |
p = pix[yi + Math.min(wm, Math.max(i, 0))]; | |
sir = stack[i + radius]; | |
sir[0] = (p & 0xff0000) >> 16; | |
sir[1] = (p & 0x00ff00) >> 8; | |
sir[2] = (p & 0x0000ff); | |
rbs = r1 - Math.abs(i); | |
rsum += sir[0] * rbs; | |
gsum += sir[1] * rbs; | |
bsum += sir[2] * rbs; | |
if (i > 0) { | |
rinsum += sir[0]; | |
ginsum += sir[1]; | |
binsum += sir[2]; | |
} else { | |
routsum += sir[0]; | |
goutsum += sir[1]; | |
boutsum += sir[2]; | |
} | |
} | |
stackpointer = radius; | |
for (x = 0; x < w; x++) { | |
r[yi] = dv[rsum]; | |
g[yi] = dv[gsum]; | |
b[yi] = dv[bsum]; | |
rsum -= routsum; | |
gsum -= goutsum; | |
bsum -= boutsum; | |
stackstart = stackpointer - radius + div; | |
sir = stack[stackstart % div]; | |
routsum -= sir[0]; | |
goutsum -= sir[1]; | |
boutsum -= sir[2]; | |
if (y == 0) { | |
vmin[x] = Math.min(x + radius + 1, wm); | |
} | |
p = pix[yw + vmin[x]]; | |
sir[0] = (p & 0xff0000) >> 16; | |
sir[1] = (p & 0x00ff00) >> 8; | |
sir[2] = (p & 0x0000ff); | |
rinsum += sir[0]; | |
ginsum += sir[1]; | |
binsum += sir[2]; | |
rsum += rinsum; | |
gsum += ginsum; | |
bsum += binsum; | |
stackpointer = (stackpointer + 1) % div; | |
sir = stack[(stackpointer) % div]; | |
routsum += sir[0]; | |
goutsum += sir[1]; | |
boutsum += sir[2]; | |
rinsum -= sir[0]; | |
ginsum -= sir[1]; | |
binsum -= sir[2]; | |
yi++; | |
} | |
yw += w; | |
} | |
for (x = 0; x < w; x++) { | |
rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; | |
yp = -radius * w; | |
for (i = -radius; i <= radius; i++) { | |
yi = Math.max(0, yp) + x; | |
sir = stack[i + radius]; | |
sir[0] = r[yi]; | |
sir[1] = g[yi]; | |
sir[2] = b[yi]; | |
rbs = r1 - Math.abs(i); | |
rsum += r[yi] * rbs; | |
gsum += g[yi] * rbs; | |
bsum += b[yi] * rbs; | |
if (i > 0) { | |
rinsum += sir[0]; | |
ginsum += sir[1]; | |
binsum += sir[2]; | |
} else { | |
routsum += sir[0]; | |
goutsum += sir[1]; | |
boutsum += sir[2]; | |
} | |
if (i < hm) { | |
yp += w; | |
} | |
} | |
yi = x; | |
stackpointer = radius; | |
for (y = 0; y < h; y++) { | |
// Preserve alpha channel: ( 0xff000000 & pix[yi] ) | |
pix[yi] = (0xff000000 & pix[yi]) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum]; | |
rsum -= routsum; | |
gsum -= goutsum; | |
bsum -= boutsum; | |
stackstart = stackpointer - radius + div; | |
sir = stack[stackstart % div]; | |
routsum -= sir[0]; | |
goutsum -= sir[1]; | |
boutsum -= sir[2]; | |
if (x == 0) { | |
vmin[y] = Math.min(y + r1, hm) * w; | |
} | |
p = x + vmin[y]; | |
sir[0] = r[p]; | |
sir[1] = g[p]; | |
sir[2] = b[p]; | |
rinsum += sir[0]; | |
ginsum += sir[1]; | |
binsum += sir[2]; | |
rsum += rinsum; | |
gsum += ginsum; | |
bsum += binsum; | |
stackpointer = (stackpointer + 1) % div; | |
sir = stack[stackpointer]; | |
routsum += sir[0]; | |
goutsum += sir[1]; | |
boutsum += sir[2]; | |
rinsum -= sir[0]; | |
ginsum -= sir[1]; | |
binsum -= sir[2]; | |
yi += w; | |
} | |
} | |
// Log.e("pix", w + " " + h + " " + pix.length); | |
bitmap.setPixels(pix, 0, w, 0, 0, w, h); | |
return (bitmap); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<!-- Contains all the nice material pallete colors, the getRandomMaterialColor method in the ColorUtils class | |
picks a random color from these resources --> | |
<color name="red_50">#FFEBEE</color> | |
<color name="red_100">#FFCDD2</color> | |
<color name="red_200">#EF9A9A</color> | |
<color name="red_300">#E57373</color> | |
<color name="red_400">#EF5350</color> | |
<color name="red_500">#F44336</color> | |
<color name="red_600">#E53935</color> | |
<color name="red_700">#D32F2F</color> | |
<color name="red_800">#C62828</color> | |
<color name="red_900">#B71C1C</color> | |
<color name="red_A100">#FF8A80</color> | |
<color name="red_A200">#FF5252</color> | |
<color name="red_A400">#FF1744</color> | |
<color name="red_A700">#D50000</color> | |
<color name="deep_purple_50">#EDE7F6</color> | |
<color name="deep_purple_100">#D1C4E9</color> | |
<color name="deep_purple_200">#B39DDB</color> | |
<color name="deep_purple_300">#9575CD</color> | |
<color name="deep_purple_400">#7E57C2</color> | |
<color name="deep_purple_500">#673AB7</color> | |
<color name="deep_purple_600">#5E35B1</color> | |
<color name="deep_purple_700">#512DA8</color> | |
<color name="deep_purple_800">#4527A0</color> | |
<color name="deep_purple_900">#311B92</color> | |
<color name="deep_purple_A100">#B388FF</color> | |
<color name="deep_purple_A200">#7C4DFF</color> | |
<color name="deep_purple_A400">#651FFF</color> | |
<color name="deep_purple_A700">#6200EA</color> | |
<color name="light_blue_50">#E1F5FE</color> | |
<color name="light_blue_100">#B3E5FC</color> | |
<color name="light_blue_200">#81D4FA</color> | |
<color name="light_blue_300">#4FC3F7</color> | |
<color name="light_blue_400">#29B6F6</color> | |
<color name="light_blue_500">#03A9F4</color> | |
<color name="light_blue_600">#039BE5</color> | |
<color name="light_blue_700">#0288D1</color> | |
<color name="light_blue_800">#0277BD</color> | |
<color name="light_blue_900">#01579B</color> | |
<color name="light_blue_A100">#80D8FF</color> | |
<color name="light_blue_A200">#40C4FF</color> | |
<color name="light_blue_A400">#00B0FF</color> | |
<color name="light_blue_A700">#0091EA</color> | |
<color name="green_50">#E8F5E9</color> | |
<color name="green_100">#C8E6C9</color> | |
<color name="green_200">#A5D6A7</color> | |
<color name="green_300">#81C784</color> | |
<color name="green_400">#66BB6A</color> | |
<color name="green_500">#4CAF50</color> | |
<color name="green_600">#43A047</color> | |
<color name="green_700">#388E3C</color> | |
<color name="green_800">#2E7D32</color> | |
<color name="green_900">#1B5E20</color> | |
<color name="green_A100">#B9F6CA</color> | |
<color name="green_A200">#69F0AE</color> | |
<color name="green_A400">#00E676</color> | |
<color name="green_A700">#00C853</color> | |
<color name="yellow_50">#FFFDE7</color> | |
<color name="yellow_100">#FFF9C4</color> | |
<color name="yellow_200">#FFF59D</color> | |
<color name="yellow_300">#FFF176</color> | |
<color name="yellow_400">#FFEE58</color> | |
<color name="yellow_500">#FFEB3B</color> | |
<color name="yellow_600">#FDD835</color> | |
<color name="yellow_700">#FBC02D</color> | |
<color name="yellow_800">#F9A825</color> | |
<color name="yellow_900">#F57F17</color> | |
<color name="yellow_A100">#FFFF8D</color> | |
<color name="yellow_A200">#FFFF00</color> | |
<color name="yellow_A400">#FFEA00</color> | |
<color name="yellow_A700">#FFD600</color> | |
<color name="deep_orange_50">#FBE9E7</color> | |
<color name="deep_orange_100">#FFCCBC</color> | |
<color name="deep_orange_200">#FFAB91</color> | |
<color name="deep_orange_300">#FF8A65</color> | |
<color name="deep_orange_400">#FF7043</color> | |
<color name="deep_orange_500">#FF5722</color> | |
<color name="deep_orange_600">#F4511E</color> | |
<color name="deep_orange_700">#E64A19</color> | |
<color name="deep_orange_800">#D84315</color> | |
<color name="deep_orange_900">#BF360C</color> | |
<color name="deep_orange_A100">#FF9E80</color> | |
<color name="deep_orange_A200">#FF6E40</color> | |
<color name="deep_orange_A400">#FF3D00</color> | |
<color name="deep_orange_A700">#DD2C00</color> | |
<color name="pink_50">#FCE4EC</color> | |
<color name="pink_100">#F8BBD0</color> | |
<color name="pink_200">#F48FB1</color> | |
<color name="pink_300">#F06292</color> | |
<color name="pink_400">#EC407A</color> | |
<color name="pink_500">#E91E63</color> | |
<color name="pink_600">#D81B60</color> | |
<color name="pink_700">#C2185B</color> | |
<color name="pink_800">#AD1457</color> | |
<color name="pink_900">#880E4F</color> | |
<color name="pink_A100">#FF80AB</color> | |
<color name="pink_A200">#FF4081</color> | |
<color name="pink_A400">#F50057</color> | |
<color name="pink_A700">#C51162</color> | |
<color name="indigo_50">#E8EAF6</color> | |
<color name="indigo_100">#C5CAE9</color> | |
<color name="indigo_200">#9FA8DA</color> | |
<color name="indigo_300">#7986CB</color> | |
<color name="indigo_400">#5C6BC0</color> | |
<color name="indigo_500">#3F51B5</color> | |
<color name="indigo_600">#3949AB</color> | |
<color name="indigo_700">#303F9F</color> | |
<color name="indigo_800">#283593</color> | |
<color name="indigo_900">#1A237E</color> | |
<color name="indigo_A100">#8C9EFF</color> | |
<color name="indigo_A200">#536DFE</color> | |
<color name="indigo_A400">#3D5AFE</color> | |
<color name="indigo_A700">#304FFE</color> | |
<color name="cyan_50">#E0F7FA</color> | |
<color name="cyan_100">#B2EBF2</color> | |
<color name="cyan_200">#80DEEA</color> | |
<color name="cyan_300">#4DD0E1</color> | |
<color name="cyan_400">#26C6DA</color> | |
<color name="cyan_500">#00BCD4</color> | |
<color name="cyan_600">#00ACC1</color> | |
<color name="cyan_700">#0097A7</color> | |
<color name="cyan_800">#00838F</color> | |
<color name="cyan_900">#006064</color> | |
<color name="cyan_A100">#84FFFF</color> | |
<color name="cyan_A200">#18FFFF</color> | |
<color name="cyan_A400">#00E5FF</color> | |
<color name="cyan_A700">#00B8D4</color> | |
<color name="amber_50">#FFF8E1</color> | |
<color name="amber_100">#FFECB3</color> | |
<color name="amber_200">#FFE082</color> | |
<color name="amber_300">#FFD54F</color> | |
<color name="amber_400">#FFCA28</color> | |
<color name="amber_500">#FFC107</color> | |
<color name="amber_600">#FFB300</color> | |
<color name="amber_700">#FFA000</color> | |
<color name="amber_800">#FF8F00</color> | |
<color name="amber_900">#FF6F00</color> | |
<color name="amber_A100">#FFE57F</color> | |
<color name="amber_A200">#FFD740</color> | |
<color name="amber_A400">#FFC400</color> | |
<color name="amber_A700">#FFAB00</color> | |
<color name="brown_50">#EFEBE9</color> | |
<color name="brown_100">#D7CCC8</color> | |
<color name="brown_200">#BCAAA4</color> | |
<color name="brown_300">#A1887F</color> | |
<color name="brown_400">#8D6E63</color> | |
<color name="brown_500">#795548</color> | |
<color name="brown_600">#6D4C41</color> | |
<color name="brown_700">#5D4037</color> | |
<color name="brown_800">#4E342E</color> | |
<color name="brown_900">#3E2723</color> | |
<color name="purple_50">#F3E5F5</color> | |
<color name="purple_100">#E1BEE7</color> | |
<color name="purple_200">#CE93D8</color> | |
<color name="purple_300">#BA68C8</color> | |
<color name="purple_400">#AB47BC</color> | |
<color name="purple_500">#9C27B0</color> | |
<color name="purple_600">#8E24AA</color> | |
<color name="purple_700">#7B1FA2</color> | |
<color name="purple_800">#6A1B9A</color> | |
<color name="purple_900">#4A148C</color> | |
<color name="purple_A100">#EA80FC</color> | |
<color name="purple_A200">#E040FB</color> | |
<color name="purple_A400">#D500F9</color> | |
<color name="purple_A700">#AA00FF</color> | |
<color name="blue_50">#E3F2FD</color> | |
<color name="blue_100">#BBDEFB</color> | |
<color name="blue_200">#90CAF9</color> | |
<color name="blue_300">#64B5F6</color> | |
<color name="blue_400">#42A5F5</color> | |
<color name="blue_500">#2196F3</color> | |
<color name="blue_600">#1E88E5</color> | |
<color name="blue_700">#1976D2</color> | |
<color name="blue_800">#1565C0</color> | |
<color name="blue_900">#0D47A1</color> | |
<color name="blue_A100">#82B1FF</color> | |
<color name="blue_A200">#448AFF</color> | |
<color name="blue_A400">#2979FF</color> | |
<color name="blue_A700">#2962FF</color> | |
<color name="teal_50">#E0F2F1</color> | |
<color name="teal_100">#B2DFDB</color> | |
<color name="teal_200">#80CBC4</color> | |
<color name="teal_300">#4DB6AC</color> | |
<color name="teal_400">#26A69A</color> | |
<color name="teal_500">#009688</color> | |
<color name="teal_600">#00897B</color> | |
<color name="teal_700">#00796B</color> | |
<color name="teal_800">#00695C</color> | |
<color name="teal_900">#004D40</color> | |
<color name="teal_A100">#A7FFEB</color> | |
<color name="teal_A200">#64FFDA</color> | |
<color name="teal_A400">#1DE9B6</color> | |
<color name="teal_A700">#00BFA5</color> | |
<color name="lime_50">#F9FBE7</color> | |
<color name="lime_100">#F0F4C3</color> | |
<color name="lime_200">#E6EE9C</color> | |
<color name="lime_300">#DCE775</color> | |
<color name="lime_400">#D4E157</color> | |
<color name="lime_500">#CDDC39</color> | |
<color name="lime_600">#C0CA33</color> | |
<color name="lime_700">#AFB42B</color> | |
<color name="lime_800">#9E9D24</color> | |
<color name="lime_900">#827717</color> | |
<color name="lime_A100">#F4FF81</color> | |
<color name="lime_A200">#EEFF41</color> | |
<color name="lime_A400">#C6FF00</color> | |
<color name="lime_A700">#AEEA00</color> | |
<color name="orange_50">#FFF3E0</color> | |
<color name="orange_100">#FFE0B2</color> | |
<color name="orange_200">#FFCC80</color> | |
<color name="orange_300">#FFB74D</color> | |
<color name="orange_400">#FFA726</color> | |
<color name="orange_500">#FF9800</color> | |
<color name="orange_600">#FB8C00</color> | |
<color name="orange_700">#F57C00</color> | |
<color name="orange_800">#EF6C00</color> | |
<color name="orange_900">#E65100</color> | |
<color name="orange_A100">#FFD180</color> | |
<color name="orange_A200">#FFAB40</color> | |
<color name="orange_A400">#FF9100</color> | |
<color name="orange_A700">#FF6D00</color> | |
<!-- array of the colors, that are of A700 intensity as shown in google's material pallette --> | |
<array name="materialA700Colors"> | |
<item>#FF6D00</item> | |
<item>#AEEA00</item> | |
<item>#00BFA5</item> | |
<item>#2962FF</item> | |
<item>#AA00FF</item> | |
<item>#3E2723</item> | |
<item>#00B8D4</item> | |
<item>#304FFE</item> | |
<item>#C51162</item> | |
<item>#DD2C00</item> | |
<item>#00C853</item> | |
<item>#0091EA</item> | |
<item>#6200EA</item> | |
<item>#D50000</item> | |
</array> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.graphics.Color; | |
/** | |
* Utility class for color manipulation. | |
*/ | |
public class ColorUtils { | |
@SuppressWarnings("unused") | |
public enum MaterialColorIntensity { | |
INTENSITY_50("50"), | |
INTENSITY_100("100"), | |
INTENSITY_200("200"), | |
INTENSITY_300("300"), | |
INTENSITY_400("400"), | |
INTENSITY_500("500"), | |
INTENSITY_600("600"), | |
INTENSITY_700("700"), | |
INTENSITY_800("800"), | |
INTENSITY_900("900"), | |
INTENSITY_A100("A100"), | |
INTENSITY_A200("A200"), | |
INTENSITY_A400("A400"), | |
INTENSITY_A700("A700"); | |
private final String text; | |
MaterialColorIntensity(final String text) { | |
this.text = text; | |
} | |
@Override | |
public String toString() { | |
return this.text; | |
} | |
} | |
private static String[] colors = new String[]{ | |
"red", "deep_purple", "light_blue", "green", "yellow", "deep_orange", "pink", "indigo", | |
"cyan", "amber", "brown", "purple", "blue", "teal", "lime", "orange" | |
}; | |
/** | |
* Lightens a color by a given factor. | |
* | |
* @param color The color to lighten | |
* @param factor The factor to lighten the color. 0 will make the color unchanged. 1 will make the | |
* color white. | |
* @return lighter version of the specified color. | |
*/ | |
public static int lighter(int color, float factor) { | |
int red = (int) ((Color.red(color) * (1 - factor) / 255 + factor) * 255); | |
int green = (int) ((Color.green(color) * (1 - factor) / 255 + factor) * 255); | |
int blue = (int) ((Color.blue(color) * (1 - factor) / 255 + factor) * 255); | |
return Color.argb(Color.alpha(color), red, green, blue); | |
} | |
/** | |
* Method for generating a random material color of the given intensity, from the google material | |
* palette. | |
* | |
* @param colorIntensity the intensity id as given in the | |
* https://www.google.com/design/spec/style/color.html web page. | |
* @param appContext your application context reference. | |
* | |
* @return a random color of the given intensity. | |
*/ | |
public static int getRandomMaterialColor(MaterialColorIntensity colorIntensity, Context appContext) { | |
int randomIndex = (int) (Math.random() * colors.length); | |
String colorString = String.format("%s_%s", colors[randomIndex], colorIntensity.toString()); | |
int identifier = context.getResources() | |
.getIdentifier(colorString, "color", context.getPackageName()); | |
return context.getResources().getColor(identifier); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment