Skip to content

Instantly share code, notes, and snippets.

View JimBobSquarePants's full-sized avatar
💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)

James Jackson-South JimBobSquarePants

💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)
View GitHub Profile
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
void Main()
{
using Image image = Image.Load(@"C:\Users\james\Downloads\fish.png");
image.Mutate(x => x.Quantize(new WuQuantizer(new() { MaxColors = 7 })));
image.Save(@"C:\Users\james\Downloads\fish-7-colors.png");
@JimBobSquarePants
JimBobSquarePants / Attempt{T}.cs
Created January 14, 2023 11:15
A nullable aware Attempt type for wrapping nullable async responses.
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// A wrapper for nullable values that correctly handles the return type based on the result.
/// </summary>
/// <typeparam name="T">The type of nullable value.</typeparam>
public readonly struct Attempt<T>
{
/// <summary>
/// Gets a value indicating whether the attempted return was successful.
@JimBobSquarePants
JimBobSquarePants / ByteMemoryManager.cs
Created September 20, 2022 02:25
MemoryManager<T> implementation that uses an underlying Memory<byte> source.
/// <summary>
/// A custom <see cref="MemoryManager{T}"/> that can wrap <see cref="Memory{T}"/> of <see cref="byte"/> instances
/// and cast them to be <see cref="Memory{T}"/> for any arbitrary unmanaged <typeparamref name="T"/> value type.
/// </summary>
/// <typeparam name="T">The value type to use when casting the wrapped <see cref="Memory{T}"/> instance.</typeparam>
internal sealed class ByteMemoryManager<T> : MemoryManager<T>
where T : struct
{
/// <summary>
/// The wrapped <see cref="Memory{T}"/> of <see cref="byte"/> instance.
@JimBobSquarePants
JimBobSquarePants / SkiaPort.md
Last active April 6, 2022 15:44
Skia to System.Numerics Port Question
@JimBobSquarePants
JimBobSquarePants / ToSplitTitleString.cs
Created October 13, 2021 13:45
Add spaces to string based upon the position of Uppercase letters
/// <summary>
/// Returns the input string with spaces inserted at each uppercase character.
/// </summary>
/// <param name="input">The input string to split.</param>
/// <returns>The <see cref="string"/>.</returns>
public static string ToSplitTitleString(string input)
{
int spaces = 0;
Rune whitespace = new(0x0020);
Rune previous = default;
@JimBobSquarePants
JimBobSquarePants / ColorDistanceCache.cs
Created June 5, 2021 13:27
A naïve cache for holding color distances. Needs thread safety!
/// <summary>
/// A cache for storing color distance matching results.
/// </summary>
/// <remarks>
/// The cache is limited to 646866 entries at 0.62MB.
/// </remarks>
private struct ColorDistanceCache
{
private const int IndexBits = 5;
private const int IndexAlphaBits = 3;
@JimBobSquarePants
JimBobSquarePants / TestOutput.txt
Created February 2, 2021 17:39
Failing Avalonia LineBreakEnumerator Test Output.
Test Name: Avalonia.Visuals.UnitTests.Media.TextFormatting.LineBreakerTests.ICUTests
Test Outcome: Passed
Result StandardOutput:
Line Breaker Tests
------------------
Failed test on line 49
Code Points: 35 65532
Expected Breaks: 1 2
Actual Breaks: 2
Char Props: Alphabetic ContingentBreak
@JimBobSquarePants
JimBobSquarePants / LineBreakerTests.cs
Last active February 2, 2021 06:20
Failing Avalonia LineBreaker tests
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Avalonia.Media.TextFormatting.Unicode;
using Avalonia.Utilities;
using Xunit;
@JimBobSquarePants
JimBobSquarePants / ScopedServiceRunner.cs
Created November 10, 2020 21:46
Allows the controlled running of scoped services with automatic cleanup.
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace ThingsWhatImNotProudOf
{
/// <summary>
/// Allows the manufacture and running of items within a smaller lifetime scope than the parent.
/// To be used sparingly when unable to inject a service within the required scope.
/// </summary>
@JimBobSquarePants
JimBobSquarePants / intersection.cs
Last active June 24, 2020 11:36
Calculating intersections between two segments.
void Main()
{
var tl = new Vector2(0, 0);
var br = new Vector2(5, 3);
var tr = new Vector2(5, 0);
var bl = new Vector2(0, 3);
var tlbr = new Segment(tl, br);
var trbl = new Segment(tr, bl);