mirror of
https://github.com/zhigang1992/rmq.git
synced 2026-01-12 17:52:17 +08:00
Final refactoring; removes color pollution; adds from_base
This commit is contained in:
@@ -57,17 +57,3 @@ class UIViewController
|
||||
@_rmq_data ||= RubyMotionQuery::ControllerData.new
|
||||
end
|
||||
end
|
||||
|
||||
class UIColor
|
||||
def with(options)
|
||||
r, g, b, a = Pointer.new('d'), Pointer.new('d'), Pointer.new('d'), Pointer.new('d')
|
||||
self.getRed(r, green: g, blue: b, alpha: a)
|
||||
|
||||
r = options[:r] || options[:red] || r.value
|
||||
g = options[:g] || options[:green] || g.value
|
||||
b = options[:b] || options[:blue] || b.value
|
||||
a = options[:a] || options[:alpha] || a.value
|
||||
|
||||
UIColor.colorWithRed(r, green: g, blue: b, alpha: a)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
module RubyMotionQuery
|
||||
class RMQ
|
||||
# @return [Color]
|
||||
# @param color value options
|
||||
# @return [UIColor]
|
||||
# @example
|
||||
# color('#ffffff')
|
||||
# color('ffffff')
|
||||
# color('#336699cc')
|
||||
# color('369c')
|
||||
# color(255,255,255,0.5)
|
||||
# color(r: 255,g: 255,b: 255,a: 0.5)
|
||||
# color(red: 255,green: 255,blue: 255,alpha: 0.5)
|
||||
# color.from_hsva(h: 100,s: 140,b: 80,a: 1.0)
|
||||
def self.color(*params)
|
||||
if params.empty?
|
||||
Color
|
||||
@@ -9,7 +19,17 @@ module RubyMotionQuery
|
||||
end
|
||||
end
|
||||
|
||||
# @return [Color]
|
||||
# @param color value options
|
||||
# @return [UIColor]
|
||||
# @example
|
||||
# color('#ffffff')
|
||||
# color('ffffff')
|
||||
# color('#336699cc')
|
||||
# color('369c')
|
||||
# color(255,255,255,0.5)
|
||||
# color(r: 255,g: 255,b: 255,a: 0.5)
|
||||
# color(red: 255,green: 255,blue: 255,alpha: 0.5)
|
||||
# color.from_hsva(h: 100,s: 140,b: 80,a: 1.0)
|
||||
def color(*params)
|
||||
self.class.color(*params)
|
||||
end
|
||||
@@ -80,7 +100,7 @@ module RubyMotionQuery
|
||||
# my_label.color = rmq.color.foo # or just color.foo in a stylesheet
|
||||
def add_named(key, hex_or_color)
|
||||
color = if hex_or_color.is_a?(String)
|
||||
Color.from_hex(hex_or_color)
|
||||
ColorFactory.from_hex(hex_or_color)
|
||||
else
|
||||
hex_or_color
|
||||
end
|
||||
@@ -100,12 +120,7 @@ module RubyMotionQuery
|
||||
# color.from_hex('#336699cc')
|
||||
# color.from_hex('369c')
|
||||
def from_hex(str)
|
||||
r,g,b,a = case (str =~ /^#?(\h{3,8})$/ && $1.size)
|
||||
when 3, 4 then $1.scan(/./ ).map {|c| (c*2).to_i(16) }
|
||||
when 6, 8 then $1.scan(/../).map {|c| c.to_i(16) }
|
||||
else raise ArgumentError
|
||||
end
|
||||
from_rgba(r, g, b, a ? (a/255.0) : 1.0)
|
||||
ColorFactory.from_hex(str)
|
||||
end
|
||||
|
||||
# @return [UIColor]
|
||||
@@ -113,7 +128,7 @@ module RubyMotionQuery
|
||||
# @example
|
||||
# rmq.color.from_rgba(255,255,255,0.5)
|
||||
def from_rgba(r,g,b,a)
|
||||
UIColor.colorWithRed((r/255.0), green: (g/255.0), blue: (b/255.0), alpha: a)
|
||||
ColorFactory.from_rgba(r,g,b,a)
|
||||
end
|
||||
|
||||
# @return [UIColor]
|
||||
@@ -121,11 +136,11 @@ module RubyMotionQuery
|
||||
# @example
|
||||
# rmq.color.from_hsva(100,140,80,1.0)
|
||||
def from_hsva(h,s,v,a)
|
||||
UIColor.alloc.initWithHue(h, saturation: s, brightness: v, alpha: a)
|
||||
ColorFactory.from_hsva(h,s,v,a)
|
||||
end
|
||||
|
||||
def random
|
||||
from_rgba(rand(255), rand(255), rand(255), 1.0)
|
||||
ColorFactory.from_rgba(rand(255), rand(255), rand(255), 1.0)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -133,16 +148,30 @@ module RubyMotionQuery
|
||||
class ColorFactory
|
||||
def self.build(params)
|
||||
return Color if params.empty?
|
||||
return from_rgba_with_precision(*params) if params.count > 1
|
||||
return from_rgba(*params) if params.count > 1
|
||||
|
||||
param = params.first
|
||||
return from_hex_with_precision(params.join) if param.is_a?(String)
|
||||
return from_hex(params.join) if param.is_a?(String)
|
||||
|
||||
return from_base_color(param) if base_values(param)
|
||||
return try_rgba(param) if rgba_values(param)
|
||||
return try_hsva(param) if hsva_values(param)
|
||||
return try_hex(param) if hex_values(param)
|
||||
end
|
||||
|
||||
def self.from_base_color(values)
|
||||
base = values[:base] || values[:color]
|
||||
r, g, b, a = Pointer.new('d'), Pointer.new('d'), Pointer.new('d'), Pointer.new('d')
|
||||
base.getRed(r, green: g, blue: b, alpha: a)
|
||||
|
||||
r = values[:r] || values[:red] || r.value
|
||||
g = values[:g] || values[:green] || g.value
|
||||
b = values[:b] || values[:blue] || b.value
|
||||
a = values[:a] || values[:alpha] || a.value
|
||||
|
||||
UIColor.colorWithRed(r, green: g, blue: b, alpha: a)
|
||||
end
|
||||
|
||||
def self.try_rgba(values)
|
||||
r = values[:red] || values[:r]
|
||||
g = values[:green] || values[:g]
|
||||
@@ -150,7 +179,7 @@ module RubyMotionQuery
|
||||
a = values[:alpha] || values[:a] || 1.0
|
||||
raise ArgumentError unless r && g && b && a
|
||||
|
||||
from_rgba_with_precision(r, g, b, a)
|
||||
from_rgba(r, g, b, a)
|
||||
end
|
||||
|
||||
def self.try_hsva(values)
|
||||
@@ -167,7 +196,7 @@ module RubyMotionQuery
|
||||
hex = values[:hex] || values[:x]
|
||||
alpha = values[:alpha] || values[:a]
|
||||
|
||||
color = from_hex_with_precision(hex)
|
||||
color = from_hex(hex)
|
||||
color = color.colorWithAlphaComponent(alpha) if alpha
|
||||
color
|
||||
end
|
||||
@@ -176,6 +205,10 @@ module RubyMotionQuery
|
||||
values[:red] || values[:r] || values[:green] || values[:g] || values[:blue]
|
||||
end
|
||||
|
||||
def self.base_values(values)
|
||||
values[:base] || values[:color]
|
||||
end
|
||||
|
||||
def self.hsva_values(values)
|
||||
values[:hue] || values[:h] || values[:saturation] || values[:s] || values[:brightness]
|
||||
end
|
||||
@@ -184,17 +217,21 @@ module RubyMotionQuery
|
||||
values[:hex] || values[:x]
|
||||
end
|
||||
|
||||
def self.from_rgba_with_precision(r,g,b,a=1.0)
|
||||
def self.from_rgba(r,g,b,a=1.0)
|
||||
UIColor.colorWithRed((r/255.0), green: (g/255.0), blue: (b/255.0), alpha: a)
|
||||
end
|
||||
|
||||
def self.from_hex_with_precision(str)
|
||||
def self.from_hex(str)
|
||||
r,g,b,a = case (str =~ /^#?(\h{3,8})$/ && $1.size)
|
||||
when 3, 4 then $1.scan(/./ ).map {|c| (c*2).to_i(16) }
|
||||
when 6, 8 then $1.scan(/../).map {|c| c.to_i(16) }
|
||||
else raise ArgumentError
|
||||
end
|
||||
from_rgba_with_precision(r, g, b, a ? (a/255.0) : 1.0)
|
||||
from_rgba(r, g, b, a ? (a/255.0) : 1.0)
|
||||
end
|
||||
|
||||
def self.from_hsva(h,s,v,a)
|
||||
UIColor.alloc.initWithHue(h, saturation: s, brightness: v, alpha: a)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -201,5 +201,11 @@ describe 'color' do
|
||||
@rmq.color(h: 4, s: 3, b: 2)
|
||||
end
|
||||
end
|
||||
it 'should allow you to create another color from color with options' do
|
||||
color = @rmq.color(base: UIColor.blueColor, alpha: 0.5)
|
||||
color.should == UIColor.colorWithRed(0, green: 0, blue: 255, alpha: 0.5)
|
||||
color = @rmq.color(base: color, red: 255)
|
||||
color.should == UIColor.colorWithRed(255, green: 0, blue: 255, alpha: 0.5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -101,13 +101,6 @@ describe 'ext' do
|
||||
vc.rmq(view2).apply_style(:style_one)
|
||||
view2.style_applied.should == true
|
||||
end
|
||||
|
||||
it 'should allow you to create another color from color with options' do
|
||||
color = UIColor.blueColor.with(alpha: 0.5)
|
||||
color.should == UIColor.colorWithRed(0, green: 0, blue: 255, alpha: 0.5)
|
||||
color = UIColor.blueColor.with(red: 255)
|
||||
color.should == UIColor.colorWithRed(255, green: 0, blue: 255, alpha: 1.0)
|
||||
end
|
||||
end
|
||||
|
||||
class ExtTestView < UIView
|
||||
|
||||
Reference in New Issue
Block a user