Skip to content

Instantly share code, notes, and snippets.

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

James Jackson-South JimBobSquarePants

💭
(•_•) ( •_•)>⌐■-■ (⌐■_■)
View GitHub Profile
@JimBobSquarePants
JimBobSquarePants / DominantColor.cs
Created June 7, 2018 11:58
Get dominant color from an image using ImageSharp and ImageProcessor
// ##### ImageSharp #####
using (var image = Image.Load<Rgb24>(inPath))
{
image.Mutate(
x => x
// Scale the image down preserving the aspect ratio. This will speed up quantization.
// We use nearest neighbor as it will be the fastest approach.
.Resize(new ResizeOptions() { Sampler = KnownResamplers.NearestNeighbor, Size = new Size(100, 0) })
@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 / pre-wrap.css
Created November 20, 2012 17:15
Browser specific (not valid) styles to make preformatted text wrap
/* Browser specific (not valid) styles to make preformatted text wrap */
pre {
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
@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 / ImageProcessorPreProcessor.cs
Created July 23, 2015 15:40
Demonstration code for Preprocessing uploaded images with Umbraco
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ImageProcessorPreProcessor.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Attaches ImageProcessor to the Media.Saving event to ensure that uploaded files do not exceed
// a maximum size.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
@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;