Module: ColorNamer

Defined in:
lib/color_namer.rb

Defined Under Namespace

Classes: ColorNames

Class Method Summary collapse

Class Method Details

.color_namesObject



54
55
56
# File 'lib/color_namer.rb', line 54

def color_names
  ColorNamer::ColorNames.names
end

.name(color) ⇒ Object

You can pass any Color object to this method



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/color_namer.rb', line 20

def name(color) # You can pass any Color object to this method
  
  find_rgb     = color.to_rgb
  find_hsl     = color.to_hsl
  return_color = nil
  df           = nil
  
  self.color_names.each do |hash, name, shade|
    return [hash, name, shade] if "##{hash}" == find_rgb.html
    
    # This part is ported from Javascript version found at http://chir.ag/tech/download/ntc
    
    rgb = Color::RGB.from_html(hash)
    hsl = rgb.to_hsl
    ndf1 = ((find_rgb.red - rgb.red) ** 2) + ((find_rgb.green - rgb.green) ** 2) + ((find_rgb.blue - rgb.blue) ** 2)
    ndf2 = (
            (((find_hsl.h * 255) - (hsl.h * 255)) ** 2) + 
            (((find_hsl.s * 255) - (hsl.s * 255)) ** 2) + 
            (((find_hsl.l * 255) - (hsl.l * 255)) ** 2)
           ).abs
    
    ndf = ndf1 + ndf2 * 2
    
    if df.nil? or df > ndf
      df = ndf
      return_color = [hash, name, shade]
    end
    
  end
  
  return return_color
  
end

.name_from_html_hash(hash) ⇒ Object



7
8
9
10
# File 'lib/color_namer.rb', line 7

def name_from_html_hash(hash)
  color = Color::RGB.from_html(hash)
  name(color)
end

.name_from_rgb(*rgb) ⇒ Object

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
# File 'lib/color_namer.rb', line 12

def name_from_rgb(*rgb)
  rgb = rgb[0].split(',').map{ |v| v.strip } if rgb.size == 1
  raise ArgumentError, "Wrong format of RGB value. Use 'r,g,b' or [r,g,b]" if rgb.size < 3
  rgb.map!(&:to_i)
  color = Color::RGB.new(rgb[0], rgb[1], rgb[2])
  name(color)
end