Module: AdapterHelper::RgbToHex

Included in:
Premailer::Adapter::Nokogiri, Premailer::Adapter::NokogiriFast, Premailer::Adapter::Nokogumbo
Defined in:
lib/premailer/adapter/rgb_to_hex.rb

Instance Method Summary collapse

Instance Method Details

#ensure_hex(color) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/premailer/adapter/rgb_to_hex.rb', line 25

def ensure_hex(color)
  match_data = is_rgb?(color)
  if match_data
    "#{to_hex(match_data[1])}#{to_hex(match_data[2])}#{to_hex(match_data[3])}"
  else
    color
  end
end

#is_rgb?(color) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/premailer/adapter/rgb_to_hex.rb', line 9

def is_rgb?(color)
  pattern = %r{
  rgb
  \(\s*                    # literal open, with optional whitespace
  (\d{1,3})                # capture 1-3 digits
  (?:\s*,\s*|\s+)          # comma or whitespace
  (\d{1,3})                # capture 1-3 digits
  (?:\s*,\s*|\s+)          # comma or whitespacee
  (\d{1,3})                # capture 1-3 digits
  \s*(?:\/\s*\d*\.?\d*%?)? # optional alpha modifier
  \s*\)                    # literal close, with optional whitespace
  }x

  pattern.match(color)
end

#to_hex(str) ⇒ Object



5
6
7
# File 'lib/premailer/adapter/rgb_to_hex.rb', line 5

def to_hex(str)
  str.to_i.to_s(16).rjust(2, '0').upcase
end