isd-color-palette 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (23) hide show
  1. data/README.md +63 -0
  2. data/lib/isd-color-palette.rb +9 -0
  3. data/lib/isd-color-palette/controller/isd_basic_color_collection_view_controller.rb +123 -0
  4. data/lib/isd-color-palette/controller/isd_color_palette_container_view_controller.rb +87 -0
  5. data/lib/isd-color-palette/controller/isd_color_palette_view_controller.rb +136 -0
  6. data/lib/isd-color-palette/controller/isd_rgba_color_view_controller.rb +118 -0
  7. data/lib/isd-color-palette/model/string_ids_extention.rb +12 -0
  8. data/lib/isd-color-palette/model/uicolor_isd_extention.rb +39 -0
  9. data/lib/isd-color-palette/view/isd_basic_color_collection_reusable_view.rb +38 -0
  10. data/lib/isd-color-palette/view/isd_color_palette_collection_view_cell.rb +69 -0
  11. data/lib/isd-color-palette/view/isd_rgba_slider_view.rb +124 -0
  12. data/lib/resources/ISDColorPalette.storyboard +435 -0
  13. data/lib/resources/ISDColorPalette.storyboardc/DDB-9Z-aLd-view-cjl-ZX-JRH.nib +0 -0
  14. data/lib/resources/ISDColorPalette.storyboardc/ISDColorPaletteViewController.nib +0 -0
  15. data/lib/resources/ISDColorPalette.storyboardc/Info.plist +0 -0
  16. data/lib/resources/ISDColorPalette.storyboardc/Ojg-L0-U3y-view-RZ8-kV-96R.nib +0 -0
  17. data/lib/resources/ISDColorPalette.storyboardc/UIViewController-DDB-9Z-aLd.nib +0 -0
  18. data/lib/resources/ISDColorPalette.storyboardc/UIViewController-Ojg-L0-U3y.nib +0 -0
  19. data/lib/resources/ISDColorPalette.storyboardc/UIViewController-Yhi-Ks-PNF.nib +0 -0
  20. data/lib/resources/ISDColorPalette.storyboardc/UIViewController-lfU-qe-W46.nib +0 -0
  21. data/lib/resources/ISDColorPalette.storyboardc/Yhi-Ks-PNF-view-DbN-fh-FRU.nib +0 -0
  22. data/lib/resources/ISDColorPalette.storyboardc/ccp-rq-upX-view-Gjb-sR-XRJ.nib +0 -0
  23. metadata +87 -0
