Class: RubyMotionQuery::Color

Inherits:
UIColor
  • Object
show all
Defined in:
motion/ruby_motion_query/color.rb

Overview

Examples:

# Standard colors:

# In a stylesheet, you can just use color. Anywhere else these would be
# rmq.color.clear, etc

color.clear      
color.white      
color.light_gray 
color.gray       
color.dark_gray  
color.black      

color.red        
color.green      
color.blue       
color.yellow     
color.orange     
color.purple     
color.brown      
color.cyan       
color.magenta    

color.table_view 
color.scroll_view
color.flipside   
color.under_page 
color.light_text 
color.dark_text  

Class Method Summary (collapse)

Class Method Details

+ (UIColor) add_named(key, hex_or_color)

Add your own standard color

Examples:

rmq.color.add_named(:foo, '#ffffff')
my_label.color = rmq.color.foo # or just color.foo in a stylesheet

Returns:

  • (UIColor)


77
78
79
80
81
82
83
84
85
86
87
# File 'motion/ruby_motion_query/color.rb', line 77

def add_named(key, hex_or_color)
  color = if hex_or_color.is_a?(String)
    Color.from_hex(hex_or_color)
  else
    hex_or_color
  end

  Color.define_singleton_method(key) do
    color
  end
end

+ (UIColor) from_hex(hex_color)

Creates a color from a hex triplet

Thanks bubblewrap for this method

Examples:

color.from_hex('#ffffff')
color.from_hex('ffffff')

Parameters:

  • hex

    with or without the #

Returns:

  • (UIColor)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'motion/ruby_motion_query/color.rb', line 98

def from_hex(hex_color)
  hex_color = hex_color.gsub("#", "")   
  case hex_color.size 
    when 3
      colors = hex_color.scan(%r{[0-9A-Fa-f]}).map{ |el| (el * 2).to_i(16) }
    when 6
      colors = hex_color.scan(%r<[0-9A-Fa-f]{2}>).map{ |el| el.to_i(16) }        
    else
      raise ArgumentError
  end 
  if colors.size == 3
    from_rgba(colors[0], colors[1], colors[2], 1.0)
  else
    raise ArgumentError
  end 
end

+ (UIColor) from_hsva(h, s, v, a)

Examples:

rmq.color.from_hsva(100,140,80,1.0)

Returns:

  • (UIColor)


127
128
129
# File 'motion/ruby_motion_query/color.rb', line 127

def from_hsva(h,s,v,a)
  UIColor.alloc.initWithHue(h, saturation: s, brightness: v, alpha: a)
end

+ (UIColor) from_rgba(r, g, b, a)

Examples:

rmq.color.from_rgba(255,255,255,0.5)

Returns:

  • (UIColor)


119
120
121
# File 'motion/ruby_motion_query/color.rb', line 119

def from_rgba(r,g,b,a)
  UIColor.colorWithRed((r/255.0), green: (g/255.0), blue: (b/255.0), alpha: a)
end

+ (Object) random



131
132
133
# File 'motion/ruby_motion_query/color.rb', line 131

def random
  from_rgba(rand(255), rand(255), rand(255), 1.0)
end