From b5abbac576da3edd41c5ead33e333ae1942e21ef Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Sat, 30 Nov 2024 15:31:07 -0800 Subject: [PATCH 1/5] fix issue 158 Shading is computed wrong. * added unit tests --- .../PixelBufferImplementation/Pixel.cs | 8 +++-- src/Tests/Consolonia.Core.Tests/PixelTests.cs | 34 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs index 87086b91..750f61b5 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs @@ -125,9 +125,13 @@ public Pixel Blend(Pixel pixelAbove) newBackground = Background; break; case PixelBackgroundMode.Shaded: +#if DEBUG + if (pixelAbove.Foreground.Symbol.Text != " ") + throw new ArgumentOutOfRangeException(nameof(pixelAbove), "Someone is attempting to shade a pixel with a non-blank symbol and the behavior of that is not defined"); +#endif (newForeground, newBackground) = Shade(); - newForeground = newForeground.Blend(pixelAbove.Foreground); - break; + return new Pixel(newForeground, newBackground); + default: throw new ArgumentOutOfRangeException(nameof(pixelAbove)); } diff --git a/src/Tests/Consolonia.Core.Tests/PixelTests.cs b/src/Tests/Consolonia.Core.Tests/PixelTests.cs index b2d19f13..d59d0b9c 100644 --- a/src/Tests/Consolonia.Core.Tests/PixelTests.cs +++ b/src/Tests/Consolonia.Core.Tests/PixelTests.cs @@ -142,6 +142,40 @@ public void BlendColoredBackground() Assert.That(newPixel.Background.Color, Is.EqualTo(Colors.Blue)); } + [Test] + public void BlendShadedBackground() + { + var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), new PixelBackground(Colors.White)); + var pixel2 = new Pixel(new PixelBackground(PixelBackgroundMode.Shaded)); + Pixel newPixel = pixel.Blend(pixel2); + Assert.True(newPixel.Foreground.Symbol.Text == "x"); + // foreground should be lighter than original + Assert.True(newPixel.Foreground.Color.R < pixel.Foreground.Color.R && + newPixel.Foreground.Color.G < pixel.Foreground.Color.G && + newPixel.Foreground.Color.B < pixel.Foreground.Color.B); + // background should be darker than original + Assert.True(newPixel.Background.Color.R < pixel.Background.Color.R && + newPixel.Background.Color.G < pixel.Background.Color.G && + newPixel.Background.Color.B < pixel.Background.Color.B); + } + + [Test] + public void BlendShadedBackground2() + { + var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), new PixelBackground(Colors.Black)); + var pixel2 = new Pixel(new PixelBackground(PixelBackgroundMode.Shaded)); + Pixel newPixel = pixel.Blend(pixel2); + Assert.True(newPixel.Foreground.Symbol.Text == "x"); + // foreground should be darker than original + Assert.True(newPixel.Foreground.Color.R < pixel.Foreground.Color.R && + newPixel.Foreground.Color.G < pixel.Foreground.Color.G && + newPixel.Foreground.Color.B < pixel.Foreground.Color.B); + // background should be darker than original + Assert.True(newPixel.Background.Color.R > pixel.Background.Color.R && + newPixel.Background.Color.G > pixel.Background.Color.G && + newPixel.Background.Color.B > pixel.Background.Color.B); + } + [Test] public void HashCode() { From 0d1c24e0aebf5aae0b3524a47affe62b0d86f357 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 30 Nov 2024 23:35:26 +0000 Subject: [PATCH 2/5] Automated JetBrains cleanup Co-authored-by: <+@users.noreply.github.com> --- .../Drawing/PixelBufferImplementation/Pixel.cs | 3 ++- src/Tests/Consolonia.Core.Tests/PixelTests.cs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs index 750f61b5..29fd9e20 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs @@ -127,7 +127,8 @@ public Pixel Blend(Pixel pixelAbove) case PixelBackgroundMode.Shaded: #if DEBUG if (pixelAbove.Foreground.Symbol.Text != " ") - throw new ArgumentOutOfRangeException(nameof(pixelAbove), "Someone is attempting to shade a pixel with a non-blank symbol and the behavior of that is not defined"); + throw new ArgumentOutOfRangeException(nameof(pixelAbove), + "Someone is attempting to shade a pixel with a non-blank symbol and the behavior of that is not defined"); #endif (newForeground, newBackground) = Shade(); return new Pixel(newForeground, newBackground); diff --git a/src/Tests/Consolonia.Core.Tests/PixelTests.cs b/src/Tests/Consolonia.Core.Tests/PixelTests.cs index d59d0b9c..4e0b3664 100644 --- a/src/Tests/Consolonia.Core.Tests/PixelTests.cs +++ b/src/Tests/Consolonia.Core.Tests/PixelTests.cs @@ -145,7 +145,8 @@ public void BlendColoredBackground() [Test] public void BlendShadedBackground() { - var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), new PixelBackground(Colors.White)); + var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), + new PixelBackground(Colors.White)); var pixel2 = new Pixel(new PixelBackground(PixelBackgroundMode.Shaded)); Pixel newPixel = pixel.Blend(pixel2); Assert.True(newPixel.Foreground.Symbol.Text == "x"); @@ -162,7 +163,8 @@ public void BlendShadedBackground() [Test] public void BlendShadedBackground2() { - var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), new PixelBackground(Colors.Black)); + var pixel = new Pixel(new PixelForeground(new SimpleSymbol("x"), Colors.Gray), + new PixelBackground(Colors.Black)); var pixel2 = new Pixel(new PixelBackground(PixelBackgroundMode.Shaded)); Pixel newPixel = pixel.Blend(pixel2); Assert.True(newPixel.Foreground.Symbol.Text == "x"); From f523d008825c7eab7586f3c346a5068ffe1fb340 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Wed, 4 Dec 2024 12:10:58 -0800 Subject: [PATCH 3/5] * Changed Shaded pixel blending with to dim the original pixel then blend the foreground in. If Foreground is transparent then no foreground blending is done. * Changed Foreground.Weight and Style to be nullable so we can better know if there is a new value to blend when blending foreground * Updated interface signature and serialization instructions. --- .../PixelBufferImplementation/Pixel.cs | 11 ++-- .../PixelForeground.cs | 35 +++++++------ src/Consolonia.Core/Drawing/RenderTarget.cs | 52 +++++++++---------- src/Consolonia.Core/Dummy/DummyConsole.cs | 4 +- .../Infrastructure/IConsole.cs | 4 +- .../InputLessDefaultNetConsole.cs | 4 +- src/Consolonia.Designer/ConsolePreview.cs | 8 +-- src/Consolonia.NUnit/UnitTestConsole.cs | 4 +- .../PixelForegroundTests.cs | 32 ++++++------ src/Tests/Consolonia.Core.Tests/PixelTests.cs | 6 +-- 10 files changed, 83 insertions(+), 77 deletions(-) diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs index 29fd9e20..ccd9b227 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs @@ -125,12 +125,13 @@ public Pixel Blend(Pixel pixelAbove) newBackground = Background; break; case PixelBackgroundMode.Shaded: -#if DEBUG - if (pixelAbove.Foreground.Symbol.Text != " ") - throw new ArgumentOutOfRangeException(nameof(pixelAbove), - "Someone is attempting to shade a pixel with a non-blank symbol and the behavior of that is not defined"); -#endif + // shade the current pixel (newForeground, newBackground) = Shade(); + + // blend the pixelAbove foreground into the shaded pixel + newForeground = newForeground.Blend(pixelAbove.Foreground); + + // resulting in new pixel with shaded background and blended foreground return new Pixel(newForeground, newBackground); default: throw new ArgumentOutOfRangeException(nameof(pixelAbove)); diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs index ebc6ad3e..d487e11c 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs @@ -14,13 +14,13 @@ public PixelForeground() { Symbol = new SimpleSymbol(" "); Color = Colors.Transparent; - Weight = FontWeight.Normal; - Style = FontStyle.Normal; + Weight = null; + Style = null; TextDecoration = null; } public PixelForeground(ISymbol symbol, Color color, - FontWeight weight = FontWeight.Normal, FontStyle style = FontStyle.Normal, + FontWeight? weight = null, FontStyle? style = null, TextDecorationLocation? textDecoration = null) { ArgumentNullException.ThrowIfNull(symbol); @@ -36,15 +36,13 @@ public PixelForeground(ISymbol symbol, Color color, [JsonConverter(typeof(ColorConverter))] public Color Color { get; init; } - [DefaultValue(FontWeight.Normal)] - [JsonProperty(DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] - public FontWeight Weight { get; init; } + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public FontWeight? Weight { get; init; } - [DefaultValue(FontStyle.Normal)] - [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] - public FontStyle Style { get; init; } + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + public FontStyle? Style { get; init; } - [JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)] + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] public TextDecorationLocation? TextDecoration { get; init; } public bool Equals(PixelForeground other) @@ -68,10 +66,17 @@ public PixelForeground Blend(PixelForeground pixelAboveForeground) ISymbol symbolAbove = pixelAboveForeground.Symbol; ArgumentNullException.ThrowIfNull(symbolAbove); - ISymbol newSymbol = Symbol.Blend(ref symbolAbove); - - return new PixelForeground(newSymbol, pixelAboveForeground.Color, pixelAboveForeground.Weight, - pixelAboveForeground.Style, pixelAboveForeground.TextDecoration); + if (pixelAboveForeground.Color == Colors.Transparent) + { + // if pixelAbove is transparent then the foreground below should be unchanged. + return this; + } + + return new PixelForeground(Symbol.Blend(ref symbolAbove), + pixelAboveForeground.Color, + pixelAboveForeground.Weight ?? Weight, + pixelAboveForeground.Style ?? Style, + pixelAboveForeground.TextDecoration ?? TextDecoration); } public override bool Equals([NotNullWhen(true)] object obj) @@ -81,7 +86,7 @@ public override bool Equals([NotNullWhen(true)] object obj) public override int GetHashCode() { - return HashCode.Combine(Symbol, Color, (int)Weight, (int)Style, TextDecoration); + return HashCode.Combine(Symbol, Color, (int)(Weight ?? FontWeight.Normal), (int)(Style ?? FontStyle.Normal), TextDecoration); } public static bool operator ==(PixelForeground left, PixelForeground right) diff --git a/src/Consolonia.Core/Drawing/RenderTarget.cs b/src/Consolonia.Core/Drawing/RenderTarget.cs index 40d56366..88763b07 100644 --- a/src/Consolonia.Core/Drawing/RenderTarget.cs +++ b/src/Consolonia.Core/Drawing/RenderTarget.cs @@ -92,8 +92,8 @@ private void OnResized(Size size, WindowResizeReason reason) // initalize the cache with Pixel.Empty as it literally means nothing for (ushort y = 0; y < height; y++) - for (ushort x = 0; x < width; x++) - cache[x, y] = Pixel.Empty; + for (ushort x = 0; x < width; x++) + cache[x, y] = Pixel.Empty; return cache; } @@ -107,34 +107,34 @@ private void RenderToDevice() var flushingBuffer = new FlushingBuffer(_console); for (ushort y = 0; y < pixelBuffer.Height; y++) - for (ushort x = 0; x < pixelBuffer.Width;) - { - Pixel pixel = pixelBuffer[(PixelBufferCoordinate)(x, y)]; - - if (pixel.IsCaret) + for (ushort x = 0; x < pixelBuffer.Width;) { - if (caretPosition != null) - throw new InvalidOperationException("Caret is already shown"); - caretPosition = new PixelBufferCoordinate(x, y); - } + Pixel pixel = pixelBuffer[(PixelBufferCoordinate)(x, y)]; - /* todo: There is not IWindowImpl.Invalidate anymore. - if (!_consoleWindow.InvalidatedRects.Any(rect => - rect.ContainsExclusive(new Point(x, y)))) continue;*/ + if (pixel.IsCaret) + { + if (caretPosition != null) + throw new InvalidOperationException("Caret is already shown"); + caretPosition = new PixelBufferCoordinate(x, y); + } - //todo: indexOutOfRange during resize - if (_cache[x, y] == pixel) - { - x++; - continue; - } + /* todo: There is not IWindowImpl.Invalidate anymore. + if (!_consoleWindow.InvalidatedRects.Any(rect => + rect.ContainsExclusive(new Point(x, y)))) continue;*/ - _cache[x, y] = pixel; + //todo: indexOutOfRange during resize + if (_cache[x, y] == pixel) + { + x++; + continue; + } - flushingBuffer.WritePixel(new PixelBufferCoordinate(x, y), pixel); + _cache[x, y] = pixel; - x++; - } + flushingBuffer.WritePixel(new PixelBufferCoordinate(x, y), pixel); + + x++; + } flushingBuffer.Flush(); @@ -156,8 +156,8 @@ private struct FlushingBuffer private readonly StringBuilder _stringBuilder; private Color _lastBackgroundColor; private Color _lastForegroundColor; - private FontStyle _lastStyle = FontStyle.Normal; - private FontWeight _lastWeight = FontWeight.Normal; + private FontStyle? _lastStyle; + private FontWeight? _lastWeight; private TextDecorationLocation? _lastTextDecoration; private PixelBufferCoordinate _currentBufferPoint; private PixelBufferCoordinate _lastBufferPointStart; diff --git a/src/Consolonia.Core/Dummy/DummyConsole.cs b/src/Consolonia.Core/Dummy/DummyConsole.cs index ba552142..b5ad80d3 100644 --- a/src/Consolonia.Core/Dummy/DummyConsole.cs +++ b/src/Consolonia.Core/Dummy/DummyConsole.cs @@ -54,8 +54,8 @@ public void PauseIO(Task task) { } - public void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle style, - FontWeight weight, TextDecorationLocation? textDecoration, string str) + public void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle? style, + FontWeight? weight, TextDecorationLocation? textDecoration, string str) { } diff --git a/src/Consolonia.Core/Infrastructure/IConsole.cs b/src/Consolonia.Core/Infrastructure/IConsole.cs index fa7ccf92..fb49195a 100644 --- a/src/Consolonia.Core/Infrastructure/IConsole.cs +++ b/src/Consolonia.Core/Infrastructure/IConsole.cs @@ -26,8 +26,8 @@ public interface IConsole : IDisposable void SetCaretPosition(PixelBufferCoordinate bufferPoint); PixelBufferCoordinate GetCaretPosition(); - void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle style, - FontWeight weight, TextDecorationLocation? textDecoration, string str); + void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle? style, + FontWeight? weight, TextDecorationLocation? textDecoration, string str); void WriteText(string str); diff --git a/src/Consolonia.Core/Infrastructure/InputLessDefaultNetConsole.cs b/src/Consolonia.Core/Infrastructure/InputLessDefaultNetConsole.cs index af58de54..fa3f9dfc 100644 --- a/src/Consolonia.Core/Infrastructure/InputLessDefaultNetConsole.cs +++ b/src/Consolonia.Core/Infrastructure/InputLessDefaultNetConsole.cs @@ -97,8 +97,8 @@ public PixelBufferCoordinate GetCaretPosition() return _headBufferPoint; } - public void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle style, - FontWeight weight, TextDecorationLocation? textDecoration, string str) + public void Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle? style, + FontWeight? weight, TextDecorationLocation? textDecoration, string str) { PauseTask?.Wait(); SetCaretPosition(bufferPoint); diff --git a/src/Consolonia.Designer/ConsolePreview.cs b/src/Consolonia.Designer/ConsolePreview.cs index e2cd21b6..92a40bb9 100644 --- a/src/Consolonia.Designer/ConsolePreview.cs +++ b/src/Consolonia.Designer/ConsolePreview.cs @@ -373,9 +373,9 @@ private class TextBlockComposer private readonly StringBuilder _textBuilder; private Color _lastBackgroundColor; private Color _lastForegroundColor; - private FontStyle _lastStyle = FontStyle.Normal; + private FontStyle? _lastStyle; private TextDecorationLocation? _lastTextDecorations; - private FontWeight _lastWeight = FontWeight.Normal; + private FontWeight? _lastWeight; private double _textRunCharWidth; public TextBlockComposer(StackPanel panel, double charWidth) @@ -433,8 +433,8 @@ public void Flush() Text = text, Foreground = new SolidColorBrush(_lastForegroundColor), Background = new SolidColorBrush(_lastBackgroundColor), - FontWeight = _lastWeight, - FontStyle = _lastStyle, + FontWeight = _lastWeight ?? FontWeight.Normal, + FontStyle = _lastStyle ?? FontStyle.Normal, FontSize = 17, TextDecorations = _lastTextDecorations switch { diff --git a/src/Consolonia.NUnit/UnitTestConsole.cs b/src/Consolonia.NUnit/UnitTestConsole.cs index 6fdad0cb..c42cd357 100644 --- a/src/Consolonia.NUnit/UnitTestConsole.cs +++ b/src/Consolonia.NUnit/UnitTestConsole.cs @@ -55,8 +55,8 @@ PixelBufferCoordinate IConsole.GetCaretPosition() return _fakeCaretPosition; } - void IConsole.Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle style, - FontWeight weight, TextDecorationLocation? textDecoration, string str) + void IConsole.Print(PixelBufferCoordinate bufferPoint, Color background, Color foreground, FontStyle? style, + FontWeight? weight, TextDecorationLocation? textDecoration, string str) { (ushort x, ushort y) = bufferPoint; diff --git a/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs b/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs index adf93160..52e1d1a8 100644 --- a/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs +++ b/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs @@ -17,9 +17,9 @@ public void Constructor() Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Transparent)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo(" ")); Assert.That(pixelForeground.Symbol.Width, Is.EqualTo(1)); - Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Normal)); - Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Normal)); - Assert.That(pixelForeground.TextDecoration, Is.Null); + Assert.IsNull(pixelForeground.Weight); + Assert.IsNull(pixelForeground.Style); + Assert.IsNull(pixelForeground.TextDecoration); } [Test] @@ -29,9 +29,9 @@ public void ConstructorWithSymbol() var pixelForeground = new PixelForeground(symbol, Colors.Red); Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Red)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo("a")); - Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Normal)); - Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Normal)); - Assert.That(pixelForeground.TextDecoration, Is.Null); + Assert.IsNull(pixelForeground.Weight); + Assert.IsNull(pixelForeground.Style); + Assert.IsNull(pixelForeground.TextDecoration); } [Test] @@ -42,9 +42,9 @@ public void ConstructorWithSymbolAndWeight() Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Red)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo("a")); Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Bold)); - Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Normal)); - Assert.That(pixelForeground.TextDecoration, Is.Null); - } + Assert.IsNull(pixelForeground.Style); + Assert.IsNull(pixelForeground.TextDecoration); + } [Test] public void ConstructorWithSymbolAndStyle() @@ -53,9 +53,9 @@ public void ConstructorWithSymbolAndStyle() var pixelForeground = new PixelForeground(symbol, Colors.Red, style: FontStyle.Italic); Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Red)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo("a")); - Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Normal)); + Assert.IsNull(pixelForeground.Weight); Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Italic)); - Assert.That(pixelForeground.TextDecoration, Is.Null); + Assert.IsNull(pixelForeground.TextDecoration); } [Test] @@ -66,8 +66,8 @@ public void ConstructorWithSymbolAndTextDecorations() var pixelForeground = new PixelForeground(symbol, Colors.Red, textDecoration: textDecoration); Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Red)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo("a")); - Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Normal)); - Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Normal)); + Assert.IsNull(pixelForeground.Weight); + Assert.IsNull(pixelForeground.Style); Assert.That(pixelForeground.TextDecoration, Is.EqualTo(TextDecorationLocation.Underline)); } @@ -79,9 +79,9 @@ public void ConstructorWithWideCharacter() var pixelForeground = new PixelForeground(symbol, Colors.Red); Assert.That(pixelForeground.Color, Is.EqualTo(Colors.Red)); Assert.That(pixelForeground.Symbol.Text, Is.EqualTo("🎵")); - Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Normal)); - Assert.That(pixelForeground.Style, Is.EqualTo(FontStyle.Normal)); - Assert.That(pixelForeground.TextDecoration, Is.Null); + Assert.IsNull(pixelForeground.Weight); + Assert.IsNull(pixelForeground.Style); + Assert.IsNull(pixelForeground.TextDecoration); } [Test] diff --git a/src/Tests/Consolonia.Core.Tests/PixelTests.cs b/src/Tests/Consolonia.Core.Tests/PixelTests.cs index 4e0b3664..eeae1b37 100644 --- a/src/Tests/Consolonia.Core.Tests/PixelTests.cs +++ b/src/Tests/Consolonia.Core.Tests/PixelTests.cs @@ -59,9 +59,9 @@ public void ConstructorDrawingBoxSymbolAndColor() new PixelBackground(Colors.Blue)); Assert.That(pixel.Foreground.Symbol.Text, Is.EqualTo("┼")); Assert.That(pixel.Foreground.Color, Is.EqualTo(Colors.Red)); - Assert.That(pixel.Foreground.Style, Is.EqualTo(FontStyle.Normal)); - Assert.That(pixel.Foreground.Weight, Is.EqualTo(FontWeight.Normal)); - Assert.That(pixel.Foreground.TextDecoration, Is.Null); + Assert.IsNull(pixel.Foreground.Style); + Assert.IsNull(pixel.Foreground.Weight); + Assert.IsNull(pixel.Foreground.TextDecoration); Assert.That(pixel.Background.Color, Is.EqualTo(Colors.Blue)); Assert.That(pixel.Background.Mode, Is.EqualTo(PixelBackgroundMode.Colored)); } From 1ec065fb1799d0fc232229aa9adc6d8f13875f59 Mon Sep 17 00:00:00 2001 From: Tom Laird-McConnell Date: Wed, 4 Dec 2024 12:14:45 -0800 Subject: [PATCH 4/5] using --- .../Drawing/PixelBufferImplementation/PixelForeground.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs index d487e11c..14c7158a 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs @@ -1,5 +1,4 @@ using System; -using System.ComponentModel; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using Avalonia.Media; From 316fed58ac69e49c4e887cbf138233e15d2bd5fb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 4 Dec 2024 20:18:30 +0000 Subject: [PATCH 5/5] Automated JetBrains cleanup Co-authored-by: <+@users.noreply.github.com> --- .../PixelBufferImplementation/Pixel.cs | 4 +- .../PixelForeground.cs | 11 ++--- src/Consolonia.Core/Drawing/RenderTarget.cs | 48 +++++++++---------- .../PixelForegroundTests.cs | 2 +- 4 files changed, 32 insertions(+), 33 deletions(-) diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs index ccd9b227..2f54c442 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs @@ -127,10 +127,10 @@ public Pixel Blend(Pixel pixelAbove) case PixelBackgroundMode.Shaded: // shade the current pixel (newForeground, newBackground) = Shade(); - + // blend the pixelAbove foreground into the shaded pixel newForeground = newForeground.Blend(pixelAbove.Foreground); - + // resulting in new pixel with shaded background and blended foreground return new Pixel(newForeground, newBackground); diff --git a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs index 14c7158a..92b85a70 100644 --- a/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs +++ b/src/Consolonia.Core/Drawing/PixelBufferImplementation/PixelForeground.cs @@ -66,15 +66,13 @@ public PixelForeground Blend(PixelForeground pixelAboveForeground) ArgumentNullException.ThrowIfNull(symbolAbove); if (pixelAboveForeground.Color == Colors.Transparent) - { // if pixelAbove is transparent then the foreground below should be unchanged. return this; - } - return new PixelForeground(Symbol.Blend(ref symbolAbove), - pixelAboveForeground.Color, + return new PixelForeground(Symbol.Blend(ref symbolAbove), + pixelAboveForeground.Color, pixelAboveForeground.Weight ?? Weight, - pixelAboveForeground.Style ?? Style, + pixelAboveForeground.Style ?? Style, pixelAboveForeground.TextDecoration ?? TextDecoration); } @@ -85,7 +83,8 @@ public override bool Equals([NotNullWhen(true)] object obj) public override int GetHashCode() { - return HashCode.Combine(Symbol, Color, (int)(Weight ?? FontWeight.Normal), (int)(Style ?? FontStyle.Normal), TextDecoration); + return HashCode.Combine(Symbol, Color, (int)(Weight ?? FontWeight.Normal), (int)(Style ?? FontStyle.Normal), + TextDecoration); } public static bool operator ==(PixelForeground left, PixelForeground right) diff --git a/src/Consolonia.Core/Drawing/RenderTarget.cs b/src/Consolonia.Core/Drawing/RenderTarget.cs index 88763b07..9d5b4555 100644 --- a/src/Consolonia.Core/Drawing/RenderTarget.cs +++ b/src/Consolonia.Core/Drawing/RenderTarget.cs @@ -92,8 +92,8 @@ private void OnResized(Size size, WindowResizeReason reason) // initalize the cache with Pixel.Empty as it literally means nothing for (ushort y = 0; y < height; y++) - for (ushort x = 0; x < width; x++) - cache[x, y] = Pixel.Empty; + for (ushort x = 0; x < width; x++) + cache[x, y] = Pixel.Empty; return cache; } @@ -107,34 +107,34 @@ private void RenderToDevice() var flushingBuffer = new FlushingBuffer(_console); for (ushort y = 0; y < pixelBuffer.Height; y++) - for (ushort x = 0; x < pixelBuffer.Width;) - { - Pixel pixel = pixelBuffer[(PixelBufferCoordinate)(x, y)]; + for (ushort x = 0; x < pixelBuffer.Width;) + { + Pixel pixel = pixelBuffer[(PixelBufferCoordinate)(x, y)]; - if (pixel.IsCaret) - { - if (caretPosition != null) - throw new InvalidOperationException("Caret is already shown"); - caretPosition = new PixelBufferCoordinate(x, y); - } + if (pixel.IsCaret) + { + if (caretPosition != null) + throw new InvalidOperationException("Caret is already shown"); + caretPosition = new PixelBufferCoordinate(x, y); + } - /* todo: There is not IWindowImpl.Invalidate anymore. - if (!_consoleWindow.InvalidatedRects.Any(rect => - rect.ContainsExclusive(new Point(x, y)))) continue;*/ + /* todo: There is not IWindowImpl.Invalidate anymore. + if (!_consoleWindow.InvalidatedRects.Any(rect => + rect.ContainsExclusive(new Point(x, y)))) continue;*/ - //todo: indexOutOfRange during resize - if (_cache[x, y] == pixel) - { - x++; - continue; - } + //todo: indexOutOfRange during resize + if (_cache[x, y] == pixel) + { + x++; + continue; + } - _cache[x, y] = pixel; + _cache[x, y] = pixel; - flushingBuffer.WritePixel(new PixelBufferCoordinate(x, y), pixel); + flushingBuffer.WritePixel(new PixelBufferCoordinate(x, y), pixel); - x++; - } + x++; + } flushingBuffer.Flush(); diff --git a/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs b/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs index 52e1d1a8..73ebefec 100644 --- a/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs +++ b/src/Tests/Consolonia.Core.Tests/PixelForegroundTests.cs @@ -44,7 +44,7 @@ public void ConstructorWithSymbolAndWeight() Assert.That(pixelForeground.Weight, Is.EqualTo(FontWeight.Bold)); Assert.IsNull(pixelForeground.Style); Assert.IsNull(pixelForeground.TextDecoration); - } + } [Test] public void ConstructorWithSymbolAndStyle()