Drawing noise in Cocoa (preferably fast)
Drawing noise in Cocoa (preferably fast)
- Subject: Drawing noise in Cocoa (preferably fast)
- From: Stephen Blinkhorn <email@hidden>
- Date: Tue, 14 Jun 2011 15:56:41 -0600
Hi all,
I'm looking to fill a large custom view with noise. The solution I
have arrived at so far is taking much too long to draw for it to be
useable. Does anyone know any better ways of achieving this in Cocoa?
The basic idea is to add an amount of noise to a base color pixel by
pixel, for example:
// create base color
NSColor *base_color = [NSColor colorWithCalibratedRed:0.25 green:0.3
blue:0.35 alpha:1.0]; //[style colorForKey:@"Section"];
// get color components
CGFloat red, green, blue, alpha;
[base_color getRed:&red green:&green blue:&blue alpha:&alpha];
// create rect and boundary variables
NSRect noise_rect = NSMakeRect(0, 0, 1, 1);
NSInteger width = [self bounds].size.width;
NSInteger height = [self bounds].size.height;
// main loop
NSInteger x_pos = 0;
for(; x_pos<width; x_pos+=1)
{
NSInteger y_pos = 0;
for(; y_pos<height; y_pos+=1)
{
// create noise (0..1 range)
float pixel_noise = (rand() % 1000 * 0.0001);
// add noise to color variables
[[NSColor colorWithCalibratedRed:red+pixel_noise green:green
+pixel_noise blue:blue+pixel_noise alpha:alpha] set];
// draw a 1X1 pixel rect
noise_rect.origin.x = x_pos;
noise_rect.origin.y = y_pos;
NSRectFill(noise_rect);
};
};
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden