Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding color categories: #2

  • Loading branch information...
commit 8859b94f2d951afd0d977274c94ccc60edbfb50c 1 parent 963d03c
David Keegan authored
Showing with 60 additions and 1 deletion.
  1. +14 −0 KGNoise.h
  2. +39 −1 KGNoise.m
  3. +7 −0 readme.md
14 KGNoise.h
View
@@ -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
40 KGNoise.m
View
@@ -8,6 +8,8 @@
#import "KGNoise.h"
+static NSUInteger const kImageSize = 128;
+
#if TARGET_OS_IPHONE
CGFloat *gradientComponentsForColors(UIColor *color1, UIColor *color2){
#else
@@ -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;}
@@ -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
7 readme.md
View
@@ -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.
Please sign in to comment.
Something went wrong with that request. Please try again.