Skip to content

Commit

Permalink
[M3][Color] Refactored MaterialColorSpec to take @ColorRes or @ColorI…
Browse files Browse the repository at this point in the history
…nt for the demo.

PiperOrigin-RevId: 441778151
(cherry picked from commit f43995f)
  • Loading branch information
Material Design Team authored and dsn5ft committed May 4, 2022
1 parent 1dad5f6 commit 207cb53
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 78 deletions.
32 changes: 32 additions & 0 deletions 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;
}
}
33 changes: 20 additions & 13 deletions catalog/java/io/material/catalog/color/ColorHeaderItem.java
Expand Up @@ -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<ColorItem> colors) {
ColorItem sample = colors.get(0);
Expand All @@ -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<String> 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);
}
Expand Down
10 changes: 6 additions & 4 deletions catalog/java/io/material/catalog/color/ColorItem.java
Expand Up @@ -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();
}
}
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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));
Expand All @@ -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());
}
}

Expand All @@ -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");
}
Expand Down
70 changes: 22 additions & 48 deletions catalog/java/io/material/catalog/color/MaterialColorSpec.java
Expand Up @@ -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<String> 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);
}
}

0 comments on commit 207cb53

Please sign in to comment.