material-components / material-components/material-components-android
[MaterialColors] Resolve attribute colors using Resources
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 17.4k
- Forks
- 3.2k
- PR merge metrics
- No merged PRs in 30d
Description
I'm building a library that desaturates colors when Dark theme is enabled (as described in [Material Docs](https://material.io/design/color/dark-theme.html#ui-application))
Basically, I'm overriding `getColor` and `getColorStateList` in `android.content.res.Resources`, and them returning the requested color desaturated. But when it comes to material widgets, it doesn't work.
So, diving into material widgets, I noticed that the colors are retrieved calling `MaterialColors.getColor` that calls `MaterialAttributes.resolveOrThrow`, and after resolving the attributeId, the color is directly available in `typedValue.data` (line 71). But in this way, the call to`Resources` is skipped and my desaturation methods will never be called.
My solution is to call `ContextCompat.getColor(context, typedValue.resourceId)` instead of `typeValue.data` at line 71 of `MaterialAttributes.java`.
I understand that this can cause some performance impact by retrieving the color another time, but I'm writing this library because a lot of application has dynamic primary and accent colors, and it's a pain to calculate manually the desaturated colors by hand and then add to `night-values`.
Or please let me know if the problem can be resolved in another way.
[Here the desaturation code](https://github.com/ologe/color-desaturation/blob/master/lib/src/main/java/dev/olog/lib/DarkDesaturatedResources.java)
Here's the screenshot
[not desaturated](https://github.com/ologe/color-desaturation/blob/master/img/light_mode.png)
[desaturated](https://github.com/ologe/color-desaturation/blob/master/img/dark_mode.png)
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 in MaterialAttributes.java at line 71 and trace the MaterialColors.getColor path through MaterialAttributes.resolveOrThrow. Compare the direct typedValue.data lookup with the issue's ContextCompat.getColor proposal, then verify that material widget colors pass through the overridden Resources methods and are desaturated as expected.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100