From 332bfeab53ff5c9628137f7a2273df1004ac6637 Mon Sep 17 00:00:00 2001 From: Unknown6656 Date: Mon, 30 Sep 2024 19:51:59 +0200 Subject: [PATCH] added more color constructors --- appveyor.yml | 6 +-- pkgversion.txt | 2 +- src/AssemblyInfo.cs | 16 +++---- src/ConsoleColor.cs | 114 ++++++++++++++++++++++++++++++++++++++++++++ version.txt | 4 +- 5 files changed, 128 insertions(+), 14 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 666a803..3053a4c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,10 +1,10 @@ ################################################################ -# Auto-generated 2024-09-28 14:32:53.440 # +# Auto-generated 2024-09-30 19:51:16.740 # # ANY CHANGES TO THIS DOCUMENT WILL BE LOST UPON RE-GENERATION # ################################################################ # -# git commit: 623aee557cae9d55815d45bfda7053bb4b15e72d -version: 1.0.144.8816 +# git commit: 7fe3be69aaea53c3859c0b4c02c8f41bef8de116 +version: 1.0.145.8818 image: Visual Studio 2022 configuration: Release install: diff --git a/pkgversion.txt b/pkgversion.txt index 73bd76c..209ecae 100644 --- a/pkgversion.txt +++ b/pkgversion.txt @@ -1 +1 @@ -1.0.144.8816 \ No newline at end of file +1.0.145.8818 \ No newline at end of file diff --git a/src/AssemblyInfo.cs b/src/AssemblyInfo.cs index 7f04230..a60d9c4 100644 --- a/src/AssemblyInfo.cs +++ b/src/AssemblyInfo.cs @@ -1,15 +1,15 @@ ////////////////////////////////////////////////////////////////////////// -// Auto-generated 2024-09-28 14:32:53.440 // +// Auto-generated 2024-09-30 19:51:16.740 // // ANY CHANGES TO THIS DOCUMENT WILL BE LOST UPON RE-GENERATION // ////////////////////////////////////////////////////////////////////////// using System.Reflection; using System; -[assembly: AssemblyVersion("1.0.144.8816")] -[assembly: AssemblyFileVersion("1.0.144.8816")] -[assembly: AssemblyInformationalVersion("v.1.0.144.8816, commit: 623aee557cae9d55815d45bfda7053bb4b15e72d")] +[assembly: AssemblyVersion("1.0.145.8818")] +[assembly: AssemblyFileVersion("1.0.145.8818")] +[assembly: AssemblyInformationalVersion("v.1.0.145.8818, commit: 7fe3be69aaea53c3859c0b4c02c8f41bef8de116")] [assembly: AssemblyCompany("Unknown6656")] [assembly: AssemblyCopyright("Copyright © 2020 - 2024, Unknown6656")] [assembly: AssemblyProduct("Unknown6656.Console by Unknown6656")] @@ -37,11 +37,11 @@ public static class __module__ /// /// The library's current version. /// - public static Version? LibraryVersion { get; } = Version.Parse("1.0.144.8816"); + public static Version? LibraryVersion { get; } = Version.Parse("1.0.145.8818"); /// /// The Git hash associated with the current build. /// - public const string GitHash = "623aee557cae9d55815d45bfda7053bb4b15e72d"; + public const string GitHash = "7fe3be69aaea53c3859c0b4c02c8f41bef8de116"; /// /// The name of the GitHub repository associated with . /// @@ -51,7 +51,7 @@ public static class __module__ /// public const string RepositoryURL = "https://github.com/Unknown6656-Megacorp/Unknown6656.Console"; /// - /// The date and time of the current build (2024-09-28 14:32:53.440). + /// The date and time of the current build (2024-09-30 19:51:16.740). /// - public static DateTime DateBuilt { get; } = DateTime.FromFileTimeUtc(0x01db11a289f3ef64L); + public static DateTime DateBuilt { get; } = DateTime.FromFileTimeUtc(0x01db1361593bb034L); } diff --git a/src/ConsoleColor.cs b/src/ConsoleColor.cs index 9581598..6b54f72 100644 --- a/src/ConsoleColor.cs +++ b/src/ConsoleColor.cs @@ -432,6 +432,54 @@ public ConsoleColor(KnownColor color) /// The to initialize the with. public ConsoleColor(sysconsolecolor? color) => _color = color is sysconsolecolor cc ? new Union.Case0(cc) : null; + /// + /// Initializes a new instance of the struct with the specified gray value. + /// This value is clamped to the range of [0..1]. + /// + /// The gray value in the range of [0..1] to initialize the with. + public ConsoleColor(double gray) + : this(gray, gray, gray) + { + } + + /// + /// Initializes a new instance of the struct with the specified RGB values. + /// Each value is clamped to the range of [0..1]. + /// + /// The red component value in the range of [0..1]. + /// The green component value in the range of [0..1]. + /// The blue component value in the range of [0..1]. + public ConsoleColor(double R, double G, double B) + : this( + (byte)Math.Round(double.Clamp(R, 0, 1) * 255), + (byte)Math.Round(double.Clamp(G, 0, 1) * 255), + (byte)Math.Round(double.Clamp(B, 0, 1) * 255) + ) + { + } + + /// + /// Initializes a new instance of the struct with the specified gray value. + /// This value is expected to be inside the range of [0..255]. + /// + /// The gray value to initialize the with. + public ConsoleColor(byte gray) + : this(gray, gray, gray) + { + } + + /// + /// Initializes a new instance of the struct with the specified RGB values. + /// Each value is expected to be inside the range of [0..255]. + /// + /// The red component value. + /// The green component value. + /// The blue component value. + public ConsoleColor(byte R, byte G, byte B) + : this(Color.FromArgb(R, G, B)) + { + } + /// public override int GetHashCode() { @@ -537,6 +585,28 @@ public string GetVT520SGRCode(ColorMode mode) #pragma warning restore CS8509 } + /// + /// Parses a VT100/VT500/VT520/ANSI escape sequence string to a instance. + /// + /// The VT100/VT500/VT520/ANSI escape sequence string to be parsed as a color. + /// Returns the instance that represents the specified given color string (or if the parsing was unsuccessful). + /// Returns if the parsing was successful; otherwise, . + public static bool TryParse(string vt520_color, out ConsoleColor color) + { + try + { + color = FromVT520(vt520_color); + + return true; + } + catch + { + color = Default; + + return false; + } + } + /// /// Converts a VT100/VT500/VT520/ANSI escape sequence string to a instance. /// @@ -663,4 +733,48 @@ static ConsoleColor parse_rgb(string color) /// /// The to convert. public static implicit operator ConsoleColor(sysconsolecolor? color) => FromConsoleColor(color); + + /// + /// Converts a VT520 color string to a instance. + /// + /// The VT520 color string. + public static explicit operator ConsoleColor(string vt520_color) => FromVT520(vt520_color); + + /// + /// Converts an RGB tuple to a instance. + /// + /// The RGB tuple. + public static explicit operator ConsoleColor((byte r, byte g, byte b) rgb) => new(rgb.r, rgb.g, rgb.b); + + /// + /// Converts an RGB tuple to a instance. + /// + /// The RGB tuple. + public static explicit operator ConsoleColor((int r, int g, int b) rgb) => new((byte)rgb.r, (byte)rgb.g, (byte)rgb.b); + + /// + /// Converts an RGB tuple to a instance. + /// + /// The RGB tuple. + public static explicit operator ConsoleColor((float r, float g, float b) rgb) => new(rgb.r, rgb.g, rgb.b); + + /// + /// Converts an RGB tuple to a instance. + /// + /// The RGB tuple. + public static explicit operator ConsoleColor((double r, double g, double b) rgb) => new(rgb.r, rgb.g, rgb.b); + + /// + /// Converts a instance to a nullable instance. + /// + /// The instance. + public static explicit operator sysconsolecolor?(ConsoleColor color) => color.ToSystemColor(); + + /// + /// Converts a instance to a instance. + /// + /// The instance. + /// Thrown when the conversion is not possible. + public static explicit operator sysconsolecolor(ConsoleColor color) => + color.ToSystemColor() ?? throw new ArgumentException($"Unable to convert '{color}' to a valid instance of {typeof(sysconsolecolor)}.", nameof(color)); } diff --git a/version.txt b/version.txt index 4bf0a5c..5875fd1 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ -1.0.144.8816 -623aee557cae9d55815d45bfda7053bb4b15e72d \ No newline at end of file +1.0.145.8818 +7fe3be69aaea53c3859c0b4c02c8f41bef8de116 \ No newline at end of file