Skip to content

Instantly share code, notes, and snippets.

@nexy791
Created November 13, 2020 13:41
Show Gist options
  • Select an option

  • Save nexy791/4d3689f43f6d744b289d13546d936e81 to your computer and use it in GitHub Desktop.

Select an option

Save nexy791/4d3689f43f6d744b289d13546d936e81 to your computer and use it in GitHub Desktop.
package com.ribsky.vmistlauncher;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.palette.graphics.Palette;
public class WallpaperEngine{
Context context;
public WallpaperEngine(Context context){
this.context = context;
}
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public boolean isColorDark(int color){
double darkness = 1-(0.299* Color.red(color) + 0.587*Color.green(color) + 0.114*Color.blue(color))/255;
if(darkness>0.5){
return false;
}else{
return true;
}
}
public boolean isWallpaperDark(Bitmap bitmap){
final int[] dcolor = new int[1];
Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {
public void onGenerated(Palette p) {
dcolor[0] = p.getDominantColor(context.getResources().getColor(R.color.black));
}
});
return isColorDark(dcolor[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment