This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
Color class lacks FromRGB method that takes ints #308
Comments
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
|
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.
In Xamarin.Forms you could specify a
Color
withint
arguments in both the constructor andFromRGB(A)
methods. You can't do this in theColor
class in MAUI. There's no constructor that takes ints, and noFromRGB(A)
method that takes ints. Instead, the the nearestFromRGB
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)
withint
arguments: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 theint
values being passed toFromRGB
- all good. What the hell's going on? Then you discover that there's noFromRGB
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 aFromRGB
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.The text was updated successfully, but these errors were encountered: