Class: ColorConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/color_config.rb

Constant Summary collapse

FILES =
["colors.yml", "#{ENV['HOME']}/.todo.rb/colors.yml"]

Instance Method Summary collapse

Constructor Details

#initializeColorConfig

Returns a new instance of ColorConfig.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/color_config.rb', line 7

def initialize
  @dict = {
    'context' =>  "cyan",
    'project' => "DC143C",
    'priority' => 'FFFF00'
  }
  if (file = FILES.detect {|x| File.exist?(x)})
    @dict.merge!(YAML::load(File.read(file)))
  end
  # correct any non hex color names
  @dict.each {|k, v|
    if v !~ /[A-F0-9]{6}/
      c = Color::CSS[v]
      if c
        @dict[k] = c.html
      end
    end
  }
end

Instance Method Details

#html(key) ⇒ Object



36
37
38
# File 'lib/color_config.rb', line 36

def html(key)
  @dict[key] && "#" + raw(key).scan(/[A-F0-9]{2}/).join
end

#raw(key) ⇒ Object



27
28
29
30
# File 'lib/color_config.rb', line 27

def raw(key)
  s = @dict[key].to_s.sub(/^#/, '').upcase
  s.length == 3 ? (s + s) : s
end

#rgb(key) ⇒ Object



32
33
34
# File 'lib/color_config.rb', line 32

def rgb(key)
  @dict[key] && "RGB_" + raw(key).scan(/[A-F0-9]{2}/).join
end