Class: Gradient

Inherits:
Object
  • Object
show all
Defined in:
lib/css2cocoa/gradient.rb

Overview

“color-stop(.9,#999999)” puts Gradient.parse_gradient(“-webkit-gradient(linear, 0% 96%, 0% 100%, from(#38AFFF), to(#FFFFFF))”)

Class Method Summary collapse

Class Method Details

.generate_gradient_color_method(colors) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/css2cocoa/gradient.rb', line 44

def self.generate_gradient_color_method(colors)
  allocation    = "gradient.colors = [NSArray arrayWithObjects:"
  nil_object    =	"nil];"
  string_colors = ""
  colors.each do |color|
    string_colors << Gradient.ui_color_from_rgb(color)
  end
  allocation + string_colors + nil_object
end

.generate_gradient_location_method(locations) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/css2cocoa/gradient.rb', line 53

def self.generate_gradient_location_method(locations)
  allocation    = "gradient.locations = [NSArray arrayWithObjects:"
  nil_object    =	"nil];"
  string_locations = ""
  locations.each do |location|
    string_locations << Gradient.generate_ns_number(location)
  end
  allocation + string_locations + nil_object
end

.generate_ns_number(number) ⇒ Object



67
68
69
# File 'lib/css2cocoa/gradient.rb', line 67

def self.generate_ns_number(number)
  "[NSNumber numberWithFloat:#{number.to_f}],"
end

.generate_rgb_helperObject



40
41
42
# File 'lib/css2cocoa/gradient.rb', line 40

def self.generate_rgb_helper
  "#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]"
end

.parse_gradient(css_gradient, helper = true) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/css2cocoa/gradient.rb', line 4

def self.parse_gradient(css_gradient, helper=true)
  css_gradient_array = css_gradient.split(", ")
  start_location = css_gradient.split(",")[1].split("%").last.to_f/100.0
  end_location = css_gradient.split(",")[2].split("%").last.to_f/100.0
  
  start_color, stop_color = css_gradient.split(",")[3..-1]
  colors_and_locations = {}

  
  begin
    color_stops = css_gradient_array[5].split(",c")
    color_stops.each do |color_stop|
      stop, color = color_stop.scan(/(\.\d+|\w+)/).flatten[2..3] #[[".9", nil], [nil, "999999"]]
      colors_and_locations[stop.to_f] = color
    end
    puts "Generating multiple colour gradient!"
  rescue NoMethodError => e
    puts "Generating two colour gradient!"
  end

  colors_and_locations[start_location.to_f] = start_color.scan(/(\.\d+|\w+)/).flatten.last
  colors_and_locations[end_location.to_f]   = stop_color.scan(/(\.\d+|\w+)/).flatten.last
  colors = []
  locations = []
  colors_and_locations.sort.each do |location, color|
    colors << color
    locations << location
  end
  
"""
#{Gradient.generate_rgb_helper if helper}
#{Gradient.generate_gradient_location_method(locations)}
#{Gradient.generate_gradient_color_method(colors)}
"""
end

.ui_color_from_rgb(hex_value) ⇒ Object



63
64
65
# File 'lib/css2cocoa/gradient.rb', line 63

def self.ui_color_from_rgb(hex_value)
  "(id)UIColorFromRGB(0x#{hex_value}).CGColor," 			   
end