bennyguitar / Colours

A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.
MIT License
3.09k stars 300 forks source link

Swift Version? #24

Closed klaas closed 9 years ago

klaas commented 9 years ago

Has anybody thought of o Swift version?

(I using it right now in Swift via bridging headers, but a native Swift version would be nicer 😉)

bennyguitar commented 9 years ago

Yeah I was working on a version a while ago. In fact, if you check the branches, you should see a swift one with method stubs already in it. :)

The biggest stumbling block I came across was looking for a way to have one swift file with implementations for both UIColor and NSColor. It's been a while since I've taken a peek or a stab at making this transition, but I do remember that being a sticking point. Right now, there's just one Colours.{h,m} and I'd like to keep that simplicity (change one method, and it automatically works for the other platform).

I do agree - a swift version would be nicer. Things like UIColor(fromHexString: String) would be better as UIColor(hexString: String) and so on and so forth.

klaas commented 9 years ago

There was a talk at WWDC 2014 about sharing code for OS X and iOS (Session 233). They don't recommend 'shimming' in Swift (At least for UIView and NSView). But they don't specifically say how to solve this kind of problem.

screen shot 2014-09-22 at 16 28 09

There is a question and a good answer on stackoverflow regarding the same issue: http://stackoverflow.com/a/24403905/292145

Using a typealias seems pretty neat.

I'll have a look at the swift branch later this week. Thanks!

bennyguitar commented 9 years ago

Nice, thanks for that screenshot and link. It's been a minute since I've looked at writing the Swift version hence my old-school (heh) use of Array<Double> instead of [Double] in the implementation haha. I almost had it too - just needed to put the class declaration inside of the if/else compiler directives.

hartbit commented 9 years ago

I believe that the best advantage of using Swift, would be to transform the rgbaArray/hsbaArray/rgbaDictionary/hsbaDictionary and colorFromRGBAArray/colorFromRGBADictionary/colorFromHSBAArray/colorFromHSBADictionary to use tuples instead:

Here's the code for replacing the RGBA methods:

extension UIColor {
    convenience init(rgba: (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat)) {
        self.init(red: rgba.r, green: rgba.g, blue: rgba.b, alpha: rgba.a)
    }

    func rgba() -> (r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0

        if self.respondsToSelector("getRed:green:blue:alpha:") {
            self.getRed(&r, green: &g, blue: &b, alpha: &a)
        } else {
            let components = CGColorGetComponents(self.CGColor)
            r = components[0]
            g = components[1]
            b = components[2]
            a = components[3]
        }

        return (r, g, b, a)
    }
}

var rgba = UIColor.redColor().rgba()
rgba.g = 1
let newColor = UIColor(rgba: rgba)
bennyguitar commented 9 years ago

I like this idea a lot, and I may try some refactoring this weekend to get a pure Swift implementation working. Harnessing the unique things about Swift is what I'd like to do if I'm going to rewrite it for that language.

bennyguitar commented 9 years ago

Swift version is coming along! It's still missing some methods, but it's more or less in working order. I'm going to close this now, and let specific swift issues have new threads here. Sorry it took so long to get started and somewhere on it, but now procrastination has finally lost.