Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ColorPickerView.mm « Classes « Maps « iphone - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc5c55b8bac9c77cf86816b95028e1510163497d (plain)
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
#import "ColorPickerView.h"
#import "CircleView.h"
#import <QuartzCore/QuartzCore.h>

#include "Framework.h"

#define BUTTONMARGIN 7
#define BUTTONMARGINHEIGHT 16
#define BORDERMARGIN 32
#define GLOBALMARGIN 22
#define HEADERHEIGHT 56
#define LINEHEIGHT 2

struct Tcolor
{
  NSString * color;
  float rgb[3];
};

static Tcolor const g_color [] =
{
  {@"placemark-red", {255, 51, 51}},
  {@"placemark-yellow", {255, 255, 51}},
  {@"placemark-blue", {51, 204, 255}},
  {@"placemark-green", {102, 255, 51}},
  {@"placemark-purple", {153, 51, 255}},
  {@"placemark-orange", {255, 102, 0}},
  {@"placemark-brown", {102, 51, 0}},
  {@"placemark-pink", {255, 51, 255}},
};

@implementation ColorPickerView

- (id)initWithWidth:(CGFloat)width andSelectButton:(size_t)selectedIndex
{
  CGFloat const customWidth = width - 2 * GLOBALMARGIN;
  CGFloat const buttonDiameter = (customWidth - 3 * BUTTONMARGIN - 2 * BORDERMARGIN) / 4;
  self = [super initWithFrame:CGRectMake(0, 0, customWidth, 2 * (BORDERMARGIN + buttonDiameter) + LINEHEIGHT + BUTTONMARGINHEIGHT + HEADERHEIGHT)];
  self.backgroundColor = [UIColor colorWithRed:245/255.f green:245/255.f blue:245/255.f alpha:1.f];
  if (self)
  {
    self.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|
                            UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;

    UILabel * header = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, customWidth, HEADERHEIGHT)];
    header.backgroundColor = [UIColor clearColor];
    header.text = NSLocalizedString(@"bookmark_color", nil);
    header.font = [UIFont fontWithName:@"Helvetica" size:20];
    header.textAlignment = NSTextAlignmentCenter;
    header.textColor = [UIColor colorWithRed:51/255.f green:204/255.f blue:255/255.f alpha:1];

    [self addSubview:header];

    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, HEADERHEIGHT - LINEHEIGHT, customWidth, LINEHEIGHT)];
    line.backgroundColor = [UIColor colorWithRed:51/255.f green:204/255.f blue:255/255.f alpha:1];
    [self addSubview:line];

    self.layer.cornerRadius = 10;
    for (size_t i = 0; i < 8; ++i)
    {
      UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(BORDERMARGIN + (i % 4) * (buttonDiameter + BUTTONMARGIN), BORDERMARGIN + (i / 4) * (buttonDiameter + BUTTONMARGINHEIGHT) + HEADERHEIGHT + LINEHEIGHT, buttonDiameter, buttonDiameter)];
      UIColor * c = [ColorPickerView buttonColor:i];
      if (i != selectedIndex)
        [button setBackgroundImage:[CircleView createCircleImageWith:buttonDiameter andColor:c] forState:UIControlStateNormal];
      else
      {
        CGFloat const selectionDiametr = buttonDiameter * 0.6;
        CGFloat const origin = buttonDiameter / 2 - selectionDiametr / 2;
        UIColor * col = [UIColor colorWithRed:1.f green:1.f blue:1.f alpha:1];
        CircleView * selectedCircle = [[CircleView alloc] initWithFrame:CGRectMake(origin, origin, selectionDiametr, selectionDiametr) andColor:col];
        [button setBackgroundImage:[CircleView createCircleImageWith:buttonDiameter andColor:c andSubview:selectedCircle] forState:UIControlStateNormal];
      }

      button.layer.cornerRadius = 26;
      [button addTarget:self
                 action:@selector(touch:)
       forControlEvents:UIControlEventTouchUpInside];
      button.tag = i;
      [button setContentMode:UIViewContentModeScaleAspectFit];
      [self addSubview:button];
    }
  }
  return self;
}

- (void)touch:(UIButton *)button
{
  [self.delegate colorPicked:button.tag];
}

+ (UIColor *)buttonColor:(size_t)index
{
  return [UIColor colorWithRed:g_color[index].rgb[0]/255.f green:g_color[index].rgb[1]/255.f blue:g_color[index].rgb[2]/255.f alpha:0.8];
}


//store here temporary
+ (UIColor *)colorForName:(NSString *)name
{
  size_t const index = [self getColorIndex:name];
  return [self buttonColor:index];
}

+ (NSString *)colorName:(size_t)index
{
  if (index < ARRAY_SIZE(g_color))
    return g_color[index].color;
  NSLog(@"WARNING! Color doesn't exist");
  return @"";
}

+ (size_t)getColorIndex:(NSString *)name
{
  for (size_t i = 0; i < ARRAY_SIZE(g_color); ++i)
    if ([name isEqualToString:g_color[i].color])
      return i;
  return 0;
}

@end