Skip to content

Commit 0347587

Browse files
Material Design Teamdsn5ft
authored andcommitted
[M3][Color] Added ColorGrid classes for color harmonization demo.
PiperOrigin-RevId: 445883341
1 parent 1a42c74 commit 0347587

File tree

6 files changed

+293
-3
lines changed

6 files changed

+293
-3
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 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 io.material.catalog.R;
20+
21+
import android.content.Context;
22+
import android.view.LayoutInflater;
23+
import android.view.View;
24+
import android.view.ViewGroup;
25+
import android.widget.TextView;
26+
import androidx.annotation.IdRes;
27+
import androidx.annotation.NonNull;
28+
import com.google.android.material.color.ColorRoles;
29+
30+
/** A class to form side-by-side color palettes. */
31+
final class ColorGrid {
32+
33+
@NonNull private final MaterialColorSpec materialColorSpecAccent;
34+
@NonNull private final MaterialColorSpec materialColorSpecOnAccent;
35+
@NonNull private final MaterialColorSpec materialColorSpecAccentContainer;
36+
@NonNull private final MaterialColorSpec materialColorSpecOnAccentContainer;
37+
38+
static ColorGrid createFromColorGridData(Context context, ColorGridData colorGridData) {
39+
ColorRoles colorRoles = colorGridData.getColorRoles();
40+
ColorRoleNames colorRoleNames = colorGridData.getColorRoleNames();
41+
MaterialColorSpec[] materialColorSpecs =
42+
new MaterialColorSpec[] {
43+
MaterialColorSpec.createFromColorValue(
44+
colorRoleNames.getAccentName(), colorRoles.getAccent()),
45+
MaterialColorSpec.createFromColorValue(
46+
colorRoleNames.getOnAccentName(), colorRoles.getOnAccent()),
47+
MaterialColorSpec.createFromColorValue(
48+
colorRoleNames.getAccentContainerName(), colorRoles.getAccentContainer()),
49+
MaterialColorSpec.createFromColorValue(
50+
colorRoleNames.getOnAccentContainerName(), colorRoles.getOnAccentContainer()),
51+
};
52+
return new ColorGrid(
53+
materialColorSpecs[0], materialColorSpecs[1], materialColorSpecs[2], materialColorSpecs[3]);
54+
}
55+
56+
static ColorGrid createFromAttrResId(Context context, String[] colorNames, int[] attrResIds) {
57+
if (colorNames.length < 4 || colorNames.length != attrResIds.length) {
58+
throw new IllegalArgumentException(
59+
"Color names need to be at least four and correspond to attribute resource ids.");
60+
}
61+
return new ColorGrid(
62+
MaterialColorSpec.createFromAttrResId(context, colorNames[0], attrResIds[0]),
63+
MaterialColorSpec.createFromAttrResId(context, colorNames[1], attrResIds[1]),
64+
MaterialColorSpec.createFromAttrResId(context, colorNames[2], attrResIds[2]),
65+
MaterialColorSpec.createFromAttrResId(context, colorNames[3], attrResIds[3]));
66+
}
67+
68+
private ColorGrid(
69+
@NonNull MaterialColorSpec materialColorSpecAccent,
70+
@NonNull MaterialColorSpec materialColorSpecOnAccent,
71+
@NonNull MaterialColorSpec materialColorSpecAccentContainer,
72+
@NonNull MaterialColorSpec materialColorSpecOnAccentContainer) {
73+
this.materialColorSpecAccent = materialColorSpecAccent;
74+
this.materialColorSpecOnAccent = materialColorSpecOnAccent;
75+
this.materialColorSpecAccentContainer = materialColorSpecAccentContainer;
76+
this.materialColorSpecOnAccentContainer = materialColorSpecOnAccentContainer;
77+
}
78+
79+
View renderView(Context context, ViewGroup container) {
80+
View catalogColorsGrid =
81+
LayoutInflater.from(context)
82+
.inflate(R.layout.cat_colors_grid, container, /* attachToRoot= */ false);
83+
84+
bindColorSpecItem(catalogColorsGrid, R.id.cat_color_accent, materialColorSpecAccent);
85+
bindColorSpecItem(catalogColorsGrid, R.id.cat_color_on_accent, materialColorSpecOnAccent);
86+
bindColorSpecItem(
87+
catalogColorsGrid, R.id.cat_color_accent_container, materialColorSpecAccentContainer);
88+
bindColorSpecItem(
89+
catalogColorsGrid, R.id.cat_color_on_accent_container, materialColorSpecOnAccentContainer);
90+
91+
return catalogColorsGrid;
92+
}
93+
94+
private static void bindColorSpecItem(
95+
View gridView, @IdRes int textViewId, MaterialColorSpec materialColorSpec) {
96+
TextView colorSpec = gridView.findViewById(textViewId);
97+
98+
colorSpec.setText(materialColorSpec.getDescription());
99+
colorSpec.setTextColor(ColorDemoUtils.getTextColor(materialColorSpec.getColorValue()));
100+
colorSpec.setBackgroundColor(materialColorSpec.getColorValue());
101+
}
102+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 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.content.Context;
20+
import androidx.annotation.ArrayRes;
21+
import androidx.annotation.ColorInt;
22+
import androidx.annotation.ColorRes;
23+
import com.google.android.material.color.ColorRoles;
24+
import com.google.android.material.color.MaterialColors;
25+
26+
/** A class that provides data for {@link ColorGrid}. */
27+
final class ColorGridData {
28+
29+
private final ColorRoles colorRoles;
30+
private final ColorRoleNames colorRoleNames;
31+
32+
static ColorGridData createFromColorResId(
33+
Context context, @ColorRes int colorResourceId, @ArrayRes int colorNameIds) {
34+
return createFromColorValue(
35+
context, context.getResources().getColor(colorResourceId), colorNameIds);
36+
}
37+
38+
static ColorGridData createFromColorValue(
39+
Context context, @ColorInt int seedColorValue, @ArrayRes int colorNameIds) {
40+
String[] colorNames = context.getResources().getStringArray(colorNameIds);
41+
return new ColorGridData(
42+
MaterialColors.getColorRoles(context, seedColorValue),
43+
new ColorRoleNames(colorNames[0], colorNames[1], colorNames[2], colorNames[3]));
44+
}
45+
46+
private ColorGridData(ColorRoles colorRoles, ColorRoleNames colorRoleNames) {
47+
this.colorRoles = colorRoles;
48+
this.colorRoleNames = colorRoleNames;
49+
}
50+
51+
ColorRoles getColorRoles() {
52+
return colorRoles;
53+
}
54+
55+
ColorRoleNames getColorRoleNames() {
56+
return colorRoleNames;
57+
}
58+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
/** A class defines the name for color role. */
20+
final class ColorRoleNames {
21+
22+
private final String accentName;
23+
private final String onAccentName;
24+
private final String accentContainerName;
25+
private final String onAccentContainerName;
26+
27+
public ColorRoleNames(
28+
String accentName,
29+
String onAccentName,
30+
String accentContainerName,
31+
String onAccentContainerName) {
32+
this.accentName = accentName;
33+
this.onAccentName = onAccentName;
34+
this.accentContainerName = accentContainerName;
35+
this.onAccentContainerName = onAccentContainerName;
36+
}
37+
38+
/** Returns the accent color name. */
39+
public String getAccentName() {
40+
return accentName;
41+
}
42+
43+
/** Returns the on_accent color name. */
44+
public String getOnAccentName() {
45+
return onAccentName;
46+
}
47+
48+
/** Returns the accent_container color name. */
49+
public String getAccentContainerName() {
50+
return accentContainerName;
51+
}
52+
53+
/** Returns the on_accent_container color name. */
54+
public String getOnAccentContainerName() {
55+
return onAccentContainerName;
56+
}
57+
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@
1717
package io.material.catalog.color;
1818

1919
import android.content.Context;
20+
import androidx.annotation.AttrRes;
2021
import androidx.annotation.ColorInt;
2122
import androidx.annotation.ColorRes;
22-
import androidx.annotation.StringRes;
2323
import androidx.core.content.ContextCompat;
24+
import com.google.android.material.color.MaterialColors;
2425

2526
/**
2627
* Represents a color value as defined in the Material Spec.
@@ -51,7 +52,14 @@ static MaterialColorSpec createFromResource(Context context, @ColorRes int color
5152
}
5253

5354
static MaterialColorSpec createFromColorValue(
54-
Context context, @StringRes int colorNameResId, @ColorInt int colorValue) {
55-
return new MaterialColorSpec(context.getString(colorNameResId), colorValue);
55+
String colorNameResource, @ColorInt int colorValue) {
56+
return new MaterialColorSpec(colorNameResource, colorValue);
57+
}
58+
59+
static MaterialColorSpec createFromAttrResId(
60+
Context context, String colorNameResource, @AttrRes int attrRes) {
61+
return createFromColorValue(
62+
colorNameResource,
63+
MaterialColors.getColor(context, attrRes, colorNameResource + "cannot be resolved."));
5664
}
5765
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2021 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout
19+
xmlns:android="http://schemas.android.com/apk/res/android"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:orientation="vertical" >
23+
24+
<LinearLayout
25+
android:layout_width="match_parent"
26+
android:layout_height="wrap_content"
27+
android:orientation="horizontal" >
28+
29+
<TextView
30+
android:id="@+id/cat_color_accent"
31+
android:padding="@dimen/cat_colors_grid_item_padding"
32+
android:layout_width="0dp"
33+
android:layout_weight="1"
34+
android:layout_height="@dimen/cat_colors_grid_item_height" />
35+
36+
<TextView
37+
android:id="@+id/cat_color_on_accent"
38+
android:padding="@dimen/cat_colors_grid_item_padding"
39+
android:layout_width="0dp"
40+
android:layout_weight="1"
41+
android:layout_height="@dimen/cat_colors_grid_item_height" />
42+
</LinearLayout>
43+
44+
<LinearLayout
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:orientation="horizontal" >
48+
49+
<TextView
50+
android:id="@+id/cat_color_accent_container"
51+
android:padding="@dimen/cat_colors_grid_item_padding"
52+
android:layout_width="0dp"
53+
android:layout_weight="1"
54+
android:layout_height="@dimen/cat_colors_grid_item_height" />
55+
56+
<TextView
57+
android:id="@+id/cat_color_on_accent_container"
58+
android:padding="@dimen/cat_colors_grid_item_padding"
59+
android:layout_width="0dp"
60+
android:layout_weight="1"
61+
android:layout_height="@dimen/cat_colors_grid_item_height" />
62+
</LinearLayout>
63+
</LinearLayout>

catalog/java/io/material/catalog/color/res/values/dimens.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717

1818
<resources>
1919
<dimen name="cat_colors_header_space">32dp</dimen>
20+
<dimen name="cat_colors_grid_item_padding">4dp</dimen>
21+
<dimen name="cat_colors_grid_item_height">60dp</dimen>
2022
</resources>

0 commit comments

Comments
 (0)