Skip to content

Commit

Permalink
190-strange-coloring-remove-console-brush (#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
jinek authored Dec 25, 2024
1 parent 328be8f commit 2dd81ad
Show file tree
Hide file tree
Showing 28 changed files with 280 additions and 425 deletions.
2 changes: 1 addition & 1 deletion src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2677,7 +2677,7 @@ resharper_int_variable_overflow_in_unchecked_context_highlighting = warning
resharper_invalid_value_type_highlighting = warning
resharper_invalid_xml_doc_comment_highlighting = warning
resharper_invert_condition_1_highlighting = hint
resharper_invert_if_highlighting = hint
resharper_invert_if_highlighting = none
resharper_invocation_is_skipped_highlighting = hint
resharper_invoke_as_extension_method_highlighting = suggestion
resharper_is_expression_always_false_highlighting = warning
Expand Down
3 changes: 1 addition & 2 deletions src/Consolonia.Core/Controls/Dialog/DialogWrap.axaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:drawing1="clr-namespace:Consolonia.Core.Drawing;assembly=Consolonia.Core"
x:Class="Consolonia.Core.Controls.Dialog.DialogWrap">

<Panel>
<Border Background="{drawing1:ConsoleBrush Mode=Shaded}"
<Border Background="{DynamicResource ThemeShadeBrush}"
IsVisible="{Binding RelativeSource={RelativeSource Self}, Path=IsEffectivelyEnabled}" />

<!-- ReSharper disable once UnusedMember.Local Used for manual search-->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,96 +1,14 @@
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Media;
using Consolonia.Core.Drawing.PixelBufferImplementation;
using Consolonia.Core.Infrastructure;

namespace Consolonia.Core.Drawing
{
[DebuggerDisplay("{Color} [{Mode}]")]
public class ConsoleBrush : AvaloniaObject, IImmutableBrush
public static class BrushExtensions //todo: investigate why resharper does not complain about wrong file naming
{
public static readonly StyledProperty<Color> ColorProperty =
AvaloniaProperty.Register<ConsoleBrush, Color>(nameof(Color));

public static readonly StyledProperty<PixelBackgroundMode> ModeProperty =
AvaloniaProperty.Register<ConsoleBrush, PixelBackgroundMode>(nameof(Mode));

// ReSharper disable once UnusedMember.Global
public ConsoleBrush(Color color, PixelBackgroundMode mode) : this(color)
{
Mode = mode;
}

public ConsoleBrush(Color color)
{
Color = color;
}

public ConsoleBrush()
{
}

public PixelBackgroundMode Mode
{
get => GetValue(ModeProperty);
set => SetValue(ModeProperty, value);
}

public Color Color
{
get => GetValue(ColorProperty);
set => SetValue(ColorProperty, value);
}

public double Opacity => 1;
public ITransform Transform => null;
public RelativePoint TransformOrigin => RelativePoint.TopLeft;

/// <summary>
/// Convert a IBrush to a Brush.
/// </summary>
/// <remarks>
/// NOTE: If it's a ConsoleBrush it will be passed through unchanged, unless mode is set then it will convert
/// consolebrush to mode
/// </remarks>
/// <param name="brush"></param>
/// <param name="mode">Default is Colored.</param>
/// <returns></returns>
public static ConsoleBrush FromBrush(IBrush brush, PixelBackgroundMode? mode = null)
{
ArgumentNullException.ThrowIfNull(brush, nameof(brush));

switch (brush)
{
case ConsoleBrush consoleBrush:
if (mode != null && consoleBrush.Mode != mode)
return new ConsoleBrush(consoleBrush.Color, mode.Value);
return consoleBrush;

case LineBrush lineBrush:
switch (lineBrush.Brush)
{
case ConsoleBrush consoleBrush:
return consoleBrush;
case ISolidColorBrush br:
return new ConsoleBrush(br.Color, mode ?? PixelBackgroundMode.Colored);
default:
ConsoloniaPlatform.RaiseNotSupported(6);
return null;
}

case ISolidColorBrush solidBrush:
return new ConsoleBrush(solidBrush.Color, mode ?? PixelBackgroundMode.Colored);

default:
ConsoloniaPlatform.RaiseNotSupported(6);
return null;
}
}

public static ConsoleBrush FromPosition(IBrush brush, int x, int y, int width, int height)
public static Color FromPosition(this IBrush brush, int x, int y, int width, int height)
{
//todo: apply brush opacity
ArgumentNullException.ThrowIfNull(brush);
if (x < 0 || x > width)
throw new ArgumentOutOfRangeException(nameof(x), "x is out bounds");
Expand All @@ -115,7 +33,7 @@ public static ConsoleBrush FromPosition(IBrush brush, int x, int y, int width, i

// Average the two colors to get the final color
Color color = BlendColors(horizontalColor, verticalColor);
return new ConsoleBrush(color);
return color;
}
case IRadialGradientBrush radialBrush:
{
Expand All @@ -142,7 +60,7 @@ public static ConsoleBrush FromPosition(IBrush brush, int x, int y, int width, i

// Interpolate the color based on the normalized distance
Color color = InterpolateColor(radialBrush, normalizedDistance);
return new ConsoleBrush(color);
return color;
}
case IConicGradientBrush conicBrush:
{
Expand All @@ -160,21 +78,18 @@ public static ConsoleBrush FromPosition(IBrush brush, int x, int y, int width, i

// Average the two colors to get the final color
Color color = BlendColors(horizontalColor, verticalColor);
return new ConsoleBrush(color);
return color;
}

case ISolidColorBrush solidColorBrush:
return solidColorBrush.Color;

default:
return FromBrush(brush);
ConsoloniaPlatform.RaiseNotSupported(751, brush); //todo: allow RaiseNotSupported to return a result
return new Color();
}
}

// ReSharper disable once UnusedMember.Global used by Avalonia
// ReSharper disable once UnusedParameter.Global
public IBrush ProvideValue(IServiceProvider _)
{
return this;
}

private static Color InterpolateColor(IGradientBrush brush, double relativePosition)
{
IGradientStop before = null;
Expand Down
37 changes: 13 additions & 24 deletions src/Consolonia.Core/Drawing/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Text;
using Avalonia;
using Avalonia.Media;
using Avalonia.Media.Immutable;
using Avalonia.Media.TextFormatting;
using Avalonia.Platform;
using Consolonia.Core.Drawing.PixelBufferImplementation;
Expand Down Expand Up @@ -528,12 +527,11 @@ private void FillRectangleWithBrush(IBrush brush, IPen pen, Rect r)
int px = (int)(r2.TopLeft.X + x);
int py = (int)(r2.TopLeft.Y + y);

ConsoleBrush backgroundBrush = ConsoleBrush.FromPosition(brush, x, y, (int)width, (int)height);
Color backgroundColor = brush.FromPosition(x, y, (int)width, (int)height);
CurrentClip.ExecuteWithClipping(new Point(px, py), () =>
{
_pixelBuffer.Set(new PixelBufferCoordinate((ushort)px, (ushort)py),
pixel => pixel.Blend(new Pixel(new PixelBackground(backgroundBrush.Mode,
backgroundBrush.Color))));
pixel => pixel.Blend(new Pixel(new PixelBackground(backgroundColor))));
});
}
}
Expand Down Expand Up @@ -653,9 +651,9 @@ private Line TransformLineInternal(Line line)
lineStyle = null;
if (pen is not
{
Brush: ConsoleBrush or LineBrush or ImmutableSolidColorBrush,
Brush: LineBrush or ISolidColorBrush,
// Thickness: 1,
DashStyle: null or { Dashes: { Count: 0 } },
DashStyle: null or { Dashes.Count: 0 },
LineCap: PenLineCap.Flat,
LineJoin: PenLineJoin.Miter
})
Expand All @@ -664,23 +662,15 @@ private Line TransformLineInternal(Line line)
return null;
}

if (pen.Brush is LineBrush lineBrush)
lineStyle = lineBrush.LineStyle;

ConsoleBrush consoleBrush = ConsoleBrush.FromBrush(pen.Brush);
IBrush brush = pen.Brush;

switch (consoleBrush.Mode)
if (brush is LineBrush lineBrush)
{
case PixelBackgroundMode.Colored:
return consoleBrush.Color;
case PixelBackgroundMode.Transparent:
return null;
case PixelBackgroundMode.Shaded:
ConsoloniaPlatform.RaiseNotSupported(8);
return null;
default:
throw new ArgumentOutOfRangeException(nameof(pen));
lineStyle = lineBrush.LineStyle;
brush = lineBrush.Brush;
}

return ((ISolidColorBrush)brush).Color;
}

