Set name colors according to the overall image

* Extract dominant / vibrant color from the bottom 20%
  of the picture and set it as textColor

Signed-off-by: Joey <joey@lineageos.org>
Change-Id: I57f47b5418acb54bc074fe480aebbdcf21222934
diff --git a/app/src/main/java/org/lineageos/backgrounds/holders/WallpaperHolder.java b/app/src/main/java/org/lineageos/backgrounds/holders/WallpaperHolder.java
index ed4742d..55e7554 100644
--- a/app/src/main/java/org/lineageos/backgrounds/holders/WallpaperHolder.java
+++ b/app/src/main/java/org/lineageos/backgrounds/holders/WallpaperHolder.java
@@ -26,6 +26,7 @@
 import org.lineageos.backgrounds.R;
 import org.lineageos.backgrounds.bundle.WallpaperBundle;
 import org.lineageos.backgrounds.ui.SelectionInterface;
+import org.lineageos.backgrounds.util.ColorUtils;
 
 public class WallpaperHolder extends RecyclerView.ViewHolder {
     @NonNull
@@ -49,6 +50,10 @@
         Drawable drawable = bundle.getContentDrawable();
         if (drawable != null) {
             previewView.setImageDrawable(drawable);
+
+            // Tint title for contrast
+            final int color = ColorUtils.extractContrastColor(ColorUtils.extractPaletteFromBottom(drawable));
+            nameView.setTextColor(color);
         }
 
         String name = bundle.getName();
diff --git a/app/src/main/java/org/lineageos/backgrounds/util/ColorUtils.java b/app/src/main/java/org/lineageos/backgrounds/util/ColorUtils.java
index 13b1e72..f735645 100644
--- a/app/src/main/java/org/lineageos/backgrounds/util/ColorUtils.java
+++ b/app/src/main/java/org/lineageos/backgrounds/util/ColorUtils.java
@@ -33,25 +33,47 @@
         return Palette.from(bm).generate();
     }
 
+    public static Palette extractPaletteFromBottom(@NonNull final Drawable drawable) {
+        final Bitmap originalBm = TypeConverter.drawableToBitmap(drawable);
+        // Crop bottom 20%
+        final int cropY = (int) (originalBm.getHeight() * 0.8f);
+        final Bitmap bottomPart = Bitmap.createBitmap(originalBm, 0, cropY,
+                originalBm.getWidth(), originalBm.getHeight() - cropY);
+        return Palette.from(bottomPart).generate();
+    }
+
     @ColorInt
     public static int extractColor(@NonNull final Palette palette) {
-        final int muted = palette.getMutedColor(Color.WHITE);
-        if (muted != Color.WHITE) {
-            return muted;
+        final int dominant = palette.getDominantColor(Color.WHITE);
+        if (dominant != Color.WHITE) {
+            return dominant;
         }
         final int vibrant = palette.getVibrantColor(Color.WHITE);
         if (vibrant != Color.WHITE) {
             return vibrant;
         }
-        return darken(palette.getDominantColor(Color.WHITE));
+        return palette.getMutedColor(Color.WHITE);
     }
 
+
     @ColorInt
-    private static int darken(@ColorInt final int color) {
-        return androidx.core.graphics.ColorUtils.blendARGB(color, Color.BLACK, 0.2f);
+    public static int extractContrastColor(@NonNull final Palette palette) {
+        int color = Color.BLACK;
+
+        final Palette.Swatch dominant = palette.getDominantSwatch();
+        if (dominant != null) {
+            color = dominant.getRgb();
+        } else {
+            final Palette.Swatch vibrant = palette.getVibrantSwatch();
+            if (vibrant != null) {
+                color = vibrant.getRgb();
+            }
+        }
+
+        return isColorLight(color) ? Color.BLACK : Color.WHITE;
     }
 
-    public static boolean isColorLight(@ColorInt final int color) {
+    static boolean isColorLight(@ColorInt final int color) {
         int red = Color.red(color);
         int green = Color.green(color);
         int blue = Color.blue(color);
@@ -60,6 +82,6 @@
         float hsl[] = new float[3];
 
         androidx.core.graphics.ColorUtils.RGBToHSL(red, green, blue, hsl);
-        return hsl[2] > 0.5f;
+        return hsl[2] > 0.76f;
     }
 }
diff --git a/app/src/main/res/drawable/bg_wallpaper_name.xml b/app/src/main/res/drawable/bg_wallpaper_name.xml
deleted file mode 100644
index b79fb89..0000000
--- a/app/src/main/res/drawable/bg_wallpaper_name.xml
+++ /dev/null
@@ -1,24 +0,0 @@
-<!--
-  Copyright (C) 2019 The LineageOS Project
-
-  Licensed under the Apache License, Version 2.0 (the "License");
-  you may not use this file except in compliance with the License.
-  You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-  Unless required by applicable law or agreed to in writing, software
-  distributed under the License is distributed on an "AS IS" BASIS,
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-  See the License for the specific language governing permissions and
-  limitations under the License.
-  -->
-<shape xmlns:android="http://schemas.android.com/apk/res/android"
-    android:shape="rectangle">
-
-    <gradient
-        android:angle="270"
-        android:centerColor="@color/item_nameBg"
-        android:endColor="@color/item_nameBg"
-        android:startColor="@android:color/transparent" />
-</shape>
diff --git a/app/src/main/res/layout/item_wallpaper.xml b/app/src/main/res/layout/item_wallpaper.xml
index 2347586..595a9c7 100644
--- a/app/src/main/res/layout/item_wallpaper.xml
+++ b/app/src/main/res/layout/item_wallpaper.xml
@@ -39,12 +39,10 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="bottom"
-        android:background="@drawable/bg_wallpaper_name"
         android:ellipsize="end"
         android:maxLines="1"
         android:paddingHorizontal="8dp"
-        android:paddingVertical="4dp"
-        android:textColor="@android:color/white"
+        android:paddingBottom="8dp"
         android:textSize="18sp"
         tools:text="@string/main_wallpaper_pick" />
 
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index 85759fa..3263e96 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -22,6 +22,5 @@
 
     <color name="ic_launcher_background">#70AE98</color>
 
-    <color name="item_nameBg">#1f000000</color>
     <color name="apply_closeBg">#4f000000</color>
 </resources>
diff --git a/app/src/main/res/values/wallpapers_mono.xml b/app/src/main/res/values/wallpapers_mono.xml
index 46ea46e..f9b0afa 100644
--- a/app/src/main/res/values/wallpapers_mono.xml
+++ b/app/src/main/res/values/wallpapers_mono.xml
@@ -25,6 +25,6 @@
     <color name="wallpaper_color_purple">#9C8ADE</color>
     <color name="wallpaper_color_red">#E58B88</color>
     <color name="wallpaper_color_teal">#619196</color>
-    <color name="wallpaper_color_white">#FFFFFF</color>
+    <color name="wallpaper_color_white">#F0F1F2</color>
     <color name="wallpaper_color_yellow">#ECBE7A</color>
 </resources>