Class: HtmlColor

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/generators/active_seed/templates/example_html_colors/html_color.rb

Instance Method Summary collapse

Instance Method Details

#best_contrast(colors) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/active_seed/templates/example_html_colors/html_color.rb', line 20

def best_contrast(colors)
  best_contrast = 0;
  return_color = "000000"
  colors.each do |c|
    contrast = self.luminosity_contrast(c)
    if contrast > best_contrast
      best_contrast = contrast
      return_color = c.hex_code
    end
  end
  return_color
end

#blueObject



41
42
43
# File 'lib/generators/active_seed/templates/example_html_colors/html_color.rb', line 41

def blue
  self.hex_code.to_i(16) & 255
end

#greenObject



37
38
39
# File 'lib/generators/active_seed/templates/example_html_colors/html_color.rb', line 37

def green
  (self.hex_code.to_i(16) >> 8) & 255
end

#luminosity_contrast(compare) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/generators/active_seed/templates/example_html_colors/html_color.rb', line 4

def luminosity_contrast(compare)
  lum1 = 0.2126 * (self.red/255 ** 2.2)
  + 0.7152 * (self.green/255 ** 2.2)
  + 0.0722 * (self.blue/255 ** 2.2)

  lum2 = 0.2126 * (compare.red/255 ** 2.2)
  + 0.7152 * (compare.green/255 ** 2.2)
  + 0.0722 * (compare.blue/255 ** 2.2)
 
  if(lum1 > lum2)
    return (lum1+0.05) / (lum2+0.05);
  else
    return (lum2+0.05) / (lum1+0.05);
  end
end

#redObject



33
34
35
# File 'lib/generators/active_seed/templates/example_html_colors/html_color.rb', line 33

def red
  (self.hex_code.to_i(16) >> 16) & 255
end