Skip to content

Commit f43995f

Browse files
Material Design Teamdrchen
authored andcommitted
[M3][Color] Refactored MaterialColorSpec to take @ColorRes or @ColorInt for the demo.
PiperOrigin-RevId: 441778151
1 parent 26308d9 commit f43995f

File tree

5 files changed

+83
-78
lines changed

5 files changed

+83
-78
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2022 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.material.catalog.color;
18+
19+
import android.graphics.Color;
20+
import androidx.annotation.ColorInt;
21+
import com.google.android.material.color.MaterialColors;
22+
23+
/** Utility methods for Color. */
24+
final class ColorDemoUtils {
25+
26+
private ColorDemoUtils() {}
27+
28+
static int getTextColor(@ColorInt int backgroundColor) {
29+
// Use white text color if the background color is considered dark.
30+
return MaterialColors.isColorLight(backgroundColor) ? Color.BLACK : Color.WHITE;
31+
}
32+
}

catalog/java/io/material/catalog/color/ColorHeaderItem.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,17 @@
1919
import android.content.Context;
2020
import androidx.annotation.ColorRes;
2121
import androidx.annotation.NonNull;
22+
import java.util.Arrays;
2223
import java.util.List;
2324

2425
/** A class for the headers in the color palette. */
2526
public class ColorHeaderItem implements ColorAdapterItem {
2627

28+
public static final String SYSTEM_PREFIX = "system_";
2729
private static final String COLOR_600 = "600";
2830

29-
@ColorRes private final int backgroundColor;
30-
private final String name;
31+
@ColorRes private final int backgroundColorRes;
32+
private final String description;
3133

3234
ColorHeaderItem(Context context, List<ColorItem> colors) {
3335
ColorItem sample = colors.get(0);
@@ -37,26 +39,31 @@ public class ColorHeaderItem implements ColorAdapterItem {
3739
break;
3840
}
3941
}
40-
MaterialColorSpec materialColor = MaterialColorSpec.create(context, sample.getColorRes());
41-
backgroundColor = materialColor.getResourceId();
42-
name = materialColor.getName();
42+
backgroundColorRes = sample.getColorRes();
43+
description = sample.getColorResName();
4344
}
4445

4546
@ColorRes
4647
int getBackgroundColorRes() {
47-
return backgroundColor;
48-
}
49-
50-
/** Returns the raw name of the color. */
51-
@NonNull
52-
public String getName() {
53-
return name;
48+
return backgroundColorRes;
5449
}
5550

5651
/** Returns the display name for the header, e.g. Blue. */
5752
@NonNull
5853
public String getDisplayName() {
59-
String name = getName();
54+
String name;
55+
int splitIndex = description.lastIndexOf("_");
56+
if (description.startsWith(SYSTEM_PREFIX)) {
57+
// Split the resource name into the color name and value, ie. system_accent1_500 to
58+
// system_accent1 and 500.
59+
name = description.substring(0, splitIndex);
60+
} else {
61+
// Get the name of the color an value without prefixes
62+
String trimmedResName = description.substring(splitIndex + 1);
63+
// Split the resource name into the color name and value, ie. blue500 to blue and 500.
64+
List<String> parts = Arrays.asList(trimmedResName.split("(?<=\\D)(?=\\d)", -1));
65+
name = parts.get(0);
66+
}
6067
String headerColor = name.replace('_', ' ');
6168
return Character.toUpperCase(headerColor.charAt(0)) + headerColor.substring(1);
6269
}

catalog/java/io/material/catalog/color/ColorItem.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@
2525
public class ColorItem implements ColorAdapterItem {
2626

2727
private final MaterialColorSpec colorSpec;
28+
@ColorRes private final int colorRes;
2829

2930
ColorItem(Context context, @ColorRes int colorRes) {
30-
colorSpec = MaterialColorSpec.create(context, colorRes);
31+
this.colorRes = colorRes;
32+
colorSpec = MaterialColorSpec.createFromResource(context, colorRes);
3133
}
3234

3335
/** Returns the resource ID of the color. */
3436
@NonNull
3537
@ColorRes
3638
public int getColorRes() {
37-
return colorSpec.getResourceId();
39+
return colorRes;
3840
}
3941

4042
/** Returns the resource name of the color, e.g. system_accent1_100. */
4143
@NonNull
4244
public String getColorResName() {
43-
return colorSpec.getResourceName();
45+
return colorSpec.getDescription();
4446
}
4547

4648
/** Returns the int value of the color. */
4749
@ColorInt
4850
int getColorValue() {
49-
return colorSpec.getValue();
51+
return colorSpec.getColorValue();
5052
}
5153
}

