Forked from craiggrummitt/SKTexture gradient extension
Last active
October 6, 2016 20:42
-
-
Save RayHughes/3162fcbda6439893b539 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 | |
- Create a linear gradient texture for a SKShapeNode and SKSpriteNode | |
- UIColor from hex value | |
- Get (n)th character of a string | |
- Convert string to float | |
Based on Extensions.swift by Craig Grummit (https://gist.github.com/craiggrummitt/ad855e358004b5480960) | |
Swift 2 Compatability by Ray Hughes (https://gist.github.com/RayHughes/3162fcbda6439893b539) | |
*/ | |
import SpriteKit | |
import UIKit | |
enum GradientDirection { | |
case Up | |
case Left | |
case UpLeft | |
case UpRight | |
} | |
extension SKTexture { | |
/** | |
Create a linear gradient texture for a SKShapeNode and SKSpriteNode | |
**Inspired by:** https://gist.github.com/Tantas/7fc01803d6b559da48d6 | |
Usage | |
===== | |
Creating Texture | |
----------------- | |
**Note: Make sure to define params** | |
var imageTexture = SKTexture(size: size, color1: color1, color2: color2, direction: GradientDirection.Up) | |
Applying Texture to SKSpriteNode | |
-------------------------------- | |
var nodeElement = SKSpriteNode(texture: imageTexture) | |
Compact Usage | |
------------- | |
var nodeElement = SKSpriteNode(texture: SKTexture(size: size, color1: color1, color2: color2, direction: GradientDirection.Up)) | |
- Parameters: | |
- color1: (CIColor) Gradient starting color | |
- color2: (CIColor) Gradient ending color | |
- direction: (enum) GradientDirection | |
- .Up: Bottom to top gradient | |
- .Left: Right to left gradient | |
- .Upleft Bottom right to upward left | |
- .UpRight: Bottom left to upard right | |
- Returns: (CGImage) Image created from gradient image | |
*/ | |
convenience init(size:CGSize,color1:CIColor,color2:CIColor,direction:GradientDirection = .Up) { | |
let coreImageContext = CIContext(options: nil) | |
let gradientFilter = CIFilter(name: "CILinearGradient") | |
var startVector:CIVector | |
var endVector:CIVector | |
gradientFilter!.setDefaults() | |
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 { | |
/** | |
Get (n)th character of a string | |
**Credit:** http://stackoverflow.com/a/29218836/3220708 | |
Usage | |
===== | |
let hexColor = UIColor(hex: "#00FF00") | |
- Parameters: | |
- hex: (string) | |
- Returns: (string) (n)th character of string | |
*/ | |
convenience init(var hex: String) { | |
var alpha: Float = 100 | |
let hexLength = hex.characters.count | |
if !(hexLength == 7 || hexLength == 9) { | |
// A hex must be either 7 or 9 characters (#GGRRBBAA) | |
print("improper call to 'colorFromHex', hex length must be 7 or 9 chars (#GGRRBBAA)") | |
self.init(white: 0, alpha: 1) | |
return | |
} | |
if hexLength == 9 { | |
let temp = hex[7...8] | |
alpha = temp.floatValue | |
hex = hex[0...6] | |
} | |
// Establishing the rgb color | |
var rgb: UInt32 = 0 | |
let 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 { | |
/** | |
Get (n)th character of a string | |
**Credit:** http://stackoverflow.com/questions/24092884/get-nth-character-of-a-string-in-swift-programming-language/24144365#24144365 | |
- Parameters: | |
- Range<Int> | |
- Returns: (string) (n)th character of string | |
*/ | |
subscript (r: Range<Int>) -> String { | |
get { | |
let startIndex = self.startIndex.advancedBy(r.startIndex) | |
let endIndex = self.startIndex.advancedBy(r.endIndex - r.startIndex) | |
return self[Range(start: startIndex, end: endIndex)] | |
} | |
} | |
/** | |
Convert convert string to float. | |
**Credit:** http://stackoverflow.com/questions/24085665/convert-string-to-float-in-apples-swift/24088249#24088249 | |
Usage | |
===== | |
string.floatValue | |
- Parameters: | |
- string | |
- Returns: (float) (n)th character of string | |
*/ | |
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