Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a UIColor category

  • Loading branch information...
commit 2a0b9241c4c8ac72e10296d6c8e29ce424ff2c37 1 parent 3d3cb5c
Cory Imdieke authored
Showing with 69 additions and 0 deletions.
  1. +16 −0 Example/UIColor+KGNoise.h
  2. +53 −0 Example/UIColor+KGNoise.m
View
16 Example/UIColor+KGNoise.h
@@ -0,0 +1,16 @@
+//
+// UIColor+KGNoise.h
+// KGNoiseExample
+//
+// Created by Cory Imdieke on 9/14/12.
+// Copyright (c) 2012 BitSuites, LLC. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface UIColor (KGNoise)
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity;
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode;
+
+@end
View
53 Example/UIColor+KGNoise.m
@@ -0,0 +1,53 @@
+//
+// UIColor+KGNoise.m
+// KGNoiseExample
+//
+// Created by Cory Imdieke on 9/14/12.
+// Copyright (c) 2012 BitSuites, LLC. All rights reserved.
+//
+
+#import "UIColor+KGNoise.h"
+#import "KGNoise.h"
+
+@implementation UIColor (KGNoise)
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity{
+ return [self colorWithNoiseOpacity:opacity andBlendMode:kCGBlendModeScreen];
+}
+
+- (UIColor *)colorWithNoiseOpacity:(CGFloat)opacity andBlendMode:(CGBlendMode)blendMode{
+ // Create a context to draw in
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(NULL,
+ 256.0,
+ 256.0,
+ 8, /* bits per channel */
+ (256.0 * 4), /* 4 channels per pixel * numPixels/row */
+ colorSpace,
+ kCGImageAlphaPremultipliedLast);
+ CGColorSpaceRelease(colorSpace);
+
+ UIGraphicsPushContext(context);
+
+ // Fill with the color
+ CGContextSetFillColorWithColor(context, [self CGColor]);
+ CGContextFillRect(context, CGRectMake(0.0, 0.0, 256.0, 256.0));
+
+ // Noise on top
+ [KGNoise drawNoiseWithOpacity:opacity andBlendMode:blendMode];
+
+ // Create a CGImage from the context
+ CGImageRef rawImage = CGBitmapContextCreateImage(context);
+ UIGraphicsPopContext();
+ CGContextRelease(context);
+
+ // Create a UIImage from the CGImage
+ UIImage *finishedImage = [UIImage imageWithCGImage:rawImage];
+ CGImageRelease(rawImage);
+
+ UIColor *patternColor = [UIColor colorWithPatternImage:finishedImage];
+
+ return patternColor;
+}
+
+@end
Please sign in to comment.
Something went wrong with that request. Please try again.