catalog/java/io/material/catalog/color/ColorPaletteDemoFragment.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import android.content.ClipData;
2222
import android.content.ClipboardManager;
2323
import android.content.Context;
24-
import android.graphics.Color;
2524
import android.os.Bundle;
2625
import androidx.recyclerview.widget.LinearLayoutManager;
2726
import androidx.recyclerview.widget.RecyclerView;
@@ -45,8 +44,6 @@
4544
/** A fragment that displays the Color Palette demo for the Catalog app. */
4645
public abstract class ColorPaletteDemoFragment extends DemoFragment {
4746

48-
private static final int GREY_10 = 0xFF202124;
49-
5047
private ColorsAdapter adapter;
5148

5249
@Nullable
@@ -136,7 +133,7 @@ void bind(ColorItem colorItem) {
136133
int value = ContextCompat.getColor(context, colorItem.getColorRes());
137134
String colorResName = colorItem.getColorResName();
138135
String resQualifier =
139-
colorResName.startsWith(MaterialColorSpec.SYSTEM_PREFIX) ? "@android:color/" : "@color/";
136+
colorResName.startsWith(ColorHeaderItem.SYSTEM_PREFIX) ? "@android:color/" : "@color/";
140137

141138
nameView.setText(
142139
context.getResources().getString(R.string.cat_color_res, resQualifier, colorResName));
@@ -151,12 +148,7 @@ void bind(ColorItem colorItem) {
151148

152149
@ColorInt
153150
private int getTextColor(ColorItem colorItem) {
154-
if (colorItem.getColorValue() < MaterialColorSpec.TEXT_COLOR_SWITCH_VALUE) {
155-
// Use dark grey if color value is less than TEXT_COLOR_SWITCH_VALUE.
156-
return GREY_10;
157-
} else {
158-
return Color.WHITE;
159-
}
151+
return ColorDemoUtils.getTextColor(colorItem.getColorValue());
160152
}
161153
}
162154

@@ -173,9 +165,7 @@ private String generateColorsText(@NonNull ColorsAdapter adapter) {
173165
int value = ContextCompat.getColor(getContext(), colorItem.getColorRes());
174166
String colorResName = colorItem.getColorResName();
175167
String resQualifier =
176-
colorResName.startsWith(MaterialColorSpec.SYSTEM_PREFIX)
177-
? "@android:color/"
178-
: "@color/";
168+
colorResName.startsWith(ColorHeaderItem.SYSTEM_PREFIX) ? "@android:color/" : "@color/";
179169
colorsText.append(String.format("#%06x", value & 0xFFFFFF)).append("\n");
180170
colorsText.append(resQualifier).append(colorResName).append("\n");
181171
}

catalog/java/io/material/catalog/color/MaterialColorSpec.java

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,67 +17,41 @@
1717
package io.material.catalog.color;
1818

1919
import android.content.Context;
20+
import androidx.annotation.ColorInt;
2021
import androidx.annotation.ColorRes;
21-
import java.util.Arrays;
22-
import java.util.List;
22+
import androidx.annotation.StringRes;
23+
import androidx.core.content.ContextCompat;
2324

2425
/**
25-
* Represents a color value as defined in the Material Spec. These are all defined by color name +
26-
* color value. ie. blue500.
26+
* Represents a color value as defined in the Material Spec.
2727
*/
28-
public final class MaterialColorSpec {
29-
public static final String SYSTEM_PREFIX = "system_";
30-
static final int TEXT_COLOR_SWITCH_VALUE = 400;
28+
final class MaterialColorSpec {
3129

32-
@ColorRes private final int resourceId;
33-
private final String resourceName;
34-
private final String name;
35-
private final int value;
30+
private final String description;
31+
@ColorInt private final int colorValue;
3632

37-
MaterialColorSpec(int resourceId, String resourceName, String name, int value) {
38-
this.resourceId = resourceId;
39-
this.resourceName = resourceName;
40-
this.name = name;
41-
this.value = value;
33+
MaterialColorSpec(String description, int colorValue) {
34+
this.description = description;
35+
this.colorValue = colorValue;
4236
}
4337

44-
@ColorRes
45-
int getResourceId() {
46-
return resourceId;
38+
String getDescription() {
39+
return description;
4740
}
4841

49-
String getResourceName() {
50-
return resourceName;
42+
@ColorInt
43+
int getColorValue() {
44+
return colorValue;
5145
}
5246

53-
String getName() {
54-
return name;
47+
static MaterialColorSpec createFromResource(Context context, @ColorRes int colorRes) {
48+
return new MaterialColorSpec(
49+
context.getResources().getResourceEntryName(colorRes),
50+
ContextCompat.getColor(context, colorRes));
5551
}
5652

57-
int getValue() {
58-
return value;
59-
}
60-
61-
static MaterialColorSpec create(Context context, @ColorRes int colorRes) {
62-
String resName = context.getResources().getResourceEntryName(colorRes);
63-
String name;
64-
String value;
65-
if (resName.startsWith(SYSTEM_PREFIX)) {
66-
// Split the resource name into the color name and value, ie. system_accent1_500 to
67-
// system_accent1 and 500.
68-
int splitIndex = resName.lastIndexOf("_");
69-
name = resName.substring(0, splitIndex);
70-
value = resName.substring(splitIndex + 1);
71-
} else {
72-
// Get the name of the color an value without prefixes
73-
// String trimmedResName = resName;
74-
int splitIndex = resName.lastIndexOf("_");
75-
String trimmedResName = resName.substring(splitIndex + 1);
76-
// Split the resource name into the color name and value, ie. blue500 to blue and 500.
77-
List<String> parts = Arrays.asList(trimmedResName.split("(?<=\\D)(?=\\d)", -1));
78-
name = parts.get(0);
79-
value = parts.get(1);
80-
}
81-
return new MaterialColorSpec(colorRes, resName, name, Integer.parseInt(value));
53+
static MaterialColorSpec createFromColorValue(
54+
Context context, @StringRes int colorNameResId, @ColorInt int colorValue) {
55+
return new MaterialColorSpec(context.getString(colorNameResId), colorValue);
8256
}
8357
}

0 commit comments

Comments
 (0)