Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: f811900c67
Sam Soffes soffes
ARC
file 137 lines (96 sloc) 4.055 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
//
// SSDrawingUtilities.m
// SSToolkit
//
// Created by Sam Soffes on 8/20/10.
// Copyright 2010-2011 Sam Soffes. All rights reserved.
//

#import "SSDrawingUtilities.h"

CGFloat SSFLimit(CGFloat f, CGFloat min, CGFloat max) {
return fminf(fmaxf(f, min), max);
}


CGRect CGRectSetX(CGRect rect, CGFloat x) {
return CGRectMake(x, rect.origin.y, rect.size.width, rect.size.height);
}


CGRect CGRectSetY(CGRect rect, CGFloat y) {
return CGRectMake(rect.origin.x, y, rect.size.width, rect.size.height);
}


CGRect CGRectSetWidth(CGRect rect, CGFloat width) {
return CGRectMake(rect.origin.x, rect.origin.y, width, rect.size.height);
}


CGRect CGRectSetHeight(CGRect rect, CGFloat height) {
return CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, height);
}


CGRect CGRectSetOrigin(CGRect rect, CGPoint origin) {
return CGRectMake(origin.x, origin.y, rect.size.width, rect.size.height);
}


CGRect CGRectSetSize(CGRect rect, CGSize size) {
return CGRectMake(rect.origin.x, rect.origin.y, size.width, size.height);
}


CGRect CGRectSetZeroOrigin(CGRect rect) {
return CGRectMake(0.0f, 0.0f, rect.size.width, rect.size.height);
}


CGRect CGRectSetZeroSize(CGRect rect) {
return CGRectMake(rect.origin.x, rect.origin.y, 0.0f, 0.0f);
}


CGSize CGSizeAspectScaleToSize(CGSize size, CGSize toSize) {
// Probably a more efficient way to do this...
CGFloat aspect = 1.0f;

if (size.width > toSize.width) {
aspect = toSize.width / size.width;
}

if (size.height > toSize.height) {
aspect = fminf(toSize.height / size.height, aspect);
}

return CGSizeMake(size.width * aspect, size.height * aspect);
}


CGRect CGRectAddPoint(CGRect rect, CGPoint point) {
return CGRectMake(rect.origin.x + point.x, rect.origin.y + point.y, rect.size.width, rect.size.height);
}


void SSDrawRoundedRect(CGContextRef context, CGRect rect, CGFloat cornerRadius) {
CGPoint min = CGPointMake(CGRectGetMinX(rect), CGRectGetMinY(rect));
CGPoint mid = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
CGPoint max = CGPointMake(CGRectGetMaxX(rect), CGRectGetMaxY(rect));

CGContextMoveToPoint(context, min.x, mid.y);
CGContextAddArcToPoint(context, min.x, min.y, mid.x, min.y, cornerRadius);
CGContextAddArcToPoint(context, max.x, min.y, max.x, mid.y, cornerRadius);
CGContextAddArcToPoint(context, max.x, max.y, mid.x, max.y, cornerRadius);
CGContextAddArcToPoint(context, min.x, max.y, min.x, mid.y, cornerRadius);

CGContextClosePath(context);
CGContextFillPath(context);
}


CGGradientRef SSCreateGradientWithColors(NSArray *colors) {
return SSCreateGradientWithColorsAndLocations(colors, nil);
}


CGGradientRef SSCreateGradientWithColorsAndLocations(NSArray *colors, NSArray *locations) {
NSUInteger colorsCount = [colors count];
if (colorsCount < 2) {
return nil;
}

CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors objectAtIndex:0] CGColor]);

CGFloat *gradientLocations = NULL;
NSUInteger locationsCount = [locations count];
if (locationsCount == colorsCount) {
gradientLocations = (CGFloat *)malloc(sizeof(CGFloat) * locationsCount);
for (NSUInteger i = 0; i < locationsCount; i++) {
gradientLocations[i] = [[locations objectAtIndex:i] floatValue];
}
}

NSMutableArray *gradientColors = [[NSMutableArray alloc] initWithCapacity:colorsCount];
[colors enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) {
[gradientColors addObject:(id)[(UIColor *)object CGColor]];
}];

CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);

if (gradientLocations) {
free(gradientLocations);
}

return gradient;
}


void SSDrawGradientInRect(CGContextRef context, CGGradientRef gradient, CGRect rect) {
CGContextSaveGState(context);
CGContextClipToRect(context, rect);
CGPoint start = CGPointMake(rect.origin.x, rect.origin.y);
CGPoint end = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
CGContextDrawLinearGradient(context, gradient, start, end, 0);
CGContextRestoreGState(context);
}
Something went wrong with that request. Please try again.