Skip to content

Commit

Permalink
added more color constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknown6656 committed Sep 30, 2024
1 parent 7fe3be6 commit 332bfea
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 14 deletions.
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkgversion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.144.8816
1.0.145.8818
16 changes: 8 additions & 8 deletions src/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -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")]
Expand Down Expand Up @@ -37,11 +37,11 @@ public static class __module__
/// <summary>
/// The library's current version.
/// </summary>
public static Version? LibraryVersion { get; } = Version.Parse("1.0.144.8816");
public static Version? LibraryVersion { get; } = Version.Parse("1.0.145.8818");
/// <summary>
/// The Git hash associated with the current build.
/// </summary>
public const string GitHash = "623aee557cae9d55815d45bfda7053bb4b15e72d";
public const string GitHash = "7fe3be69aaea53c3859c0b4c02c8f41bef8de116";
/// <summary>
/// The name of the GitHub repository associated with <see cref="RepositoryURL"/>.
/// </summary>
Expand All @@ -51,7 +51,7 @@ public static class __module__
/// </summary>
public const string RepositoryURL = "https://github.com/Unknown6656-Megacorp/Unknown6656.Console";
/// <summary>
/// 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).
/// </summary>
public static DateTime DateBuilt { get; } = DateTime.FromFileTimeUtc(0x01db11a289f3ef64L);
public static DateTime DateBuilt { get; } = DateTime.FromFileTimeUtc(0x01db1361593bb034L);
}
114 changes: 114 additions & 0 deletions src/ConsoleColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,54 @@ public ConsoleColor(KnownColor color)
/// <param name="color">The <see cref="sysconsolecolor"/> to initialize the <see cref="ConsoleColor"/> with.</param>
public ConsoleColor(sysconsolecolor? color) => _color = color is sysconsolecolor cc ? new Union<sysconsolecolor, Color>.Case0(cc) : null;

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColor"/> struct with the specified gray value.
/// This value is clamped to the range of <c>[0..1]</c>.
/// </summary>
/// <param name="gray">The gray value in the range of <c>[0..1]</c> to initialize the <see cref="ConsoleColor"/> with.</param>
public ConsoleColor(double gray)
: this(gray, gray, gray)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColor"/> struct with the specified RGB values.
/// Each value is clamped to the range of <c>[0..1]</c>.
/// </summary>
/// <param name="R">The red component value in the range of <c>[0..1]</c>.</param>
/// <param name="G">The green component value in the range of <c>[0..1]</c>.</param>
/// <param name="B">The blue component value in the range of <c>[0..1]</c>.</param>
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)
)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColor"/> struct with the specified gray value.
/// This value is expected to be inside the range of <c>[0..255]</c>.
/// </summary>
/// <param name="gray">The gray value to initialize the <see cref="ConsoleColor"/> with.</param>
public ConsoleColor(byte gray)
: this(gray, gray, gray)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConsoleColor"/> struct with the specified RGB values.
/// Each value is expected to be inside the range of <c>[0..255]</c>.
/// </summary>
/// <param name="R">The red component value.</param>
/// <param name="G">The green component value.</param>
/// <param name="B">The blue component value.</param>
public ConsoleColor(byte R, byte G, byte B)
: this(Color.FromArgb(R, G, B))
{
}

/// <inheritdoc/>
public override int GetHashCode()
{
Expand Down Expand Up @@ -537,6 +585,28 @@ public string GetVT520SGRCode(ColorMode mode)
#pragma warning restore CS8509
}

/// <summary>
/// Parses a VT100/VT500/VT520/ANSI escape sequence string to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="vt520_color">The VT100/VT500/VT520/ANSI escape sequence string to be parsed as a color.</param>
/// <param name="color">Returns the <see cref="ConsoleColor"/> instance that represents the specified given color string (or <see cref="Default"/> if the parsing was unsuccessful).</param>
/// <returns>Returns <see langword="true"/> if the parsing was successful; otherwise, <see langword="false"/>.</returns>
public static bool TryParse(string vt520_color, out ConsoleColor color)
{
try
{
color = FromVT520(vt520_color);

return true;
}
catch
{
color = Default;

return false;
}
}

/// <summary>
/// Converts a VT100/VT500/VT520/ANSI escape sequence string to a <see cref="ConsoleColor"/> instance.
/// <para/>
Expand Down Expand Up @@ -663,4 +733,48 @@ static ConsoleColor parse_rgb(string color)
/// </summary>
/// <param name="color">The <see cref="sysconsolecolor"/> to convert.</param>
public static implicit operator ConsoleColor(sysconsolecolor? color) => FromConsoleColor(color);

/// <summary>
/// Converts a VT520 color string to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="vt520_color">The VT520 color string.</param>
public static explicit operator ConsoleColor(string vt520_color) => FromVT520(vt520_color);

/// <summary>
/// Converts an RGB tuple to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="rgb">The RGB tuple.</param>
public static explicit operator ConsoleColor((byte r, byte g, byte b) rgb) => new(rgb.r, rgb.g, rgb.b);

/// <summary>
/// Converts an RGB tuple to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="rgb">The RGB tuple.</param>
public static explicit operator ConsoleColor((int r, int g, int b) rgb) => new((byte)rgb.r, (byte)rgb.g, (byte)rgb.b);

/// <summary>
/// Converts an RGB tuple to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="rgb">The RGB tuple.</param>
public static explicit operator ConsoleColor((float r, float g, float b) rgb) => new(rgb.r, rgb.g, rgb.b);

/// <summary>
/// Converts an RGB tuple to a <see cref="ConsoleColor"/> instance.
/// </summary>
/// <param name="rgb">The RGB tuple.</param>
public static explicit operator ConsoleColor((double r, double g, double b) rgb) => new(rgb.r, rgb.g, rgb.b);

/// <summary>
/// Converts a <see cref="ConsoleColor"/> instance to a nullable <see cref="sysconsolecolor"/> instance.
/// </summary>
/// <param name="color">The <see cref="ConsoleColor"/> instance.</param>
public static explicit operator sysconsolecolor?(ConsoleColor color) => color.ToSystemColor();

/// <summary>
/// Converts a <see cref="ConsoleColor"/> instance to a <see cref="sysconsolecolor"/> instance.
/// </summary>
/// <param name="color">The <see cref="ConsoleColor"/> instance.</param>
/// <exception cref="ArgumentException">Thrown when the conversion is not possible.</exception>
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));
}
4 changes: 2 additions & 2 deletions version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
1.0.144.8816
623aee557cae9d55815d45bfda7053bb4b15e72d
1.0.145.8818
7fe3be69aaea53c3859c0b4c02c8f41bef8de116

0 comments on commit 332bfea

Please sign in to comment.