Created
January 6, 2012 11:18
-
-
Save VoQn/1570166 to your computer and use it in GitHub Desktop.
Interactive RGB Color Hex Convert
This file contains hidden or 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
# Interactive RGB Color Hex Convert | |
# usage: | |
# $ ruby rgb_to_hex.rb | |
# r g b > 255 51 234 | |
# hex: #ff33aa | |
# r g b > 0 | |
# ### Please type 3 int values: red green blue | |
# r g b > 132 256 -1 | |
# ### Please type 3 int values: (0 <= x <= 255) | |
# r g b > quit | |
# bye | |
# rgb is Integer Array etc [r, g, b] (0 <= n <= 255) | |
def conv(rgb) | |
msg = '### Please type 3 int values' | |
return "#{msg}:: red green blue" if rgb.length < 3 | |
return "#{msg}:: (0 <= x <= 255)" unless rgb.all? {|i| (0..255).include? i } | |
"hex: ##{rgb.map {|i| '%02x' % i }.join '' }" | |
end | |
def prompt | |
print 'r g b > ' | |
gets.strip | |
end | |
# main loop | |
def main | |
input = prompt() | |
until input == 'quit' | |
puts conv(input.split.map &:to_i) | |
input = prompt() | |
end | |
puts 'bye' | |
end | |
if $0 == __FILE__ | |
main | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment