cura 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -21
  3. data/cura.gemspec +1 -1
  4. data/examples/hello_world/lib/hello_world.rb +10 -10
  5. data/examples/mruby-examples/mrbgem.rake +5 -6
  6. data/examples/todo_list/data.db +0 -0
  7. data/examples/todo_list/lib/todo_list/application.rb +24 -18
  8. data/lib/cura/adapter.rb +13 -20
  9. data/lib/cura/application.rb +47 -51
  10. data/lib/cura/attributes/has_ancestry.rb +4 -8
  11. data/lib/cura/attributes/has_application.rb +3 -7
  12. data/lib/cura/attributes/has_attributes.rb +1 -9
  13. data/lib/cura/attributes/has_children.rb +14 -20
  14. data/lib/cura/attributes/has_colors.rb +14 -18
  15. data/lib/cura/attributes/has_coordinates.rb +9 -15
  16. data/lib/cura/attributes/has_dimensions.rb +12 -18
  17. data/lib/cura/attributes/has_events.rb +10 -18
  18. data/lib/cura/attributes/has_focusability.rb +5 -11
  19. data/lib/cura/attributes/has_initialize.rb +1 -5
  20. data/lib/cura/attributes/has_offsets.rb +16 -20
  21. data/lib/cura/attributes/has_orientation.rb +12 -18
  22. data/lib/cura/attributes/has_relative_coordinates.rb +4 -8
  23. data/lib/cura/attributes/has_root.rb +18 -22
  24. data/lib/cura/attributes/has_side_attributes.rb +18 -24
  25. data/lib/cura/attributes/has_windows.rb +13 -19
  26. data/lib/cura/borders.rb +0 -4
  27. data/lib/cura/color.rb +84 -91
  28. data/lib/cura/component/base.rb +29 -33
  29. data/lib/cura/component/button.rb +10 -16
  30. data/lib/cura/component/group.rb +14 -18
  31. data/lib/cura/component/label.rb +44 -48
  32. data/lib/cura/component/listbox.rb +24 -28
  33. data/lib/cura/component/pack.rb +14 -18
  34. data/lib/cura/component/scrollbar.rb +41 -45
  35. data/lib/cura/component/textbox.rb +21 -25
  36. data/lib/cura/cursor.rb +15 -23
  37. data/lib/cura/error/base.rb +0 -3
  38. data/lib/cura/error/invalid_adapter.rb +1 -7
  39. data/lib/cura/error/invalid_application.rb +1 -7
  40. data/lib/cura/error/invalid_color.rb +1 -7
  41. data/lib/cura/error/invalid_component.rb +1 -7
  42. data/lib/cura/error/invalid_middleware.rb +1 -7
  43. data/lib/cura/event.rb +4 -8
  44. data/lib/cura/event/base.rb +17 -24
  45. data/lib/cura/event/click.rb +1 -6
  46. data/lib/cura/event/dispatcher.rb +20 -26
  47. data/lib/cura/event/focus.rb +1 -6
  48. data/lib/cura/event/handler.rb +16 -24
  49. data/lib/cura/event/key_down.rb +11 -17
  50. data/lib/cura/event/middleware/aimer/base.rb +4 -10
  51. data/lib/cura/event/middleware/aimer/dispatcher_target.rb +2 -8
  52. data/lib/cura/event/middleware/aimer/mouse_focus.rb +6 -11
  53. data/lib/cura/event/middleware/aimer/target_option.rb +4 -10
  54. data/lib/cura/event/middleware/base.rb +0 -4
  55. data/lib/cura/event/middleware/dispatch.rb +0 -4
  56. data/lib/cura/event/middleware/translator/base.rb +4 -10
  57. data/lib/cura/event/middleware/translator/mouse_click.rb +4 -8
  58. data/lib/cura/event/mouse.rb +5 -11
  59. data/lib/cura/event/mouse_button.rb +21 -27
  60. data/lib/cura/event/mouse_wheel_down.rb +1 -6
  61. data/lib/cura/event/mouse_wheel_up.rb +1 -6
  62. data/lib/cura/event/resize.rb +0 -4
  63. data/lib/cura/event/selected.rb +1 -6
  64. data/lib/cura/event/unfocus.rb +1 -6
  65. data/lib/cura/focus_controller.rb +19 -23
  66. data/lib/cura/key.rb +277 -283
  67. data/lib/cura/margins.rb +0 -4
  68. data/lib/cura/offsets.rb +14 -18
  69. data/lib/cura/padding.rb +0 -4
  70. data/lib/cura/pencil.rb +3 -7
  71. data/lib/cura/version.rb +1 -3
  72. data/lib/cura/window.rb +11 -16
  73. data/spec/cura/attributes/has_ancestry_spec.rb +39 -39
  74. data/spec/cura/attributes/has_application_spec.rb +20 -20
  75. data/spec/cura/attributes/has_attributes_spec.rb +26 -26
  76. data/spec/cura/attributes/has_children_spec.rb +54 -54
  77. data/spec/cura/attributes/has_colors_spec.rb +4 -4
  78. data/spec/cura/attributes/has_coordinates_spec.rb +4 -4
  79. data/spec/cura/attributes/has_dimensions_spec.rb +4 -4
  80. data/spec/cura/attributes/has_events_spec.rb +4 -4
  81. data/spec/cura/attributes/has_focusability_spec.rb +18 -18
  82. data/spec/cura/attributes/has_offsets_spec.rb +4 -4
  83. data/spec/cura/attributes/has_orientation_spec.rb +38 -38
  84. data/spec/cura/attributes/has_relative_coordinates_spec.rb +4 -4
  85. data/spec/cura/attributes/has_side_attributes_spec.rb +4 -4
  86. data/spec/spec_helper.rb +1 -1
  87. data/spec/support/shared_examples_for_attributes.rb +41 -41
  88. metadata +1 -1
