Bug 38908 - Drawable.SetColorFilter takes the wrong parameters
Summary: Drawable.SetColorFilter takes the wrong parameters
Status: RESOLVED ANSWERED
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-02-19 11:28 UTC by Nikola
Modified: 2016-02-23 09:35 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Nikola 2016-02-19 11:28:45 UTC
Description: 

Drawable.SetColorFilter method differs from the one in the native Android SDK

It wouldn't be a problem if there was a way to actually obtain a color from the resource as a Color object.
Currently, the workaround is:

Color.ParseColor(_context.Resources.GetString(Resource.Color.myColor));

-------------------

Expectations:

Drawable.SetColorFilter takes an integer and PorterDuff.Mode

-------------------

Actual results:

Drawable.SetColorFilter takes a Color and PorterDuff.Mode

-------------------

Android Native SDK: https://developer.android.com/reference/android/graphics/drawable/Drawable.html#setColorFilter(int, android.graphics.PorterDuff.Mode)

Xamarin: https://developer.xamarin.com/api/member/Android.Graphics.Drawables.Drawable.SetColorFilter/p/Android.Graphics.Color/Android.Graphics.PorterDuff+Mode/
Comment 1 Atsushi Eno 2016-02-19 12:31:26 UTC
@jonp: is that intended?
Comment 2 Jonathan Pryor 2016-02-19 21:37:06 UTC
@Atsushi: Yes, it's intended.

@Nikola: As much as possible (and backward compatibility limits this somewhat), we try to map Android's `int color` parameters/etc. to `Android.Graphics.Color color`. This is by design.

As per the Java documentation, the Java `int` parameter is a color:

> Specify a color and Porter-Duff mode to be the color filter for this drawable

As such, using Color as the parameter type is appropriate.

> It wouldn't be a problem if there was a way to actually obtain a
> color from the resource as a Color object.

Using Resources.GetColor(int) doesn't work?

https://developer.xamarin.com/api/member/Android.Content.Res.Resources.GetColor/p/System.Int32/
Comment 3 Nikola 2016-02-22 08:20:30 UTC
Resources.GetColor(int) is deprecated as of Android Marshmallow 6.0 Using it in code will show us that. The better one to use is GetColor(int, Theme) which is not documented at all. 

May I ask why is your design trying to replace integers with color objects?
Comment 4 Jonathan Pryor 2016-02-23 08:31:05 UTC
> May I ask why is your design trying to replace integers with color objects?

They're not "objects", they're structs (value types), and the reason to replace color-related `int` types with Color is to make the API easier to use.

Rephrased, which would you prefer to read or write?

    drawable.SetColorFilter(0xFF0000, Mode.whatever)

or

    drawable.SetColorFilter(Color.Red, Mode.whatever);
Comment 5 Nikola 2016-02-23 09:30:14 UTC
> They're not "objects", they're structs (value types), and the reason to replace color-related `int` types with Color is to make the API easier to use.

My bad. They are structs, not objects. But I do not see how mapping the original API to yours differently is making it easier to use. Why not have both then? 

> Rephrased, which would you prefer to read or write?

I have no problem with both
Comment 6 Atsushi Eno 2016-02-23 09:35:17 UTC
> Why not have both

because, unlike method arguments, you cannot have two method overloads that only differ in the return value e.g.

int GetColorFilter(Mode mode)
Color GetColorFilter(Mode mode)