3 ms·
Like! Much as I love ObjC, reducing verbosity via better syntax can go a long way to making code more readable. For example, I ended up putting together some d
by dennis_vartan 13y ago
Like! Much as I love ObjC, reducing verbosity via better syntax can go a long way to making code more readable.
For example, I ended up putting together some defines like this after building a bunch of UI programmatically.
#define RGB(r, g, b) [NSColor colorWithDeviceRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:1]
#define RGBA(r, g, b, a) [NSColor colorWithDeviceRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)]
RGB(255, 104, 0) may be a bit too C-ish, but it sure beats the long form. Have similar GRAY(x) and GRAYA(x) defines for producing grayscale colors.
Definitely interested to see more projects along these lines.
- cheesylard 13y ago#define RGB(r,g,b) RGBA(r,g,b,1)
- wrl 13y agoI prefer: #define RGB(color) \ (((color & 0xFF0000) >> 16) / 255.f), \ (((color & 0x00FF00) >> 8) / 255.f), \ (((color & 0x0000FF)) / 255.f) Pass in an HTML-style color hex triplet.
- stefan_kendall 13y agoI use the same thing to construct NSDecimalNumber instances. N(100.5) instead of [NSDecimalNumber decimalNumberFromString:@"100.5"] SO handy.
- bennyg 13y agoYou can do this now @(100.5) for NSNumber literals.
- dennis_vartan 13y agoAnd even shorter, just @100.5 if what you have is a literal. The parentheses are needed if you're boxing a value, e.g.: @(price + 100.5) In detail: http://clang.llvm.org/docs/ObjectiveCLiterals.html http://clang.llvm.org/docs/ObjectiveCLiterals.html
- numo16 13y agoI use a few like this in some of my helper macros[0] that I usually pull into my iOS projects. [0]: https://gist.github.com/numo16/3407652 https://gist.github.com/numo16/3407652
- bennyg 13y agoI wrote a utility class with idiomatic Obj-C color methods like the one you mentioned and more. I hate to plug it, but I'm fairly proud of it. It also generates color schemes when you throw a UIColor object at it, and includes 100 different UIColor generics like [UIColor seafoamColor]. https://github.com/bennyguitar/Colours-for-iOS https://github.com/bennyguitar/Colours-for-iOS
- reginaldjcooper 13y agoWhy on Earth would you hate to plug this? I could definitely see someone using this and being thankful they don't have to do it themselves. Hell, I'd use it if I hadn't already done my own category. Suggestions: you might want to allow for three-digit hex strings and eight/four digit hex strings with alpha, I found that convenient in mine. And in the documentation it would be way cool if you made the names of the colors self-referential. Nits: I would have made all the system color methods like "systemThingColor", capitalized RGB and HSB, and made "RGBDictionary" return @{@"red": ...} (and made static NSStrings for the keys so nobody needs to go to the .m file to see what will happen). Obviously I am a pedant and I live right on the edge of the 80 character limit; of these I'd only suggest making static dictionary keys as an actually desirable change. Nice work, you shouldn't be ashamed to share it at all.
- khangtoh 13y agolooks really nice, looking forward to using it!