Skip to content

Commit

Permalink
Adding color categories: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
kgn committed Nov 9, 2012
1 parent 963d03c commit 8859b94
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
14 changes: 14 additions & 0 deletions KGNoise.h
Expand Up @@ -22,6 +22,20 @@

@end

#pragma mark - KGNoise Color

#if TARGET_OS_IPHONE
@interface UIColor(KGNoise)
- (UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity;
- (UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
@end
#else
@interface NSColor(KGNoise)
- (NSColor *)colorWithNoiseWithOpacity:(CGFloat)opacity;
- (NSColor *)colorWithNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
@end
#endif

#pragma mark - KGNoiseView

#if TARGET_OS_IPHONE
Expand Down
40 changes: 39 additions & 1 deletion KGNoise.m
Expand Up @@ -8,6 +8,8 @@

#import "KGNoise.h"

static NSUInteger const kImageSize = 128;

#if TARGET_OS_IPHONE
CGFloat *gradientComponentsForColors(UIColor *color1, UIColor *color2){
#else
Expand Down Expand Up @@ -54,7 +56,7 @@ + (void)drawNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMod
static CGImageRef noiseImageRef = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
NSUInteger width = 128, height = width;
NSUInteger width = kImageSize, height = width;
NSUInteger size = width*height;
char *rgba = (char *)malloc(size); srand(115);
for(NSUInteger i=0; i < size; ++i){rgba[i] = rand()%256;}
Expand Down Expand Up @@ -96,6 +98,42 @@ + (void)drawNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMod

@end

#pragma mark - KGNoise Color

#if TARGET_OS_IPHONE
@implementation UIColor(KGNoise)
- (UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity{
return [self colorWithNoiseWithOpacity:opacity andBlendMode:kCGBlendModeScreen];
}
- (UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
CGRect rect = {CGPointZero, kImageSize, kImageSize};
UIGraphicsBeginImageContextWithOptions(rect.size, YES, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
[self setFill]; CGContextFillRect(context, rect);
[KGNoise drawNoiseWithOpacity:opacity andBlendMode:blendMode];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return [UIColor colorWithPatternImage:image];
}
@end
#else
@implementation NSColor(KGNoise)
- (NSColor *)colorWithNoiseWithOpacity:(CGFloat)opacity{
return [self colorWithNoiseWithOpacity:opacity andBlendMode:kCGBlendModeScreen];
}
- (NSColor *)colorWithNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
CGRect rect = {CGPointZero, kImageSize, kImageSize};
NSImage *image = [[NSImage alloc] initWithSize:rect.size];
[image lockFocus];
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
[self setFill]; CGContextFillRect(context, rect);
[KGNoise drawNoiseWithOpacity:opacity andBlendMode:blendMode];
[image unlockFocus];
return [NSColor colorWithPatternImage:image];
}
@end
#endif

#pragma mark - KGNoiseView

@implementation KGNoiseView
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Expand Up @@ -25,6 +25,13 @@ KGNoise is distributed under the MIT license, see the license file for more info
+ (void)drawNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
```

# UIColor/NSColor(KGNoise)

```
- (NSColor/UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity;
- (NSColor/UIColor *)colorWithNoiseWithOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
```

# KGNoiseView

There is also a subclass of `NSView` or `UIView`, depending on your platform, that you can use out of the box to draw noise on a solid color. The noise opacity, blending mode, and background color are all customizable.
Expand Down

0 comments on commit 8859b94

Please sign in to comment.