Skip to content

Add blittable color types Argb<T> and Rgba<T> to System.Numerics.Vectors#124663

Merged
ViveliDuCh merged 11 commits intodotnet:mainfrom
ViveliDuCh:blittable-color-pr
Mar 2, 2026
Merged

Add blittable color types Argb<T> and Rgba<T> to System.Numerics.Vectors#124663
ViveliDuCh merged 11 commits intodotnet:mainfrom
ViveliDuCh:blittable-color-pr

Conversation

@ViveliDuCh
Copy link
Member

@ViveliDuCh ViveliDuCh commented Feb 20, 2026

Closes #48615

This PR builds on @AlexRadch's original implementation in #106575 and addresses the review feedback to avoid introducing a new assembly. The Argb and Rgba color types are placed in System.Private.CoreLib alongside the other System.Numerics types (Vector2, Matrix4x4, Quaternion, etc.), with System.Numerics.Vectors remaining a pure facade assembly that forwards all types from CoreLib.

Original work by @AlexRadch:

  • Argb<T>, Rgba<T> struct implementations with static helper methods
  • System.Drawing.Primitives integration (Color.FromArgb/ToArgbValue)
  • System.ComponentModel.TypeConverter support
  • Test coverage (324 tests)

Addressing review feedback:

  • Move all Color source files into System.Private.CoreLib (src/System/Numerics/Colors/) following the same pattern as existing Numerics types.
  • Add ThrowHelper methods and string resources to CoreLib.
  • Keep System.Numerics.Vectors as a pure facade (no ContractTypesPartiallyMoved, no compiled source)
  • Remove 6 external references to the deleted System.Numerics.Colors project (NetCoreAppLibrary.props, netstandard shim, Drawing.Primitives, TypeConverter, crossgen2_comparison.py)
  • Fix csproj conventions (<ProjectReference> over bare <Reference>, remove redundant properties)

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces new blittable color primitives (Argb<T> and Rgba<T>) under System.Numerics.Colors, implemented directly in the System.Numerics.Vectors partial-facade assembly, and wires them into System.Drawing.Color for efficient interop-friendly conversions.

Changes:

  • Add System.Numerics.Colors.Argb<T> / Rgba<T> implementations plus uint endianness helpers.
  • Add System.Drawing.Color interop APIs (FromArgb(Argb<byte>), ToArgbValue(), and cast operators) and expand test coverage.
  • Update System.Numerics.Vectors ref surface and add new unit tests for the new types.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 29 comments.

Show a summary per file
File Description
src/libraries/System.Numerics.Vectors/src/System.Numerics.Vectors.csproj Enables partial contract move + compiles new Colors types into the facade assembly.
src/libraries/System.Numerics.Vectors/src/System/ThrowHelper.cs Adds argument-validation helper for span-based color APIs.
src/libraries/System.Numerics.Vectors/src/System/Numerics/Colors/Argb.cs Adds uint conversion helpers for Argb<byte>.
src/libraries/System.Numerics.Vectors/src/System/Numerics/Colors/Argb.T.cs Adds generic Argb<T> blittable struct implementation.
src/libraries/System.Numerics.Vectors/src/System/Numerics/Colors/Rgba.cs Adds uint conversion helpers for Rgba<byte>.
src/libraries/System.Numerics.Vectors/src/System/Numerics/Colors/Rgba.T.cs Adds generic Rgba<T> blittable struct implementation.
src/libraries/System.Numerics.Vectors/src/Resources/Strings.resx Introduces resource string for span-length validation errors.
src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.csproj Includes the new System.Numerics.Colors ref file in the ref assembly build.
src/libraries/System.Numerics.Vectors/ref/System.Numerics.Colors.cs Declares the public ref surface for System.Numerics.Colors types.
src/libraries/System.Numerics.Vectors/tests/System.Numerics.Vectors.Tests.csproj Adds the new Colors test files to the test project build.
src/libraries/System.Numerics.Vectors/tests/TestHelpers.cs Adds shared test data + numeric helpers for the color tests.
src/libraries/System.Numerics.Vectors/tests/Argb.Tests.cs Adds unit tests for Argb static endianness helpers.
src/libraries/System.Numerics.Vectors/tests/Argb.T.Tests.cs Adds unit tests for Argb<T> struct behaviors.
src/libraries/System.Numerics.Vectors/tests/Rgba.Tests.cs Adds unit tests for Rgba static endianness helpers.
src/libraries/System.Numerics.Vectors/tests/Rgba.T.Tests.cs Adds unit tests for Rgba<T> struct behaviors.
src/libraries/System.Drawing.Primitives/src/System/Drawing/Color.cs Adds conversion APIs between Color and Argb<byte>.
src/libraries/System.Drawing.Primitives/ref/System.Drawing.Primitives.cs Updates the ref surface for new Color APIs.
src/libraries/System.Drawing.Primitives/tests/ColorTests.cs Adds tests for the new Color <-> Argb<byte> APIs.
src/libraries/System.Drawing.Primitives/tests/ColorTranslatorTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/PointTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/PointFTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/RectangleTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/RectangleFTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/SizeTests.cs Normalizes test namespace naming.
src/libraries/System.Drawing.Primitives/tests/SizeFTests.cs Normalizes test namespace naming.
src/libraries/System.ComponentModel.TypeConverter/src/System.ComponentModel.TypeConverter.csproj Adds a project reference to System.Numerics.Vectors.

Copilot AI review requested due to automatic review settings February 23, 2026 22:37
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated 7 comments.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think adding this to System.Numetics.Vectors is a good plan

@jkotas
Copy link
Member

jkotas commented Feb 26, 2026

My preference would be to add these types to System.Drawing.Common.

If it is not acceptable, we should add them to System.Private.Corelib. It is slightly better perf-wise than adding them to System.Numerics.Vectors. We have number of other graphics related types in CoreLib for historic reasons (and we cannot fix that since they got a cycle with intrinsics now), adding a few more would be consistent with that at least.

@akoeplinger
Copy link
Member

Reposting since the review comment might be hidden but the agreement from the discussion in #124663 (comment) was to move the implementation to Corelib.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.

@ViveliDuCh
Copy link
Member Author

@jkotas, @tannergooding, @ericstj, @akoeplinger, @JeremyKuhne Thanks everyone for the thorough discussion/debate on where these types should live. I really appreciate the different perspectives. Per the consensus reached, I've moved the Color type implementations into System.Private.CoreLib alongside the other System.Numerics types, with System.Numerics.Vectors remaining a pure facade.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

Copilot AI review requested due to automatic review settings February 28, 2026 20:42
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 25 out of 25 changed files in this pull request and generated no new comments.

@ViveliDuCh
Copy link
Member Author

/ba-g Opened issue #125037 for the unknown test error

@ViveliDuCh ViveliDuCh merged commit fdd418a into dotnet:main Mar 2, 2026
145 of 152 checks passed
@ViveliDuCh ViveliDuCh deleted the blittable-color-pr branch March 2, 2026 23:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API Proposal: Add blittable Color to System.Numerics

8 participants