Skip to content

[MaterialColors] Resolve attribute colors using Resources #492

Open
@ologe

Description

@ologe

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions