Skip to content

Instantly share code, notes, and snippets.

View crozone's full-sized avatar
💭
Working on ipodloader2 and Tetris for macOS 9

Ryan Crosby crozone

💭
Working on ipodloader2 and Tetris for macOS 9
  • Australia
View GitHub Profile
@crozone
crozone / RoundDown.cs
Created July 4, 2023 08:16
C# Round down to next power of two. Analogous to BitOperations.RoundUpToPowerOf2().
using System.Numerics;
/// <summary>
/// Round the given integral value down to a power of 2.
/// This is the equivalent to returning a number with a single bit set at the most significant location a bit is set in the input value.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// The smallest power of 2 which is less than or equal to <paramref name="value"/>.
/// If <paramref name="value"/> is 0 or the result overflows, returns 0.
@crozone
crozone / AsyncLock.cs
Created July 13, 2023 02:28
C# AsyncLock based on SemaphoreSlim
public class AsyncLock
{
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1);
private readonly Task<IDisposable> releaser;
public AsyncLock()
{
releaser = Task.FromResult((IDisposable)new AsyncLockReleaser(this));
}
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Channels;
public static class ChannelReaderExtensions
{
/// <summary>
/// Creates an <see cref="IAsyncEnumerable{T}"/> that enables reading all of the data from the channel
@crozone
crozone / ColorHelpers.cs
Created May 7, 2024 02:55
HSL to RGB Color conversion
internal static class ColorHelpers
{
/// <summary>
/// Converts HSL color value to RGB fractional color value
/// </summary>
/// <param name="hue">Hue angle value between [0,360]</param>
/// <param name="saturation">Saturation value between [0,1]</param>
/// <param name="lightness">Lightness value between [0,1]</param>
/// <returns>RGB color values, with each value between [0,1]</returns>
/// <see href="https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB_alternative"/>