Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

Launcher: robust iconPlate edge detection and multicolored dominantColor extraction

jiantai7f94ce6e9bcb
diff --git a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
index 4cf0143bf..6dfdc8e9b 100644
--- a/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
+++ b/launcher/app/src/main/java/com/gathin/tvhome/AppAdapter.java
@@ -694,7 +694,7 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
* The colour covering most of an icon, for a card that should read as that app: a small saturated badge
* (a red "new" corner on a blue icon) must not win, as it can with {@link #vividColor}. Grey if none.
*/
- private static int dominantColor(Drawable live) {
+ static int dominantColor(Drawable live) {
if (live == null) return 0xFF6E6E73;
Drawable d = live.getConstantState() != null ? live.getConstantState().newDrawable().mutate() : live;
Rect saved = new Rect(d.getBounds());
@@ -706,6 +706,7 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
float[] weight = new float[12];
float[][] sum = new float[12][3];
float[] hsv = new float[3];
+ float totalWeight = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < n; x++) {
int p = bmp.getPixel(x, y);
@@ -715,15 +716,20 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
int bin = (int) (hsv[0] / 30f) % 12;
float w = hsv[1] * hsv[2];
weight[bin] += w;
+ totalWeight += w;
sum[bin][0] += Color.red(p) * w;
sum[bin][1] += Color.green(p) * w;
sum[bin][2] += Color.blue(p) * w;
}
}
bmp.recycle();
+ if (totalWeight < n * n * 0.03f) return 0xFF6E6E73; // a monochrome icon, like a grey gear
int best = 0;
for (int i = 1; i < 12; i++) if (weight[i] > weight[best]) best = i;
- if (weight[best] < n * n * 0.03f) return 0xFF6E6E73; // a monochrome icon, like a grey gear
+ if (weight[best] < n * n * 0.03f) {
+ // Multicolored icon without a single dominant hue: pick its most vivid colour
+ return vividColor(live);
+ }
float w = weight[best];
return Color.rgb((int) (sum[best][0] / w), (int) (sum[best][1] / w), (int) (sum[best][2] / w));
}
@@ -751,7 +757,7 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
* card's top and bottom edges so the mark reads as filling the tile; null when the icon has no
* opaque plate (a bare mark on transparency).
*/
- private static int[] iconPlate(Drawable live) {
+ static int[] iconPlate(Drawable live) {
if (live == null) return null;
Drawable d = live.getConstantState() != null ? live.getConstantState().newDrawable().mutate() : live;
Rect saved = new Rect(d.getBounds());
@@ -762,10 +768,20 @@ public class AppAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
d.setBounds(saved);
// Just inside a rounded square's corners; one side may go a step further in, past a corner badge.
// An icon padded on both sides is a small plate in the middle, which no fill would meet.
- int a = plateColor(bmp, 7, n), b = plateColor(bmp, n - 8, n);
int topRow = 7, bottomRow = n - 8;
- if (a == 0 && b != 0) a = plateColor(bmp, topRow = 10, n);
- else if (b == 0 && a != 0) b = plateColor(bmp, bottomRow = n - 11, n);
+ int a = plateColor(bmp, topRow, n), b = plateColor(bmp, bottomRow, n);
+ if (a == 0) {
+ for (int r : new int[]{5, 6, 4, 10}) {
+ int c = plateColor(bmp, r, n);
+ if (c != 0) { a = c; topRow = r; break; }
+ }
+ }
+ if (b == 0) {
+ for (int r : new int[]{n - 6, n - 7, n - 5, n - 11}) {
+ int c = plateColor(bmp, r, n);
+ if (c != 0) { b = c; bottomRow = r; break; }
+ }
+ }
bmp.recycle();
if (a == 0 || b == 0) return null;
// The sampled rows sit at these heights of the tile; extend the line through them to its edges. A row