@@ -1,35 +1,31 @@
1
- if Kernel.respond_to?(:require)
2
- require "cura/window"
3
- end
1
+ require "cura/window" if Kernel.respond_to?(:require)
4
2
 
5
3
  module Cura
6
4
  module Attributes
7
-
8
5
  # Allows an object to have windows.
9
6
  # TODO: Lots of code is the same as HasChildren
10
7
  module HasWindows
11
-
12
8
  def initialize(*arguments)
13
9
  @windows = []
14
-
10
+
15
11
  super
16
12
  end
17
-
13
+
18
14
  # Get the windows of this object.
19
15
  attr_reader :windows
20
-
16
+
21
17
  # Add a window to this object.
22
18
  #
23
19
  # @param [Window] window
24
20
  # @return [Window]
25
21
  def add_window(window)
26
22
  raise TypeError, "window must be a Cura::Window" unless window.is_a?(Window)
27
-
23
+
28
24
  @windows << window
29
-
25
+
30
26
  window
31
27
  end
32
-
28
+
33
29
  # Remove a window from this object's windows at the given index.
34
30
  #
35
31
  # @param [#to_i] index
@@ -37,7 +33,7 @@ module Cura
37
33
  def delete_window_at(index)
38
34
  @windows.delete_at(index.to_i)
39
35
  end
40
-
36
+
41
37
  # Remove a window from this object's windows.
42
38
  #
43
39
  # @param [Window] window
@@ -45,26 +41,24 @@ module Cura
45
41
  def delete_window(window)
46
42
  raise TypeError, "window must be a Cura::Window" unless window.is_a?(Window)
47
43
  index = @windows.index(window)
48
-
44
+
49
45
  delete_window_at(index)
50
46
  end
51
-
47
+
52
48
  # Remove all windows.
53
49
  def delete_windows
54
50
  (0...@windows.count).to_a.each { |index| delete_window_at(index) }
55
51
  end
56
-
52
+
57
53
  protected # TODO: These should be protected?
58
-
54
+
59
55
  def update_windows
60
56
  windows.each(&:update)
61
57
  end
62
-
58
+
63
59
  def draw_windows
64
60
  windows.each(&:draw)
65
61
  end
66
-
67
62
  end
68
-
69
63
  end
70
64
  end
@@ -4,13 +4,9 @@ if Kernel.respond_to?(:require)
4
4
  end
5
5
 
6
6
  module Cura
7
-
8
7
  # The border side attributes of a component.
9
8
  class Borders
10
-
11
9
  include Attributes::HasInitialize
12
10
  include Attributes::HasSideAttributes
13
-
14
11
  end
15
-
16
12
  end
@@ -4,44 +4,40 @@ if Kernel.respond_to?(:require)
4
4
  end
5
5
 
6
6
  module Cura
7
-
8
7
  # Colors.
