Created
August 7, 2013 17:19
-
-
Save citrus/6176244 to your computer and use it in GitHub Desktop.
A utility to convert a hex color to a UIColor in RubyMotion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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