lib/decolmor/main.rb in decolmor-1.0.0 vs lib/decolmor/main.rb in decolmor-1.1.0
- old
+ new
@@ -1,11 +1,17 @@
module Decolmor
#========= HEX <==> RGB(A) =============================================
- def self.hex_to_rgb(hex)
- rgb = hex.gsub('#','').scan(/../).map(&:hex).map(&:to_i)
- rgb.size == 4 ? rgb + [(rgb.delete_at(3) / 255.to_f).round(5)] : rgb
+ def self.hex_to_rgb(hex, alpha_round = 3)
+ hex = hex.gsub('#','')
+ hex = if [3, 4].include? hex.length
+ hex.chars.map{ |char| char * 2 }
+ else
+ hex.scan(/../)
+ end
+ rgb = hex.map(&:hex)
+ rgb.size == 4 ? rgb + [(rgb.delete_at(3) / 255.to_f).round(alpha_round)] : rgb
end
def self.rgb_to_hex(rgb)
template = rgb.size == 3 ? "#%02X%02X%02X" : "#%02X%02X%02X%02X"
rgb = rgb[0..2] + [(rgb[3] * 255).round] if rgb.size == 4