9
8
  class Color
10
-
11
9
  include Attributes::HasInitialize
12
10
  include Attributes::HasAttributes
13
-
11
+
14
12
  class << self
15
-
16
13
  # The default color to be overidden by adapters.
17
14
  # Usually, for TUI's to use the terminal theme's colors.
18
15
  # TODO: Remove.
19
16
  def default
20
17
  super
21
18
  end
22
-
19
+
23
20
  def black
24
21
  new
25
22
  end
26
-
23
+
27
24
  def white
28
25
  new(255, 255, 255)
29
26
  end
30
-
27
+
31
28
  def red
32
29
  new(255, 0, 0)
33
30
  end
34
-
31
+
35
32
  def green
36
33
  new(0, 255, 0)
37
34
  end
38
-
35
+
39
36
  def blue
40
37
  new(0, 0, 255)
41
38
  end
42
-
43
39
  end
44
-
40
+
45
41
  def initialize(r=0, g=0, b=0, a=255)
46
42
  if r.respond_to?(:to_h)
47
43
  super(r.to_h)
@@ -51,68 +47,68 @@ module Cura
51
47
  @blue = b
52
48
  @alpha = a
53
49
  end
54
-
50
+
55
51
  @lab = rgb_to_lab([@red, @green, @blue]) # TODO: Update on rgb setters?
56
52
  end
57
-
53
+
58
54
  # @method red
59
55
  # Get the red channel of this color.
60
56
  #
61
57
  # @return [Integer]
62
-
58
+
63
59
  # @method red=(value)
64
60
  # Set the red channel of this color.
65
61
  #
66
62
  # @param [#to_i] value
67
63
  # @return [Integer]
68
-
64
+
69
65
  # @method green
70
66
  # Get the green channel of this color.
71
67
  #
72
68
  # @return [Integer]
73
-
69
+
74
70
  # @method green=(value)
75
71
  # Set the green channel of this color.
76
72
  #
77
73
  # @param [#to_i] value
78
74
  # @return [Integer]
79
-
75
+
80
76
  # @method blue=(value)
81
77
  # Get the blue channel of this color.
82
78
  #
83
79
  # @return [Integer]
84
-
80
+
85
81
  # @method blue=(value)
86
82
  # Set the blue channel of this color.
87
83
  #
88
84
  # @param [#to_i] value
89
85
  # @return [Integer]
90
-
86
+
91
87
  # @method alpha
92
88
  # Get the alpha channel of this color.
93
89
  #
94
90
  # @return [Integer]
95
-
91
+
96
92
  # @method alpha=(value)
97
93
  # Set the alpha channel of this color.
98
94
  #
99
95
  # @param [#to_i] value
100
96
  # @return [Integer]
101
-
97
+
102
98
  [:red, :green, :blue, :alpha].each do |channel|
103
99
  attribute(channel) { |value| convert_and_constrain_value(value) }
104
100
  end
105
-
101
+
106
102
  attr_reader :lab
107
-
103
+
108
104
  def -(other)
109
105
  delta_e_2000(@lab, other.lab)
110
106
  end
111
107
 
112
108
  def <=>(other)
113
- self.hsl[0] <=> other.hsl[0]
109
+ hsl[0] <=> other.hsl[0]
114
110
  end
115
-
111
+
116
112
  # Determing if this color is equivalent to another object.
117
113
  #
118
114
  # @param [Object] other
@@ -120,7 +116,7 @@ module Cura
120
116
  def ==(other)
121
117
  other.is_a?(Color) ? matches_color?(other) : super
122
118
  end
123
-
119
+
124
120
  def hsl
125
121
  @hsl ||= rgb_to_hsl(@rgb)
126
122
  end
@@ -128,61 +124,61 @@ module Cura
128
124
  def yiq
129
125
  @yiq ||= rgb_to_yiq(@rgb)
130
126
  end
131
-
127
+
132
128
  def to_a
133
129
  [@red, @green, @blue, @alpha]
134
130
  end
135
-
131
+
136
132
  def hex
137
133
  to_a.each_with_object("") { |part, memo| memo << "%02x" % part }
138
134
  end
139
-
135
+
140
136
  protected
141
-
137
+
142
138
  def matches_color?(other)
143
139
  @alpha == other.alpha && @red == other.red && @green == other.green && @blue && other.blue
144
140
  end
