Skip to content

Instantly share code, notes, and snippets.

@citrus
Created August 7, 2013 17:19
Show Gist options
  • Save citrus/6176244 to your computer and use it in GitHub Desktop.
Save citrus/6176244 to your computer and use it in GitHub Desktop.
A utility to convert a hex color to a UIColor in RubyMotion
#!/usr/bin/env ruby
require "bigdecimal"
def to_percent(c)
(c / BigDecimal.new("255.0")).round(3).to_f
end
color = ARGV.shift.to_s.strip.sub("#", "")
unless color.length == 3 || color.length == 6
puts "Invalid Color"
exit
end
color = color * 2 if color.length == 3
r, g, b = color.scan(/.{2}/).map(&:hex)
puts "R: #{r}"
puts "G: #{g}"
puts "B: #{b}"
color = "UIColor.colorWithRed(#{to_percent(r)}, green:#{to_percent(g)}, blue:#{to_percent(b)}, alpha:1)"
puts color
system "echo '#{color}' | pbcopy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment