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

Commit 452d42f5 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Pick the primary color as the best color to use."

parents c2a64487 a6684f67
Loading
Loading
Loading
Loading
+3 −25
Original line number Diff line number Diff line
@@ -109,42 +109,20 @@ public class Tonal implements ExtractionType {
        final int mainColorsSize = mainColors.size();
        final int hints = inWallpaperColors.getColorHints();
        final boolean supportsDarkText = (hints & WallpaperColors.HINT_SUPPORTS_DARK_TEXT) != 0;
        final boolean generatedFromBitmap = (hints & WallpaperColors.HINT_FROM_BITMAP) != 0;

        if (mainColorsSize == 0) {
            return false;
        }

        // Decide what's the best color to use.
        // We have 2 options:
        // • Just pick the primary color
        // • Filter out blacklisted colors. This is useful when palette is generated
        //   automatically from a bitmap.
        Color bestColor = null;
        final float[] hsl = new float[3];
        for (int i = 0; i < mainColorsSize; i++) {
            final Color color = mainColors.get(i);
            final int colorValue = color.toArgb();
            ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue),
                    Color.blue(colorValue), hsl);

            // Stop when we find a color that meets our criteria
            if (!generatedFromBitmap) {
                bestColor = color;
                break;
            }
        }

        // Fail if not found
        if (bestColor == null) {
            return false;
        }
        // Pick the primary color as the best color to use.
        final Color bestColor = mainColors.get(0);

        // Tonal is not really a sort, it takes a color from the extracted
        // palette and finds a best fit amongst a collection of pre-defined
        // palettes. The best fit is tweaked to be closer to the source color
        // and replaces the original palette.
        int colorValue = bestColor.toArgb();
        final float[] hsl = new float[3];
        ColorUtils.RGBToHSL(Color.red(colorValue), Color.green(colorValue), Color.blue(colorValue),
                hsl);

+1 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ android_test {
        "junit",
        "androidx.test.rules",
        "mockito-target-minus-junit4",
        "truth-prebuilt",
    ],
    java_resource_dirs: ["res"],
    certificate: "platform",
+32 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.internal.colorextraction.types;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@@ -66,6 +68,36 @@ public class TonalTest {
                normal.getMainColor() == Tonal.MAIN_COLOR_LIGHT);
    }

    @Test
    public void extractInto_fromBitmap() {
        Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
        GradientColors normal = new GradientColors();
        GradientColors dark = new GradientColors();
        GradientColors extraDark = new GradientColors();
        WallpaperColors wallColors = new WallpaperColors(Color.valueOf(Color.RED), null, null,
                WallpaperColors.HINT_FROM_BITMAP);

        // WHEN colors are extracted from a wallpaper with only a red primary color.
        tonal.extractInto(wallColors, normal, dark, extraDark);
        // THEN the main extracted color is red
        assertThat(normal.getMainColor()).isEqualTo(Color.RED);
    }

    @Test
    public void extractInto_supportsDarkText() {
        Tonal tonal = new Tonal(InstrumentationRegistry.getContext());
        GradientColors normal = new GradientColors();
        GradientColors dark = new GradientColors();
        GradientColors extraDark = new GradientColors();
        WallpaperColors wallColors = new WallpaperColors(Color.valueOf(Color.RED), null, null,
                WallpaperColors.HINT_SUPPORTS_DARK_TEXT);

        // WHEN colors are extracted from a wallpaper with only a red primary color.
        tonal.extractInto(wallColors, normal, dark, extraDark);
        // THEN the main extracted color is red
        assertThat(normal.getMainColor()).isEqualTo(Color.RED);
    }

    @Test
    public void colorRange_containsColor() {
        Tonal.ColorRange colorRange = new Tonal.ColorRange(new Range<>(0f, 50f),