module Resistor::ColorCode

Constants

DIGIT
4-Band-Code

The 1st and 2nd digit of a resistance value

5-Band-Code

The 1st, 2nd and 3rd digit of a resistance value

E12_SERIES
E24_SERIES
E48_SERIES
E96_SERIES
MULTIPLIER
4-Band-Code

The 3rd color band indicates the multiplier.

5-Band-Code

The 4th color band indicates the multiplier.

TOLERANCE
4-Band-Code

The 4th color band indicates the tolerance.

5-Band-Code

The 5th color band indicates the tolerance.

Public Instance Methods

decode(code, options = {}) click to toggle source

Converts a color code to a resistance value.

@param code [Array<Symbol>, Array<String>] color code @option options [Integer] :band_number(4) @raise [ArgumentError] Error raised

when the supplied color code is not an array,
or when the color of the first band is black.

@return [Float] resistance value

# File lib/resistor/color_code.rb, line 172
def decode(code, options = {})
  raise ArgumentError unless code.is_a? Array
  code = code.map(&:to_sym)
  return 0.0 if code == [:black]
  raise ArgumentError if code[0] == :black

  default = Resistor::Options.new
  options[:band_number] ||= default.band_number

  case options[:band_number]
  when 4 then four_band_decode(code)
  when 5 then five_band_decode(code)
  else raise ArgumentError
  end
end
encode(ohm, options = {}) click to toggle source

Converts a resistance value to a color code. The value must be between 0.1 and 99_000_000.

@param ohm [Integer, Float] resistance value @option options [Integer, Float] :tolerance(5.0) @option options [Integer] :band_number(4) @raise [ArgumentError] Error raised

when the supplied resistance value is less than 0.1.

@return [Array<Symbol>] color code

# File lib/resistor/color_code.rb, line 94
def encode(ohm, options = {})
  return [DIGIT.key(0)] if ohm == 0
  raise ArgumentError if ohm < 0.1

  default = Resistor::Options.new
  options[:tolerance] ||= default.tolerance
  options[:band_number] ||= default.band_number

  case options[:band_number]
  when 4 then four_band_encode(ohm, options)
  when 5 then five_band_encode(ohm, options)
  else raise ArgumentError
  end
end
five_band_decode(code) click to toggle source

This method is used by the ‘Colorcode.decode` method when `options` is 5.

# File lib/resistor/color_code.rb, line 198
def five_band_decode(code)
  (DIGIT[code[0]]*100 + DIGIT[code[1]]*10 + DIGIT[code[2]]) *
  10**MULTIPLIER[code[3]]
  .to_f
end
five_band_encode(ohm, options = {}) click to toggle source

This method is used by the ‘Colorcode.encode` method when `options` is 5.

# File lib/resistor/color_code.rb, line 137
def five_band_encode(ohm, options = {})
  if ohm < 10
    ohm_str = (ohm*100).to_s.split('')
    [DIGIT.key(ohm_str[0].to_i),
     DIGIT.key(ohm_str[1].to_i),
     DIGIT.key(ohm_str[2].to_i),
     MULTIPLIER.key(-2),
     TOLERANCE.key(options[:tolerance])]

  elsif ohm < 100
    ohm_str = (ohm*10).to_s.split('')
    [DIGIT.key(ohm_str[0].to_i), 
     DIGIT.key(ohm_str[1].to_i),
     DIGIT.key(ohm_str[2].to_i),
     MULTIPLIER.key(-1),
     TOLERANCE.key(options[:tolerance])]

  else
    ohm_str = ohm.to_i.to_s.split('')
    [DIGIT.key(ohm_str[0].to_i), 
     DIGIT.key(ohm_str[1].to_i),
     DIGIT.key(ohm_str[2].to_i),
     MULTIPLIER.key(ohm_str.size - 3),
     TOLERANCE.key(options[:tolerance])]
  end
end
four_band_decode(code) click to toggle source

This method is used by the ‘Colorcode.decode` method when `options` is 4.

# File lib/resistor/color_code.rb, line 190
def four_band_decode(code)
  (DIGIT[code[0]]*10 + DIGIT[code[1]]) *
  10**MULTIPLIER[code[2]]
  .to_f
end
four_band_encode(ohm, options = {}) click to toggle source

This method is used by the ‘Colorcode.encode` method when `options` is 4.

# File lib/resistor/color_code.rb, line 111
def four_band_encode(ohm, options = {})
  if ohm < 1
    ohm_str = (ohm*100).to_s.split('')
    [DIGIT.key(ohm_str[0].to_i),
     DIGIT.key(ohm_str[1].to_i),
     MULTIPLIER.key(-2),
     TOLERANCE.key(options[:tolerance])]

  elsif ohm < 10
    ohm_str = (ohm*10).to_s.split('')
    [DIGIT.key(ohm_str[0].to_i),
     DIGIT.key(ohm_str[1].to_i),
     MULTIPLIER.key(-1),
     TOLERANCE.key(options[:tolerance])]

  else
    ohm_str = ohm.to_i.to_s.split('')
    [DIGIT.key(ohm_str[0].to_i),
     DIGIT.key(ohm_str[1].to_i),
     MULTIPLIER.key(ohm_str.size - 2),
     TOLERANCE.key(options[:tolerance])]
  end
end