Module: Resistor::ColorCode
- Defined in:
- lib/resistor/color_code.rb
Constant Summary collapse
- 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
{ :black => 0, :brown => 1, :red => 2, :orange => 3, :yellow => 4, :green => 5, :blue => 6, :purple => 7, :gray => 8, :white => 9 }.freeze
- MULTIPLIER =
- 4-Band-Code
-
The 3rd color band indicates the multiplier.
- 5-Band-Code
-
The 4th color band indicates the multiplier.
{ :black => 0, :brown => 1, :red => 2, :orange => 3, :yellow => 4, :green => 5, :blue => 6, :gold => -1, :silver => -2 }.freeze
- TOLERANCE =
- 4-Band-Code
-
The 4th color band indicates the tolerance.
- 5-Band-Code
-
The 5th color band indicates the tolerance.
{ :brown => 1.0, :red => 2.0, :orange => 0.05, :green => 0.5, :blue => 0.25, :purple => 0.1, :gold => 5.0, :silver => 10.0 }.freeze
- E12_SERIES =
{ 1 => [0, 2, 5, 8], 2 => [2, 7], 3 => [3, 9], 4 => [7], 5 => [6], 6 => [8], 8 => [2], }.freeze
- E24_SERIES =
{ 1 => [0, 1, 2, 3, 5, 6, 8], 2 => [0, 2, 4, 7], 3 => [0, 3, 6, 9], 4 => [3, 7], 5 => [1, 6], 6 => [2, 8], 7 => [5], 8 => [2], 9 => [1] }.freeze
- E48_SERIES =
[ 100, 105, 110, 115, 121, 127, 133, 140, 147, 154, 162, 169, 178, 187, 196, 205, 215, 226, 237, 249, 261, 274, 287, 301, 316, 332, 348, 365, 383, 402, 422, 442, 464, 487, 511, 536, 562, 590, 619, 649, 681, 715, 750, 787, 825, 866, 909, 953 ].freeze
- E96_SERIES =
[ 100, 102, 105, 107, 110, 113, 115, 118, 121, 124, 127, 130, 133, 137, 140, 143, 147, 150, 154, 158, 162, 165, 169, 174, 178, 182, 187, 191, 196, 200, 205, 210, 215, 221, 226, 232, 237, 243, 249, 255, 261, 267, 274, 280, 287, 294, 301, 309, 316, 324, 332, 340, 348, 357, 365, 374, 383, 392, 402, 412, 422, 432, 442, 453, 464, 475, 487, 499, 511, 523, 536, 549, 562, 576, 590, 604, 619, 634, 649, 665, 681, 698, 715, 732, 750, 768, 787, 806, 825, 845, 866, 887, 909, 931, 953, 976 ].freeze
Class Method Summary collapse
-
.decode(code, options = {}) ⇒ Float
Converts a color code to a resistance value.
-
.encode(ohm, options = {}) ⇒ Array<Symbol>
Converts a resistance value to a color code.
-
.five_band_decode(code) ⇒ Object
This method is used by the ‘Colorcode.decode` method when `options` is 5.
-
.five_band_encode(ohm, options = {}) ⇒ Object
This method is used by the ‘Colorcode.encode` method when `options` is 5.
-
.four_band_decode(code) ⇒ Object
This method is used by the ‘Colorcode.decode` method when `options` is 4.
-
.four_band_encode(ohm, options = {}) ⇒ Object
This method is used by the ‘Colorcode.encode` method when `options` is 4.
Class Method Details
.decode(code, options = {}) ⇒ Float
Converts a color code to a resistance value.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/resistor/color_code.rb', line 172 def decode(code, = {}) 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 [:band_number] ||= default.band_number case [:band_number] when 4 then four_band_decode(code) when 5 then five_band_decode(code) else raise ArgumentError end end |
.encode(ohm, options = {}) ⇒ Array<Symbol>
Converts a resistance value to a color code. The value must be between 0.1 and 99_000_000.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/resistor/color_code.rb', line 94 def encode(ohm, = {}) return [DIGIT.key(0)] if ohm == 0 raise ArgumentError if ohm < 0.1 default = Resistor::Options.new [:tolerance] ||= default.tolerance [:band_number] ||= default.band_number case [:band_number] when 4 then four_band_encode(ohm, ) when 5 then five_band_encode(ohm, ) else raise ArgumentError end end |
.five_band_decode(code) ⇒ Object
This method is used by the ‘Colorcode.decode` method when `options` is 5.
198 199 200 201 202 |
# 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 = {}) ⇒ Object
This method is used by the ‘Colorcode.encode` method when `options` is 5.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/resistor/color_code.rb', line 137 def five_band_encode(ohm, = {}) 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([: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([: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([:tolerance])] end end |
.four_band_decode(code) ⇒ Object
This method is used by the ‘Colorcode.decode` method when `options` is 4.
190 191 192 193 194 |
# 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 = {}) ⇒ Object
This method is used by the ‘Colorcode.encode` method when `options` is 4.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/resistor/color_code.rb', line 111 def four_band_encode(ohm, = {}) 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([: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([: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([:tolerance])] end end |