Class: Motion::Xray::XrayColorSliders

Inherits:
UIControl
  • Object
show all
Defined in:
lib/motion-xray/views/xray_color_swatch.rb

Constant Summary collapse

ColorSection =
0
RedSection =
1
GreenSection =
2
BlueSection =
3
AlphaSection =
4

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorObject

Returns the value of attribute color


74
75
76
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 74

def color
  @color
end

Instance Method Details

#drawRect(rect) ⇒ Object


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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 89

def drawRect(rect)
  super

  r = color.red
  g = color.green
  b = color.blue
  a = color.alpha
  return unless r && g && b && a

  context = UIGraphicsGetCurrentContext()
  color_space = CGColorSpaceCreateDeviceRGB()
  slider_height = bounds.height / 5
  slider_size = bounds.size
  slider_size.height = slider_height
  path = UIBezierPath.bezierPathWithRect([[0, 0], slider_size])
  @triangle.frame = [[0, 0], slider_size]
  @triangle.drawRect(rect)

  CGContextSaveGState(context)
  CGContextTranslateCTM(context, 0, 0)
  color.setFill
  path.fill
  CGContextRestoreGState(context)

  big_oval_width = slider_height - 4
  big_oval = [[-big_oval_width / 2, (slider_height - big_oval_width) / 2], [big_oval_width, big_oval_width]]
  big_oval_path = UIBezierPath.bezierPathWithOvalInRect(big_oval)
  small_oval_width = 4
  small_oval = [[-small_oval_width / 2, (slider_height - small_oval_width) / 2], [small_oval_width, small_oval_width]]
  small_oval_path = UIBezierPath.bezierPathWithOvalInRect(small_oval)

  points = [0, 1].to_pointer(:float)

  cgcolors_red = [
    UIColor.colorWithRed(0, green:g, blue:b, alpha:1).CGColor,
    UIColor.colorWithRed(1, green:g, blue:b, alpha:1).CGColor,
  ]
  gradient_red = CGGradientCreateWithColors(color_space, cgcolors_red, points)
  CGContextSaveGState(context)
  CGContextTranslateCTM(context, 0, 1 * slider_height)
  path.addClip
  CGContextDrawLinearGradient(context, gradient_red, self.bounds.top_left, self.bounds.top_right, 0)
  CGContextTranslateCTM(context, r * bounds.width, 0)
  UIColor.colorWithRed(1, green:g, blue:b, alpha:1).invert.setStroke
  big_oval_path.stroke
  small_oval_path.stroke
  CGContextRestoreGState(context)

  cgcolors_green = [
    UIColor.colorWithRed(r, green:0, blue:b, alpha:1).CGColor,
    UIColor.colorWithRed(r, green:1, blue:b, alpha:1).CGColor,
  ]
  gradient_green = CGGradientCreateWithColors(color_space, cgcolors_green, points)
  CGContextSaveGState(context)
  CGContextTranslateCTM(context, 0, 2 * slider_height)
  path.addClip
  CGContextDrawLinearGradient(context, gradient_green, self.bounds.top_left, self.bounds.top_right, 0)
  CGContextTranslateCTM(context, g * bounds.width, 0)
  UIColor.colorWithRed(r, green:1, blue:b, alpha:1).invert.setStroke
  big_oval_path.stroke
  small_oval_path.stroke
  CGContextRestoreGState(context)

  cgcolors_blue = [
    UIColor.colorWithRed(r, green:g, blue:0, alpha:1).CGColor,
    UIColor.colorWithRed(r, green:g, blue:1, alpha:1).CGColor,
  ]
  gradient_blue = CGGradientCreateWithColors(color_space, cgcolors_blue, points)
  CGContextSaveGState(context)
  CGContextTranslateCTM(context, 0, 3 * slider_height)
  path.addClip
  CGContextDrawLinearGradient(context, gradient_blue, self.bounds.top_left, self.bounds.top_right, 0)
  CGContextTranslateCTM(context, b * bounds.width, 0)
  UIColor.colorWithRed(r, green:g, blue:1, alpha:1).invert.setStroke
  big_oval_path.stroke
  small_oval_path.stroke
  CGContextRestoreGState(context)

  cgcolors_alpha = [
    UIColor.colorWithRed(r, green:g, blue:b, alpha:0).CGColor,
    UIColor.colorWithRed(r, green:g, blue:b, alpha:1).CGColor,
  ]
  gradient_alpha = CGGradientCreateWithColors(color_space, cgcolors_alpha, points)
  CGContextSaveGState(context)
  CGContextTranslateCTM(context, 0, 4 * slider_height)
  path.addClip
  CGContextDrawLinearGradient(context, gradient_alpha, self.bounds.top_left, self.bounds.top_right, 0)
  CGContextTranslateCTM(context, a * bounds.width, 0)
  :white.uicolor.mix_with(color.uicolor(1).invert, a).setStroke
  big_oval_path.stroke
  small_oval_path.stroke
  CGContextRestoreGState(context)
end

#initWithFrame(frame) ⇒ Object


76
77
78
79
80
81
82
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 76

def initWithFrame(frame)
  super.tap do
    self.backgroundColor = :black.uicolor
    @color = :clear.uicolor
    @triangle = XrayTriangleSwatch.alloc.initWithFrame(CGRect.empty)
  end
end

#touched_color_at(point) ⇒ Object


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 200

def touched_color_at(point)
  slider_height = bounds.height / 5
  section = (point.y / slider_height).floor
  amount = [[point.x / bounds.width, 0].max, 1].min

  # assigns @touched_section only the first time (in `touchesBegan()`)
  @touched_section ||= section
  # makes sure we're still touching the same section
  section = @touched_section
  return if section == ColorSection

  r = color.red
  g = color.green
  b = color.blue
  a = color.alpha

  case section
  when RedSection
    r = amount
  when GreenSection
    g = amount
  when BlueSection
    b = amount
  when AlphaSection
    amount = (amount * 100).round / 100.0
    a = amount
  end

  self.color = UIColor.colorWithRed(r, green:g, blue:b, alpha:a)
  self.sendActionsForControlEvents(:value_changed.uicontrolevent)
end

#touchesBegan(touches, withEvent: event) ⇒ Object


183
184
185
186
187
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 183

def touchesBegan(touches, withEvent:event)
  point = touches.anyObject.locationInView(self)
  @touched_section = nil
  touched_color_at(point)
end

#touchesMoved(touches, withEvent: event) ⇒ Object


189
190
191
192
# File 'lib/motion-xray/views/xray_color_swatch.rb', line 189

def touchesMoved(touches, withEvent:event)
  point = touches.anyObject.locationInView(self)
  touched_color_at(point)
end