Skip to content

Commit

Permalink
add - doc - Added the implicit operator support for Color
Browse files Browse the repository at this point in the history
We've added the implicit operator support for Color

---

You don't need to write `new Color()` everytime you want to make an instance of Color; you can directly write a string or a number. However, if you need to use the RGB color numbers, we don't support implicit operators for this kind.

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Aug 6, 2023
1 parent a2d2934 commit b493b6b
Show file tree
Hide file tree
Showing 2 changed files with 218 additions and 0 deletions.
186 changes: 186 additions & 0 deletions Terminaux.Tests/Colors/ColorInitializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ public void TestInitializeColorInstanceFrom255Colors()
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from 255 colors using an implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFrom255ColorsImplicit()
{
// Create instance
Color ColorInstance = 18;

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("18");
ColorInstance.Type.ShouldBe(ColorType._255Color);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;5;18m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;5;18m");
ColorInstance.R.ShouldBe(0);
ColorInstance.G.ShouldBe(0);
ColorInstance.B.ShouldBe(135);
ColorInstance.IsBright.ShouldBeTrue();
ColorInstance.IsDark.ShouldBeFalse();
ColorInstance.Hex.ShouldBe("#000087");
ColorInstance.ColorEnum255.ShouldBe(ConsoleColors.DarkBlue_000087);
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from 255 colors (Protanopia)
/// </summary>
Expand Down Expand Up @@ -538,6 +569,37 @@ public void TestInitializeColorInstanceFromTrueColor()
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from true color using the implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFromTrueColorImplicit()
{
// Create instance
Color ColorInstance = "94;0;63";

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("94;0;63");
ColorInstance.Type.ShouldBe(ColorType.TrueColor);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;2;94;0;63m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;2;94;0;63m");
ColorInstance.R.ShouldBe(94);
ColorInstance.G.ShouldBe(0);
ColorInstance.B.ShouldBe(63);
ColorInstance.IsBright.ShouldBeTrue();
ColorInstance.IsDark.ShouldBeFalse();
ColorInstance.Hex.ShouldBe("#5E003F");
ColorInstance.ColorEnum255.ShouldBe((ConsoleColors)(-1));
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from true color numbers
/// </summary>
Expand Down Expand Up @@ -600,6 +662,37 @@ public void TestInitializeColorInstanceFromHex()
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from true color using the implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFromHexImplicit()
{
// Create instance
Color ColorInstance = "#0F0F0F";

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("15;15;15");
ColorInstance.Type.ShouldBe(ColorType.TrueColor);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;2;15;15;15m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;2;15;15;15m");
ColorInstance.R.ShouldBe(15);
ColorInstance.G.ShouldBe(15);
ColorInstance.B.ShouldBe(15);
ColorInstance.IsBright.ShouldBeFalse();
ColorInstance.IsDark.ShouldBeTrue();
ColorInstance.Hex.ShouldBe("#0F0F0F");
ColorInstance.ColorEnum255.ShouldBe((ConsoleColors)(-1));
ColorInstance.ColorEnum16.ShouldBe((ConsoleColor)(-1));
}

/// <summary>
/// Tests initializing color instance from color name taken from <see cref="ConsoleColors"/>
/// </summary>
Expand Down Expand Up @@ -631,6 +724,37 @@ public void TestInitializeColorInstanceFromName()
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests initializing color instance from color name taken from <see cref="ConsoleColors"/> using an implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFromNameImplicit()
{
// Create instance
Color ColorInstance = "Magenta";

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("13");
ColorInstance.Type.ShouldBe(ColorType._16Color);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;5;13m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;5;13m");
ColorInstance.R.ShouldBe(255);
ColorInstance.G.ShouldBe(0);
ColorInstance.B.ShouldBe(255);
ColorInstance.IsBright.ShouldBeTrue();
ColorInstance.IsDark.ShouldBeFalse();
ColorInstance.Hex.ShouldBe("#FF00FF");
ColorInstance.ColorEnum255.ShouldBe((ConsoleColors)(-1));
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests initializing color instance from enum
/// </summary>
Expand Down Expand Up @@ -662,6 +786,37 @@ public void TestInitializeColorInstanceFromEnum()
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests initializing color instance from enum using an implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFromEnumImplicit()
{
// Create instance
var ColorInstance = new Color(ConsoleColors.Magenta);

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("13");
ColorInstance.Type.ShouldBe(ColorType._16Color);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;5;13m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;5;13m");
ColorInstance.R.ShouldBe(255);
ColorInstance.G.ShouldBe(0);
ColorInstance.B.ShouldBe(255);
ColorInstance.IsBright.ShouldBeTrue();
ColorInstance.IsDark.ShouldBeFalse();
ColorInstance.Hex.ShouldBe("#FF00FF");
ColorInstance.ColorEnum255.ShouldBe((ConsoleColors)(-1));
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests initializing color instance from enum (16 colors)
/// </summary>
Expand Down Expand Up @@ -693,6 +848,37 @@ public void TestInitializeColorInstanceFromEnum16()
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests initializing color instance from enum (16 colors) using an implicit operator
/// </summary>
[Test]
[Description("Initialization")]
public void TestInitializeColorInstanceFromEnum16Implicit()
{
// Create instance
Color ColorInstance = ConsoleColor.Magenta;

// Check for null
ColorInstance.ShouldNotBeNull();
ColorInstance.PlainSequence.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceBackground.ShouldNotBeNullOrEmpty();
ColorInstance.VTSequenceForeground.ShouldNotBeNullOrEmpty();

// Check for property correctness
ColorInstance.PlainSequence.ShouldBe("13");
ColorInstance.Type.ShouldBe(ColorType._16Color);
ColorInstance.VTSequenceBackground.ShouldBe(Color255.GetEsc() + "[48;5;13m");
ColorInstance.VTSequenceForeground.ShouldBe(Color255.GetEsc() + "[38;5;13m");
ColorInstance.R.ShouldBe(255);
ColorInstance.G.ShouldBe(0);
ColorInstance.B.ShouldBe(255);
ColorInstance.IsBright.ShouldBeTrue();
ColorInstance.IsDark.ShouldBeFalse();
ColorInstance.Hex.ShouldBe("#FF00FF");
ColorInstance.ColorEnum255.ShouldBe((ConsoleColors)(-1));
ColorInstance.ColorEnum16.ShouldBe(ConsoleColor.Magenta);
}

/// <summary>
/// Tests getting empty color
/// </summary>
Expand Down
32 changes: 32 additions & 0 deletions Terminaux/Colors/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,38 @@ public Color(string ColorSpecifier)
Hex = $"#{R:X2}{G:X2}{B:X2}";
}

/// <summary>
/// Makes a new instance of color class from specifier.
/// </summary>
/// <param name="ColorDef">The color taken from <see cref="ConsoleColors"/></param>
/// <exception cref="ColorSeqException"></exception>
public static implicit operator Color(ConsoleColors ColorDef) =>
new(Convert.ToInt32(ColorDef));

/// <summary>
/// Makes a new instance of color class from specifier.
/// </summary>
/// <param name="ColorDef">The color taken from <see cref="ConsoleColor"/></param>
/// <exception cref="ColorSeqException"></exception>
public static implicit operator Color(ConsoleColor ColorDef) =>
new(Convert.ToInt32(ColorTools.CorrectStandardColor(ColorDef)));

/// <summary>
/// Makes a new instance of color class from specifier.
/// </summary>
/// <param name="ColorNum">The color number</param>
/// <exception cref="ColorSeqException"></exception>
public static implicit operator Color(int ColorNum) =>
new($"{ColorNum}");

/// <summary>
/// Makes a new instance of color class from specifier.
/// </summary>
/// <param name="ColorSpecifier">A color specifier. It must be a valid number from 0-255 if using 255-colors, a VT sequence if using true color as follows: &lt;R&gt;;&lt;G&gt;;&lt;B&gt;, or a hexadecimal representation of a number (#AABBCC for example)</param>
/// <exception cref="ColorSeqException"></exception>
public static implicit operator Color(string ColorSpecifier) =>
new(ColorSpecifier);

/// <inheritdoc/>
public override bool Equals(object obj) =>
base.Equals(obj);
Expand Down

0 comments on commit b493b6b

Please sign in to comment.