material-components / material-components/material-components-android

[MaterialColors] Resolve attribute colors using Resources

Open
#492 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature request Feature: Color Feature: Resources
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)

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 toResources 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

Here's the screenshot
not desaturated
desaturated

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.