API Proposal: Add blittable Color to System.Numerics
Background and Motivation
System.Drawing.Color contains extra metadata that makes it unusable in interop scenarios and inefficient in arrays.
namespace System.Drawing
{
public readonly struct Color : IEquatable<Color>
{
private readonly string? name;
private readonly long value;
private readonly short knownColor;
private readonly short state;
}
}
Color is also mutable, despite being readonly. If it is constructed from a system KnownColor value, it always looks up the value on every access.
In order to facilitate exchange of raw color data throughout the .NET ecosystem and external libraries we should expose common color types that only contain raw color data.
The intent is to follow up with these base types to provide a basic set of the most common functionality needed around colors as described here: https://github.com/dotnet/runtime/issues/48615#issuecomment-883803660
The intent is NOT to start building a general purpose image manipulation library. Libraries such as ImageSharp are the right answer for this.
WinForms and System.Drawing will be able to leverage this for performance, which was the original driver for this request.
// Native Win32 definitions
// GDI
typedef DWORD COLORREF;
// GDI+
typedef DWORD ARGB;
Proposed API
using System;
namespace System.Drawing
{
partial struct Color : IEquatable<Color>
{
public static Color FromArgb(System.Numerics.Colors.Argb<byte> argb);
public static implicit operator Color(System.Numerics.Colors.Argb<byte> argb);
// ToNumericsArgb? ToArgbNumerics?
public System.Numerics.Colors.Argb<byte> ToArgbValue();
public static explicit operator System.Numerics.Colors.Argb<byte>(in Color color);
}
}
// WPF
namespace System.Windows.Media
{
public struct Color : IEquatable<Color>, IFormattable
{
public static Color FromArgb(System.Numerics.Colors.Argb<byte> argb);
public static implicit operator Color(System.Numerics.Colors.Argb<byte> argb);
public static Color FromArgb(System.Numerics.Colors.Argb<float> argb);
public static implicit operator Color(System.Numerics.Colors.Argb<float> argb);
public System.Numerics.Colors.Argb<byte> ToArgbValue();
public static explicit operator System.Numerics.Colors.Argb<byte>(in Color color);
public System.Numerics.Colors.Argb<float> ToArgbValue();
public static explicit operator System.Numerics.Colors.Argb<float>(in Color color);
}
}
namespace System.Numerics.Colors
{
public static class Argb
{
public static Argb<byte> CreateBigEndian(uint color);
public static Argb<byte> CreateLittleEndian(uint color);
public static uint ToUInt32LittleEndian(this Argb<byte> color);
public static uint ToUInt32BigEndian(this Argb<byte> color);
}
public readonly struct Argb<T> : IEquatable<Argb<T>>, IFormattable, ISpanFormattable where T : struct
{
public T A { get; }
public T R { get; }
public T G { get; }
public T B { get; }
public Argb(T a, T r, T g, T b);
public Argb(ReadOnlySpan<T> values);
public void CopyTo(Span<T> destination);
public bool Equals(Argb<T> other);
public string ToString(string format, IFormatProvider formatProvider);
// whatever ISpanFormattable says
public Rgba<T> ToRgba();
}
public static class Rgba
{
public static Rgba<byte> CreateLittleEndian(uint color);
public static Rgba<byte> CreateBigEndian(uint color);
public static uint ToUInt32LittleEndian(this Rgba<byte> color);
public static uint ToUInt32BigEndian(this Rgba<byte> color);
}
public readonly struct Rgba<T> : IEquatable<Rgba<T>>, IFormattable, ISpanFormattable where T : struct
{
public T R { get; }
public T G { get; }
public T B { get; }
public T A { get; }
public Rgba(T r, T g, T b, T a);
public Rgba(ReadOnlySpan<T> values);
public void CopyTo(Span<T> destination);
public bool Equals(Rgba<T> other);
public string ToString(string format, IFormatProvider formatProvider);
// whatever ISpanFormattable says
public Argb<T> ToArgb();
}
}
Original Proposal
public readonly struct ColorArgb
{
// Layout matches GDI COLORREF and GDI+ ARGB
public uint Value { get; }
public byte A => (byte)(Value >> 24);
public byte R => (byte)(Value >> 16);
public byte G => (byte)(Value >> 8);
public byte B => (byte)(Value);
public ColorArgb(uint value) => Value = value;
public ColorArgb(byte r, byte g, byte b) : this(r, g, b, byte.MaxValue) { }
public ColorArgb(byte r, byte g, byte b, byte a) => Value = (uint)(a << 24 | r << 16 | g << 8 | b);
public ColorArgb(KnownColor knownColor) => Value = KnownColorTable.KnownColorToArgb(knownColor);
public ColorArgb(Color color) => Value = (uint)color.ToArgb();
// Color has these 3 methods.
public float GetHue() => 0;
public float GetSaturation() => 0;
public float GetBrightness() => 0;
// Frequently checked as only GDI+ supports transparency.
public bool HasTransparency => A != byte.MaxValue;
public bool FullyTransparent => A == 0;
public static implicit operator Color(ColorArgb color) => Color.FromArgb((int)color.Value);
public static implicit operator ARGB(ColorArgb color) => (ARGB)color.Value;
public static implicit operator ColorArgb(ARGB argb) => new ColorArgb((uint)argb);
public static implicit operator ColorArgb(Color color) => new ColorArgb(color);
public static implicit operator ColorArgb(KnownColor knownColor) => new ColorArgb(knownColor);
}
// For ease of use in interop definitions (P/Invoke, function pointers, COM)
// (as close as we get to a typedef in C#)
public enum ARGB : uint { }
Alternative Designs
We could drop a System.Drawing specific color type in System.Drawing for this purpose (the original proposal). Discussing this with @pgovind and @tannergooding we feel there is value in defining more broadly available color types in System.Numerics.
Using Vector4 is possible for this purpose, but that makes data interchange and just general usage difficult. (Was this ARGB or RGBA, etc?) The intent is also to add additional methods in the future that are dependent on specific layout (such as conversion to HSL/HSV, etc.).
Risks
No known risks.
Tagging subscribers to this area: @safern, @tarekgh See info in area-owners.md if you want to be subscribed.
Issue Details
Background and Motivation
System.Drawing.Color contains extra metadata that makes it unusable in interop scenarios and inefficient in arrays.
namespace System.Drawing
{
public readonly struct Color : IEquatable<Color>
{
private readonly string? name; // Do not rename (binary serialization)
private readonly long value; // Do not rename (binary serialization)
private readonly short knownColor; // Do not rename (binary serialization)
private readonly short state; // Do not rename (binary serialization)
}
}
Color is also mutable, despite being readonly. If it is constructed from a system KnownColor value, it always looks up the value on every access.
In order to facilitate GDI and GDI+ interop scenarios and creation of more performant overloads for System.Drawing and Windows Forms, we should expose a type only contains the raw color data.
// Native definitions
// GDI
typedef DWORD COLORREF;
// GDI+
typedef DWORD ARGB;
Proposed API
public readonly struct ColorArgb
{
// Layout matches GDI COLORREF and GDI+ ARGB
public uint Value { get; }
public byte A => (byte)(Value >> 24);
public byte R => (byte)(Value >> 16);
public byte G => (byte)(Value >> 8);
public byte B => (byte)(Value);
public ColorArgb(uint value) => Value = value;
public ColorArgb(byte r, byte g, byte b) : this(r, g, b, byte.MaxValue) { }
public ColorArgb(byte r, byte g, byte b, byte a) => Value = (uint)(a << 24 | r << 16 | g << 8 | b);
public ColorArgb(KnownColor knownColor) => Value = KnownColorTable.KnownColorToArgb(knownColor);
public ColorArgb(Color color) => Value = (uint)color.ToArgb();
// Color has these 3 methods.
public float GetHue() => 0;
public float GetSaturation() => 0;
public float GetBrightness() => 0;
// Frequently checked as only GDI+ supports transparency.
public bool HasTransparency => A != byte.MaxValue;
public bool FullyTransparent => A == 0;
public static implicit operator Color(ColorArgb color) => Color.FromArgb((int)color.Value);
public static implicit operator ARGB(ColorArgb color) => (ARGB)color.Value;
public static implicit operator ColorArgb(ARGB argb) => new ColorArgb((uint)argb);
public static implicit operator ColorArgb(Color color) => new ColorArgb(color);
public static implicit operator ColorArgb(KnownColor knownColor) => new ColorArgb(knownColor);
}
// For ease of use in interop definitions (P/Invoke, function pointers, COM)
// (as close as we get to a typedef in C#)
public enum ARGB : uint { }
Alternative Designs
We could make a more generic set of color structs to handle all typical color usage scenarios, but that isn't the intent here. #32418 tracks such a request. This type is specifically to address the existing space that System.Drawing exists in, and as such lives in System.Drawing.
Risks
No known risks.
| Author: | JeremyKuhne |
|---|---|
| Assignees: | - |
| Labels: |
|
| Milestone: | - |
Wow what horrors. ColorArgb is clearly better for the reasons given. But then Color8 in the linked thread solves the same problem. So apart from the namespace it's the same proposal.
~~I dislike this proposal for a few reasons:~~
-
~~The name does not give any information about color depth, while
Color8does.~~ It's<T>now so that's very clear. -
~~I think we should be using and encouraging RGBA instead of ARGB. RGBA is the most used format, while ARGB is mostly a just thing in Microsoft products.~~ The proposal has both RGBA and ARGB now, but I still think that RGBA should be presented as the "default" option by naming it just
Color.
So apart from the namespace it's the same proposal.
@charlesroddie Yes, it is very close. This is the native color, and this is specifically for direct interop. Color8 is not the same format and I don't want to muddy that space with a layout that doesn't match the "standard".
@aaronfranke
The name does not give any information about color depth, while Color8 does.
True, and I think Color8 is a much better "new" color. This is explicitly for perf and direct interop scenarios.
I think we should be using and encouraging RGBA instead of ARGB.
I agree, but it doesn't address the specific interop need here.
I want to be super clear- this is not intended to be the "new" Color. This is meant to allow us to have a shared native definition for GDI and GDI+ scenarios, which is what System.Drawing is specifically written to. We can take multiple steps to push people to use the "new" Color types when they become available. Type naming, subnamespacing, hiding from intellisense, etc. are options we can lean on.
This is meant to allow us to have a shared native definition for GDI and GDI+ scenarios, which is what System.Drawing is specifically written to.
Sorry I didn't see the interop part. If it's a Windows-specific feature then I don't think it's a candidate for dotnet/runtime. Perhaps a WindowsGraphicsPrimitives library that WPF/WinForms can depend on? Or a GraphicsPrimitives library that does the same thing for a broader set of consumers?
If it's a Windows-specific feature then I don't think it's a candidate for dotnet/runtime.
Well, it is Windows specific in the sense that it is GDI/GDI+ specific, which is what System.Drawing is a projection/mapping of. So generally I agree with you, but this assembly is really a legacy Windows thing we're carrying and not the future of drawing/graphics support for .NET I think.
Given that this is needed by Winforms to improve perf and System.Drawing.Common could benefit from it, let's mark is as api-ready-for-review and discuss it in the review meeting.
If this is reviewed, then #32418 should also be reviewed. Since so many things need a Color type, it makes sense to include one (or two) in .NET for everyone to use, including Winforms and System.Drawing and Maui and much more.
In API Review we feel that this is just setting us up for an API bifurcation between Color and ColorArgb, and that there doesn't seem to be a compelling scenario for that bifurcation.
With a compelling scenario we could have a different discussion.
It seems like there may be scenarios, but we should reconcile the layering (above or below Color) and determine if we need this, or a companion, for MAUI.
Updated the proposal based on offline discussions with @pgovind and @tannergooding.
Tagging subscribers to this area: @tannergooding, @pgovind See info in area-owners.md if you want to be subscribed.
Issue Details
Background and Motivation
System.Drawing.Color contains extra metadata that makes it unusable in interop scenarios and inefficient in arrays.
namespace System.Drawing
{
public readonly struct Color : IEquatable<Color>
{
private readonly string? name; // Do not rename (binary serialization)
private readonly long value; // Do not rename (binary serialization)
private readonly short knownColor; // Do not rename (binary serialization)
private readonly short state; // Do not rename (binary serialization)
}
}
Color is also mutable, despite being readonly. If it is constructed from a system KnownColor value, it always looks up the value on every access.
In order to facilitate GDI and GDI+ interop scenarios and creation of more performant overloads for System.Drawing and Windows Forms, we should expose a type only contains the raw color data.
// Native definitions
// GDI
typedef DWORD COLORREF;
// GDI+
typedef DWORD ARGB;
Proposed API
using System;
namespace System.Drawing
{
public readonly struct Color : IEquatable<Color>
{
public static implicit operator Color(System.Numerics.Colors.Argb<byte> argb);
public static explicit operator System.Numerics.Colors.Argb<byte>(in Color color);
public static implicit operator Color(System.Numerics.Colors.Rgba<byte> rgba);
public static explicit operator System.Numerics.Colors.Rgba<byte>(in Color color);
}
}
namespace System.Numerics.Colors
{
public static class Argb
{
public static Argb<byte> CreateLittleEndian(uint color);
public static uint ToUInt32LittleEndian(this Argb<byte> color);
}
public readonly struct Argb<T> : IEquatable<Argb<T>>, IFormattable where T : struct
{
public T A { get; }
public T R { get; }
public T G { get; }
public T B { get; }
public Argb(T a, T r, T g, T b);
public Argb(ReadOnlySpan<T> values);
public readonly void CopyTo(Span<T> destination);
public bool Equals(Argb<T> other);
public string ToString(string format, IFormatProvider formatProvider);
}
public static class Rgba
{
public static Rgba<byte> CreateLittleEndian(uint color);
public static uint ToUInt32LittleEndian(this Rgba<byte> color);
}
public readonly struct Rgba<T> : IEquatable<Rgba<T>>, IFormattable where T : struct
{
public T R { get; }
public T G { get; }
public T B { get; }
public T A { get; }
public Rgba(T r, T g, T b, T a);
public Rgba(ReadOnlySpan<T> values);
public readonly void CopyTo(Span<T> destination);
public bool Equals(Rgba<T> other);
public string ToString(string format, IFormatProvider formatProvider);
}
}
Original Proposal
public ColorArgb(uint value) => Value = value;
public ColorArgb(byte r, byte g, byte b) : this(r, g, b, byte.MaxValue) { }
public ColorArgb(byte r, byte g, byte b, byte a) => Value = (uint)(a << 24 | r << 16 | g << 8 | b);
public ColorArgb(KnownColor knownColor) => Value = KnownColorTable.KnownColorToArgb(knownColor);
public ColorArgb(Color color) => Value = (uint)color.ToArgb();
// Color has these 3 methods.
public float GetHue() => 0;
public float GetSaturation() => 0;
public float GetBrightness() => 0;
// Frequently checked as only GDI+ supports transparency.
public bool HasTransparency => A != byte.MaxValue;
public bool FullyTransparent => A == 0;
public static implicit operator Color(ColorArgb color) => Color.FromArgb((int)color.Value);
public static implicit operator ARGB(ColorArgb color) => (ARGB)color.Value;
public static implicit operator ColorArgb(ARGB argb) => new ColorArgb((uint)argb);
public static implicit operator ColorArgb(Color color) => new ColorArgb(color);
public static implicit operator ColorArgb(KnownColor knownColor) => new ColorArgb(knownColor);
}
// For ease of use in interop definitions (P/Invoke, function pointers, COM)
// (as close as we get to a typedef in C#)
public enum ARGB : uint { }
</details>
## Alternative Designs
We could drop a System.Drawing specific color type in System.Drawing for this purpose (the original proposal). Discussing this with @pgovind and @tannergooding we feel there is value in defining more broadly available color types in System.Numerics.
Using Vector4 is _possible_ for this purpose, but that makes data interchange and just general usage difficult. (Was this ARGB or RGBA, etc?) The intent is also to add additional methods in the future that are dependent on specific layout (such as conversion to HSL/HSV, etc.).
## Risks
No known risks.
<table>
<tr>
<th align="left">Author:</th>
<td>JeremyKuhne</td>
</tr>
<tr>
<th align="left">Assignees:</th>
<td>-</td>
</tr>
<tr>
<th align="left">Labels:</th>
<td>
`api-needs-work`, `api-suggestion`, `area-System.Drawing`, `area-System.Numerics`
</td>
</tr>
<tr>
<th align="left">Milestone:</th>
<td>6.0.0</td>
</tr>
</table>
</details>
1 question I had here was: Is System.Numerics the right place to add Colors?
My perspective here is that Numerics is meant for more math related types. So if I wanted to add a type related to linear algebra, matrices, computational geometry etc, I'd add it to Numerics. Colors seems more graphics related (for ex: I can't think of a non-graphics scenario when I'd use Colors) and less math related to me, so I wonder if it'll fit better in a Graphics namespace (not sure we have one in the runtime libraries at the moment). Having said that, I don't really care for the namespace that much. With the new linker work, IMO it doesn't matter which namespace the type lives in. Ideally only the Colors type would remain after the linker passes and at that point it doesn't matter if it was System.Numerics.Colors or System.GraphicsNamespaceThatDoesntExistYet.Colors.
My perspective here is that most of the types in Numerics are already graphics related. Yes, you could squint and say they math/linear algebra related, but in practice they are designed and expose methods that are purposefully built around 2D and 3D graphics scenarios and are largely based on the types that originally shipped in XNA.
For additional clarity, it is not a goal for us to support all color formats or encodings, users have a range of options for that from ImageSharp to WIC. However, the baseline color support being discussed here fits in well with the currently exposed APIs and with what other SIMD oriented math libraries that are similar to our own (such as GLM, DirectX Math, Unity, etc) expose. It effectively covers the default "surface" formats of the native OS and will largely be oriented around byte and float (which are the respective "default" storage and operation formats for things like Maui, Wpf, WinForms, Drawing, and DX/OGL/VK).
@pgovind / @tannergooding I think this is ready for API review again. Do you agree?
@JeremyKuhne, what's the importance of getting this in for 6.0.0 vs changing it to 7.0.0 or future?
@tannergooding it is getting late for 6 so it isn't critical. If we don't have time now it would be good to get it taken care of early in 7 so we can actually build on it.
Thanks! Going to mark this as 7.0.0 so we hopefully get to it early in the triage process.
We lost quorum during the API Review meeting for this. There was concern that it's proposed as a primitive and there's not clear evidence that the interested parties have signed off that it's usable for them. So it'd be great to get that feedback from someone before approval.
Notes we took when discussing the proposal:
- The types should be ISpanFormattable
- The constraints on these types would ideally be
IScalar. If we can get the constraint more constrained that would be great. - They're IFormattable, should they be IParsable?
- The Argb and Rgba non-generic types should also have BigEndian versions of their routines.
- The System.Drawing.Color from
Argb<byte>should be an overload ofColor.FromArgb - Eliminate the RGBA interaction to System.Drawing.Color in favor of conversions from RGBA to ARGB
- Argb
.ToRgba , and Rgba .ToArgb . Don't need the Froms.
- Argb
namespace System.Drawing
{
partial struct Color : IEquatable<Color>
{
public static Color FromArgb(System.Numerics.Colors.Argb<byte> argb);
public static implicit operator Color(System.Numerics.Colors.Argb<byte> argb);
// ToNumericsArgb? ToArgbNumerics?
public System.Numerics.Colors.Argb<byte> ToArgbValue();
public static explicit operator System.Numerics.Colors.Argb<byte>(in Color color);
}
}
namespace System.Numerics.Colors
{
public static class Argb
{
public static Argb<byte> CreateBigEndian(uint color);
public static Argb<byte> CreateLittleEndian(uint color);
public static uint ToUInt32LittleEndian(this Argb<byte> color);
public static uint ToUInt32BigEndian(this Argb<byte> color);
}
public readonly struct Argb<T> : IEquatable<Argb<T>>, IFormattable, ISpanFormattable where T : struct
{
public T A { get; }
public T R { get; }
public T G { get; }
public T B { get; }
public Argb(T a, T r, T g, T b);
public Argb(ReadOnlySpan<T> values);
public void CopyTo(Span<T> destination);
public bool Equals(Argb<T> other);
public string ToString(string format, IFormatProvider formatProvider);
// whatever ISpanFormattable says
public Rgba<T> ToRgba();
}
public static class Rgba
{
public static Rgba<byte> CreateLittleEndian(uint color);
public static Rgba<byte> CreateBigEndian(uint color);
public static uint ToUInt32LittleEndian(this Rgba<byte> color);
public static uint ToUInt32BigEndian(this Rgba<byte> color);
}
public readonly struct Rgba<T> : IEquatable<Rgba<T>>, IFormattable, ISpanFormattable where T : struct
{
public T R { get; }
public T G { get; }
public T B { get; }
public T A { get; }
public Rgba(T r, T g, T b, T a);
public Rgba(ReadOnlySpan<T> values);
public void CopyTo(Span<T> destination);
public bool Equals(Rgba<T> other);
public string ToString(string format, IFormatProvider formatProvider);
// whatever ISpanFormattable says
public Argb<T> ToArgb();
}
}
Just popping by from Twitter, saw Numerics and couldn't resist...
Colour types (cue the "English English" link) are something we have needed in .NET for a while, because any image processing library just ends up whipping up their own.
However, the APIs here are quite thin and by the looks of things are just for SIMD acceleration - if this is the case then that's fine, only that there are currently no non-float Ts approved for the new generic vector types - I would not be surprised if performance freaks wield these types for general SIMD mathematics which is a shame as beyond the field order being intrinsically meaningful with these types, there aren't that many differences between these and the vectors.
This, in addition to there being no defined interactions between the various layout types, indicates that these are just identical storage types with different names and field orders.
What's my point? This just seems like a lot of not a lot, and doesn't really seem that useful as a standalone namespace that can be used outside of System.Drawing by other libraries; it seems to just exist as a vectorised storage type, the API set of which being increasingly fragmented.
I would rather a Vector4<byte>, which admittedly is a bit too general purpose and doesn't have the meaningful layout; and/or more APIs (like conversions between the colours) to make it more useful to anyone that isn't System.Drawing and warrant it not being in that namespace - possibly fit for another API proposal, sure.
I just don't think such thin APIs so tailored and specific to System.Drawing have much reason being outside of it.