@@ -0,0 +1,63 @@
1
+ # ISDColorPalette
2
+
3
+ ISDColorPalette is a color selection panel for the RubyMotion iOS app.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # in Gemfile
9
+ gem 'isd-color-palette'
10
+ ```
11
+
12
+ ISDColorPalette uses Sugarcube and BubbleWrap.
13
+
14
+ ```ruby
15
+ # in Gemfile
16
+ gem 'bubble-wrap'
17
+ # minimum set
18
+ gem 'sugarcube', :require => [
19
+ 'sugarcube-core',
20
+ 'sugarcube-localized',
21
+ 'sugarcube-color',
22
+ 'sugarcube-uikit',
23
+ 'sugarcube-nsuserdefaults'
24
+ ]
25
+ ```
26
+
27
+
28
+ ## Usage
29
+
30
+
31
+ ```ruby
32
+ # attr_accessor :color
33
+
34
+ # get a controller.
35
+ c = ISDColorPaletteViewController.colorPaletteViewController
36
+ # set an initial color.
37
+ c.selectedColor = self.color
38
+ # set a callback.
39
+ c.selected_color_block = Proc.new {|color| did_select_color color }
40
+ # push the controller.
41
+ self.navigationController.pushViewController c, animated:true
42
+ ```
43
+
44
+ If you want to get nil instead of the clear color, set #return_nil true.
45
+
46
+ ```
47
+ c.return_nil = true
48
+ ```
49
+
50
+ The selected_color_block is called after a color is selected.
51
+
52
+ ```
53
+ def did_select_color color
54
+ p color
55
+ self.color = color
56
+ end
57
+ ```
58
+
59
+ ## License
60
+
61
+ MIT License
62
+
63
+
@@ -0,0 +1,9 @@
1
+ unless defined?(Motion::Project::Config)
2
+ raise "This file must be required within a RubyMotion project Rakefile."
3
+ end
4
+
5
+ lib_dir_path = File.dirname(File.expand_path(__FILE__))
6
+ Motion::Project::App.setup do |app|
7
+ app.files.unshift(Dir.glob(File.join(lib_dir_path, "isd-color-palette/**/*.rb")))
8
+ app.resources_dirs << File.join(lib_dir_path, 'resources')
9
+ end
@@ -0,0 +1,123 @@
1
+ # -*- coding: utf-8 -*-
2
+ class ISDBasicColorCollectionViewController < UICollectionViewController
3
+
4
+ attr_reader :recent_colors, :system_colors, :web_colors
5
+
6
+ def viewDidLoad
7
+ super
8
+
9
+ load_recent_colors
10
+
11
+ @system_colors = [:clear, :white, :black, :dark_gray, :light_gray, :gray,
12
+ :red, :blue, :cyan, :yellow, :magenta, :orange, :purple, :brown
13
+ ]
14
+ @web_colors = Symbol.css_colors.dup.sort{|a, b|
15
+ f = (a.last >> 16) <=> (b.last >> 16)
16
+ case f
17
+ when 0
18
+ f = (a.last >> 8 & 0xff) <=> (b.last >> 8 & 0xff)
19
+ case f
20
+ when 0
21
+ (a.last & 0xff) <=> (b.last & 0xff)
22
+ else
23
+ f
24
+ end
25
+ else
26
+ f
27
+ end
28
+ }.map{|e| e.first}
29
+ end
30
+
31
+ def viewWillDisappear animated
32
+ super
33
+ record_recent_color
34
+ save_recent_colors
35
+ end
36
+
37
+ def numberOfSectionsInCollectionView collectionView
38
+ 3
39
+ end
40
+
41
+ def collectionView collectionView, numberOfItemsInSection:section
42
+ case section
43
+ when 0
44
+ self.recent_colors.size
45
+ when 1
46
+ self.system_colors.size
47
+ when 2
48
+ self.web_colors.size
49
+ end
50
+ end
51
+
52
+ def collectionView collectionView, viewForSupplementaryElementOfKind:kind, atIndexPath:indexPath
53
+ view = collectionView.dequeueReusableSupplementaryViewOfKind kind, withReuseIdentifier:"Header", forIndexPath:indexPath
54
+ case indexPath.section
55
+ when 0
56
+ view.headerLabel.text = "Recent"._
57
+ when 1
58
+ view.headerLabel.text = "System"._
59
+ when 2
60
+ view.headerLabel.text = "Web"._
61
+ end
62
+ view
63
+ end
64
+
65
+ CellIdentifier = "ColorWell"
66
+ def collectionView collectionView, cellForItemAtIndexPath:indexPath
67
+ cell = collectionView.dequeueReusableCellWithReuseIdentifier CellIdentifier, forIndexPath:indexPath
68
+ case indexPath.section
69
+ when 0
70
+ cell.color = self.recent_colors[indexPath.row].uicolor
71
+ when 1
72
+ cell.color = self.system_colors[indexPath.row].uicolor
73
+ when 2
74
+ cell.color = self.web_colors[indexPath.row].uicolor
75
+ else
76
+ end
77
+ cell
78
+ end
79
+
80
+ def collectionView collectionView, didSelectItemAtIndexPath:indexPath
81
+ cell = collectionView.cellForItemAtIndexPath indexPath
82
+ parentViewController.parentViewController.didChangeColor cell.color
83
+ collectionView.selectItemAtIndexPath indexPath, animated:true, scrollPosition:UICollectionViewScrollPositionNone
84
+ end
85
+
86
+
87
+ def collectionView collectionView, didDeselectItemAtIndexPath:indexPath
88
+ cell = collectionView.cellForItemAtIndexPath indexPath
89
+ collectionView.deselectItemAtIndexPath indexPath, animated:true
90
+ end
91
+
92
+
93
+ private
94
+
95
+ def load_recent_colors
96
+ @recent_colors = []
97
+ begin
98
+ a = NSUserDefaults["isd_color_palette_recent_colors"] || []
99
+ a.each do |s|
100
+ @recent_colors << UIColor.color_by_serialize(s)
101
+ end
102
+ rescue
103
+ end
104
+ end
105
+
106
+ def record_recent_color
107
+ color = self.parentViewController.parentViewController.selectedColor
108
+ @recent_colors.unshift color
109
+ end
110
+
111
+ def save_recent_colors
112
+ a = @recent_colors.map do |c|
113
+ (c || :clear.uicolor).to_serialize
114
+ end
115
+ # limit 7 x 3
116
+ a.uniq!
117
+ a[0, 21]
118
+ NSUserDefaults["isd_color_palette_recent_colors"] = a
119
+ end
120
+
121
+
122
+ end
123
+
@@ -0,0 +1,87 @@
1
+ # -*- coding: utf-8 -*-
2
+ class ISDColorPaletteContainerViewController < UIViewController
3
+
4
+ attr_accessor :basicColorView # IBOutlet UIView
5
+ attr_accessor :rgbaColorView # IBOutlet UIView
6
+
7
+ attr_reader :views
8
+
9
+ =begin
10
+ def initWithNibName nibNameOrNil, bundle:nibBundleOrNil
11
+ super
12
+ self
13
+ end
14
+ =end
15
+
16
+ def viewDidLoad
17
+ super
18
+ @views = [
19
+ self.basicColorView,
20
+ self.rgbaColorView
21
+ ]
22
+
23
+ select_view self.basicColorView
24
+ end
25
+
26
+ def select_view view
27
+ self.views.each do |v|
28
+ if v
29
+ v.hidden = v != view
30
+ end
31
+ end
32
+ end
33
+
34
+ =begin
35
+ def viewDidUnload
36
+ super
37
+ end
38
+ =end
39
+
40
+ =begin
41
+ def viewWillAppear animated
42
+ super
43
+ end
44
+ =end
45
+
46
+ =begin
47
+ def viewDidAppear animated
48
+ super
49
+ end
50
+ =end
51
+
52
+ =begin
53
+ def viewWillDisappear animated
54
+ super
55
+ end
56
+ =end
57
+
58
+ =begin
59
+ def viewDidDisappear animated
60
+ super
61
+ end
62
+ =end
63
+
64
+ =begin
65
+ def shouldAutorotateToInterfaceOrientation interfaceOrientation
66
+ interfaceOrientation == UIInterfaceOrientationPortrait
67
+ end
68
+ =end
69
+
70
+ =begin
71
+ def shouldAutorotate
72
+ true
73
+ end
74
+ =end
75
+
76
+ =begin
77
+ def supportedInterfaceOrientations
78
+ UIInterfaceOrientationMaskPortrait
79
+ end
80
+ =end
81
+
82
+ def didChangeValue sender # IBAction
83
+ view = self.views[sender.selectedSegmentIndex]
84
+ select_view view
85
+ end
86
+
87
+ end
@@ -0,0 +1,136 @@
1
+ # -*- coding: utf-8 -*-
2
+ class ISDColorPaletteViewController < UIViewController
3
+
4
+ attr_accessor :colorWellView # IBOutlet UIView
5
+ attr_accessor :colorNameLabel # IBOutlet UILabel
6
+
7
+ attr_accessor :selectedColor
8
+
9
+ attr_accessor :selected_color_block # -> {|color| }
10
+
11
+ # If it's true, return nil when the color is the clear color.
12
+ attr_accessor :return_nil
13
+
14
+
15
+ attr_reader :color_layer, :solid_layer
16
+
17
+ class << self
18
+
19
+ def colorPaletteViewController
20
+ sb = UIStoryboard.storyboardWithName "ISDColorPalette", bundle:nil
21
+ sb.instantiateViewControllerWithIdentifier "ISDColorPaletteViewController"
22
+ end
23
+
24
+ end
25
+
26
+ =begin
27
+ def initWithNibName nibNameOrNil, bundle:nibBundleOrNil
28
+ super
29
+ self
30
+ end
31
+ =end
32
+
33
+ def viewDidLoad
34
+ super
35
+ l = colorWellView.layer
36
+ l.borderColor = :black.uicolor.cgcolor
37
+ l.borderWidth = 2
38
+ l.cornerRadius = 4
39
+ l.masksToBounds = true
40
+ @color_layer = l
41
+
42
+ @solid_layer = CALayer.new
43
+ r2 = Math.sqrt 2
44
+ w = h = @color_layer.frame.size.height * r2
45
+ x = CGRectGetMaxX(@color_layer.bounds) - w / 2
46
+ y = -h / 2
47
+ @solid_layer.frame = CGRectMake(x, y, w, h)
48
+ rad = 45 * Math::PI / 180
49
+ @solid_layer.transform = CATransform3DMakeRotation(rad, 0, 0, 1)
50
+ @color_layer.addSublayer @solid_layer
51
+
52
+ set_color self.selectedColor
53
+ end
54
+
55
+ def selectedColor= color
56
+ willChangeValueForKey "selectedColor"
57
+ @selectedColor = color
58
+ didChangeValueForKey "selectedColor"
59
+ set_color color
60
+ end
61
+
62
+ =begin
63
+ def viewDidUnload
64
+ super
65
+ end
66
+ =end
67
+
68
+ =begin
69
+ def viewWillAppear animated
70
+ super
71
+ end
72
+ =end
73
+
74
+ =begin
75
+ def viewDidAppear animated
76
+ super
77
+ set_color self.selectedColor
78
+ end
79
+ =end
80
+
81
+ =begin
82
+ def viewWillDisappear animated
83
+ super
84
+ end
85
+ =end
86
+
87
+ =begin
88
+ def viewDidDisappear animated
89
+ super
90
+ end
91
+ =end
92
+
93
+ =begin
94
+ def shouldAutorotateToInterfaceOrientation interfaceOrientation
95
+ interfaceOrientation == UIInterfaceOrientationPortrait
96
+ end
97
+ =end
98
+
99
+ =begin
100
+ def shouldAutorotate
101
+ true
102
+ end
103
+ =end
104
+
105
+ =begin
106
+ def supportedInterfaceOrientations
107
+ UIInterfaceOrientationMaskPortrait
108
+ end
109
+ =end
110
+
111
+ def didChangeColor color
112
+ self.selectedColor = color
113
+ if self.selected_color_block
114
+ color = nil if self.return_nil && color == :clear.uicolor
115
+ self.selected_color_block.call color
116
+ end
117
+ end
118
+
119
+
120
+ private
121
+
122
+ def set_color color
123
+ return unless colorWellView
124
+
125
+ color ||= :clear.uicolor
126
+
127
+ self.color_layer.backgroundColor = color.uicolor.cgcolor
128
+ self.color_layer.removeAllAnimations
129
+ self.solid_layer.backgroundColor = color.uicolor(1).cgcolor
130
+ self.solid_layer.removeAllAnimations
131
+
132
+ colorNameLabel.text = color == :clear.uicolor ? "Clear"._ : color.color_name._
133
+ colorNameLabel.textColor = color.monochrome.red >= 0.5 ? :black.uicolor : :white.uicolor
134
+ end
135
+
136
+ end
@@ -0,0 +1,118 @@
1
+ # -*- coding: utf-8 -*-
2
+ class ISDRgbaColorViewController < UIViewController
3
+ include BW::KVO
4
+
5
+ attr_accessor :rView # IBOutlet ISDRgbaSliderView
6
+ attr_accessor :gView # IBOutlet ISDRgbaSliderView
7
+ attr_accessor :bView # IBOutlet ISDRgbaSliderView
8
+ attr_accessor :aView # IBOutlet ISDRgbaSliderView
9
+
10
+ attr_accessor :color
11
+
12
+ attr_reader :views
13
+
14
+ =begin
15
+ def initWithNibName nibNameOrNil, bundle:nibBundleOrNil
16
+ super
17
+ self
18
+ end
19
+ =end
20
+
21
+ def viewDidLoad
22
+ super
23
+ identities = %w(R G B A)
24
+ @views = [rView, gView, bView, aView]
25
+ @views.each_with_index do |v, i|
26
+ v.identity = identities[i]
27
+ end
28
+
29
+ end
30
+
31
+ =begin
32
+ def viewDidUnload
33
+ super
34
+ end
35
+ =end
36
+
37
+ def dealloc
38
+ unregist_kvo
39
+ end
40
+
41
+ def viewWillAppear animated
42
+ super
43
+
44
+ regist_kvo
45
+
46
+ c = self.parentViewController.parentViewController
47
+ set_color c.selectedColor
48
+ end
49
+
50
+ def regist_kvo
51
+ return if @registed_kvo
52
+
53
+ c = self.parentViewController.parentViewController
54
+ observe c, "selectedColor" do |old, new|
55
+ set_color new
56
+ end
57
+ end
58
+
59
+ def unregist_kvo
60
+ return unless @registed_kvo
61
+
62
+ c = self.parentViewController.parentViewController
63
+ unobserve c, "selectedColor"
64
+ end
65
+
66
+ =begin
67
+ def viewDidAppear animated
68
+ super
69
+ end
70
+ =end
71
+
72
+ =begin
73
+ def viewWillDisappear animated
74
+ super
75
+ end
76
+ =end
77
+
78
+ =begin
79
+ def viewDidDisappear animated
80
+ super
81
+ end
82
+ =end
83
+
84
+ =begin
85
+ def shouldAutorotateToInterfaceOrientation interfaceOrientation
86
+ interfaceOrientation == UIInterfaceOrientationPortrait
87
+ end
88
+ =end
89
+
90
+ =begin
91
+ def shouldAutorotate
92
+ true
93
+ end
94
+ =end
95
+
96
+ =begin
97
+ def supportedInterfaceOrientations
98
+ UIInterfaceOrientationMaskPortrait
99
+ end
100
+ =end
101
+
102
+ def set_color color
103
+ @views.each do |v|
104
+ v.color = color
105
+ end
106
+ end
107
+
108
+ def didChangeColor sender
109
+ c = self.parentViewController.parentViewController
110
+ color = UIColor.colorWithRed self.views[0].color_value,
111
+ green:self.views[1].color_value,
112
+ blue:self.views[2].color_value,
113
+ alpha:self.views[3].color_value
114
+ parentViewController.parentViewController.didChangeColor color
115
+ end
116
+
117
+
118
+ end