145
-
141
+
146
142
  # Convert the input to an Integer and constrain in or between 0 and 255.
147
143
  def convert_and_constrain_value(value)
148
144
  value = value.to_i
149
-
145
+
150
146
  [255, [0, value].max].min
151
147
  end
152
148
 
153
149
  # source: http://www.easyrgb.com/index.php?X=MATH&H=02#text2
154
150
  def rgb_to_xyz(color)
155
151
  r, g, b = color.map do |v|
156
- v /= 255.0
157
- if v > 0.04045
158
- v = ((v + 0.055 ) / 1.055)**2.4
159
- else
160
- v = v / 12.92
161
- end
162
- v *= 100
152
+ v /= 255.0
153
+ if v > 0.04045
154
+ v = ((v + 0.055) / 1.055)**2.4
155
+ else
156
+ v /= 12.92
157
+ end
158
+ v *= 100
163
159
  end
164
160
 
165
- #Observer = 2°, Illuminant = D65
161
+ # Observer = 2 degrees, Illuminant = D65
166
162
  x = r * 0.4124 + g * 0.3576 + b * 0.1805
167
163
  y = r * 0.2126 + g * 0.7152 + b * 0.0722
168
164
  z = r * 0.0193 + g * 0.1192 + b * 0.9505
169
165
 
170
- return [x, y, z]
166
+ [x, y, z]
171
167
  end
172
168
 
173
169
  def xyz_to_lab(color)
174
- f = lambda { |t|
175
- return t**(1.0/3) if t > (6.0 / 29)**3
170
+ f = lambda do |t|
171
+ return t**(1.0 / 3) if t > (6.0 / 29)**3
176
172
  return (1.0 / 3) * ((29.0 / 6)**2) * t + (4.0 / 29)
177
- }
173
+ end
178
174
 
179
175
  x, y, z = color
180
176
  xn, yn, zn = rgb_to_xyz([255, 255, 255])
181
- l = 116 * f.call(y/yn) - 16
182
- a = 500 * (f.call(x/xn) - f.call(y/yn))
183
- b = 200 * (f.call(y/yn) - f.call(z/zn))
177
+ l = 116 * f.call(y / yn) - 16
178
+ a = 500 * (f.call(x / xn) - f.call(y / yn))
179
+ b = 200 * (f.call(y / yn) - f.call(z / zn))
184
180
 
185
- return [l, a, b]
181
+ [l, a, b]
186
182
  end
187
183
 
188
184
  def rgb_to_lab(rgb_val)
@@ -198,27 +194,27 @@ module Cura
198
194
  lig = (max + min) / 2.0
199
195
 
200
196
  if delta == 0
201
- hue = 0
202
- sat = 0
197
+ hue = 0
198
+ sat = 0
203
199
  else
204
- sat = if lig < 0.5
205
- delta / (0.0 + (max + min))
206
- else
207
- delta / (2.0 - max - min)
208
- end
209
-
210
- delta_r = (((max - r) / 6.0 ) + (delta / 2.0)) / delta
211
- delta_g = (((max - g) / 6.0 ) + (delta / 2.0)) / delta
212
- delta_b = (((max - b) / 6.0 ) + (delta / 2.0)) / delta
213
-
214
- hue = case max
215
- when r then delta_b - delta_g
216
- when g then (1.0/3) + delta_r - delta_b
217
- when b then (2.0/3) + delta_g - delta_r
218
- end
219
-
220
- hue += 1 if hue < 0
221
- hue -= 1 if hue > 1
200
+ sat = if lig < 0.5
201
+ delta / (0.0 + (max + min))
202
+ else
203
+ delta / (2.0 - max - min)
204
+ end
205
+
206
+ delta_r = (((max - r) / 6.0) + (delta / 2.0)) / delta
207
+ delta_g = (((max - g) / 6.0) + (delta / 2.0)) / delta
208
+ delta_b = (((max - b) / 6.0) + (delta / 2.0)) / delta
209
+
210
+ hue = case max
211
+ when r then delta_b - delta_g
212
+ when g then (1.0 / 3) + delta_r - delta_b
213
+ when b then (2.0 / 3) + delta_g - delta_r
214
+ end
215
+
216
+ hue += 1 if hue < 0
217
+ hue -= 1 if hue > 1
222
218
  end
223
219
 
224
220
  [360 * hue, 100 * sat, 100 * lig]
