Blending Mode sample images

kCGBlendModeDifferenceThere are times you are called upon to either explain or understand the various miscellany of Quartz blending modes. The simplest way to figure this out is, I find, to generate cheat sheet images to view as you read the documentation.

The following method automatically builds a set of reference images, naming them by the blend mode applied to create each one, and saving them to the application Documents folder.

- (void) generateBlendingImages
{
    NSArray *names = @[@"kCGBlendModeNormal", @"kCGBlendModeMultiply", @"kCGBlendModeScreen", @"kCGBlendModeOverlay", @"kCGBlendModeDarken", @"kCGBlendModeLighten", @"kCGBlendModeColorDodge", @"kCGBlendModeColorBurn", @"kCGBlendModeSoftLight", @"kCGBlendModeHardLight", @"kCGBlendModeDifference", @"kCGBlendModeExclusion", @"kCGBlendModeHue", @"kCGBlendModeSaturation", @"kCGBlendModeColor", @"kCGBlendModeLuminosity", @"kCGBlendModeClear", @"kCGBlendModeCopy", @"kCGBlendModeSourceIn", @"kCGBlendModeSourceOut", @"kCGBlendModeSourceAtop", @"kCGBlendModeDestinationOver", @"kCGBlendModeDestinationIn", @"kCGBlendModeDestinationOut", @"kCGBlendModeDestinationAtop", @"kCGBlendModeXOR", @"kCGBlendModePlusDarker", @"kCGBlendModePlusLighter"];

    CGRect rect = CGRectMake(0, 0, 100, 100);
    UIBezierPath *shape1 = [UIBezierPath bezierPathWithOvalInRect:rect];
    rect.origin.x += 50;
    UIBezierPath *shape2 = [UIBezierPath bezierPathWithOvalInRect:rect];

    for (int i = kCGBlendModeNormal; i <= kCGBlendModePlusLighter; i++)
    {
        NSString *name = [names[i - kCGBlendModeNormal] stringByAppendingPathExtension:@"png"];
        UIGraphicsBeginImageContext(CGSizeMake(150, 100));

        [greenColor set];
        [shape1 fill];

        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), i);

        [purpleColor set];
        [shape2 fill];

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        NSData *data = UIImagePNGRepresentation(image);
        [data writeToFile:DOCS_PATH(name) atomically:YES];
        UIGraphicsEndImageContext();

    }
}

Comments are closed.