Skip to content

Instantly share code, notes, and snippets.

@thephw
Created February 7, 2014 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thephw/8869841 to your computer and use it in GitHub Desktop.
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
########################
## 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