/// <summary>
Expand Down Expand Up @@ -731,14 +721,13 @@ private void DrawLineSymbolAndMoveHead(ref Point head, bool isVertical, ISymbol

private void DrawStringInternal(IBrush foreground, string text, IGlyphTypeface typeface, Point origin = new())
{
foreground = ConsoleBrush.FromBrush(foreground);
if (foreground is not ConsoleBrush { Mode: PixelBackgroundMode.Colored } consoleBrush)
if (foreground is not ISolidColorBrush solidColorBrush)
{
ConsoloniaPlatform.RaiseNotSupported(4);
return;
}

// if (!Transform.IsTranslateOnly()) ConsoloniaPlatform.RaiseNotSupported(15);
// if (!Transform.IsTranslateOnly()) ConsoloniaPlatform.RaiseNotSupported(15); //todo: what to do if a rotation?

Point whereToDraw = origin.Transform(Transform);
int currentXPosition = 0;
Expand All @@ -751,7 +740,7 @@ private void DrawLineSymbolAndMoveHead(ref Point head, bool isVertical, ISymbol
{
Point characterPoint =
whereToDraw.Transform(Matrix.CreateTranslation(currentXPosition, currentYPosition));
Color foregroundColor = consoleBrush.Color;
Color foregroundColor = solidColorBrush.Color;

switch (glyph)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public bool Equals(DrawingBoxSymbol other)

[JsonIgnore] public string Text { get; init; }

[JsonIgnore] public ushort Width { get; } = 1;
[JsonIgnore] public ushort Width => 1;

public bool IsWhiteSpace()
public bool NothingToDraw()
{
return UpRightDownLeft == EmptySymbol;
}

public ISymbol Blend(ref ISymbol symbolAbove)
{
if (symbolAbove.IsWhiteSpace()) return this;
if (symbolAbove.NothingToDraw()) return this;

if (symbolAbove is not DrawingBoxSymbol drawingBoxSymbol)
return symbolAbove;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public interface ISymbol
/// </summary>
ushort Width { get; }

bool IsWhiteSpace();
bool NothingToDraw();

/// <summary>
/// Blend 2 symbols together
Expand Down
Loading

0 comments on commit 2dd81ad

Please sign in to comment.