Transformation with colorFilter not working correctly
See original GitHub issueDescribe 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:
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
Version
1.0.0
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. UsingsetImageResource
andcolorFilter =
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.@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.