-
Notifications
You must be signed in to change notification settings - Fork 550
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .NET Interactive Support (#1710)
- Loading branch information
1 parent
1916562
commit 11591db
Showing
13 changed files
with
193 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using Microsoft.AspNetCore.Html; | ||
|
||
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags; | ||
|
||
namespace SkiaSharp.DotNet.Interactive | ||
{ | ||
public static class ColorRenderer | ||
{ | ||
public static IHtmlContent Render(this SKColor color) | ||
{ | ||
var colorString = color.Alpha == 255 | ||
? $"rgb({color.Red},{color.Green},{color.Blue})" | ||
: $"rgba({color.Red},{color.Green},{color.Blue},{color.Alpha / 255.0:0.0##})"; | ||
|
||
return GetHtml(colorString); | ||
} | ||
|
||
public static IHtmlContent Render(this SKColorF color) => | ||
((SKColor)color).Render(); | ||
|
||
private static IHtmlContent GetHtml(string colorString) => | ||
div( | ||
span[style: | ||
$"width: 2em; " + | ||
$"background: {colorString}; " + | ||
$"display: inline-block; " + | ||
$"border: 1px solid black; "]( | ||
new HtmlString(" ")), | ||
span( | ||
new HtmlString(" "), | ||
colorString)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using Microsoft.AspNetCore.Html; | ||
|
||
using static Microsoft.DotNet.Interactive.Formatting.PocketViewTags; | ||
|
||
namespace SkiaSharp.DotNet.Interactive | ||
{ | ||
public static class RasterRenderer | ||
{ | ||
public static IHtmlContent Render(this SKBitmap bitmap) | ||
{ | ||
using var image = SKImage.FromBitmap(bitmap); | ||
return Render(image); | ||
} | ||
|
||
public static IHtmlContent Render(this SKPixmap pixmap) | ||
{ | ||
using var image = SKImage.FromPixels(pixmap); | ||
return Render(image); | ||
} | ||
|
||
public static IHtmlContent Render(this SKPicture picture) | ||
{ | ||
using var image = SKImage.FromPicture(picture, picture.CullRect.Size.ToSizeI()); | ||
return Render(image); | ||
} | ||
|
||
public static IHtmlContent Render(this SKSurface surface) | ||
{ | ||
using var image = surface.Snapshot(); | ||
return Render(image); | ||
} | ||
|
||
public static IHtmlContent Render(this SKImage image) | ||
{ | ||
using var data = image.Encode(SKEncodedImageFormat.Png, 100); | ||
return GetHtml(data); | ||
} | ||
|
||
private static IHtmlContent GetHtml(SKData data) | ||
{ | ||
var base64 = Convert.ToBase64String(data.AsSpan()); | ||
|
||
return div( | ||
img[src: "data:image/png;base64," + base64]); | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
source/SkiaSharp.DotNet.Interactive/SkiaSharp.DotNet.Interactive.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<RootNamespace>SkiaSharp.DotNet.Interactive</RootNamespace> | ||
<AssemblyName>SkiaSharp.DotNet.Interactive</AssemblyName> | ||
<MDocDocumentationDirectory></MDocDocumentationDirectory> | ||
<SignAssembly>false</SignAssembly> | ||
<PackagingGroup>SkiaSharp</PackagingGroup> | ||
<PackagingLocation>interactive-extensions\dotnet</PackagingLocation> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="microsoft.dotnet.interactive" Version="1.0.0-beta.21255.3" PrivateAssets="all" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\binding\SkiaSharp\SkiaSharp.csproj" /> | ||
</ItemGroup> | ||
|
||
<!-- uncomment this block for local testing --> | ||
<!--<PropertyGroup> | ||
<IncludeBuildOutput>false</IncludeBuildOutput> | ||
<IsPackable>true</IsPackable> | ||
<PackageVersion>0.0.1-preview.$([System.DateTime]::Now.ToString('yyyy.M.d.H.m.s'))</PackageVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<ProjectReference Update="..\..\binding\SkiaSharp\SkiaSharp.csproj" PrivateAssets="all" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\..\output\native\osx\libSkiaSharp*" Pack="true" PackagePath="runtimes\osx\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\windows\x64\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-x64\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\windows\x86\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-x86\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\windows\arm64\libSkiaSharp*" Pack="true" PackagePath="runtimes\win-arm64\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\linux\x64\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-x64\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\linux\x86\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-x86\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\linux\arm64\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-arm64\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\native\linux\arm\libSkiaSharp*" Pack="true" PackagePath="runtimes\linux-arm\native\%(Filename)%(Extension)" /> | ||
<None Include="..\..\output\SkiaSharp\nuget\lib\netstandard2.0\SkiaSharp.dll" Pack="true" PackagePath="lib\netstandard2.0\SkiaSharp.dll" /> | ||
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="$(PackagingLocation)" /> | ||
</ItemGroup>--> | ||
|
||
</Project> |
27 changes: 27 additions & 0 deletions
27
source/SkiaSharp.DotNet.Interactive/SkiaSharpKernelExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.DotNet.Interactive; | ||
using Microsoft.DotNet.Interactive.Formatting; | ||
|
||
namespace SkiaSharp.DotNet.Interactive | ||
{ | ||
public class SkiaSharpKernelExtension : IKernelExtension | ||
{ | ||
public Task OnLoadAsync(Kernel kernel) | ||
{ | ||
// colors | ||
Formatter.Register<SKColor>((color, writer) => writer.Write(color.Render()), "text/html"); | ||
Formatter.Register<SKColorF>((color, writer) => writer.Write(color.Render()), "text/html"); | ||
|
||
// "images" | ||
Formatter.Register<SKBitmap>((bmp, writer) => writer.Write(bmp.Render()), "text/html"); | ||
Formatter.Register<SKPixmap>((pix, writer) => writer.Write(pix.Render()), "text/html"); | ||
Formatter.Register<SKPicture>((pic, writer) => writer.Write(pic.Render()), "text/html"); | ||
Formatter.Register<SKSurface>((surface, writer) => writer.Write(surface.Render()), "text/html"); | ||
Formatter.Register<SKImage>((img, writer) => writer.Write(img.Render()), "text/html"); | ||
|
||
// TODO: colorspaces and other things | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
92 changes: 0 additions & 92 deletions
92
source/SkiaSharp.Workbooks/SkiaSharpRepresentationProvider.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.