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 protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to