HRCgUtil.m 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*-
  2. * Copyright (c) 2011 Ryota Hayashi
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. *
  25. * $FreeBSD$
  26. */
  27. #import "HRCgUtil.h"
  28. void HRSetRoundedRectanglePath(CGContextRef context,const CGRect rect,CGFloat radius){
  29. CGFloat lx = CGRectGetMinX(rect);
  30. CGFloat cx = CGRectGetMidX(rect);
  31. CGFloat rx = CGRectGetMaxX(rect);
  32. CGFloat by = CGRectGetMinY(rect);
  33. CGFloat cy = CGRectGetMidY(rect);
  34. CGFloat ty = CGRectGetMaxY(rect);
  35. CGContextMoveToPoint(context, lx, cy);
  36. CGContextAddArcToPoint(context, lx, by, cx, by, radius);
  37. CGContextAddArcToPoint(context, rx, by, rx, cy, radius);
  38. CGContextAddArcToPoint(context, rx, ty, cx, ty, radius);
  39. CGContextAddArcToPoint(context, lx, ty, lx, cy, radius);
  40. CGContextClosePath(context);
  41. }
  42. void HRDrawSquareColorBatch(CGContextRef context,CGPoint position,HRRGBColor* color,float size){
  43. float cx = position.x;
  44. float cy = position.y;
  45. float rRize = size;
  46. float backRSize = rRize + 3.0f;
  47. float shadowRSize = backRSize + 3.0f;
  48. CGRect rectEllipse = CGRectMake(cx - rRize, cy - rRize, rRize*2, rRize*2);
  49. CGRect rectBackEllipse = CGRectMake(cx - backRSize, cy - backRSize, backRSize*2, backRSize*2);
  50. CGRect rectShadowEllipse = CGRectMake(cx - shadowRSize, cy - shadowRSize, shadowRSize*2, shadowRSize*2);
  51. CGContextSaveGState(context);
  52. HRSetRoundedRectanglePath(context, rectBackEllipse,8.0f);
  53. CGContextClip(context);
  54. HRSetRoundedRectanglePath(context, rectShadowEllipse,8.0f);
  55. CGContextSetLineWidth(context, 5.5f);
  56. [[UIColor whiteColor] set];
  57. CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 4.0f, [UIColor colorWithWhite:0.0f alpha:0.2f].CGColor);
  58. CGContextDrawPath(context, kCGPathStroke);
  59. CGContextRestoreGState(context);
  60. CGContextSaveGState(context);
  61. CGContextSetRGBFillColor(context, color->r, color->g, color->b, 1.0f);
  62. CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 0.5f), 0.5f, [UIColor colorWithWhite:0.0f alpha:0.2f].CGColor);
  63. HRSetRoundedRectanglePath(context, rectEllipse,5.0f);
  64. CGContextDrawPath(context, kCGPathFill);
  65. CGContextRestoreGState(context);
  66. }