diff --git a/catalog/java/io/material/catalog/color/ColorDemoUtils.java b/catalog/java/io/material/catalog/color/ColorDemoUtils.java new file mode 100644 index 00000000000..85078c69215 --- /dev/null +++ b/catalog/java/io/material/catalog/color/ColorDemoUtils.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2022 The Android Open Source 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. + */ + +package io.material.catalog.color; + +import android.graphics.Color; +import androidx.annotation.ColorInt; +import com.google.android.material.color.MaterialColors; + +/** Utility methods for Color. */ +final class ColorDemoUtils { + + private ColorDemoUtils() {} + + static int getTextColor(@ColorInt int backgroundColor) { + // Use white text color if the background color is considered dark. + return MaterialColors.isColorLight(backgroundColor) ? Color.BLACK : Color.WHITE; + } +} diff --git a/catalog/java/io/material/catalog/color/ColorHeaderItem.java b/catalog/java/io/material/catalog/color/ColorHeaderItem.java index c2304562130..1a3d0dac6cc 100644 --- a/catalog/java/io/material/catalog/color/ColorHeaderItem.java +++ b/catalog/java/io/material/catalog/color/ColorHeaderItem.java @@ -19,15 +19,17 @@ import android.content.Context; import androidx.annotation.ColorRes; import androidx.annotation.NonNull; +import java.util.Arrays; import java.util.List; /** A class for the headers in the color palette. */ public class ColorHeaderItem implements ColorAdapterItem { + public static final String SYSTEM_PREFIX = "system_"; private static final String COLOR_600 = "600"; - @ColorRes private final int backgroundColor; - private final String name; + @ColorRes private final int backgroundColorRes; + private final String description; ColorHeaderItem(Context context, List colors) { ColorItem sample = colors.get(0); @@ -37,26 +39,31 @@ public class ColorHeaderItem implements ColorAdapterItem { break; } } - MaterialColorSpec materialColor = MaterialColorSpec.create(context, sample.getColorRes()); - backgroundColor = materialColor.getResourceId(); - name = materialColor.getName(); + backgroundColorRes = sample.getColorRes(); + description = sample.getColorResName(); } @ColorRes int getBackgroundColorRes() { - return backgroundColor; - } - - /** Returns the raw name of the color. */ - @NonNull - public String getName() { - return name; + return backgroundColorRes; } /** Returns the display name for the header, e.g. Blue. */ @NonNull public String getDisplayName() { - String name = getName(); + String name; + int splitIndex = description.lastIndexOf("_"); + if (description.startsWith(SYSTEM_PREFIX)) { + // Split the resource name into the color name and value, ie. system_accent1_500 to + // system_accent1 and 500. + name = description.substring(0, splitIndex); + } else { + // Get the name of the color an value without prefixes + String trimmedResName = description.substring(splitIndex + 1); + // Split the resource name into the color name and value, ie. blue500 to blue and 500. + List parts = Arrays.asList(trimmedResName.split("(?<=\\D)(?=\\d)", -1)); + name = parts.get(0); + } String headerColor = name.replace('_', ' '); return Character.toUpperCase(headerColor.charAt(0)) + headerColor.substring(1); } diff --git a/catalog/java/io/material/catalog/color/ColorItem.java b/catalog/java/io/material/catalog/color/ColorItem.java index 7864e6cc5b3..1c30a0c9cf6 100644 --- a/catalog/java/io/material/catalog/color/ColorItem.java +++ b/catalog/java/io/material/catalog/color/ColorItem.java @@ -25,27 +25,29 @@ public class ColorItem implements ColorAdapterItem { private final MaterialColorSpec colorSpec; + @ColorRes private final int colorRes; ColorItem(Context context, @ColorRes int colorRes) { - colorSpec = MaterialColorSpec.create(context, colorRes); + this.colorRes = colorRes; + colorSpec = MaterialColorSpec.createFromResource(context, colorRes); } /** Returns the resource ID of the color. */ @NonNull @ColorRes public int getColorRes() { - return colorSpec.getResourceId(); + return colorRes; } /** Returns the resource name of the color, e.g. system_accent1_100. */ @NonNull public String getColorResName() { - return colorSpec.getResourceName(); + return colorSpec.getDescription(); } /** Returns the int value of the color. */ @ColorInt int getColorValue() { - return colorSpec.getValue(); + return colorSpec.getColorValue(); } } diff --git a/catalog/java/io/material/catalog/color/ColorPaletteDemoFragment.java b/catalog/java/io/material/catalog/color/ColorPaletteDemoFragment.java index 42c4bacd651..2e5858524c8 100644 --- a/catalog/java/io/material/catalog/color/ColorPaletteDemoFragment.java +++ b/catalog/java/io/material/catalog/color/ColorPaletteDemoFragment.java @@ -21,7 +21,6 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; -import android.graphics.Color; import android.os.Bundle; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -45,8 +44,6 @@ /** A fragment that displays the Color Palette demo for the Catalog app. */ public abstract class ColorPaletteDemoFragment extends DemoFragment { - private static final int GREY_10 = 0xFF202124; - private ColorsAdapter adapter; @Nullable @@ -136,7 +133,7 @@ void bind(ColorItem colorItem) { int value = ContextCompat.getColor(context, colorItem.getColorRes()); String colorResName = colorItem.getColorResName(); String resQualifier = - colorResName.startsWith(MaterialColorSpec.SYSTEM_PREFIX) ? "@android:color/" : "@color/"; + colorResName.startsWith(ColorHeaderItem.SYSTEM_PREFIX) ? "@android:color/" : "@color/"; nameView.setText( context.getResources().getString(R.string.cat_color_res, resQualifier, colorResName)); @@ -151,12 +148,7 @@ void bind(ColorItem colorItem) { @ColorInt private int getTextColor(ColorItem colorItem) { - if (colorItem.getColorValue() < MaterialColorSpec.TEXT_COLOR_SWITCH_VALUE) { - // Use dark grey if color value is less than TEXT_COLOR_SWITCH_VALUE. - return GREY_10; - } else { - return Color.WHITE; - } + return ColorDemoUtils.getTextColor(colorItem.getColorValue()); } } @@ -173,9 +165,7 @@ private String generateColorsText(@NonNull ColorsAdapter adapter) { int value = ContextCompat.getColor(getContext(), colorItem.getColorRes()); String colorResName = colorItem.getColorResName(); String resQualifier = - colorResName.startsWith(MaterialColorSpec.SYSTEM_PREFIX) - ? "@android:color/" - : "@color/"; + colorResName.startsWith(ColorHeaderItem.SYSTEM_PREFIX) ? "@android:color/" : "@color/"; colorsText.append(String.format("#%06x", value & 0xFFFFFF)).append("\n"); colorsText.append(resQualifier).append(colorResName).append("\n"); } diff --git a/catalog/java/io/material/catalog/color/MaterialColorSpec.java b/catalog/java/io/material/catalog/color/MaterialColorSpec.java index 055e8933cc1..88d2bf9f6dd 100644 --- a/catalog/java/io/material/catalog/color/MaterialColorSpec.java +++ b/catalog/java/io/material/catalog/color/MaterialColorSpec.java @@ -17,67 +17,41 @@ package io.material.catalog.color; import android.content.Context; +import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; -import java.util.Arrays; -import java.util.List; +import androidx.annotation.StringRes; +import androidx.core.content.ContextCompat; /** - * Represents a color value as defined in the Material Spec. These are all defined by color name + - * color value. ie. blue500. + * Represents a color value as defined in the Material Spec. */ -public final class MaterialColorSpec { - public static final String SYSTEM_PREFIX = "system_"; - static final int TEXT_COLOR_SWITCH_VALUE = 400; +final class MaterialColorSpec { - @ColorRes private final int resourceId; - private final String resourceName; - private final String name; - private final int value; + private final String description; + @ColorInt private final int colorValue; - MaterialColorSpec(int resourceId, String resourceName, String name, int value) { - this.resourceId = resourceId; - this.resourceName = resourceName; - this.name = name; - this.value = value; + MaterialColorSpec(String description, int colorValue) { + this.description = description; + this.colorValue = colorValue; } - @ColorRes - int getResourceId() { - return resourceId; + String getDescription() { + return description; } - String getResourceName() { - return resourceName; + @ColorInt + int getColorValue() { + return colorValue; } - String getName() { - return name; + static MaterialColorSpec createFromResource(Context context, @ColorRes int colorRes) { + return new MaterialColorSpec( + context.getResources().getResourceEntryName(colorRes), + ContextCompat.getColor(context, colorRes)); } - int getValue() { - return value; - } - - static MaterialColorSpec create(Context context, @ColorRes int colorRes) { - String resName = context.getResources().getResourceEntryName(colorRes); - String name; - String value; - if (resName.startsWith(SYSTEM_PREFIX)) { - // Split the resource name into the color name and value, ie. system_accent1_500 to - // system_accent1 and 500. - int splitIndex = resName.lastIndexOf("_"); - name = resName.substring(0, splitIndex); - value = resName.substring(splitIndex + 1); - } else { - // Get the name of the color an value without prefixes - // String trimmedResName = resName; - int splitIndex = resName.lastIndexOf("_"); - String trimmedResName = resName.substring(splitIndex + 1); - // Split the resource name into the color name and value, ie. blue500 to blue and 500. - List parts = Arrays.asList(trimmedResName.split("(?<=\\D)(?=\\d)", -1)); - name = parts.get(0); - value = parts.get(1); - } - return new MaterialColorSpec(colorRes, resName, name, Integer.parseInt(value)); + static MaterialColorSpec createFromColorValue( + Context context, @StringRes int colorNameResId, @ColorInt int colorValue) { + return new MaterialColorSpec(context.getString(colorNameResId), colorValue); } }