Skip to content

Instantly share code, notes, and snippets.

View KrisYu's full-sized avatar

Xue Yu KrisYu

View GitHub Profile
@figgleforth
figgleforth / NSImage+pixelData.swift
Last active July 22, 2022 15:22
Extract RGBA pixel data from an NSImage in Swift
//
// NSImage+pixelData.swift
//
// Created by Figgleforth on 5/21/15.
// Copyright (c) 2015 Bojan Percevic. All rights reserved.
//
import AppKit
extension NSImage {
@marchinram
marchinram / UIImage+PixelColor.swift
Last active January 19, 2022 08:53
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
guard x >= 0 && x < Int(size.width) && y >= 0 && y < Int(size.height),
let cgImage = cgImage,
let provider = cgImage.dataProvider,
let providerData = provider.data,
let data = CFDataGetBytePtr(providerData) else {
return nil
@ericdke
ericdke / splitBy.swift
Last active July 10, 2023 09:55
Swift: split array by chunks of given size
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
extension Array {
func splitBy(subSize: Int) -> [[Element]] {
return 0.stride(to: self.count, by: subSize).map { startIndex in
let endIndex = startIndex.advancedBy(subSize, limit: self.count)
return Array(self[startIndex ..< endIndex])
}
}
}
func openAndSelectFile(filename:String){
let files = [NSURL(fileURLWithPath: filename)];
NSWorkspace.sharedWorkspace().activateFileViewerSelectingURLs(files);
}
@mchirico
mchirico / RW.swift
Created October 15, 2015 16:25
RW to file in swift
//
// RW.swift
// Web
//
// Created by Mike Chirico on 10/15/15.
// Copyright © 2015 Mike Chirico. All rights reserved.
//
import UIKit
@sanderman01
sanderman01 / OrbitCamera.cs
Created October 29, 2015 10:02
Orbital Camera in Unity
using UnityEngine;
using System.Collections;
/// <summary>
/// Defines angle limits as the maximum deviation away from the rotation of this object.
/// (in other words: if the yawlimit is 45, then you can only move up to 45 degrees away from this rotation in both directions.
/// This means the total angle available would be an angle of 90 degrees)
/// An angle of 180 allows complete freedom of movement on that axis.
/// </summary>
public class FocusPoint : MonoBehaviour
@nickstanisha
nickstanisha / trie.py
Created November 21, 2015 22:58
An object-oriented implementation of a "Trie" in Python
class Node:
def __init__(self, label=None, data=None):
self.label = label
self.data = data
self.children = dict()
def addChild(self, key, data=None):
if not isinstance(key, Node):
self.children[key] = Node(key, data)
else:
@mtackes
mtackes / NSURLSession-uploadTask-example.swift
Last active December 7, 2018 04:03
An basic example of using an upload task with a completion handler.
import Cocoa
let session = NSURLSession.sharedSession()
let request = NSURLRequest(URL: NSURL(string: "http://example.com")!)
let dataToUpload = "Some Arbitrary Data".dataUsingEncoding(NSUTF8StringEncoding)
let uploadTask = session.uploadTaskWithRequest(request, fromData: dataToUpload,
completionHandler: { (responseData, response, error) in
// Check on some response headers (if it's HTTP)
@oanhnn
oanhnn / using-multiple-github-accounts-with-ssh-keys.md
Last active December 2, 2025 12:36
Using multiple github accounts with ssh keys

Problem

I have two Github accounts: oanhnn (personal) and superman (for work). I want to use both accounts on same computer (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (each alias for an account).

How to?

  1. Generate ssh key pairs for accounts and add them to GitHub accounts.
@thomastweets
thomastweets / PNGWhiteTrim.py
Last active June 23, 2025 15:42
Python script to trim all png images with white background in a folder
import Image
import sys
import glob
import ImageOps
# Trim all png images with white background in a folder
# Usage "python PNGWhiteTrim.py ../someFolder"
try:
folderName = sys.argv[1]