Last active
April 29, 2019 09:47
-
-
Save craiggrummitt/ad855e358004b5480960 to your computer and use it in GitHub Desktop.
SKTexture gradient extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Extensions.swift | |
// WordSnatch | |
// | |
// Created by Craig on 24/03/2015. | |
// Copyright (c) 2015 Interactive Coconut. All rights reserved. | |
// | |
import SpriteKit | |
import UIKit | |
//inspired by https://gist.github.com/Tantas/7fc01803d6b559da48d6 | |
enum GradientDirection { | |
case up | |
case left | |
case upLeft | |
case upRight | |
} | |
extension SKTexture { | |
convenience init(size:CGSize,color1:CIColor,color2:CIColor,direction:GradientDirection = .up) { | |
let coreImageContext = CIContext(options: nil) | |
let gradientFilter = CIFilter(name: "CILinearGradient") | |
gradientFilter.setDefaults() | |
var startVector:CIVector | |
var endVector:CIVector | |
switch direction { | |
case .up: | |
startVector = CIVector(x: size.width/2, y: 0) | |
endVector = CIVector(x: size.width/2, y: size.height) | |
case .left: | |
startVector = CIVector(x: size.width, y: size.height/2) | |
endVector = CIVector(x: 0, y: size.height/2) | |
case .upLeft: | |
startVector = CIVector(x: size.width, y: 0) | |
endVector = CIVector(x: 0, y: size.height) | |
case .upRight: | |
startVector = CIVector(x: 0, y: 0) | |
endVector = CIVector(x: size.width, y: size.height) | |
} | |
gradientFilter.setValue(startVector, forKey: "inputPoint0") | |
gradientFilter.setValue(endVector, forKey: "inputPoint1") | |
gradientFilter.setValue(color1, forKey: "inputColor0") | |
gradientFilter.setValue(color2, forKey: "inputColor1") | |
let cgimg = coreImageContext.createCGImage(gradientFilter.outputImage, fromRect: CGRect(x: 0, y: 0, width: size.width, height: size.height)) | |
self.init(CGImage:cgimg) | |
} | |
} | |
extension UIColor { | |
//credit: http://stackoverflow.com/a/29218836/3220708 | |
convenience init(var hex: String) { | |
var alpha: Float = 100 | |
let hexLength = countElements(hex) | |
if !(hexLength == 7 || hexLength == 9) { | |
// A hex must be either 7 or 9 characters (#GGRRBBAA) | |
println("improper call to 'colorFromHex', hex length must be 7 or 9 chars (#GGRRBBAA)") | |
self.init(white: 0, alpha: 1) | |
return | |
} | |
if hexLength == 9 { | |
var temp = hex[7...8] | |
alpha = temp.floatValue | |
hex = hex[0...6] | |
} | |
// Establishing the rgb color | |
var rgb: UInt32 = 0 | |
var s: NSScanner = NSScanner(string: hex) | |
// Setting the scan location to ignore the leading `#` | |
s.scanLocation = 1 | |
// Scanning the int into the rgb colors | |
s.scanHexInt(&rgb) | |
// Creating the UIColor from hex int | |
self.init( | |
red: CGFloat((rgb & 0xFF0000) >> 16) / 255.0, | |
green: CGFloat((rgb & 0x00FF00) >> 8) / 255.0, | |
blue: CGFloat(rgb & 0x0000FF) / 255.0, | |
alpha: CGFloat(alpha / 100) | |
) | |
} | |
} | |
extension String { | |
//credit: http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language/24144365#24144365 | |
subscript (r: Range<Int>) -> String { | |
get { | |
let startIndex = advance(self.startIndex, r.startIndex) | |
let endIndex = advance(startIndex, r.endIndex - r.startIndex) | |
return self[Range(start: startIndex, end: endIndex)] | |
} | |
} | |
//credit: http://stackoverflow.com/questions/24085665/convert-string-to-float-in-apples-swift/24088249#24088249 | |
var floatValue: Float { | |
return (self as NSString).floatValue | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Compatibility for Swift 2 with Doc Blocks: https://gist.github.com/RayHughes/3162fcbda6439893b539