-
Notifications
You must be signed in to change notification settings - Fork 3k
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
gabrielemariotti
wants to merge
2
commits into
material-components:master
Choose a base branch
from
gabrielemariotti:color-utility
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Next
Next commit
ColorUtility to resolve theme color attrs programmatically
- Loading branch information
commit 399c74fb935d0ec7ac1f67f5aa9bf6ec4a44bde9
There are no files selected for viewing
282 changes: 282 additions & 0 deletions
282
lib/java/com/google/android/material/color/MaterialColorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.