Skip to content

Instantly share code, notes, and snippets.

@alessaba
Created December 31, 2017 13:36
Show Gist options
  • Save alessaba/e9770c7f625df3427cf118213b800a0e to your computer and use it in GitHub Desktop.
Save alessaba/e9770c7f625df3427cf118213b800a0e to your computer and use it in GitHub Desktop.
P3Comparison
import UIKit
import Foundation
import PlaygroundSupport
UIColor(red: 1.2, green: -0.75, blue: 2, alpha: 1) // Using the extended sRGB color values introduced in iOS 10 to display a wider gamut (P3 in this case)
extension UIColor {
convenience init(displayP3Hue hue: CGFloat, saturation: CGFloat, brightness: CGFloat, alpha: CGFloat = 1) {
/// HSB to RGB conversion doesn’t depend on color space, so we can use default UIColor space.
let converter = UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
var red: CGFloat = 0, green: CGFloat = 0, blue: CGFloat = 0
converter.getRed(&red, green: &green, blue: &blue, alpha: nil)
// Playground debug
red.description
green.description
blue.description
self.init(displayP3Red: red, green: green, blue: blue, alpha: alpha)
}
}
class ColorContraption: UIViewController{
var i : CGFloat = 0
var view1 : UIView = UIView()
var view2 : UIView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view1.frame = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height/2)
view2.frame = CGRect(x: 0, y: self.view1.bounds.height, width: self.view.bounds.width, height: self.view.bounds.height/2)
view.addSubview(view1)
view.addSubview(view2)
self.animateBackgroundColour()
}
func animateBackgroundColour () {
if i < 1 {
i += 1/256
} else {
i = 0
}
UIView.animate(withDuration: 0.05, delay: 0, options: .allowUserInteraction, animations: { () -> Void in
self.view1.backgroundColor = UIColor(displayP3Hue: self.i, saturation: 1, brightness: 1)
self.view2.backgroundColor = UIColor(hue: self.i, saturation: 1, brightness: 1, alpha: 1)
}) {(Bool) -> Void in
self.animateBackgroundColour();
}
}
}
PlaygroundPage.current.liveView = ColorContraption()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment