Skip to content

Instantly share code, notes, and snippets.

namespace RomTile;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
internal static class Program
{
private const int TileWidth = 8;
private const int TileHeight = 8;
private const int NumberOfPixelsInATile = TileWidth * TileHeight;
@jmcd
jmcd / Ball.cs
Created April 3, 2022 06:41
Bouncing balls in MonoGame
namespace MonoGameCross_PlatformDesktopApplication1
{
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
public static class Program
{
[STAThread]
private static void Main()
@jmcd
jmcd / AutoGarbage.cs
Created March 28, 2022 07:45
Hack to magic up objects with random property values
using System.Text;
public static class AutoGarbage
{
private const string Chars = "qwertyuiopasdfghjklzxcvbnm1234567890";
public static T Next<T>(this Random random) => (T)Next(random, typeof(T));
private static object Next(this Random random, Type type)
{
namespace MyApp.Domain
{
using Ardalis.GuardClauses;
public class CountryCode
{
public CountryCode(string value) => Value = Guard.Against.UnknownCountryCode(value, nameof(value));
public string Value { get; }
}
namespace OneHour
{
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
internal class Command
{
public enum CommandKind
import SwiftUI
struct ContentView: View {
@State var gradient = [
Color.red, .orange, .yellow, .green, .blue, .purple, Color.red, .orange, .yellow, .green, .blue, .purple,
]
@State var startPoint = UnitPoint(x: 0, y: -1)
@State var endPoint = UnitPoint(x: 0, y: 1)
@jmcd
jmcd / Dnd.swift
Created February 14, 2020 18:21
Checking out drag n' drop in Swift UI
import SwiftUI
struct AnimalView: View {
var model: Animal
var body: some View {
VStack {
Image(systemName: model.systemImageName)
.resizable()
.scaledToFit()
@jmcd
jmcd / MacOSDrawPNG.swift
Created January 24, 2020 08:45
Drawing to a PNG
func makePNG(_ size: NSSize, by drawing: (CGContext) -> ()) -> Data? {
guard let bitmapImageRep = NSBitmapImageRep(bitmapDataPlanes: nil,
pixelsWide: Int(size.width),
pixelsHigh: Int(size.height),
bitsPerSample: 8,
samplesPerPixel: 4,
hasAlpha: true,
isPlanar: false,
colorSpaceName: NSColorSpaceName.deviceRGB,
bytesPerRow: 0, bitsPerPixel: 0) else {
@jmcd
jmcd / RedmondButtonStyle.swift
Last active August 13, 2024 15:46
SwiftUI ButtonStyle that mimics classic beveled buttons from NeXTSTEP and Windows 95
import SwiftUI
extension Color {
static var redmondBackground = Color(white: 0.78)
static var redmondShadow = Color(white: 0.55)
}
extension Font {
static var redmondLabel = Font(UIFont(name: "MicrosoftSansSerif", size: UIFont.labelFontSize)!)
}
@jmcd
jmcd / RGBACGContext.swift
Last active September 25, 2024 21:23
Create CGContext (then images) from raw pixel data
import QuartzCore
extension CGContext {
private static let colorSpace = CGColorSpaceCreateDeviceRGB()
private static let bitmapInfo =
CGBitmapInfo.byteOrder32Big.rawValue |
CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
static func from(pixels: [UInt32], width: Int) -> CGContext? {