@@ -227,9 +223,9 @@ module Cura
227
223
  def rgb_to_yiq(rgb_val)
228
224
  r, g, b = rgb_val
229
225
 
230
- y = 0.299*r + 0.587*g + 0.114*b
231
- i = 0.569*r - 0.275*g - 0.321*b
232
- q = 0.212*r - 0.523*g + 0.311*b
226
+ y = 0.299 * r + 0.587 * g + 0.114 * b
227
+ i = 0.569 * r - 0.275 * g - 0.321 * b
228
+ q = 0.212 * r - 0.523 * g + 0.311 * b
233
229
 
234
230
  [y, i, q]
235
231
  end
@@ -241,27 +237,29 @@ module Cura
241
237
  def deg_to_rad(v)
242
238
  (v * Math::PI) / 180
243
239
  end
244
-
240
+
245
241
  def lab_to_hue(a, b)
246
242
  bias = 0
247
- return 0 if (a >= 0 && b == 0)
248
- return 180 if (a < 0 && b == 0)
249
- return 90 if (a == 0 && b > 0)
250
- return 270 if (a == 0 && b < 0)
243
+ return 0 if a >= 0 && b == 0
244
+ return 180 if a < 0 && b == 0
245
+ return 90 if a == 0 && b > 0
246
+ return 270 if a == 0 && b < 0
251
247
 
252
248
  bias = case
253
- when a > 0 && b > 0 then 0
254
- when a < 0 then 180
255
- when a > 0 && b < 0 then 360
249
+ when a > 0 && b > 0 then 0
250
+ when a < 0 then 180
251
+ when a > 0 && b < 0 then 360
256
252
  end
257
253
 
258
254
  rad_to_deg(Math.atan(b / a)) + bias
259
255
  end
260
-
256
+
261
257
  def delta_e_2000(lab1, lab2)
262
258
  l1, a1, b1 = lab1
263
259
  l2, a2, b2 = lab2
264
- kl, kc, kh = [1, 1, 1]
260
+ kl = 1
261
+ kc = 1
262
+ kh = 1
265
263
 
266
264
  xC1 = Math.sqrt(a1**2 + b1**2)
267
265
  xC2 = Math.sqrt(a2**2 + b2**2)
@@ -307,24 +305,19 @@ module Cura
307
305
  end
308
306
  xHX /= 2.0
309
307
  end
310
- xTX = 1 - 0.17 * Math.cos(deg_to_rad(xHX - 30)) + 0.24 *
311
- Math.cos(deg_to_rad(2 * xHX)) + 0.32 *
312
- Math.cos(deg_to_rad(3 * xHX + 6)) - 0.20 *
313
- Math.cos(deg_to_rad(4 * xHX - 63 ))
314
- xPH = 30 * Math.exp(-((xHX - 275) / 25.0) * ((xHX - 275) / 25.0))
308
+ xTX = 1 - 0.17 * Math.cos(deg_to_rad(xHX - 30)) + 0.24 * Math.cos(deg_to_rad(2 * xHX)) + 0.32 * Math.cos(deg_to_rad(3 * xHX + 6)) - 0.20 * Math.cos(deg_to_rad(4 * xHX - 63))
309
+ xPH = 30 * Math.exp(-((xHX - 275) / 25.0) * ((xHX - 275) / 25.0))
315
310
  xRC = 2 * Math.sqrt(xCY**7 / (xCY**7 + 25**7))
316
311
  xSL = 1 + ((0.015 * ((xLX - 50) * (xLX - 50))) /
317
312
  Math.sqrt(20 + ((xLX - 50) * (xLX - 50))))
318
313
  xSC = 1 + 0.045 * xCY
319
314
  xSH = 1 + 0.015 * xCY * xTX
320
315
  xRT = -Math.sin(deg_to_rad(2 * xPH)) * xRC
321
- xDL = xDL / (kl * xSL)
322
- xDC = xDC / (kc * xSC)
323
- xDH = xDH / (kh * xSH)
316
+ xDL /= (kl * xSL)
317
+ xDC /= (kc * xSC)
318
+ xDH /= (kh * xSH)
324
319
 
325
320
  Math.sqrt(xDL**2 + xDC**2 + xDH**2 + xRT * xDC * xDH)
326
321
  end
327
-
328
322
  end
329
-
330
323
  end