Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Color class lacks FromRGB method that takes ints #308

Open
davidbritch opened this issue Feb 7, 2022 · 1 comment
Open

Color class lacks FromRGB method that takes ints #308

davidbritch opened this issue Feb 7, 2022 · 1 comment

Comments

@davidbritch
Copy link

davidbritch commented Feb 7, 2022

In Xamarin.Forms you could specify a Color with int arguments in both the constructor and FromRGB(A) methods. You can't do this in the Color class in MAUI. There's no constructor that takes ints, and no FromRGB(A) method that takes ints. Instead, the the nearest FromRGB method takes bytes.

Initially I accepted this as one of those paper cuts that everyone will have to put up with when moving an app from Forms to MAUI. There may be sound engineering reasons for it. But it's tripped me up, and made me mad, so many times now that I'm creating this issue.

What tends to happen is this: you have Forms code that calls FromRGB(A) with int arguments:

Random random = new Random();
var color = Color.FromRGB(random.Next(255), random.Next(255), random.Next(255));

random.Next(255) returns an int with a max value of 255.

This code worked in Forms and created the correct color. In MAUI, this code builds. You run the code but you don't get the color you expected. You ruminate about this and debug your code. Then you discover the Color that's created has component values all equal to 1. You check the int values being passed to FromRGB - all good. What the hell's going on? Then you discover that there's no FromRGB that takes ints (either from intellisense, or having to inspect the MAUI source because you know not to trust intellisense). But you notice there's a FromRGB that takes bytes. You hoped that your ints would be converted to bytes correctly, but they aren't. You cast your ints to bytes and it all works as expected. Then you curse that you burnt 10 mins on such a simple problem with one line of code, and wonder what you did to deserve this speed bump being put in your path.

Even once you know about this, its through gritted teeth when you have to cast ints to bytes in other Color code. Repeatedly.

Note: if you do Color.FromRGB(128,128,128) you get the expected color. The problem is when you don't use literals. You don't get the expected color when your arguments are ints returned by methods, or int variables.

@swharden
Copy link
Contributor

swharden commented Feb 7, 2022

Hi @davidbritch, at first glance this seems like it may be an oversight and not an intentional design decision. I'll add some links to source code for context.

Related issues

Constructors that accept int

There's no constructor that takes ints

Fluent methods that accept int

there's no FromRGB that takes ints

It seems like this issue may be resolved by creating overloads that accept int that expect values between 0 and 255.

I'll look into this more tonight and create a PR that adds these overloads 👍

swharden added a commit to swharden/Microsoft.Maui.Graphics.Sandbox that referenced this issue Feb 7, 2022
swharden added a commit to swharden/Microsoft.Maui.Graphics that referenced this issue Feb 7, 2022
Without these overloads it is possible that integers (expected to be 0-255) are interpreted as floats and clamped between 0 and 1.0 dotnet#308, dotnet#253
swharden added a commit to swharden/Microsoft.Maui.Graphics that referenced this issue Feb 7, 2022
Without these overloads it is possible that integers (expected to be 0-255) are interpreted as floats and clamped between 0 and 1.0 dotnet#308, dotnet#253
swharden added a commit to swharden/Microsoft.Maui.Graphics that referenced this issue Feb 7, 2022
Since methods FromRgb() and FromRgba() have overloads that accept bytes it seems logical that there are matching constructors that accept this data type. dotnet#308, dotnet#253
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants