Skip to content

Commit

Permalink
Fixes #3170. Color improvements (#3204)
Browse files Browse the repository at this point in the history
* Reduces indentation by remove the namespace braces.

* Reduces indentation and removes unused using.

* Ensures clear selection if it isn't selecting.

* Turn on nullability context for TryParse and update usages as needed.

* Use IsNullOrWhiteSpace, which includes IsNullOrEmpty

All-whitespace values are also illegal, so may as well handle that here too

* Respect the nullable here

* It's a struct

* Use byte.MaxValue and add remark

* Just use the bytes directly

* Must respect endianness

* Add uint constructor so consumers don't have to do unchecked math

* Completely re-work parsing and implement ISpanParsable<Color>

All parsing is now almost-0 allocation, and is significantly faster than before

* Extension methods required by new code

* Use standard Math.Clamp method here

* Add some new unit tests for TryParse

* De-duplicate code and handle more cases

* Enable nullability context for the file

* Go ahead and enable the language features and analysis

* Implicit usings remove a lot of boilerplate usings.

* Add these to the dictionary to shut spell check up

* Make this thing a record struct and a union, and update constructors

This commit won't build. I'm just breaking out changes a little bit.

* Some additional XmlDoc standardizing

* Make FindClosestColor and CalculateColorDistance use the vector for SIMD

* Add a TryFormat method for support of I*Formattable

* Add an interface for support of custom formatting of Colors

* Pass by in reference

* Parse string delegates to Parse span

* Parse now does all the work

* Remove the old new code from TryParse

* Some new cast operators

* Add IFormattable.ToString implementation

* Add the rest of the code for Color in its current (unfinished) state

* Move that interface to its own file

* Add ColorParseException class

* Move Attribute to its own file, too.

* Re-implement these operators as explicit methods

* Get rid of fuzzy equality operators and update tests to use the named methods that replace them

* Add an explicit test case for ToString with null format string and explicitly specified Invariant culture

* Fix byte orders for hex format to be standard ARGB

* Prove that ToString and Parse can round-trip values

* Unroll this test into parameterized cases

* Fix a couple of comments to match byte order

* Update R# dictionary to match correct byte orders

* Remove stray comment

* Separate all types in this to their own files

* Convert this one to use the handy extension

* Add test for Argb property

* Add a file nesting rule to make some incoming file changes display nicely

* Move constructor tests to their own file and add some new tests

* Add implicit cast from uint

* More constructor tests

* Since this is now a record, the equality operators are compiler generated

Still spot-checking a few arbitrary values for completeness

* Override ToString to delegate to Attribute

* Simplify and clean up ToString. Delegate to ColorScheme

* Update the test to match new output

* These should be fields, really. It's a value type.

* Add some type checks for change control

* Allow unsafe and turn on implicit usings

* Make this one better

* Rename tests and remove redundant checks (the type checks already guarantee field consistency)

* Reorganize a bit

* Make these test 16 random values

* Existing operator tests converted

* That one is now redundant with both of the other tests that check all the named colors

* Move this to type checks and simplify a little bit

* These lambdas can be static

* Move operators to another file.

* Add global using for System.Text because it's EVERYWHERE

* Reorganization of Color and some related types.

Updated usages to reflect changes

* Update tests to reflect changes in Terminal.Gui

* Add missing keyword

* Add entry to dictionary

* Add dotsettings for Terminal.Gui

Only specifies language level

* Commit unsaved changes for usings here

* Implement last remaining TryFormat method

* A little cleanup/formality

* Sorting rules

Sort methods by name and interface they implement

* Sort code

* Match namespace for tests

None!

* Unroll ordinal check and reorganize.

* Sort before writing new tests

* These got reversed...

* Add test to prove explicit cast to Vector3 works properly

* Sort these too

* Add test for uint->Color implicit operator

* Add test for Color->uint implicit operator

* Correct this test name and re-order

* Add test for implicit conversion from Vector3 to Color

* Add test for implicit conversion from Vector4 to Color

* These also got reversed, like with Vector3

* Add test for implicit conversion from Color to Vector4

* Add test for GetHashCode

* Make sure these are all under the same namespace

* Remove a now-redundant test

* Reorganize formatting and parsing tests to another type part

* Tests moved back to Terminal.Gui.DrawingTests namespace as before.

* Add tests for the constructor taking 3 or 4 integers and sort

* Cleanup

 - Renamed some tests
 - Make a test even clearer
 - Removed redundant code
 - Got rid of unused parameter in Constructor_WithColorName_AllChannelsCorrect

* That needs to be from the reverse map

Not broken - just was sub-optimal due to my error.

* Enable nullability context in this file

Not sure how it got removed but whatev

* Respect nullability context in this file now that it's on 🤦‍♂️

* Add tests for expected exceptions with whitespace or null values

* Add test for parameterless constructor

* A couple more places for reference passing and some SkipLocalsInit attributes.

* Some XmlDoc corrections to reflect the final implementation

* Remove namespace qualifier

* Can't use these because of lambdas :(

* Removed a collection that never ended up being needed.

* Add bracing, newline, and modifier style rules

* Add spacing rules inside parens/brackets

* This was still under the Terminal.Gui.Drawing namespace. Revert that.

* Applied updated formatting settings and addressed XmlDoc reviews in #3204

* More places where spaces got added in dependent code.

Also a couple of null checks fixed to not use the equality operator

* More dependent code format fixes

* Finished re-formatting modified code that got spaces added everywhere

* Visual studio didn't actually write this file to disk til I closed out of VS...

Grr

* Delete the ReSharper settings files from this branch.

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
  • Loading branch information
dodexahedron and BDisp authored Jan 24, 2024
1 parent ad234aa commit 92a6012
Show file tree
Hide file tree
Showing 40 changed files with 2,055 additions and 1,313 deletions.
8 changes: 3 additions & 5 deletions Terminal.Gui/Configuration/ColorJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Text.Json.Serialization;
using System.Text.Json;
using System.Text.RegularExpressions;

