From 73f74fb66a64ee8ec8564fe79b8977861d342b15 Mon Sep 17 00:00:00 2001 From: Meee Date: Wed, 15 Feb 2023 08:59:33 +0700 Subject: [PATCH] Add function ToHtmlCssColorCode Modify from Jacob feedback - Change ToHtml to ToHtmlCssColorCode for autocomplete - Set Binary to null for GC to manage it --- .../UnitTests/ColorFunctionality.cs | 19 +++++++++++++++++++ .../IronSoftware.Drawing.Common/AnyBitmap.cs | 1 + .../IronSoftware.Drawing.Common/Color.cs | 9 +++++++++ NuGet/IronSoftware.Drawing.nuspec | 8 ++------ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/ColorFunctionality.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/ColorFunctionality.cs index 4e44a01..c979a44 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/ColorFunctionality.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common.Tests/UnitTests/ColorFunctionality.cs @@ -598,6 +598,25 @@ public void From32BitArgb_Should_Equal_Drawing(string colorCode) Assert.Equal(drawingColor.B, ironColor.B); } + [Theory] + [InlineData(255, 177, 177, 177, "#B1B1B1")] + [InlineData(255, 0, 0, 0, "#000000")] + [InlineData(255, 255, 0, 0, "#FF0000")] + [InlineData(255, 0, 255, 0, "#00FF00")] + [InlineData(255, 0, 0, 255, "#0000FF")] + [InlineData(255, 30, 129, 176, "#1E81B0")] + public void ToHtml_ShouldReturnCorrectHtmlString(int a, int r, int g, int b, string expectedHtml) + { + // Arrange + var color = new Color(a, r, g, b); + + // Act + var actualHtml = color.ToHtmlCssColorCode(); + + // Assert + Assert.Equal(expectedHtml, actualHtml); + } + #if !NETFRAMEWORK [FactWithAutomaticDisplayName] public void Cast_Maui_from_Color() diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs index 0bdab9d..7a3099f 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common/AnyBitmap.cs @@ -1207,6 +1207,7 @@ public void Dispose() return; this.Image.Dispose(); this.Image = null; + this.Binary = null; this.disposed = true; } diff --git a/IronSoftware.Drawing/IronSoftware.Drawing.Common/Color.cs b/IronSoftware.Drawing/IronSoftware.Drawing.Common/Color.cs index 9264aa8..3288783 100644 --- a/IronSoftware.Drawing/IronSoftware.Drawing.Common/Color.cs +++ b/IronSoftware.Drawing/IronSoftware.Drawing.Common/Color.cs @@ -1251,6 +1251,15 @@ public override int GetHashCode() return 0; } + /// + /// Translates the specified Color structure to an HTML string color representation. + /// + /// A string containing the hex representation of the color in the format #RRGGBB. + public string ToHtmlCssColorCode() + { + return $"#{this.R:X2}{this.G:X2}{this.B:X2}"; + } + #region Private Method private static InvalidOperationException NoConverterException(string color, Exception innerException) diff --git a/NuGet/IronSoftware.Drawing.nuspec b/NuGet/IronSoftware.Drawing.nuspec index 03e055e..13d6c3c 100644 --- a/NuGet/IronSoftware.Drawing.nuspec +++ b/NuGet/IronSoftware.Drawing.nuspec @@ -36,12 +36,8 @@ Supports: For general support and technical inquiries, please email us at: developers@ironsoftware.com IronSoftware.System.Drawing is an open-source solution for .NET developers to replace System.Drawing.Common with a universal and flexible library. - * AnyBitmap - - GetPixel(x, y) which is a new accessor method to get the Color of a pixel in an AnyBitmap - - Adds new constructor to AnyBitmap which creates a new AnyBitmap from an existing AnyBitmap, scaled to the a custom width and height - - Fixes a bug when saving an image without specifying ImageFormat -* Color - - [Hotfix] Fixes a bug when comparing Color with null + - Added a deconstructor for the AnyBitmap class to automatically dispose of objects, simplifying memory management. +- Added a new function call to the Color class: ToHtmlCssColorCode(). This translates the specified Color structure to an HTML string color representation. Copyright © Iron Software 2022-2023 Images, Bitmap, SkiaSharp, SixLabors, BitMiracle, Maui, SVG, TIFF, TIF, GIF, JPEG, PNG, Color, Rectangle, Drawing, C#, VB.NET, ASPX, create, render, generate, standard, netstandard2.0, core, netcore