Module: Eletro::ColorCode

Included in:
Capacitor, Resistor
Defined in:
lib/eletro/helpers/color_code.rb

Constant Summary collapse

CODE =
[:k, :b, :r, :o, :y, :g, :u, :v, :a, :w, :l, :s]
COLORS =
{
:k => :black,
:b => :brown,
:r => :red,
:o => :orange,
:y => :yellow,
:g => :green,
:u => :blue,
:v => :violet,
:a => :gray,
:w => :white,
:l => :gold,
:s => :silver}
PRECISION =
[nil, 1, 2, nil, nil, 0.5, 0.25, 01, 0.05, nil, 5, 10, 20]

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorObject (readonly) Also known as: colors

Returns the value of attribute color.



7
8
9
# File 'lib/eletro/helpers/color_code.rb', line 7

def color
  @color
end

Instance Method Details

#bold(txt) ⇒ Object



96
# File 'lib/eletro/helpers/color_code.rb', line 96

def bold(txt);      "\e[2m#{txt}\e[0m";    end

#calc(char) ⇒ Object



41
42
43
# File 'lib/eletro/helpers/color_code.rb', line 41

def calc char
  CODE.index(char.downcase.to_sym)
end

#color2value(chars) ⇒ Object

Converts the color code value to numeric



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/eletro/helpers/color_code.rb', line 48

def color2value chars
  out = calc(chars[0])
  out = (out.to_s + calc(chars[1]).to_s).to_f
  out *= (10 ** (calc(chars[2])))
  if chars.size > 3
    @precision = PRECISION[calc(chars[3])]
  else
    #out *= (10 ** (calc(chars[2])))
  end
  out
end

#parse_color_code(params) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eletro/helpers/color_code.rb', line 28

def parse_color_code(params)
  return if params.empty?
  if params =~ /\d/
    # m = params.match(/(\d*[,|{k|m|g}|\.]?\d*)\s*(k|m|g)?/)
    # return unless m
    @value = parse(params)
    @color = value2color(@value)
  else
    @color = params.split(//)
    @value = color2value(@color)
  end
end

#rgblize(color) ⇒ Object

Creates a coloured version to print on stdout



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/eletro/helpers/color_code.rb', line 78

def rgblize color
  s = case color.to_s.downcase.to_sym
    when :k then  "\e[40m"
    when :b then  "\e[0;33m"
    when :r then  "\e[41m"
    when :g then  "\e[42m"
    when :y then  "\e[1;33m"
    when :o then  "\e[43m"
    when :u then  "\e[44m"
    when :v then  "\e[45m"
    when :w then  "\e[47m"
    when :a then  "\e[31m"
    else ".."
  end
  s += "#{color.to_s.upcase}\e[0m"
end

#value2color(value) ⇒ Object

Converts a numeric value into a color code array

          st   nd   rest (multiplier)
123.0 -> ['1', '2', '3.0']


67
68
69
70
71
72
# File 'lib/eletro/helpers/color_code.rb', line 67

def value2color value
  st, nd, *rest = value.to_s.split(//)
  out = [CODE[st.to_i], CODE[nd.to_i]]
  out << CODE[value.to_i.to_s.size-2]
  out
end