namespace Terminal.Gui {
/// <summary>
Expand All @@ -28,14 +26,14 @@ public override Color Read (ref Utf8JsonReader reader, Type typeToConvert, JsonS
// Check if the value is a string
if (reader.TokenType == JsonTokenType.String) {
// Get the color string
var colorString = reader.GetString ();
ReadOnlySpan<char> colorString = reader.GetString ();

// Check if the color string is a color name
if (Enum.TryParse (colorString, ignoreCase: true, out ColorName color)) {
// Return the parsed color
return new Color(color);
return new Color(in color);
}
if (Color.TryParse (colorString, out Color parsedColor)) {
if (Color.TryParse(colorString,null, out Color parsedColor)) {
return parsedColor;
}
throw new JsonException ($"Unexpected color name: {colorString}.");
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/CursesDriver/CursesDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static Attribute MakeColor (short foreground, short background)
public override Attribute MakeColor (Color foreground, Color background)
{
if (!RunningUnitTests) {
return MakeColor (ColorNameToCursesColorNumber (foreground.ColorName), ColorNameToCursesColorNumber (background.ColorName));
return MakeColor (ColorNameToCursesColorNumber (foreground.GetClosestNamedColor ()), ColorNameToCursesColorNumber (background.GetClosestNamedColor ()));
} else {
return new Attribute (
0,
Expand Down
4 changes: 2 additions & 2 deletions Terminal.Gui/ConsoleDrivers/FakeDriver/FakeDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ public override void UpdateScreen ()
// Performance: Only send the escape sequence if the attribute has changed.
if (attr != redrawAttr) {
redrawAttr = attr;
FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.ColorName;
FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.ColorName;
FakeConsole.ForegroundColor = (ConsoleColor)attr.Foreground.GetClosestNamedColor ();
FakeConsole.BackgroundColor = (ConsoleColor)attr.Background.GetClosestNamedColor ();
}
outputWidth++;
var rune = (Rune)Contents [row, col].Rune;
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/NetDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@ public override void UpdateScreen ()

if (Force16Colors) {
output.Append (EscSeqUtils.CSI_SetGraphicsRendition (
MapColors ((ConsoleColor)attr.Background.ColorName, false), MapColors ((ConsoleColor)attr.Foreground.ColorName, true)));
MapColors ((ConsoleColor)attr.Background.GetClosestNamedColor (), false), MapColors ((ConsoleColor)attr.Foreground.GetClosestNamedColor ())));
} else {
output.Append (EscSeqUtils.CSI_SetForegroundColorRGB (attr.Foreground.R, attr.Foreground.G, attr.Foreground.B));
output.Append (EscSeqUtils.CSI_SetBackgroundColorRGB (attr.Background.R, attr.Background.G, attr.Background.B));
Expand Down
2 changes: 1 addition & 1 deletion Terminal.Gui/ConsoleDrivers/WindowsDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public bool WriteToConsole (Size size, ExtendedCharInfo [] charInfoBuffer, Coord
foreach (ExtendedCharInfo info in charInfoBuffer) {
ci [i++] = new CharInfo () {
Char = new CharUnion () { UnicodeChar = info.Char },
Attributes = (ushort)(((int)info.Attribute.Foreground.ColorName) | ((int)info.Attribute.Background.ColorName << 4))
Attributes = (ushort)((int)info.Attribute.Foreground.GetClosestNamedColor () | (int)info.Attribute.Background.GetClosestNamedColor () << 4)
};
}

Expand Down
87 changes: 87 additions & 0 deletions Terminal.Gui/Drawing/AnsiColorCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
namespace Terminal.Gui;

/// <summary>
/// The 16 foreground color codes used by ANSI Esc sequences for 256 color terminals. Add 10 to these values for background
/// color.
/// </summary>
public enum AnsiColorCode {
/// <summary>
/// The ANSI color code for Black.
/// </summary>
BLACK = 30,

/// <summary>
/// The ANSI color code for Red.
/// </summary>
RED = 31,

/// <summary>
/// The ANSI color code for Green.
/// </summary>
GREEN = 32,

/// <summary>
/// The ANSI color code for Yellow.
/// </summary>
YELLOW = 33,

/// <summary>
/// The ANSI color code for Blue.
/// </summary>
BLUE = 34,

/// <summary>
/// The ANSI color code for Magenta.
/// </summary>
MAGENTA = 35,

/// <summary>
/// The ANSI color code for Cyan.
/// </summary>
CYAN = 36,

/// <summary>
/// The ANSI color code for White.
/// </summary>
WHITE = 37,

/// <summary>
/// The ANSI color code for Bright Black.
/// </summary>
BRIGHT_BLACK = 90,

/// <summary>
/// The ANSI color code for Bright Red.
/// </summary>
BRIGHT_RED = 91,

/// <summary>
/// The ANSI color code for Bright Green.
/// </summary>
BRIGHT_GREEN = 92,

/// <summary>
/// The ANSI color code for Bright Yellow.
/// </summary>
BRIGHT_YELLOW = 93,

/// <summary>
/// The ANSI color code for Bright Blue.
/// </summary>
BRIGHT_BLUE = 94,

/// <summary>
/// The ANSI color code for Bright Magenta.
/// </summary>
BRIGHT_MAGENTA = 95,

/// <summary>
/// The ANSI color code for Bright Cyan.
/// </summary>
BRIGHT_CYAN = 96,

/// <summary>
/// The ANSI color code for Bright White.
/// </summary>
BRIGHT_WHITE = 97
}
171 changes: 171 additions & 0 deletions Terminal.Gui/Drawing/Attribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
#nullable enable
using System.Text.Json.Serialization;
namespace Terminal.Gui;

/// <summary>
/// Attributes represent how text is styled when displayed in the terminal.
/// </summary>
/// <remarks>
/// <see cref="Attribute"/> provides a platform independent representation of colors (and someday other forms of text
/// styling).
/// They encode both the foreground and the background color and are used in the <see cref="ColorScheme"/>
/// class to define color schemes that can be used in an application.
/// </remarks>
[JsonConverter (typeof (AttributeJsonConverter))]
public readonly struct Attribute : IEquatable<Attribute> {
/// <summary>
/// Default empty attribute.
/// </summary>
public static readonly Attribute Default = new (Color.White, ColorName.Black);

/// <summary>
/// The <see cref="ConsoleDriver"/>-specific color value.
/// </summary>
[JsonIgnore (Condition = JsonIgnoreCondition.Always)]
internal int PlatformColor { get; }

/// <summary>
/// The foreground color.
/// </summary>
[JsonConverter (typeof (ColorJsonConverter))]
public Color Foreground { get; }

/// <summary>
/// The background color.
/// </summary>
[JsonConverter (typeof (ColorJsonConverter))]
public Color Background { get; }

/// <summary>
/// Initializes a new instance with default values.
/// </summary>
public Attribute ()
{
PlatformColor = -1;
Foreground = Default.Foreground;
Background = Default.Background;
}

/// <summary>
/// Initializes a new instance from an existing instance.
/// </summary>
public Attribute (in Attribute attr)
{
PlatformColor = -1;
Foreground = attr.Foreground;
Background = attr.Background;
}

/// <summary>
/// Initializes a new instance with platform specific color value.
/// </summary>
/// <param name="platformColor">Value.</param>
internal Attribute (int platformColor)
{
PlatformColor = platformColor;
Foreground = Default.Foreground;
Background = Default.Background;
}

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="platformColor">platform-dependent color value.</param>
/// <param name="foreground">Foreground</param>
/// <param name="background">Background</param>
internal Attribute (int platformColor, in Color foreground, in Color background)
{
Foreground = foreground;
Background = background;
PlatformColor = platformColor;
}

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="foreground">Foreground</param>
/// <param name="background">Background</param>
public Attribute (Color foreground, Color background)
{
Foreground = foreground;
Background = background;

// TODO: Once CursesDriver supports truecolor all the PlatformColor stuff goes away
if (Application.Driver == null) {
PlatformColor = -1;
return;
}

var make = Application.Driver.MakeColor (foreground, background);
PlatformColor = make.PlatformColor;
}

/// <summary>
/// Initializes a new instance with a <see cref="ColorName"/> value. Both <see cref="Foreground"/> and
/// <see cref="Background"/> will be set to the specified color.
/// </summary>
/// <param name="colorName">Value.</param>
internal Attribute (ColorName colorName) : this (colorName, colorName) { }

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="foregroundName">Foreground</param>
/// <param name="backgroundName">Background</param>
public Attribute (in ColorName foregroundName, in ColorName backgroundName) : this (new Color (foregroundName), new Color (backgroundName)) { }


/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="foregroundName">Foreground</param>
/// <param name="background">Background</param>
public Attribute (ColorName foregroundName, Color background) : this (new Color (foregroundName), background) { }

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct.
/// </summary>
/// <param name="foreground">Foreground</param>
/// <param name="backgroundName">Background</param>
public Attribute (Color foreground, ColorName backgroundName) : this (foreground, new Color (backgroundName)) { }

/// <summary>
/// Initializes a new instance of the <see cref="Attribute"/> struct
/// with the same colors for the foreground and background.
/// </summary>
/// <param name="color">The color.</param>
public Attribute (Color color) : this (color, color) { }


/// <summary>
/// Compares two attributes for equality.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator == (Attribute left, Attribute right) => left.Equals (right);

/// <summary>
/// Compares two attributes for inequality.
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <returns></returns>
public static bool operator != (Attribute left, Attribute right) => !(left == right);

/// <inheritdoc/>
public override bool Equals (object? obj) => obj is Attribute other && Equals (other);

/// <inheritdoc/>
public bool Equals (Attribute other) => PlatformColor == other.PlatformColor &&
Foreground == other.Foreground &&
Background == other.Background;

/// <inheritdoc/>
public override int GetHashCode () => HashCode.Combine (PlatformColor, Foreground, Background);

/// <inheritdoc/>
public override string ToString () =>
// Note: Unit tests are dependent on this format
$"[{Foreground},{Background}]";
}
Loading

0 comments on commit 92a6012

Please sign in to comment.