Bug 113 - UIColor(int, int, int) seems to give only basic colors
Summary: UIColor(int, int, int) seems to give only basic colors
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: 4.x
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2011-07-29 12:25 UTC by Dean Cleaver
Modified: 2011-08-03 13:40 UTC (History)
3 users (show)

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

Project that shows the bug (2.56 MB, application/octet-stream)
2011-08-02 15:58 UTC, Dean Cleaver

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:

Description Dean Cleaver 2011-07-29 12:25:55 UTC
I recently had a problem setting the background color (actually, creating a new view and setting that as the background view) on a UITableCellView.

When I did this (location.TicketColorRed etc are int values):

backgroundColor = UIColor.FromRGB(location.TicketColorRed, location.TicketColorGreen, location.TicketColorBlue);

I only got basic colors - passing in the RGB values for "Pink" gave white. Blue, Red, Yellow etc all worked ok, but nothing in between. Changing it to this:

backgroundColor = UIColor.FromRGB((float)location.TicketColorRed / 255, (float)location.TicketColorGreen /255, (float)location.TicketColorBlue / 255);

Gave me the colors I was expecting.
Comment 1 Miguel de Icaza [MSFT] 2011-07-29 18:02:38 UTC
Unable to reproduce this bug.

Please provide a self-contained test case.   The code in the runtime does exactly what you describe.
Comment 2 Dean Cleaver 2011-08-02 15:58:28 UTC
Created attachment 49 [details]
Project that shows the bug

The interesting file is the UIColorViewSource.cs

It specifies 2 cells. It returns the first one using the (int, int, int) version of FromRGB, and turns out white. The second converts to floats, divides by 255 and uses the (float, float, float) version of FromRGB, and returns a pinkish color as expected.

Basic colors seem to work (0, 0, 255) returns blue for both, but anything else as above returns the nearest basic color.
Comment 3 Jeffrey Stedfast 2011-08-03 13:11:25 UTC
Okay, so the problem is this. There are two methods:

FromRGB (byte red, byte green, byte blue);
FromRGB (float red, float green, float blue);

Your code assumes that the compiler will choose the byte version, but it is choosing the float version and since all the values are outside the range of 0.0 to 1.0, they are getting clamped and you are not getting the results you expected.
Comment 4 Dean Cleaver 2011-08-03 13:13:21 UTC
My apologies - I am used to the .Net Color.FromARGB(int, int, int, int) methods, and didn't look close enough to see that these ones were bytes not ints - that explains it. Thank you.
Comment 5 Jeffrey Stedfast 2011-08-03 13:40:41 UTC
I brought this bug up with the rest of the team and we all agreed that we should add FromRGB() and FromRGBA() overloads which take int arguments to prevent this kind of confusion in the future.

These API additions should make it into our next release (4.1)