Skip to content

Instantly share code, notes, and snippets.

View VO.cs
namespace MyApp.Domain
{
using Ardalis.GuardClauses;
public class CountryCode
{
public CountryCode(string value) => Value = Guard.Against.UnknownCountryCode(value, nameof(value));
public string Value { get; }
}
View Ohehour.cs
namespace OneHour
{
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
internal class Command
{
public enum CommandKind
View AnimatedGradientMaskedText.swift
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 Feb 14, 2020
Checking out drag n' drop in Swift UI
View Dnd.swift
import SwiftUI
struct AnimalView: View {
var model: Animal
var body: some View {
VStack {
Image(systemName: model.systemImageName)
.resizable()
.scaledToFit()
View MacOSDrawPNG.swift
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 Jan 21, 2021
SwiftUI ButtonStyle that mimics classic beveled buttons from NeXTSTEP and Windows 95
View RedmondButtonStyle.swift
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 Dec 15, 2019
Create CGContext (then images) from raw pixel data
View RGBACGContext.swift
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? {
@jmcd
jmcd / Startup.cs
Created Apr 5, 2019
Get external (eg social) claims while logged in with another identity
View Startup.cs
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WebApplication6.Data;
using Microsoft.Extensions.Configuration;
View Magnets.swift
func positionsOfZeroNetForce(_ magnetPositions: [Double]) -> [Double] {
let threshold = 0.0000000000001
// Utility function to give 1/fn(m0) + 1/fn(m1) + ... + 1/fn(mn)
// Used for working out the force at x, and its derivitive
func sumReciprocals(_ fn: (Double)->(Double)) -> Double {
return magnetPositions.map(fn).map {
guard $0 != 0 else { return Double.nan }
return 1.0/$0