Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme] Color utility to resolving material theme color attrs programmatically #1656

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
282 changes: 282 additions & 0 deletions lib/java/com/google/android/material/color/MaterialColorUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/*
* Copyright 2020 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.
*/

package com.google.android.material.color;

import android.content.Context;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import com.google.android.material.R;

/** Utility methods for {@link MaterialColors}. */
public class MaterialColorUtils {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better to define this as an enum class under MaterialColors, say, MaterialColors.Color.PRIMARY, etc., and provide getColor() method for the enum class.


private MaterialColorUtils() {}
private static final int COLOR_PRIMARY_ATTR = R.attr.colorPrimary;
private static final int COLOR_PRIMARY_VARIANT_ATTR = R.attr.colorPrimaryVariant;
private static final int COLOR_ON_PRIMARY_ATTR = R.attr.colorOnPrimary;
private static final int COLOR_SECONDARY_ATTR = R.attr.colorSecondary;
private static final int COLOR_SECONDARY_VARIANT_ATTR = R.attr.colorSecondaryVariant;
private static final int COLOR_ON_SECONDARY_ATTR = R.attr.colorOnSecondary;
private static final int COLOR_SURFACE_ATTR = R.attr.colorSurface;
private static final int COLOR_ON_SURFACE_ATTR = R.attr.colorOnSurface;
private static final int COLOR_BACKGROUND_ATTR = android.R.attr.colorBackground;
private static final int COLOR_ON_BACKGROUND_ATTR = R.attr.colorOnBackground;
private static final int COLOR_ERROR_ATTR = R.attr.colorError;
private static final int COLOR_ON_ERROR_ATTR = R.attr.colorOnError;


/**
* Returns the primary color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorPrimary(Context context) {
return MaterialColors.getColor(context, COLOR_PRIMARY_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the primary color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorPrimary(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_PRIMARY_ATTR);
}

/**
* Returns the primary variant color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorPrimaryVariant(Context context) {
return MaterialColors.getColor(context, COLOR_PRIMARY_VARIANT_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the primary variant color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorPrimaryVariant(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_PRIMARY_VARIANT_ATTR);
}

/**
* Returns the onPrimary color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnPrimary(Context context) {
return MaterialColors.getColor(context, COLOR_ON_PRIMARY_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the OnPrimary color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnPrimary(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ON_PRIMARY_ATTR);
}

/**
* Returns the secondary color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSecondary(Context context) {
return MaterialColors.getColor(context, COLOR_SECONDARY_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the secondary color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSecondary(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_SECONDARY_ATTR);
}

/**
* Returns the secondary variant color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSecondaryVariant(Context context) {
return MaterialColors.getColor(context, COLOR_SECONDARY_VARIANT_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the secondary variant color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSecondaryVariant(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_SECONDARY_VARIANT_ATTR);
}

/**
* Returns the OnSecondary color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnSecondary(Context context) {
return MaterialColors.getColor(context, COLOR_ON_SECONDARY_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the onSecondary color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnSecondary(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ON_SECONDARY_ATTR);
}

/**
* Returns the surface color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSurface(Context context) {
return MaterialColors.getColor(context, COLOR_SURFACE_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the surface color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorSurface(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_SURFACE_ATTR);
}

/**
* Returns the onSurface color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnSurface(Context context) {
return MaterialColors.getColor(context, COLOR_ON_SURFACE_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the onSurface color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnSurface(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ON_SURFACE_ATTR);
}

/**
* Returns the colorBackground int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorBackground(Context context) {
return MaterialColors.getColor(context, COLOR_BACKGROUND_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the colorBackground int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorBackground(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_BACKGROUND_ATTR);
}

/**
* Returns the colorOnBackground int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnBackground(Context context) {
return MaterialColors.getColor(context, COLOR_ON_BACKGROUND_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the colorOnBackground int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnBackground(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ON_BACKGROUND_ATTR);
}

/**
* Returns the error color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorError(Context context) {
return MaterialColors.getColor(context, COLOR_ERROR_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the error color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorError(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ERROR_ATTR);
}

/**
* Returns the onError color int
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnError(Context context) {
return MaterialColors.getColor(context, COLOR_ON_ERROR_ATTR, MaterialColorUtils.class.getSimpleName());
}

/**
* Returns the onError color int, using the {@link Context} of the provided {@code view}.
*
* @throws IllegalArgumentException if the attribute is not set in the current theme.
*/
@ColorInt
public static int colorOnError(@NonNull View view) {
return MaterialColors.getColor(view, COLOR_ON_ERROR_ATTR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2019 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.
-->

<manifest xmlns:tools="http://schemas.android.com/tools"
package="com.google.android.material.color">

<uses-sdk
tools:overrideLibrary="androidx.test.core"/>

<application/>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright (C) 2019 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 com.google.android.material.color;

import android.content.Context;
import android.view.View;
import androidx.annotation.AttrRes;
import androidx.appcompat.app.AppCompatActivity;
import androidx.test.core.app.ApplicationProvider;
import com.google.android.material.R;
import com.google.android.material.button.MaterialButton;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.internal.DoNotInstrument;

import static com.google.common.truth.Truth.assertThat;

/** Tests for the Material themes. */
@RunWith(RobolectricTestRunner.class)
@DoNotInstrument
public class MaterialColorUtilsTest {

private static final String TAG = MaterialColorUtilsTest.class.getSimpleName();

private AppCompatActivity activity;
private final Context context = ApplicationProvider.getApplicationContext();

@Before
public void themeApplicationContext() {
context.setTheme(R.style.Theme_MaterialComponents_Light_NoActionBar_Bridge);
activity = Robolectric.buildActivity(AppCompatActivity.class).setup().get();
}

@Test
public void testMaterialColor_usingContext() {
context.setTheme(R.style.Theme_MaterialComponents_Light);
assertThat(MaterialColorUtils.colorPrimary(context)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorPrimaryVariant(context)).isEqualTo(getColor(R.attr.colorPrimaryVariant));
assertThat(MaterialColorUtils.colorOnPrimary(context)).isEqualTo(getColor(R.attr.colorOnPrimary));
assertThat(MaterialColorUtils.colorSecondary(context)).isEqualTo(getColor(R.attr.colorSecondary));
assertThat(MaterialColorUtils.colorSecondaryVariant(context)).isEqualTo(getColor(R.attr.colorSecondaryVariant));
assertThat(MaterialColorUtils.colorOnSecondary(context)).isEqualTo(getColor(R.attr.colorOnSecondary));
assertThat(MaterialColorUtils.colorSurface(context)).isEqualTo(getColor(R.attr.colorSurface));
assertThat(MaterialColorUtils.colorOnSurface(context)).isEqualTo(getColor(R.attr.colorOnSurface));
assertThat(MaterialColorUtils.colorBackground(context)).isEqualTo(getColor(android.R.attr.colorBackground));
assertThat(MaterialColorUtils.colorOnBackground(context)).isEqualTo(getColor(R.attr.colorOnBackground));
assertThat(MaterialColorUtils.colorError(context)).isEqualTo(getColor(R.attr.colorError));
assertThat(MaterialColorUtils.colorOnError(context)).isEqualTo(getColor(R.attr.colorOnError));
}

@Test
public void testMaterialColor_usingView() {
View inflated = activity.getLayoutInflater().inflate(R.layout.test_color_view, null);
MaterialButton view = inflated.findViewById(R.id.viewTestColor);
assertThat(MaterialColorUtils.colorPrimary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorPrimaryVariant(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnPrimary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSecondary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSecondaryVariant(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnSecondary(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorSurface(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnSurface(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorBackground(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnBackground(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorError(view)).isEqualTo(getColor(R.attr.colorPrimary));
assertThat(MaterialColorUtils.colorOnError(view)).isEqualTo(getColor(R.attr.colorPrimary));
}

private int getColor(@AttrRes int colorAttributeResId) {
return MaterialColors.getColor(context, colorAttributeResId, TAG);
}
}
Loading