Class: Color

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Color

Returns a new instance of Color.



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pallet.rb', line 8

def initialize(*args)
  begin
    if args.first.kind_of? String then
      set_hexval_from_string(args)
    end
  rescue ArgumentError => e
    puts "#{e}"
    puts "Bailing out till bad color is fixed in #{__FILE__} around line #{__LINE__}"
    
  end
end

Instance Method Details

#hex_strObject



25
26
27
# File 'lib/pallet.rb', line 25

def hex_str
  "##{hex_val}"
end

#hex_valObject



21
22
23
# File 'lib/pallet.rb', line 21

def hex_val
  @hex_val
end

#html_chipObject



42
43
44
45
46
47
48
49
50
# File 'lib/pallet.rb', line 42

def html_chip
  chip_w,chip_h = 50,50
  "<div class='pallet color_chip'>
    <p class='pallet color_chip' background-color:#{hex_str};width:#{chip_w}px;height:#{chip_h}px;'>
      
    </p>
    #{hex_str}
  </div>"
end

#set_hexval_from_string(str) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pallet.rb', line 29

def set_hexval_from_string(str)
    if (str.first.strip =~ /#*([A-F0-9]{3,6})/i) then
        val = $+
        if(val.size == 3) then
          val = "#{val[0] * 2}#{val[1] * 2}#{val[2] * 2}"
        end
        @hex_val = val
    else
        raise ArgumentError, "#{self} Expected a string of form '0F0F0F' or '#0F0F0F' got '#{str}'"
    end      
    true
end