Add example infrastructure

* Add "Shared" projects for all examples
* Update "Colors" example with better TrueColor demo
* Use same namespace for all examples
This commit is contained in:
Patrik Svensson
2021-04-11 22:47:26 +02:00
committed by Phil Scott
parent 2fe2bb3c32
commit 3545e0f6b5
74 changed files with 271 additions and 355 deletions

View File

@ -1,108 +0,0 @@
using System.Collections.Generic;
using System.Diagnostics;
using Spectre.Console;
using Spectre.Console.Rendering;
namespace Showcase
{
public static partial class Program
{
public sealed class ColorBox : Renderable
{
private readonly int _height;
public ColorBox(int height)
{
_height = height;
}
protected override Measurement Measure(RenderContext context, int maxWidth)
{
return new Measurement(1, maxWidth);
}
protected override IEnumerable<Segment> Render(RenderContext context, int maxWidth)
{
for (var y = 0; y < _height; y++)
{
for (var x = 0; x < maxWidth; x++)
{
var h = x / (float)maxWidth;
var l = 0.1f + ((y / (float)_height) * 0.7f);
var (r1, g1, b1) = ColorFromHSL(h, l, 1.0f);
var (r2, g2, b2) = ColorFromHSL(h, l + (0.7f / 10), 1.0f);
var background = new Color((byte)(r1 * 255), (byte)(g1 * 255), (byte)(b1 * 255));
var foreground = new Color((byte)(r2 * 255), (byte)(g2 * 255), (byte)(b2 * 255));
yield return new Segment("▄", new Style(foreground, background));
}
yield return Segment.LineBreak;
}
}
private static (float, float, float) ColorFromHSL(double h, double l, double s)
{
double r = 0, g = 0, b = 0;
if (l != 0)
{
if (s == 0)
{
r = g = b = l;
}
else
{
double temp2;
if (l < 0.5)
{
temp2 = l * (1.0 + s);
}
else
{
temp2 = l + s - (l * s);
}
var temp1 = 2.0 * l - temp2;
r = GetColorComponent(temp1, temp2, h + 1.0 / 3.0);
g = GetColorComponent(temp1, temp2, h);
b = GetColorComponent(temp1, temp2, h - 1.0 / 3.0);
}
}
return ((float)r, (float)g, (float)b);
}
private static double GetColorComponent(double temp1, double temp2, double temp3)
{
if (temp3 < 0.0)
{
temp3 += 1.0;
}
else if (temp3 > 1.0)
{
temp3 -= 1.0;
}
if (temp3 < 1.0 / 6.0)
{
return temp1 + (temp2 - temp1) * 6.0 * temp3;
}
else if (temp3 < 0.5)
{
return temp2;
}
else if (temp3 < 2.0 / 3.0)
{
return temp1 + ((temp2 - temp1) * ((2.0 / 3.0) - temp3) * 6.0);
}
else
{
return temp1;
}
}
}
}
}

View File

@ -1,7 +1,6 @@
using System;
using Spectre.Console;
namespace Showcase
namespace Spectre.Console.Examples
{
public static class ExceptionGenerator
{

View File

@ -1,10 +1,6 @@
using System;
using Spectre.Console;
using SixLabors.ImageSharp.Processing;
using System.Threading;
using Spectre.Console.Rendering;
namespace Showcase
namespace Spectre.Console.Examples
{
public static partial class Program
{
@ -102,7 +98,7 @@ namespace Showcase
"✓ [bold purple]8-bit color[/]\n" +
"✓ [bold yellow]Truecolor (16.7 million)[/]\n" +
"✓ [bold aqua]Automatic color conversion[/]"),
new ColorBox(6));
new ColorBox(height: 6));
return colorTable;
}

View File

@ -1,16 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<ExampleTitle>Showcase</ExampleTitle>
<ExampleDescription>Demonstation of Spectre.Console.</ExampleDescription>
<ExampleGroup>Misc</ExampleGroup>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\src\Spectre.Console\Spectre.Console.csproj" />
<ProjectReference Include="..\..\..\src\Spectre.Console.ImageSharp\Spectre.Console.ImageSharp.csproj" />
<ProjectReference Include="..\..\Shared\Shared.csproj" />
</ItemGroup>
</Project>