Skip to content

Commit

Permalink
fix issue 158 Shading is computed wrong.
Browse files Browse the repository at this point in the history
* added unit tests
  • Loading branch information
tomlm committed Nov 30, 2024
1 parent bfae13f commit b5abbac
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
34 changes: 34 additions & 0 deletions src/Tests/Consolonia.Core.Tests/PixelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit b5abbac

Please sign in to comment.