From 1422da6e5153c77cd1ef5fa3c798526dc85c8f62 Mon Sep 17 00:00:00 2001 From: Sean Ho Date: Sat, 29 Sep 2012 16:47:09 +1000 Subject: [PATCH] Replace UIColor.from_html with String.to_color --- .../builders/helpers/has_background_color.rb | 2 +- lib/simple_view/builders/helpers/has_color.rb | 2 +- .../builders/helpers/has_text_color.rb | 2 +- .../builders/helpers/has_tint_color.rb | 2 +- lib/simple_view/builders/ui_label_builder.rb | 6 +++--- .../builders/ui_progress_view_builder.rb | 4 ++-- lib/simple_view/builders/ui_slider_builder.rb | 6 +++--- lib/simple_view/builders/ui_switch_builder.rb | 2 +- .../builders/ui_tab_bar_builder.rb | 2 +- .../builders/ui_table_view_builder.rb | 2 +- lib/simple_view/builders/ui_view_builder.rb | 4 ---- lib/simple_view/extensions/string.rb | 19 +++++++++++++++++ lib/simple_view/extensions/ui_color.rb | 21 +++++++------------ spec/extensions/string_spec.rb | 7 +++++++ spec/extensions/ui_color_spec.rb | 18 +++++----------- 15 files changed, 53 insertions(+), 46 deletions(-) create mode 100644 lib/simple_view/extensions/string.rb create mode 100644 spec/extensions/string_spec.rb diff --git a/lib/simple_view/builders/helpers/has_background_color.rb b/lib/simple_view/builders/helpers/has_background_color.rb index a016bfe..99d3f74 100644 --- a/lib/simple_view/builders/helpers/has_background_color.rb +++ b/lib/simple_view/builders/helpers/has_background_color.rb @@ -2,7 +2,7 @@ module SimpleView module Builders module HasBackgroundColor def setBackgroundColor(color) - @view.backgroundColor = color_with(color) + @view.backgroundColor = color.to_color end alias_method :setBackground_color, :setBackgroundColor end diff --git a/lib/simple_view/builders/helpers/has_color.rb b/lib/simple_view/builders/helpers/has_color.rb index 7c8f327..2e6e9ff 100644 --- a/lib/simple_view/builders/helpers/has_color.rb +++ b/lib/simple_view/builders/helpers/has_color.rb @@ -2,7 +2,7 @@ module SimpleView module Builders module HasColor def setColor(color) - @view.color = color_with(color) + @view.color = color.to_color end end end diff --git a/lib/simple_view/builders/helpers/has_text_color.rb b/lib/simple_view/builders/helpers/has_text_color.rb index dccb542..ec2a9dd 100644 --- a/lib/simple_view/builders/helpers/has_text_color.rb +++ b/lib/simple_view/builders/helpers/has_text_color.rb @@ -2,7 +2,7 @@ module SimpleView module Builders module HasTextColor def setTextColor(color) - @view.textColor = color_with(color) + @view.textColor = color.to_color end alias_method :setText_color, :setTextColor end diff --git a/lib/simple_view/builders/helpers/has_tint_color.rb b/lib/simple_view/builders/helpers/has_tint_color.rb index 1883f08..56267d9 100644 --- a/lib/simple_view/builders/helpers/has_tint_color.rb +++ b/lib/simple_view/builders/helpers/has_tint_color.rb @@ -2,7 +2,7 @@ module SimpleView module Builders module HasTintColor def setTintColor(color) - @view.tintColor = color_with(color) + @view.tintColor = color.to_color end alias_method :setTint_color, :setTintColor end diff --git a/lib/simple_view/builders/ui_label_builder.rb b/lib/simple_view/builders/ui_label_builder.rb index 0377311..e9941db 100644 --- a/lib/simple_view/builders/ui_label_builder.rb +++ b/lib/simple_view/builders/ui_label_builder.rb @@ -4,17 +4,17 @@ class UILabelBuilder < UIViewBuilder include SimpleView::Builders::HasFont def setTextColor(color) - @view.textColor = color_with(color) + @view.textColor = color.to_color end alias_method :setText_color, :setTextColor def setHighlightedTextColor(color) - @view.highlightedTextColor = color_with(color) + @view.highlightedTextColor = color.to_color end alias_method :setHighlighted_text_color, :setHighlightedTextColor def setShadowColor(color) - @view.shadowColor = color_with(color) + @view.shadowColor = color.to_color end alias_method :setShadow_color, :setShadowColor end diff --git a/lib/simple_view/builders/ui_progress_view_builder.rb b/lib/simple_view/builders/ui_progress_view_builder.rb index feca1f2..c23cd7e 100644 --- a/lib/simple_view/builders/ui_progress_view_builder.rb +++ b/lib/simple_view/builders/ui_progress_view_builder.rb @@ -15,12 +15,12 @@ def setTrackImage(image) end def setProgressTintColor(color) - @view.progressTintColor = color_with(color) + @view.progressTintColor = color.to_color end alias_method :setProgress_tint_color, :setProgressTintColor def setTrackTintColor(color) - @view.trackTintColor = color_with(color) + @view.trackTintColor = color.to_color end alias_method :setTrack_tint_color, :setTrackTintColor end diff --git a/lib/simple_view/builders/ui_slider_builder.rb b/lib/simple_view/builders/ui_slider_builder.rb index 87f7243..a749126 100644 --- a/lib/simple_view/builders/ui_slider_builder.rb +++ b/lib/simple_view/builders/ui_slider_builder.rb @@ -34,17 +34,17 @@ def setMaximumValueImage(image) end def setMinimumTrackTintColor(color) - @view.minimumTrackTintColor = color_with(color) + @view.minimumTrackTintColor = color.to_color end alias_method :setMinimum_track_tint_color, :setMinimumTrackTintColor def setMaximumTrackTintColor(color) - @view.maximumTrackTintColor = color_with(color) + @view.maximumTrackTintColor = color.to_color end alias_method :setMaximum_track_tint_color, :setMaximumTrackTintColor def setThumbTintColor(color) - @view.thumbTintColor = color_with(color) + @view.thumbTintColor = color.to_color end alias_method :setThumb_tint_color, :setThumbTintColor end diff --git a/lib/simple_view/builders/ui_switch_builder.rb b/lib/simple_view/builders/ui_switch_builder.rb index 8a3d200..d4a9c28 100644 --- a/lib/simple_view/builders/ui_switch_builder.rb +++ b/lib/simple_view/builders/ui_switch_builder.rb @@ -2,7 +2,7 @@ module SimpleView module Builders class UISwitchBuilder < UIControlBuilder def setOnTintColor(color) - @view.onTintColor = color_with(color) + @view.onTintColor = color.to_color end alias_method :setOn_tint_color, :setOnTintColor end diff --git a/lib/simple_view/builders/ui_tab_bar_builder.rb b/lib/simple_view/builders/ui_tab_bar_builder.rb index e6938f7..50a8029 100644 --- a/lib/simple_view/builders/ui_tab_bar_builder.rb +++ b/lib/simple_view/builders/ui_tab_bar_builder.rb @@ -12,7 +12,7 @@ def setSelectedImage(image) end def setSelectedImageTintColor(color) - @view.selectedImageTintColor = color_with(color) + @view.selectedImageTintColor = color.to_color end alias_method :setSelected_image_tint_color, :setSelectedImageTintColor end diff --git a/lib/simple_view/builders/ui_table_view_builder.rb b/lib/simple_view/builders/ui_table_view_builder.rb index f8f643b..9ebb0cb 100644 --- a/lib/simple_view/builders/ui_table_view_builder.rb +++ b/lib/simple_view/builders/ui_table_view_builder.rb @@ -7,7 +7,7 @@ def view_for_class(klass, options = {}) end def setSeparatorColor color - @view.separatorColor = color_with(color) + @view.separatorColor = color.to_color end alias_method :setSeparator_color, :setSeparatorColor end diff --git a/lib/simple_view/builders/ui_view_builder.rb b/lib/simple_view/builders/ui_view_builder.rb index 207f5b2..9aaa92d 100644 --- a/lib/simple_view/builders/ui_view_builder.rb +++ b/lib/simple_view/builders/ui_view_builder.rb @@ -60,10 +60,6 @@ def font_with font font.is_a?(String) ? UIFont.parse(font) : font end - def color_with color - color.is_a?(String) ? UIColor.from_html(color) : color - end - def image_with image image.is_a?(String) ? UIImage.imageNamed(image) : image end diff --git a/lib/simple_view/extensions/string.rb b/lib/simple_view/extensions/string.rb new file mode 100644 index 0000000..53908ba --- /dev/null +++ b/lib/simple_view/extensions/string.rb @@ -0,0 +1,19 @@ +module SimpleView + module String + def to_color + html_colour = self.gsub(%r{[#;]}, '') + case html_colour.size + when 3 + colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) } + when 6 + colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) } + else + raise ArgumentError + end + + ::UIColor.colorWithRed(colours[0]/255.0, green: colours[1]/255.0, blue: colours[2]/255.0, alpha: 1) + end + end +end + +String.send(:include, SimpleView::String) \ No newline at end of file diff --git a/lib/simple_view/extensions/ui_color.rb b/lib/simple_view/extensions/ui_color.rb index 442e4e2..3302bae 100644 --- a/lib/simple_view/extensions/ui_color.rb +++ b/lib/simple_view/extensions/ui_color.rb @@ -1,16 +1,9 @@ -class UIColor - # reference: http://color.rubyforge.org/ - def self.from_html html_colour - html_colour = html_colour.gsub(%r{[#;]}, '') - case html_colour.size - when 3 - colours = html_colour.scan(%r{[0-9A-Fa-f]}).map { |el| (el * 2).to_i(16) } - when 6 - colours = html_colour.scan(%r<[0-9A-Fa-f]{2}>).map { |el| el.to_i(16) } - else - raise ArgumentError +module SimpleView + module UIColor + def to_color + self end - - UIColor.colorWithRed(colours[0]/255.0, green: colours[1]/255.0, blue: colours[2]/255.0, alpha: 1) end -end \ No newline at end of file +end + +UIColor.send(:include, SimpleView::UIColor) \ No newline at end of file diff --git a/spec/extensions/string_spec.rb b/spec/extensions/string_spec.rb new file mode 100644 index 0000000..273435e --- /dev/null +++ b/spec/extensions/string_spec.rb @@ -0,0 +1,7 @@ +describe "SimpleView::String" do + describe "#to_color" do + it "should convert string to UIColor" do + "#f00".to_color.should == UIColor.redColor + end + end +end \ No newline at end of file diff --git a/spec/extensions/ui_color_spec.rb b/spec/extensions/ui_color_spec.rb index d966f4e..f7f0ffa 100644 --- a/spec/extensions/ui_color_spec.rb +++ b/spec/extensions/ui_color_spec.rb @@ -1,16 +1,8 @@ -describe "UIColor" do - describe "#from_html" do - it "should convert HTML hex color to UIColor" do - color = UIColor.from_html("#f00") - r = Pointer.new(:float) - g = Pointer.new(:float) - b = Pointer.new(:float) - a = Pointer.new(:float) - color.getRed(r, green: g, blue: b, alpha: a) - r[0].should == 1 - g[0].should == 0 - b[0].should == 0 - a[0].should == 1 +describe "SimpleView::UIColor" do + describe "#to_color" do + it "should return self" do + color = UIColor.redColor + color.to_color.should == color end end end \ No newline at end of file