Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge remote-tracking branch 'upstream/master'

* upstream/master:
  Adding color categories: #2
  improved the radial gradient radius code based on @marcosanson pull request
  updating screenshot
  renaming screenshot and updating readme
  moving the default image into the iOS group
  adding KGNoiseLinearGradientView and KGNoiseRadialGradientView closes #5
  adding Default-568h@2x.png
  Minor edit to readme.md
  • Loading branch information...
commit 2fe5e3f0d488b4b68c3cd3bb840ca538c845c973 2 parents 273b9c9 + 8859b94
@rastersize authored
View
BIN  Example/Default-568h@2x.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
27 Example/KGIOSAppDelegate.m
@@ -15,12 +15,31 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = [[UIViewController alloc] init];
- KGNoiseView *noiseView = [[KGNoiseView alloc] initWithFrame:self.window.rootViewController.view.bounds];
- noiseView.backgroundColor = [UIColor darkGrayColor];
- noiseView.noiseBlendMode = kCGBlendModeMultiply;
- noiseView.noiseOpacity = 0.1;
+ KGNoiseRadialGradientView *noiseView = [[KGNoiseRadialGradientView alloc] initWithFrame:self.window.rootViewController.view.bounds];
+ noiseView.backgroundColor = [UIColor colorWithRed:0.814 green:0.798 blue:0.747 alpha:1.000];
+ noiseView.alternateBackgroundColor = [UIColor colorWithRed:1.000 green:0.986 blue:0.945 alpha:1.000];
+ noiseView.noiseOpacity = 0.3;
[self.window.rootViewController.view addSubview:noiseView];
+ CGRect noiseToolbarViewRect = self.window.rootViewController.view.bounds;
+ noiseToolbarViewRect.size.height = 48;
+ noiseToolbarViewRect.origin.y = CGRectGetHeight(self.window.rootViewController.view.bounds)-CGRectGetHeight(noiseToolbarViewRect);
+ KGNoiseLinearGradientView *noiseToolbarView = [[KGNoiseLinearGradientView alloc] initWithFrame:noiseToolbarViewRect];
+ noiseToolbarView.backgroundColor = [UIColor colorWithWhite:0.102 alpha:1.000];
+ noiseToolbarView.alternateBackgroundColor = [UIColor colorWithWhite:0.250 alpha:1.000];
+ noiseToolbarView.noiseBlendMode = kCGBlendModeMultiply;
+ noiseToolbarView.noiseOpacity = 0.2;
+ [self.window.rootViewController.view addSubview:noiseToolbarView];
+
+ CGRect noiseNavbarViewRect = self.window.rootViewController.view.bounds;
+ noiseNavbarViewRect.size.height = 48;
+ KGNoiseLinearGradientView *noiseNavbarView = [[KGNoiseLinearGradientView alloc] initWithFrame:noiseNavbarViewRect];
+ noiseNavbarView.backgroundColor = [UIColor colorWithRed:0.307 green:0.455 blue:0.909 alpha:1.000];
+ noiseNavbarView.alternateBackgroundColor = [UIColor colorWithRed:0.363 green:0.700 blue:0.909 alpha:1.000];
+ noiseNavbarView.noiseBlendMode = kCGBlendModeMultiply;
+ noiseNavbarView.noiseOpacity = 0.08;
+ [self.window.rootViewController.view addSubview:noiseNavbarView];
+
[self.window makeKeyAndVisible];
return YES;
}
View
4 Example/KGMacAppDelegate.h
@@ -12,7 +12,7 @@
@interface KGMacAppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
-@property (weak) IBOutlet KGNoiseView *viewLeft;
-@property (weak) IBOutlet KGNoiseView *viewRight;
+@property (weak) IBOutlet KGNoiseLinearGradientView *viewLeft;
+@property (weak) IBOutlet KGNoiseRadialGradientView *viewRight;
@end
View
6 Example/KGMacAppDelegate.m
@@ -11,11 +11,13 @@
@implementation KGMacAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
- self.viewLeft.backgroundColor = [NSColor darkGrayColor];
+ self.viewLeft.backgroundColor = [NSColor colorWithCalibratedRed:0.363 green:0.700 blue:0.909 alpha:1.000];
+ self.viewLeft.alternateBackgroundColor = [NSColor colorWithCalibratedRed:0.307 green:0.455 blue:0.909 alpha:1.000];
self.viewLeft.noiseBlendMode = kCGBlendModeMultiply;
self.viewLeft.noiseOpacity = 0.1;
- self.viewRight.backgroundColor = [NSColor lightGrayColor];
+ self.viewRight.backgroundColor = [NSColor colorWithCalibratedWhite:0.500 alpha:1.000];
+ self.viewRight.alternateBackgroundColor = [NSColor colorWithCalibratedWhite:0.750 alpha:1.000];
self.viewRight.noiseOpacity = 0.2;
}
View
28 Example/KGNoiseExample.xcodeproj/project.pbxproj
@@ -7,9 +7,10 @@
objects = {
/* Begin PBXBuildFile section */
- 7324C77A16006DD4000A0863 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7324C77916006DD4000A0863 /* UIKit.framework */; };
- 7324C77C16006DDF000A0863 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7324C77B16006DDF000A0863 /* Foundation.framework */; };
- 7324C77E16006DFC000A0863 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 7324C77D16006DFC000A0863 /* CoreGraphics.framework */; };
+ 73279ECB1631D03A004B8A68 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73279ECA1631D03A004B8A68 /* CoreGraphics.framework */; };
+ 73279ECD1631D044004B8A68 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73279ECC1631D044004B8A68 /* UIKit.framework */; };
+ 73279ECF1631D051004B8A68 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73279ECE1631D051004B8A68 /* Foundation.framework */; };
+ 73B7BF08161FA49400024C46 /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 73B7BF07161FA49400024C46 /* Default-568h@2x.png */; };
73CF1C1B1600604E006D9180 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 73CF1C1A1600604E006D9180 /* Cocoa.framework */; };
73CF1C4016006116006D9180 /* KGMacAppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CF1C3E16006116006D9180 /* KGMacAppDelegate.m */; };
73CF1C4116006116006D9180 /* MacMain.m in Sources */ = {isa = PBXBuildFile; fileRef = 73CF1C3F16006116006D9180 /* MacMain.m */; };
@@ -21,9 +22,10 @@
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
- 7324C77916006DD4000A0863 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
- 7324C77B16006DDF000A0863 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
- 7324C77D16006DFC000A0863 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
+ 73279ECA1631D03A004B8A68 /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
+ 73279ECC1631D044004B8A68 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
+ 73279ECE1631D051004B8A68 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.0.sdk/System/Library/Frameworks/Foundation.framework; sourceTree = DEVELOPER_DIR; };
+ 73B7BF07161FA49400024C46 /* Default-568h@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default-568h@2x.png"; sourceTree = "<group>"; };
73CF1C161600604E006D9180 /* KGMacNoiseExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KGMacNoiseExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
73CF1C1A1600604E006D9180 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
73CF1C1D1600604E006D9180 /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
@@ -53,9 +55,9 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
- 7324C77E16006DFC000A0863 /* CoreGraphics.framework in Frameworks */,
- 7324C77C16006DDF000A0863 /* Foundation.framework in Frameworks */,
- 7324C77A16006DD4000A0863 /* UIKit.framework in Frameworks */,
+ 73279ECF1631D051004B8A68 /* Foundation.framework in Frameworks */,
+ 73279ECD1631D044004B8A68 /* UIKit.framework in Frameworks */,
+ 73279ECB1631D03A004B8A68 /* CoreGraphics.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -107,9 +109,9 @@
73CF1C5C160068AD006D9180 /* Frameworks */ = {
isa = PBXGroup;
children = (
- 7324C77D16006DFC000A0863 /* CoreGraphics.framework */,
- 7324C77B16006DDF000A0863 /* Foundation.framework */,
- 7324C77916006DD4000A0863 /* UIKit.framework */,
+ 73279ECE1631D051004B8A68 /* Foundation.framework */,
+ 73279ECC1631D044004B8A68 /* UIKit.framework */,
+ 73279ECA1631D03A004B8A68 /* CoreGraphics.framework */,
);
name = Frameworks;
sourceTree = "<group>";
@@ -120,6 +122,7 @@
73CF1C7C160069A1006D9180 /* iOSMain.m */,
73CF1C7D160069A1006D9180 /* KGIOSAppDelegate.h */,
73CF1C7E160069A1006D9180 /* KGIOSAppDelegate.m */,
+ 73B7BF07161FA49400024C46 /* Default-568h@2x.png */,
73CF1C5C160068AD006D9180 /* Frameworks */,
);
name = "iOS Example";
@@ -203,6 +206,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
+ 73B7BF08161FA49400024C46 /* Default-568h@2x.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
102 Example/MainMenu.xib
@@ -2,13 +2,13 @@
<archive type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="8.00">
<data>
<int key="IBDocument.SystemTarget">1080</int>
- <string key="IBDocument.SystemVersion">12B19</string>
- <string key="IBDocument.InterfaceBuilderVersion">2549</string>
- <string key="IBDocument.AppKitVersion">1187</string>
- <string key="IBDocument.HIToolboxVersion">624.00</string>
+ <string key="IBDocument.SystemVersion">12C60</string>
+ <string key="IBDocument.InterfaceBuilderVersion">2843</string>
+ <string key="IBDocument.AppKitVersion">1187.34</string>
+ <string key="IBDocument.HIToolboxVersion">625.00</string>
<object class="NSMutableDictionary" key="IBDocument.PluginVersions">
<string key="NS.key.0">com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string key="NS.object.0">2549</string>
+ <string key="NS.object.0">2843</string>
</object>
<array key="IBDocument.IntegratedClassDependencies">
<string>IBNSLayoutConstraint</string>
@@ -1302,7 +1302,7 @@
<nil key="NSViewClass"/>
<nil key="NSUserInterfaceItemIdentifier"/>
<object class="NSView" key="NSWindowView" id="439893737">
- <nil key="NSNextResponder"/>
+ <reference key="NSNextResponder"/>
<int key="NSvFlags">256</int>
<array class="NSMutableArray" key="NSSubviews">
<object class="NSSplitView" id="599285279">
@@ -1314,21 +1314,25 @@
<int key="NSvFlags">256</int>
<string key="NSFrameSize">{121, 360}</string>
<reference key="NSSuperview" ref="599285279"/>
+ <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="712342299"/>
<string key="NSReuseIdentifierKey">_NS:11</string>
- <string key="NSClassName">KGNoiseView</string>
+ <string key="NSClassName">KGNoiseLinearGradientView</string>
</object>
<object class="NSCustomView" id="712342299">
<reference key="NSNextResponder" ref="599285279"/>
<int key="NSvFlags">256</int>
<string key="NSFrame">{{122, 0}, {358, 360}}</string>
<reference key="NSSuperview" ref="599285279"/>
+ <reference key="NSWindow"/>
+ <reference key="NSNextKeyView"/>
<string key="NSReuseIdentifierKey">_NS:13</string>
- <string key="NSClassName">KGNoiseView</string>
+ <string key="NSClassName">KGNoiseRadialGradientView</string>
</object>
</array>
<string key="NSFrameSize">{480, 360}</string>
<reference key="NSSuperview" ref="439893737"/>
+ <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="886914388"/>
<string key="NSReuseIdentifierKey">_NS:9</string>
<bool key="NSIsVertical">YES</bool>
@@ -1336,6 +1340,8 @@
</object>
</array>
<string key="NSFrameSize">{480, 360}</string>
+ <reference key="NSSuperview"/>
+ <reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="599285279"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1440, 900}}</string>
@@ -3341,52 +3347,54 @@
<object class="IBClassDescriber" key="IBDocument.Classes">
<array class="NSMutableArray" key="referencedPartialClassDescriptions">
<object class="IBPartialClassDescription">
- <string key="className">KGNoiseView</string>
- <string key="superclassName">UIView</string>
- <object class="IBClassDescriptionSource" key="sourceIdentifier">
- <string key="majorKey">IBProjectSource</string>
- <string key="minorKey">./Classes/KGNoiseView.h</string>
- </object>
- </object>
- <object class="IBPartialClassDescription">
- <string key="className">NSDocument</string>
- <dictionary class="NSMutableDictionary" key="actions">
- <string key="printDocument:">id</string>
- <string key="revertDocumentToSaved:">id</string>
- <string key="runPageLayout:">id</string>
- <string key="saveDocument:">id</string>
- <string key="saveDocumentAs:">id</string>
- <string key="saveDocumentTo:">id</string>
+ <string key="className">KGMacAppDelegate</string>
+ <string key="superclassName">NSObject</string>
+ <dictionary class="NSMutableDictionary" key="outlets">
+ <string key="viewLeft">KGNoiseRadialGradientView</string>
+ <string key="viewRight">KGNoiseLinearGradientView</string>
+ <string key="window">NSWindow</string>
</dictionary>
- <dictionary class="NSMutableDictionary" key="actionInfosByName">
- <object class="IBActionInfo" key="printDocument:">
- <string key="name">printDocument:</string>
- <string key="candidateClassName">id</string>
- </object>
- <object class="IBActionInfo" key="revertDocumentToSaved:">
- <string key="name">revertDocumentToSaved:</string>
- <string key="candidateClassName">id</string>
- </object>
- <object class="IBActionInfo" key="runPageLayout:">
- <string key="name">runPageLayout:</string>
- <string key="candidateClassName">id</string>
- </object>
- <object class="IBActionInfo" key="saveDocument:">
- <string key="name">saveDocument:</string>
- <string key="candidateClassName">id</string>
+ <dictionary class="NSMutableDictionary" key="toOneOutletInfosByName">
+ <object class="IBToOneOutletInfo" key="viewLeft">
+ <string key="name">viewLeft</string>
+ <string key="candidateClassName">KGNoiseRadialGradientView</string>
</object>
- <object class="IBActionInfo" key="saveDocumentAs:">
- <string key="name">saveDocumentAs:</string>
- <string key="candidateClassName">id</string>
+ <object class="IBToOneOutletInfo" key="viewRight">
+ <string key="name">viewRight</string>
+ <string key="candidateClassName">KGNoiseLinearGradientView</string>
</object>
- <object class="IBActionInfo" key="saveDocumentTo:">
- <string key="name">saveDocumentTo:</string>
- <string key="candidateClassName">id</string>
+ <object class="IBToOneOutletInfo" key="window">
+ <string key="name">window</string>
+ <string key="candidateClassName">NSWindow</string>
</object>
</dictionary>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
- <string key="minorKey">./Classes/NSDocument.h</string>
+ <string key="minorKey">./Classes/KGMacAppDelegate.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
+ <string key="className">KGNoiseLinearGradientView</string>
+ <string key="superclassName">KGNoiseView</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBProjectSource</string>
+ <string key="minorKey">./Classes/KGNoiseLinearGradientView.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
+ <string key="className">KGNoiseRadialGradientView</string>
+ <string key="superclassName">KGNoiseView</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBProjectSource</string>
+ <string key="minorKey">./Classes/KGNoiseRadialGradientView.h</string>
+ </object>
+ </object>
+ <object class="IBPartialClassDescription">
+ <string key="className">KGNoiseView</string>
+ <string key="superclassName">UIView</string>
+ <object class="IBClassDescriptionSource" key="sourceIdentifier">
+ <string key="majorKey">IBProjectSource</string>
+ <string key="minorKey">./Classes/KGNoiseView.h</string>
</object>
</object>
<object class="IBPartialClassDescription">
View
38 KGNoise.h
@@ -13,6 +13,8 @@
#import <Cocoa/Cocoa.h>
#endif
+#pragma mark - KGNoise
+
@interface KGNoise : NSObject
+ (void)drawNoiseWithOpacity:(CGFloat)opacity;
@@ -20,6 +22,22 @@
@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
@interface KGNoiseView : UIView
#else
@@ -29,3 +47,23 @@
@property (nonatomic) CGFloat noiseOpacity;
@property (nonatomic) CGBlendMode noiseBlendMode;
@end
+
+#pragma mark - KGNoiseLinearGradientView
+
+@interface KGNoiseLinearGradientView : KGNoiseView
+#if TARGET_OS_IPHONE
+@property (strong, nonatomic) UIColor *alternateBackgroundColor;
+#else
+@property (strong, nonatomic) NSColor *alternateBackgroundColor;
+#endif
+@end
+
+#pragma mark - KGNoiseRadialGradientView
+
+@interface KGNoiseRadialGradientView : KGNoiseView
+#if TARGET_OS_IPHONE
+@property (strong, nonatomic) UIColor *alternateBackgroundColor;
+#else
+@property (strong, nonatomic) NSColor *alternateBackgroundColor;
+#endif
+@end
View
BIN  KGNoise.jpg
Deleted file not rendered
View
181 KGNoise.m
@@ -8,6 +8,44 @@
#import "KGNoise.h"
+static NSUInteger const kImageSize = 128;
+
+#if TARGET_OS_IPHONE
+CGFloat *gradientComponentsForColors(UIColor *color1, UIColor *color2){
+#else
+CGFloat *gradientComponentsForColors(NSColor *color1, NSColor *color2){
+#endif
+ CGFloat *components = malloc(8*sizeof(CGFloat));
+ const CGFloat *alternateBackgroundComponents = CGColorGetComponents([color1 CGColor]);
+ if(CGColorGetNumberOfComponents([color1 CGColor]) == 2){
+ components[0] = alternateBackgroundComponents[0];
+ components[1] = alternateBackgroundComponents[0];
+ components[2] = alternateBackgroundComponents[0];
+ components[3] = alternateBackgroundComponents[1];
+ }else{
+ components[0] = alternateBackgroundComponents[0];
+ components[1] = alternateBackgroundComponents[1];
+ components[2] = alternateBackgroundComponents[2];
+ components[3] = alternateBackgroundComponents[3];
+ }
+
+ const CGFloat *backgroundComponents = CGColorGetComponents([color2 CGColor]);
+ if(CGColorGetNumberOfComponents([color2 CGColor]) == 2){
+ components[4] = backgroundComponents[0];
+ components[5] = backgroundComponents[0];
+ components[6] = backgroundComponents[0];
+ components[7] = backgroundComponents[1];
+ }else{
+ components[4] = backgroundComponents[0];
+ components[5] = backgroundComponents[1];
+ components[6] = backgroundComponents[2];
+ components[7] = backgroundComponents[3];
+ }
+ return components;
+}
+
+#pragma mark - KGNoise
+
@implementation KGNoise
+ (void)drawNoiseWithOpacity:(CGFloat)opacity{
@@ -18,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] = (char)(rand()%256);}
@@ -60,6 +98,44 @@ + (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
#if TARGET_OS_IPHONE
@@ -135,3 +211,106 @@ - (void)drawRect:(NSRect)dirtyRect{
}
@end
+
+#pragma mark - KGNoiseLinearGradientView
+
+@implementation KGNoiseLinearGradientView
+
+#if TARGET_OS_IPHONE
+- (void)setAlternateBackgroundColor:(UIColor *)alternateBackgroundColor{
+ if(_alternateBackgroundColor != alternateBackgroundColor){
+ _alternateBackgroundColor = alternateBackgroundColor;
+ [self setNeedsDisplay];
+ }
+}
+#else
+- (void)setAlternateBackgroundColor:(NSColor *)alternateBackgroundColor{
+ if(_alternateBackgroundColor != alternateBackgroundColor){
+ _alternateBackgroundColor = alternateBackgroundColor;
+ [self setNeedsDisplay:YES];
+ }
+}
+#endif
+
+#if TARGET_OS_IPHONE
+- (void)drawRect:(CGRect)dirtyRect{
+ CGContextRef context = UIGraphicsGetCurrentContext();
+#else
+- (void)drawRect:(NSRect)dirtyRect{
+ CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
+#endif
+ // if we don't have an alternate color draw solid
+ if(self.alternateBackgroundColor == nil){
+ [super drawRect:dirtyRect];
+ return;
+ }
+
+ CGRect bounds = self.bounds;
+ CGContextSaveGState(context);
+ CGColorSpaceRef baseSpace = CGColorSpaceCreateDeviceRGB();
+ CGFloat *components = gradientComponentsForColors(self.alternateBackgroundColor, self.backgroundColor);
+ CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, components, NULL, 2);
+ CGColorSpaceRelease(baseSpace), baseSpace = NULL;
+ CGPoint startPoint = CGPointMake(CGRectGetMidX(bounds), CGRectGetMinY(bounds));
+ CGPoint endPoint = CGPointMake(CGRectGetMidX(bounds), CGRectGetMaxY(bounds));
+ CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
+ CGGradientRelease(gradient), gradient = NULL;
+ CGContextRestoreGState(context);
+ free(components);
+
+ [KGNoise drawNoiseWithOpacity:self.noiseOpacity andBlendMode:self.noiseBlendMode];
+}
+
+@end
+
+#pragma mark - KGNoiseRadialGradientView
+
+@implementation KGNoiseRadialGradientView
+
+#if TARGET_OS_IPHONE
+- (void)setAlternateBackgroundColor:(UIColor *)alternateBackgroundColor{
+ if(_alternateBackgroundColor != alternateBackgroundColor){
+ _alternateBackgroundColor = alternateBackgroundColor;
+ [self setNeedsDisplay];
+ }
+}
+#else
+- (void)setAlternateBackgroundColor:(NSColor *)alternateBackgroundColor{
+ if(_alternateBackgroundColor != alternateBackgroundColor){
+ _alternateBackgroundColor = alternateBackgroundColor;
+ [self setNeedsDisplay:YES];
+ }
+}
+#endif
+
+#if TARGET_OS_IPHONE
+- (void)drawRect:(CGRect)dirtyRect{
+ CGContextRef context = UIGraphicsGetCurrentContext();
+#else
+- (void)drawRect:(NSRect)dirtyRect{
+ CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
+#endif
+ // if we don't have an alternate color draw solid
+ if(self.alternateBackgroundColor == nil){
+ [super drawRect:dirtyRect];
+ return;
+ }
+
+ CGRect bounds = self.bounds;
+ CGContextSaveGState(context);
+ size_t gradLocationsNum = 2;
+ CGFloat gradLocations[2] = {0.0f, 1.0f};
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGFloat *components = gradientComponentsForColors(self.alternateBackgroundColor, self.backgroundColor);
+ CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, gradLocations, gradLocationsNum);
+ CGColorSpaceRelease(colorSpace), colorSpace = NULL;
+ CGPoint gradCenter= CGPointMake(round(CGRectGetMidX(bounds)), round(CGRectGetMidY(bounds)));
+ CGFloat gradRadius = sqrt(pow((CGRectGetHeight(bounds)/2), 2) + pow((CGRectGetWidth(bounds)/2), 2));
+ CGContextDrawRadialGradient(context, gradient, gradCenter, 0, gradCenter, gradRadius, kCGGradientDrawsAfterEndLocation);
+ CGGradientRelease(gradient), gradient = NULL;
+ CGContextRestoreGState(context);
+ free(components);
+
+ [KGNoise drawNoiseWithOpacity:self.noiseOpacity andBlendMode:self.noiseBlendMode];
+}
+@end
View
19 readme.md
@@ -1,6 +1,6 @@
-![](https://raw.github.com/kgn/KGNoise/master/KGNoise.jpg)
+![](https://raw.github.com/kgn/KGNoise/master/screenshot.jpg)
-I've been developing this noise drawing code for quite some time. It all started with drawing noise in the title bar of [INAppStoreWindow](https://github.com/indragiek/INAppStoreWindow). The original implementation of the noise drawing for the titlebar used `CIFilter`, but this took up an unusual amount of memory and also didn't looks so great. So I began my quest for the best noise drawing solution, this project contains the third version which I feel is finally ready for prime time on the Mac and iOS!
+I've been developing this noise drawing code for quite some time. It all started with drawing noise in the title bar of [INAppStoreWindow](https://github.com/indragiek/INAppStoreWindow). The original implementation of the noise drawing for the titlebar used `CIFilter`, but this took up an unusual amount of memory and also didn't look so great. So I began my quest for the best noise drawing solution, this project contains the third version which I feel is finally ready for prime time on the Mac and iOS!
KGNoise generates random black and white pixels into a static 128x128 image that is then tiled to fill the space. The random pixels are seeded with a value that has been chosen to look the most random, this also means that the noise will look consistent between app launches.
@@ -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.
@@ -37,6 +44,14 @@ There is also a subclass of `NSView` or `UIView`, depending on your platform, th
Please note that the standard `backgroundColor` is used for `UIView`, but `backgroundColor` does not exist on `NSView` so it has been added to provide the exact same interface for both platforms.
+# KGNoiseLinearGradientView & KGNoiseRadialGradientView
+
+`KGNoiseLinearGradientView` and `KGNoiseRadialGradientView` inherit from `KGNoiseView` and draw a linear or radial gradient respectively. They provide one additional property to set the alternate background color to be used in the gradient.
+
+```obj-c
+@property (strong, nonatomic) NSColor/UIColor *alternateBackgroundColor;
+```
+
# KGNoiseExample
This project contains an example project that demonstrates how `KGNoiseView` could be used in a Mac or iOS app.
View
BIN  screenshot.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Please sign in to comment.
Something went wrong with that request. Please try again.