Skip to content

Commit

Permalink
[M3][Color] Refactored and added the Dynamic Color Palette demo in Ca…
Browse files Browse the repository at this point in the history
…talog.

PiperOrigin-RevId: 406168279
  • Loading branch information
Material Design Team authored and veganafro committed Oct 28, 2021
1 parent 3b6c804 commit b808484
Show file tree
Hide file tree
Showing 20 changed files with 982 additions and 1 deletion.
@@ -0,0 +1,25 @@
<!--
Copyright 2021 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
https://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.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M18,21L4,21L4,7L2,7v14c0,1.1 0.9,2 2,2h14v-2zM21,17L21,3c0,-1.1 -0.9,-2 -2,-2L8,1c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h11c1.1,0 2,-0.9 2,-2zM19,17L8,17L8,3h11v14z"/>
</vector>
20 changes: 20 additions & 0 deletions catalog/java/io/material/catalog/color/ColorAdapterItem.java
@@ -0,0 +1,20 @@
/*
* Copyright 2021 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;

/** Interface for the items in {@link ColorsAdapter}. */
public interface ColorAdapterItem {}
@@ -0,0 +1,38 @@
/*
* Copyright 2021 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 io.material.catalog.R;

import androidx.annotation.ArrayRes;
import androidx.annotation.LayoutRes;

/** A fragment that displays the Dynamic Palette demo for the Catalog app. */
public class ColorDynamicDemoFragment extends ColorPaletteDemoFragment {

@LayoutRes
@Override
protected int getColorsLayoutResId() {
return R.layout.cat_colors_palette_fragment;
}

@ArrayRes
@Override
protected int getColorsArrayResId() {
return R.array.cat_dynamic_colors;
}
}
63 changes: 63 additions & 0 deletions catalog/java/io/material/catalog/color/ColorHeaderItem.java
@@ -0,0 +1,63 @@
/*
* Copyright 2021 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.content.Context;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import java.util.List;

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

private static final String COLOR_600 = "600";

@ColorRes private final int backgroundColor;
private final String name;

ColorHeaderItem(Context context, List<ColorItem> colors) {
ColorItem sample = colors.get(0);
for (ColorItem color : colors) {
if (color.getColorResName().contains(COLOR_600)) {
sample = color;
break;
}
}
MaterialColorSpec materialColor = MaterialColorSpec.create(context, sample.getColorRes());
backgroundColor = materialColor.getResourceId();
name = materialColor.getName();
}

@ColorRes
int getBackgroundColorRes() {
return backgroundColor;
}

/** Returns the raw name of the color. */
@NonNull
public String getName() {
return name;
}

/** Returns the display name for the header, e.g. Blue. */
@NonNull
public String getDisplayName() {
String name = getName();
String headerColor = name.replace('_', ' ');
return Character.toUpperCase(headerColor.charAt(0)) + headerColor.substring(1);
}
}
51 changes: 51 additions & 0 deletions catalog/java/io/material/catalog/color/ColorItem.java
@@ -0,0 +1,51 @@
/*
* Copyright 2021 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.content.Context;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;

/** A class for the items in the color palette. */
public class ColorItem implements ColorAdapterItem {

private final MaterialColorSpec colorSpec;

ColorItem(Context context, @ColorRes int colorRes) {
colorSpec = MaterialColorSpec.create(context, colorRes);
}

/** Returns the resource ID of the color. */
@NonNull
@ColorRes
public int getColorRes() {
return colorSpec.getResourceId();
}

/** Returns the resource name of the color, e.g. system_accent1_100. */
@NonNull
public String getColorResName() {
return colorSpec.getResourceName();
}

/** Returns the int value of the color. */
@ColorInt
int getColorValue() {
return colorSpec.getValue();
}
}
41 changes: 41 additions & 0 deletions catalog/java/io/material/catalog/color/ColorMainDemoFragment.java
@@ -0,0 +1,41 @@
/*
* Copyright 2021 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 io.material.catalog.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.material.catalog.feature.DemoFragment;

/** A placeholder fragment that displays the main Color demo for the Catalog app. */
public class ColorMainDemoFragment extends DemoFragment {

@Nullable
@Override
public View onCreateDemoView(
@NonNull LayoutInflater layoutInflater,
@Nullable ViewGroup viewGroup,
@Nullable Bundle bundle) {
return layoutInflater.inflate(
R.layout.cat_colors_fragment, viewGroup, false /* attachToRoot */);
}
}

0 comments on commit b808484

Please sign in to comment.