-
Notifications
You must be signed in to change notification settings - Fork 1
Add ColorExtensions for System.Drawing.Color #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System.Drawing; | ||
| using FluentAssertions; | ||
| using PowerKit.Extensions; | ||
| using Xunit; | ||
|
|
||
| namespace PowerKit.Tests.Extensions; | ||
|
|
||
| public class ColorExtensionsTests | ||
| { | ||
| [Fact] | ||
| public void ToHex_Test() | ||
| { | ||
| // Act & assert | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).ToHex().Should().Be("#123456"); | ||
| Color.FromArgb(128, 0xff, 0x00, 0x00).ToHex().Should().Be("#FF0000"); | ||
| Color.FromArgb(255, 0x00, 0x00, 0x00).ToHex().Should().Be("#000000"); | ||
| Color.FromArgb(255, 0xff, 0xff, 0xff).ToHex().Should().Be("#FFFFFF"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ToRgb_Test() | ||
| { | ||
| // Act & assert | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).ToRgb().Should().Be(0x123456); | ||
| Color.FromArgb(0, 0x12, 0x34, 0x56).ToRgb().Should().Be(0x123456); | ||
| Color.FromArgb(255, 0x00, 0x00, 0x00).ToRgb().Should().Be(0x000000); | ||
| Color.FromArgb(255, 0xff, 0xff, 0xff).ToRgb().Should().Be(0xffffff); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WithAlpha_Test() | ||
| { | ||
| // Act & assert | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).WithAlpha(128).A.Should().Be(128); | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).WithAlpha(128).R.Should().Be(0x12); | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).WithAlpha(128).G.Should().Be(0x34); | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).WithAlpha(128).B.Should().Be(0x56); | ||
| Color.FromArgb(0, 0xff, 0xff, 0xff).WithAlpha(255).A.Should().Be(255); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ResetAlpha_Test() | ||
| { | ||
| // Act & assert | ||
| Color.FromArgb(0, 0x12, 0x34, 0x56).ResetAlpha().A.Should().Be(255); | ||
| Color.FromArgb(128, 0x12, 0x34, 0x56).ResetAlpha().A.Should().Be(255); | ||
| Color.FromArgb(255, 0x12, 0x34, 0x56).ResetAlpha().A.Should().Be(255); | ||
| Color.FromArgb(0, 0x12, 0x34, 0x56).ResetAlpha().R.Should().Be(0x12); | ||
| Color.FromArgb(0, 0x12, 0x34, 0x56).ResetAlpha().G.Should().Be(0x34); | ||
| Color.FromArgb(0, 0x12, 0x34, 0x56).ResetAlpha().B.Should().Be(0x56); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| #nullable enable | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.Drawing; | ||
|
|
||
| namespace PowerKit.Extensions; | ||
|
|
||
| #if !POWERKIT_INCLUDE_COVERAGE | ||
| [ExcludeFromCodeCoverage] | ||
| #endif | ||
| internal static class ColorExtensions | ||
| { | ||
| extension(Color color) | ||
| { | ||
| /// <summary> | ||
| /// Returns the hexadecimal representation of the color (e.g., <c>#RRGGBB</c>). | ||
| /// </summary> | ||
| public string ToHex() => $"#{color.R:X2}{color.G:X2}{color.B:X2}"; | ||
|
|
||
| /// <summary> | ||
| /// Returns the RGB value of the color as a 32-bit integer (without the alpha channel). | ||
| /// </summary> | ||
| public int ToRgb() => color.ToArgb() & 0xffffff; | ||
|
|
||
| /// <summary> | ||
| /// Returns a new <see cref="Color" /> with the specified alpha value. | ||
| /// </summary> | ||
| public Color WithAlpha(int alpha) => Color.FromArgb(alpha, color); | ||
|
|
||
| /// <summary> | ||
| /// Returns a new <see cref="Color" /> with the alpha channel reset to fully opaque. | ||
| /// </summary> | ||
| public Color ResetAlpha() => color.WithAlpha(255); | ||
|
Tyrrrz marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — renamed to
ToHexString()in commit 2412ff7.