Skip to content

Commit

Permalink
Automated JetBrains cleanup
Browse files Browse the repository at this point in the history
Co-authored-by:  <[email protected]>
  • Loading branch information
github-actions[bot] committed Oct 31, 2024
1 parent d5dbd0b commit 274835d
Show file tree
Hide file tree
Showing 21 changed files with 267 additions and 282 deletions.
132 changes: 62 additions & 70 deletions src/Consolonia.Core/Drawing/ConsoleBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,28 @@ 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.
/// 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
/// 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>
Expand All @@ -46,9 +63,7 @@ public static ConsoleBrush FromBrush(IBrush brush, PixelBackgroundMode? mode = n
{
case ConsoleBrush consoleBrush:
if (mode != null && consoleBrush.Mode != mode)
{
return new ConsoleBrush(consoleBrush.Color, mode.Value);
}
return consoleBrush;

case LineBrush lineBrush:
Expand Down Expand Up @@ -87,90 +102,73 @@ public static ConsoleBrush FromPosition(IBrush brush, int x, int y, int width, i
switch (brush)
{
case ILinearGradientBrush gradientBrush:
{
// Calculate the relative position within the gradient
double horizontalRelativePosition = (double)x / width;
double verticalRelativePosition = (double)y / height;
{
// Calculate the relative position within the gradient
double horizontalRelativePosition = (double)x / width;
double verticalRelativePosition = (double)y / height;

// Interpolate horizontal and vertical colors
var horizontalColor = InterpolateColor(gradientBrush, horizontalRelativePosition);
var verticalColor = InterpolateColor(gradientBrush, verticalRelativePosition);
// Interpolate horizontal and vertical colors
Color horizontalColor = InterpolateColor(gradientBrush, horizontalRelativePosition);
Color verticalColor = InterpolateColor(gradientBrush, verticalRelativePosition);

// Average the two colors to get the final color
var color = BlendColors(horizontalColor, verticalColor);
return new ConsoleBrush(color);
}
// Average the two colors to get the final color
Color color = BlendColors(horizontalColor, verticalColor);
return new ConsoleBrush(color);
}
case IRadialGradientBrush radialBrush:
{
// Calculate the normalized center coordinates
double centerX = radialBrush.Center.Point.X * width;
double centerY = radialBrush.Center.Point.Y * height;
{
// Calculate the normalized center coordinates
double centerX = radialBrush.Center.Point.X * width;
double centerY = radialBrush.Center.Point.Y * height;

// Calculate the distance from the center
double dx = x - centerX;
double dy = y - centerY;
double distance = Math.Sqrt(dx * dx + dy * dy);
// Calculate the distance from the center
double dx = x - centerX;
double dy = y - centerY;
double distance = Math.Sqrt(dx * dx + dy * dy);

// Normalize the distance based on the brush radius
double normalizedDistance = distance / (Math.Min(width, height) * radialBrush.Radius);
// Normalize the distance based on the brush radius
double normalizedDistance = distance / (Math.Min(width, height) * radialBrush.Radius);

// Clamp the normalized distance to [0, 1]
normalizedDistance = Math.Min(Math.Max(normalizedDistance, 0), 1);
// Clamp the normalized distance to [0, 1]
normalizedDistance = Math.Min(Math.Max(normalizedDistance, 0), 1);

// Interpolate the color based on the normalized distance
var color = InterpolateColor(radialBrush, normalizedDistance);
return new ConsoleBrush(color);
}
case IConicGradientBrush conicBrush:
{
// Calculate the relative position within the gradient
double horizontalRelativePosition = (double)x / width;
double verticalRelativePosition = (double)y / height;
// Interpolate the color based on the normalized distance
Color color = InterpolateColor(radialBrush, normalizedDistance);
return new ConsoleBrush(color);
}
case IConicGradientBrush conicBrush:
{
// Calculate the relative position within the gradient
double horizontalRelativePosition = (double)x / width;
double verticalRelativePosition = (double)y / height;

// Interpolate horizontal and vertical colors
var horizontalColor = InterpolateColor(conicBrush, horizontalRelativePosition);
var verticalColor = InterpolateColor(conicBrush, verticalRelativePosition);
// Interpolate horizontal and vertical colors
Color horizontalColor = InterpolateColor(conicBrush, horizontalRelativePosition);
Color verticalColor = InterpolateColor(conicBrush, verticalRelativePosition);

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

default:
return FromBrush(brush);
}
}

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

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

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

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

private static Color InterpolateColor(IGradientBrush brush, double relativePosition)
{
IGradientStop before = null;
IGradientStop after = null;

foreach (var stop in brush.GradientStops)
{
foreach (IGradientStop stop in brush.GradientStops)
if (stop.Offset <= relativePosition)
{
before = stop;
Expand All @@ -180,18 +178,12 @@ private static Color InterpolateColor(IGradientBrush brush, double relativePosit
after = stop;
break;
}
}

if (before == null && after == null)

Check notice on line 182 in src/Consolonia.Core/Drawing/ConsoleBrush.cs

View workflow job for this annotation

GitHub Actions / build

"[ConvertIfStatementToSwitchStatement] Convert 'if' statement into 'switch' statement" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Drawing/ConsoleBrush.cs(182,13)
throw new ArgumentException("no gradientstops defined");

if (before == null)
{
return after.Color;
}
if (after == null)
{
return before.Color;
}
if (before == null) return after.Color;
if (after == null) return before.Color;

double ratio = (relativePosition - before.Offset) / (after.Offset - before.Offset);
byte r = (byte)(before.Color.R + ratio * (after.Color.R - before.Color.R));
Expand Down
2 changes: 1 addition & 1 deletion src/Consolonia.Core/Drawing/ConsoloniaRenderInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ IBitmapImpl IPlatformRenderInterface.ResizeBitmap(IBitmapImpl bitmapImpl, PixelS
{
throw new NotImplementedException();
}

public IBitmapImpl LoadBitmap(
PixelFormat format,
AlphaFormat alphaFormat,
Expand Down
91 changes: 43 additions & 48 deletions src/Consolonia.Core/Drawing/DrawingContextImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,40 +118,35 @@ public void DrawGeometry(IBrush brush, IPen pen, IGeometryImpl geometry)
case VisualBrush:
throw new NotImplementedException();
case ISceneBrush sceneBrush:
{
ISceneBrushContent sceneBrushContent = sceneBrush.CreateContent();
if (sceneBrushContent != null)
{
sceneBrushContent.Render(this, Matrix.Identity);
}
return;
}
{
ISceneBrushContent sceneBrushContent = sceneBrush.CreateContent();
if (sceneBrushContent != null) sceneBrushContent.Render(this, Matrix.Identity);
return;
}
}

Rect r2 = r.TransformToAABB(Transform);

var width = r2.Width + (pen?.Thickness ?? 0);
var height = r2.Height + (pen?.Thickness ?? 0);
double width = r2.Width + (pen?.Thickness ?? 0);
double height = r2.Height + (pen?.Thickness ?? 0);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
for (int y = 0; y < height; y++)
{
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);
CurrentClip.ExecuteWithClipping(new Point(px, py), () =>
{
int px = (int)(r2.TopLeft.X + x);
int py = (int)(r2.TopLeft.Y + y);

var backgroundBrush = ConsoleBrush.FromPosition(brush, x, y, (int)width, (int)height);
CurrentClip.ExecuteWithClipping(new Point(px, py), () =>
{
_pixelBuffer.Set(new PixelBufferCoordinate((ushort)px, (ushort)py),
(pixel, bb) =>
{
return pixel.Blend(new Pixel(new PixelBackground(bb.Mode, bb.Color)));
}, backgroundBrush);
});
}
_pixelBuffer.Set(new PixelBufferCoordinate((ushort)px, (ushort)py),
(pixel, bb) => { return pixel.Blend(new Pixel(new PixelBackground(bb.Mode, bb.Color))); },

Check notice on line 142 in src/Consolonia.Core/Drawing/DrawingContextImpl.cs

View workflow job for this annotation

GitHub Actions / build

"[ConvertToLambdaExpression] Use lambda expression" on /home/runner/work/Consolonia/Consolonia/src/Consolonia.Core/Drawing/DrawingContextImpl.cs(142,46)
backgroundBrush);
});
}
}

if (pen is null or { Thickness: 0 }
or { Brush: null }) return;
or { Brush: null }) return;

DrawLineInternal(pen, new Line(r.TopLeft, false, (int)r.Width));
DrawLineInternal(pen, new Line(r.BottomLeft, false, (int)r.Width));
Expand Down Expand Up @@ -376,44 +371,44 @@ void DrawPixelAndMoveHead(int count)
switch (c)
{
case '\t':
{
const int tabSize = 8;
var consolePixel = new Pixel(' ', foregroundColor);
for (int j = 0; j < tabSize; j++)
{
const int tabSize = 8;
var consolePixel = new Pixel(' ', foregroundColor);
for (int j = 0; j < tabSize; j++)
Point newCharacterPoint = characterPoint.WithX(characterPoint.X + j);
CurrentClip.ExecuteWithClipping(newCharacterPoint, () =>
{
Point newCharacterPoint = characterPoint.WithX(characterPoint.X + j);
CurrentClip.ExecuteWithClipping(newCharacterPoint, () =>
{
_pixelBuffer.Set((PixelBufferCoordinate)newCharacterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
});
}

currentXPosition += tabSize - 1;
_pixelBuffer.Set((PixelBufferCoordinate)newCharacterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
});
}

currentXPosition += tabSize - 1;
}
break;
case '\n':
{
/* it's not clear if we need to draw anything. Cursor can be placed at the end of the line
var consolePixel = new Pixel(' ', foregroundColor);
{
/* it's not clear if we need to draw anything. Cursor can be placed at the end of the line
var consolePixel = new Pixel(' ', foregroundColor);
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);*/
}
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);*/
}
break;
case '\u200B':
currentXPosition--;
break;
default:
{
var consolePixel = new Pixel(c, foregroundColor, typeface.Style, typeface.Weight);
CurrentClip.ExecuteWithClipping(characterPoint, () =>
{
var consolePixel = new Pixel(c, foregroundColor, typeface.Style, typeface.Weight);
CurrentClip.ExecuteWithClipping(characterPoint, () =>
{
_pixelBuffer.Set((PixelBufferCoordinate)characterPoint,
(oldPixel, cp) => oldPixel.Blend(cp), consolePixel);
}
);
}
);
}
break;
}
}
Expand Down
19 changes: 8 additions & 11 deletions src/Consolonia.Core/Drawing/PixelBufferImplementation/Pixel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public Pixel(bool isCaret) : this(PixelBackgroundMode.Transparent)
IsCaret = isCaret;
}

public Pixel(char character, Color foregroundColor, FontStyle style = FontStyle.Normal, FontWeight weight = FontWeight.Normal) :
public Pixel(char character, Color foregroundColor, FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal) :
this(new SimpleSymbol(character), foregroundColor, style, weight)
{
}
Expand All @@ -29,7 +30,8 @@ public Pixel(byte drawingBoxSymbol, Color foregroundColor) : this(
{
}

public Pixel(ISymbol symbol, Color foregroundColor, FontStyle style = FontStyle.Normal, FontWeight weight = FontWeight.Normal, TextDecorationCollection textDecorations = null) : this(
public Pixel(ISymbol symbol, Color foregroundColor, FontStyle style = FontStyle.Normal,
FontWeight weight = FontWeight.Normal, TextDecorationCollection textDecorations = null) : this(
new PixelForeground(symbol, weight, style, textDecorations, foregroundColor),
new PixelBackground(PixelBackgroundMode.Transparent))
{
Expand Down Expand Up @@ -69,18 +71,14 @@ public Pixel Blend(Pixel pixelAbove)
// when a textdecoration of underline happens a DrawLine() is called over the top of the a pixel with non-zero symbol.
// this detects this situation and eats the draw line, turning it into a textdecoration
if (pixelAbove.Foreground.Symbol is DrawingBoxSymbol &&
this.Foreground.Symbol is SimpleSymbol simpleSymbol &&
((ISymbol)simpleSymbol).GetCharacter() != (Char)0)

{
Foreground.Symbol is SimpleSymbol simpleSymbol &&
((ISymbol)simpleSymbol).GetCharacter() != (char)0)
// this is a line being draw through text. add TextDecoration for underline.
newForeground = new PixelForeground(Foreground.Symbol, Foreground.Weight, Foreground.Style, TextDecorations.Underline, Foreground.Color);
}
newForeground = new PixelForeground(Foreground.Symbol, Foreground.Weight, Foreground.Style,
TextDecorations.Underline, Foreground.Color);
else
{
// do normal blend.
newForeground = Foreground.Blend(pixelAbove.Foreground);
}
newBackground = Background;
break;
case PixelBackgroundMode.Shaded:
Expand All @@ -99,6 +97,5 @@ this.Foreground.Symbol is SimpleSymbol simpleSymbol &&
{
return (Foreground.Shade(), Background.Shade());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,5 @@ public PixelBackground Shade()

return new PixelBackground(newMode, newColor);
}

}
}
Loading

0 comments on commit 274835d

Please sign in to comment.