material-components / material-components/material-components-android
[Color] Dynamic Contrast for ColorRoles
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
**Is your feature request related to a problem? Please describe.**
In the alphas of 1.11.0 dynamic contrast was introduced and so far it works great! In the updated docs you mention that custom colors should be handled with the non-dynamic contrast API, but in my apps instead of one custom color resource per tone I use a custom color to get the harmonized ColorRoles programmatically:
``` java
public ColorRoles getHarmonizedRoles(Context context, @ColorInt int color) {
return MaterialColors.getColorRoles(
context, MaterialColors.harmonizeWithPrimary(context, color)
);
}
```
I use these ColorRoles for example as Chip tinting for a yellow, harmonized Chip:
``` java
int colorYellow = ContextCompat.getColor(activity, R.color.yellow);
ColorRoles roles = ColorUtil.getHarmonizedRoles(activity, colorYellow);
chip.setChipIconTint(ColorStateList.valueOf(roles.getAccent()));
chip.setChipBackgroundColor(ColorStateList.valueOf(roles.getAccentContainer()));
chip.setTextColor(roles.getOnAccentContainer());
```
But when the contrast of the app changes with the contrast slider in Android 14, the Chip doesn't change its contrast.
**Describe the solution you'd like**
I think the solution with harmonized ColorRoles is much simpler than defining one color resource per tone, as ColorRoles handles light/dark theming automatically. Without ColorRoles, I need to define a whole range of tones for each custom color to get light/dark theming, color harmonization and dynamic contrast.
It would be amazing if dynamic contrast could be implemented into ColorRoles as well! Maybe in such a way that the output of the getter methods has already the specific contrast applied automatically.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MaterialColors.getColorRoles and harmonizeWithPrimary entry points and the ColorRoles getter methods described in the issue. Check how the Android 14 contrast slider affects dynamic contrast, then define and validate behavior where harmonized ColorRoles reflect the selected contrast for light and dark themes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java
- Domain
- design, mobile
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100