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: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: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: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: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: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: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 :