Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3ebecb91 authored by Joey's avatar Joey
Browse files

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: default avatarJoey <joey@lineageos.org>
Change-Id: I57f47b5418acb54bc074fe480aebbdcf21222934
parent 1e54dce7
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import androidx.recyclerview.widget.RecyclerView;
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 @@ public class WallpaperHolder extends RecyclerView.ViewHolder {
        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();
+30 −8
Original line number Diff line number Diff line
@@ -33,25 +33,47 @@ public final class ColorUtils {
        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 @@ public final class ColorUtils {
        float hsl[] = new float[3];

        androidx.core.graphics.ColorUtils.RGBToHSL(red, green, blue, hsl);
        return hsl[2] > 0.5f;
        return hsl[2] > 0.76f;
    }
}
+0 −24
Original line number Diff line number Diff line
<!--
  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>
+1 −3
Original line number Diff line number Diff line
@@ -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" />

+0 −1
Original line number Diff line number Diff line
@@ -22,6 +22,5 @@

    <color name="ic_launcher_background">#70AE98</color>

    <color name="item_nameBg">#1f000000</color>
    <color name="apply_closeBg">#4f000000</color>
</resources>
Loading