Skip to content

Instantly share code, notes, and snippets.

View FlexMonkey's full-sized avatar
🙂
Personal repositories are no longer maintained!

simon gladman FlexMonkey

🙂
Personal repositories are no longer maintained!
View GitHub Profile
@FlexMonkey
FlexMonkey / gist:e4c004b5a7a2897f6435
Created January 12, 2016 15:51
aspectFitInRect() - returns biggest rect of current aspect ratio that will fit into target
extension CGRect
{
func aspectFitInRect(target target: CGRect) -> CGRect
{
let scale: CGFloat =
{
let scale = target.width / self.width
return self.height * scale <= target.height ?
scale :
@FlexMonkey
FlexMonkey / gist:1cb50efd7427c37298af
Created January 13, 2016 13:51
interpolatePointsWithHermite() - adds a Hermite interpolated curve to a UIBezierPath()
extension UIBezierPath
{
func interpolatePointsWithHermite(interpolationPoints : [CGPoint])
{
let n = interpolationPoints.count - 1
for var ii = 0; ii < n; ++ii
{
var currentPoint = interpolationPoints[ii]
@FlexMonkey
FlexMonkey / gist:3b500550fe51fa659d88
Created January 13, 2016 13:52
distanceTo() - extension to CGPoint returns distance between two points
extension CGPoint
{
func distanceTo(point: CGPoint) -> CGFloat
{
return hypot(self.x - point.x, self.y - point.y)
}
}
@FlexMonkey
FlexMonkey / gist:a18b0509ce8d2068457e
Created January 14, 2016 05:47
CoreImage Theshold filter based on custom CIColorKernel
class ThresholdFilter: CIFilter
{
var inputImage : CIImage?
var threshold: CGFloat = 0.75
let thresholdKernel = CIColorKernel(string:
"kernel vec4 thresholdFilter(__sample image, float threshold)" +
"{" +
" float luma = (image.r * 0.2126) + (image.g * 0.7152) + (image.b * 0.0722);" +
@FlexMonkey
FlexMonkey / gist:7b66b0df7ab866dbc754
Created January 14, 2016 08:43
Handy UIColor extensions: multiply(), getRGBA() and getHex()
import UIKit
typealias RGBA = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
extension UIColor
{
func multiply(value: CGFloat) -> UIColor
{
let newRed = getRGBA().red * value
let newGreen = getRGBA().green * value
@FlexMonkey
FlexMonkey / gist:c73083f6cb3abe27a616
Created January 20, 2016 12:03
CIVector extension to convert to array of CGFloat
extension CIVector
{
func toArray() -> [CGFloat]
{
var returnArray = [CGFloat]()
for i in 0 ..< self.count
{
returnArray.append(self.valueAtIndex(i))
}
@FlexMonkey
FlexMonkey / gist:f108cadba2a128dcb514
Last active January 22, 2016 05:32
CGRect extension gets and sets a rectangle's corners
extension CGRect
{
/// Return corners in order of top left, top right, bottom left, bottom right
var corners: [CGPoint]
{
return [
CGPoint(x: minX, y: minY),
CGPoint(x: maxX, y: minY),
CGPoint(x: minX, y: maxY),
@FlexMonkey
FlexMonkey / gist:c4c77c9562cf252a5660
Last active January 29, 2016 16:55
Compares two images to see if they have identical content
func UIImageEqualToImage(image1: UIImage, _ image2: UIImage, ciContext: CIContext? = nil) -> Bool
{
guard let
ciImage1 = CIImage(image: image1),
ciImage2 = CIImage(image: image2) where image1.size == image2.size else
{
return false
}
let ctx = ciContext ?? CIContext()
@FlexMonkey
FlexMonkey / gist:d2b1cbd7e1b2caf8d31b
Last active January 28, 2016 17:35
Synchronously create a UIImage from an UIImagePickerControllerReferenceURL
// Could benefit from some defensive coding :)
import Photos
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
let fetchResult = PHAsset.fetchAssetsWithALAssetURLs([url], options: nil)
let asset = fetchResult.firstObject as! PHAsset
@FlexMonkey
FlexMonkey / gist:8f3b5678bf52098f371a
Created January 31, 2016 06:32
CGFloat extension - saturate() and clamped smootherStep()
extension CGFloat
{
func saturate() -> CGFloat
{
return self < 0 ? 0 : self > 1 ? 1 : self
}
func smootherStep() -> CGFloat
{
let x = self.saturate()