Suppose we need to show the effect of only the red component of it’s color on the image, Then this can be done by reading all the pixels of that image and extracting and applying only the red color on all pixels.
The other color components i.e blue and green will be filtered out and only the effect of red component will be visible on the Image.
The pixels which previous to filtering were not having any red components will appear black in this case.
The above and similar tasks of filtering the red, green and blue color from an image can be accomplished using the following code:
-(NSImage *)filterColorFromImage:(NSImage *)image retainColor:(NSString *)colorName { NSSize size = image.size; NSColor* color = [[NSColor alloc] init]; //Make NSBitmapImageRep from NSImage to set color on pixels NSBitmapImageRep* imageRep = [NSBitmapImageRepimageRepWithData:[image TIFFRepresentation]]; //Lock focus on image to convert image coordinate system to current coordinate system [image lockFocus]; //Filter Red Color if ([colorName caseInsensitiveCompare:@"red"] == NSOrderedSame) { for (int width = 0; width < size.width ; ++width) { for (int height =0; height < size.height; ++height) { //Get color of the pixel color = NSReadPixel(NSMakePoint(width, height)); //Filter red component from color NSColor* fColor = [NSColorcolorWithDeviceRed:[color redComponent] green:0.0blue:0.0alpha:[color alphaComponent]]; //Apply filterd red color to the instance of NSBitmapImageRep [imageRep setColor:fColor atX:width y:height]; } } } //Filter Blue Color elseif ([colorName caseInsensitiveCompare:@"blue"] == NSOrderedSame) { for (int width = 0; width < size.width ; ++width) { for (int height =0; height < size.height; ++height) { //Get color of the pixel color = NSReadPixel(NSMakePoint(width, height)); //Filter red component from color NSColor* fColor = [NSColorcolorWithDeviceRed:0.0green:0.0blue:[color blueComponent] alpha:[color alphaComponent]]; //Apply filterd red color to the instance of NSBitmapImageRep [imageRep setColor:fColor atX:width y:height]; } } } //Filter Green Color elseif ([colorName caseInsensitiveCompare:@"green"] == NSOrderedSame) { for (int width = 0; width < size.width ; ++width) { for (int height =0; height < size.height; ++height) { //Get color of the pixel color = NSReadPixel(NSMakePoint(width, height)); //Filter red component from color NSColor* fColor = [NSColorcolorWithDeviceRed:0.0green:[color greenComponent] blue:0.0alpha:[color alphaComponent]]; //Apply filterd red color to the instance of NSBitmapImageRep [imageRep setColor:fColor atX:width y:height]; } } } else { NSAlert* alert = [NSAlertalertWithMessageText: @"Warning!!!"defaultButton:nilalternateButton:nilotherButton: nilinformativeTextWithFormat:@"colorName passed in the function should be red, green or blue"]; [alert runModal]; [image unlockFocus]; return image; } [image unlockFocus]; //Make a NSImage from the filtered color using imageRep NSImage* filteredImage = [[NSImage alloc] initWithCGImage:[imageRep CGImage] size:size]; //return Filtered image return filteredImage; }