question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transformation with colorFilter not working correctly

See original GitHub issue

Describe the bug

  • use a vector drawable that contains a tint with alpha value (it’s just with what I found this issue, I did not test if this is the necessary)
  • tint the image with a solid PorterDuffColorFilter
  • the alpha value is not replaced correctly by the tint color

Expected behavior

The image should be tinted correctly - this means the filter should be applied as defined => SRC_IN should replace all existing pixels in the image with the provided color e.g.

To Reproduce

Code:

  • test activity with all code: here
  • drawable (it’s the default one added via android studio): here

Here’s the main code from the example:


val color = Color.WHITE
val mode = PorterDuff.Mode.SRC_IN
		
// Variant 1
binding.iv1.setImageResource(testImage)
binding.iv1.colorFilter = PorterDuffColorFilter(color, mode)

// Variant 2
binding.iv2.setImageResource(testImage)
binding.iv2.drawable.colorFilter = PorterDuffColorFilter(color, mode)

// Variant 3
binding.iv3.load(testImage) {
	transformations(ColorTintTransformation(color, mode))
}

The ColorTintTransformation class is part of the link above, it’s a simple transformation that does apply the filter on the bitmap with the help of the paint…

Logs/Screenshots

image-tint-test image-tint-test

Version

1.0.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
colinrtwhitecommented, Nov 19, 2021

After investigating with @cemore2048, it looks like Glide is using the application context to load the vector drawable whereas Coil uses the local activity context. This is why Glide loads the VectorDrawable without the tint and Coil loads it with the tint.

Both Glide and Coil then convert the vector drawable into a bitmap to perform the transformation, which bakes any tint into the bitmap. Setting the paint.colorFilter then applies the tint on top of the bitmap. Using setImageResource and colorFilter = doesn’t bake the tint into the bitmap, which is why it’s different.

Ultimately, I think Coil is working as intended in this case since using the application context to load the drawable incorrectly discards the tint and won’t always resolve the correct drawable (for instance, it will be incorrect if using a separate drawable for night mode). To have it work the same as Glide, you can remove the android:tint="?attr/colorControlNormal" from the vector drawable’s XML.

1reaction
cemore2048commented, Sep 21, 2021

@colinrtwhite not as much as I’d like. Did take a look and can reproduce getting it correct in the way anesabml was able to. But haven’t looked into a solution since the inkling here is that there’s still an underlying issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Applying color filter in StateListDrawable not working
To apply a color filter and change the color of a drawable I find that using a base drawable which is grayscale/white is...
Read more >
Customize an image | Jetpack Compose - Android Developers
Color filter - Transform pixel colors of image. The Image composable has a colorFilter parameter that can change the output of individual pixels...
Read more >
Color Balance adjustment in Photoshop - Adobe Support
Color balance can be used to correct color imperfections in your image. ... Adobe Color Picker to specify a color for a custom...
Read more >
Use color filters in Windows - Microsoft Support
Turn the Color filters switch on. Select a color filter from the list. Try each filter to see which one suits you best....
Read more >
filter | CSS-Tricks
The value of “amount” defines the proportion of the conversion. ... This filter function is not yet supported in browsers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found