Created
February 7, 2014 19:19
-
-
Save thephw/8869841 to your computer and use it in GitHub Desktop.
A quick color helper for getting other CSS color types to RGBA, it copies it to your clipboard
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
######################## | |
## Ruby color helpers ## | |
######################## | |
require 'clipboard' | |
def css_color(text, output_type = "rgba") | |
if text.start_with? "#" | |
if text.length > 4 | |
colors = [text[1..2].to_i(16).to_s(10), text[3..4].to_i(16).to_s(10), text[5..6].to_i(16).to_s(10), 1] | |
else | |
colors = [(text[1]*2).to_i(16).to_s(10), (text[2]*2).to_i(16).to_s(10), (text[3]*2).to_i(16).to_s(10), 1] | |
end | |
elsif text.start_with? "rgba" | |
colors = text.gsub(/\s+/, "").match(/rgba\((\d+),(\d+),(\d+),(.+)\)/).to_a[1..4] | |
elsif text.start_with? "rgb" | |
colors = [text.gsub(/\s+/, "").match(/rgb\((\d+),(\d+),(\d+)\)/).to_a[1..3], 1].flatten | |
end | |
out = "rgba(#{colors.join(',')})" | |
Clipboard.copy out | |
out | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment