forked from seanho/SimpleView
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace UIColor.from_html with String.to_color
- Loading branch information
Showing
15 changed files
with
53 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
end | ||
|
||
UIColor.send(:include, SimpleView::UIColor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |