Today's Menu  Portugal
journal and plan nutrition
ZCMCommon.m
Go to the documentation of this file.
1 //
2 // ZCGCommon.m
3 // CoolTable
4 //
5 // Created by Don Zeek on 10/19/13.
6 // Copyright (c) 2013 net.dzeek.y2013. All rights reserved.
7 //
8 
9 #import "ZCMCommon.h"
10 #import <UIKit/UIKit.h>
11 
12 void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
13 {
14  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
15  CGFloat locations[] = { 0.0, 1.0 };
16 
17  NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
18 
19  CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
20 
21  CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
22  CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
23 
24  CGContextSaveGState(context);
25  CGContextAddRect(context, rect);
26  CGContextClip(context);
27  CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
28  CGContextRestoreGState(context);
29 
30  CGGradientRelease(gradient);
31  CGColorSpaceRelease(colorSpace);
32 
33 }
34 
35 CGRect rectFor1PxStroke(CGRect rect)
36 {
37  return CGRectMake(rect.origin.x + 0.5, rect.origin.y + 0.5, rect.size.width - 1, rect.size.height - 1);
38 }
39 
40 void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color)
41 {
42  CGContextSaveGState(context);
43  CGContextSetLineCap(context, kCGLineCapSquare);
44  CGContextSetStrokeColorWithColor(context, color);
45  CGContextSetLineWidth(context, 1.0);
46  CGContextMoveToPoint(context, startPoint.x + 0.5, startPoint.y + 0.5);
47  CGContextAddLineToPoint(context, endPoint.x + 0.5, endPoint.y + 0.5);
48  CGContextStrokePath(context);
49  CGContextRestoreGState(context);
50 }
51 
52 void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
53 {
54  drawLinearGradient(context, rect, startColor, endColor);
55 
56  UIColor * glossColor1 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.35];
57  UIColor * glossColor2 = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.1];
58 
59  CGRect topHalf = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height/2);
60 
61  drawLinearGradient(context, topHalf, glossColor1.CGColor, glossColor2.CGColor);
62 }
63 
64 CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight)
65 {
66  CGRect arcRect = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height - arcHeight, rect.size.width, arcHeight);
67 
68  CGFloat arcRadius = (arcRect.size.height/2) + (pow(arcRect.size.width, 2) / (8*arcRect.size.height));
69  CGPoint arcCenter = CGPointMake(arcRect.origin.x + arcRect.size.width/2, arcRect.origin.y + arcRadius);
70 
71  CGFloat angle = acos(arcRect.size.width / (2*arcRadius));
72  CGFloat startAngle = radians(180) + angle;
73  CGFloat endAngle = radians(360) - angle;
74 
75  CGMutablePathRef path = CGPathCreateMutable();
76  CGPathAddArc(path, NULL, arcCenter.x, arcCenter.y, arcRadius, startAngle, endAngle, 0);
77  CGPathAddLineToPoint(path, NULL, CGRectGetMaxX(rect), CGRectGetMinY(rect));
78  CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMinY(rect));
79  CGPathAddLineToPoint(path, NULL, CGRectGetMinX(rect), CGRectGetMaxY(rect));
80 
81  return path;
82 }
CGRect rectFor1PxStroke(CGRect rect)
Definition: ZCMCommon.m:35
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
Definition: ZCMCommon.m:12
void draw1PxStroke(CGContextRef context, CGPoint startPoint, CGPoint endPoint, CGColorRef color)
Definition: ZCMCommon.m:40
CGMutablePathRef createArcPathFromBottomOfRect(CGRect rect, CGFloat arcHeight)
Definition: ZCMCommon.m:64
static double radians(double degrees)
Definition: ZCMCommon.h:16
void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
Definition: ZCMCommon.m:52