class Gradient

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

Public Class Methods

generate_gradient_color_method(colors) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/css2cocoa/gradient.rb, line 67
def self.generate_ns_number(number)
  "[NSNumber numberWithFloat:#{number.to_f}],"
end
generate_rgb_helper() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/css2cocoa/gradient.rb, line 63
def self.ui_color_from_rgb(hex_value)
  "(id)UIColorFromRGB(0x#{hex_value}).CGColor,"                          
end