In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

抵抗値の読み方が覚えられない

よくある4本の帯で右端が金色の抵抗
オーム値を入力するとそれに対応する色を表示
デザインひどいけど、気にしない。
Get Adobe Flash player
by kjkmr 11 Jul 2010
    Embed
/**
 * Copyright kjkmr ( http://wonderfl.net/user/kjkmr )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/8cZc
 */

package {
    /*
    よくある4本の帯で右端が金色の抵抗
    オーム値を入力するとそれに対応する色を表示
    デザインひどいけど、気にしない。
    */
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.text.TextFieldType;
    import flash.events.Event;
    
    public class Ohm extends Sprite {
        
        public static const NUM_COLORS:Array = [
            0x000000,//0:黒
            0x990000,//1:茶
            0xff0000,//2:赤
            0xfcba3f,//3:橙
            0xffff00,//4:黄
            0x00ff00,//5:緑
            0x0000ff,//6:青
            0xff00ff,//7:紫
            0xadadad,//8:灰
            0xffffff //9:白
        ];
        
        public static const NUM_COLORS_NAME:Array = [
            "黒",
            "茶",
            "赤",
            "橙",
            "黄",
            "緑",
            "青",
            "紫",
            "灰",
            "白"
        ];
        
        private var _text:Sprite;
        private var _textInput:TextField;
        
        private var _result:Sprite;
        private var _resultText:TextField;
        
        public function Ohm() {
            
            _text = new Sprite();
            _text.graphics.beginFill( 0xe9e9e9 );
            _text.graphics.drawRect( 0, 0, 200, 40 );
            
            _textInput = new TextField();
            var tf:TextFormat = new TextFormat( "_sans", 24 );
            tf.align = "center";
            _textInput.defaultTextFormat = tf;
            _textInput.width = 200;
            _textInput.height = 40;
            _textInput.type = TextFieldType.INPUT;
            _textInput.y = 5;
            _textInput.restrict = "0-9 kKmM\.";
            _textInput.addEventListener( Event.CHANGE, _onChange );
            _text.addChild( _textInput );
            
            _text.x = ( stage.stageWidth - _text.width ) * 0.5 >> 0;
            _text.y = 10;
            this.addChild( _text );
            
            tf.align = "left";
            tf.size = 16;
            _resultText = new TextField;
            _resultText.defaultTextFormat = tf;
            _resultText.width - 80;
            _resultText.height = 60;
            _resultText.x = ( stage.stageWidth - 80 ) * 0.5 >> 0;
            _resultText.y = 100;
            _resultText.multiline = true;
            addChild( _resultText );
            
            _result = new Sprite();
            _result.x = ( stage.stageWidth - 80 ) * 0.5 >> 0;
            _result.y = 150;
            addChild( _result );
            
            
            _textInput.text = "100K";
            _onChange();
        }
        
        private function _onChange( i_event:Event = null ):void {
            _result.graphics.clear();
            _resultText.text = "";
            
            var input:String = _textInput.text;
            var reg:RegExp = /^([0-9]+(\.[0-9]+)?)([km]?)$/i;
            if ( !input.match(reg) ) return;
            var numStr:String = input.replace(reg,"$1");
            var num:Number = parseFloat(numStr);
            var unitStr:String = input.replace(/^[0-9.]+([km]?)$/i,"$1").toLowerCase();
            switch( unitStr ) {
                case "k": num *= 1000; break;
                case "m": num *= 1000000; break;
            }
            
            numStr = num.toString();
            
            var d1:uint = parseInt(numStr.substr(0,1));
            var d2:uint = parseInt(numStr.substr(1,1));
            var m:int = numStr.length - 2;
            
            
            _result.graphics.clear();
            _result.graphics.beginFill( NUM_COLORS[d1] );
            _result.graphics.drawRect( 0, 0, 20, 20 );
            
            _result.graphics.beginFill( NUM_COLORS[d2] );
            _result.graphics.drawRect( 20, 0, 20, 20 );
            
            _result.graphics.beginFill( NUM_COLORS[m] );
            _result.graphics.drawRect( 40, 0, 20, 20 );
            
            //誤差はとりあえず良く使う+-5%(金)固定
            _result.graphics.beginFill( 0xefdb6b );
            _result.graphics.drawRect( 60, 0, 20, 20 );
            _result.graphics.endFill();
            
            
            _resultText.text = d1.toString()+"   "+d2.toString()+"   "+m+"\n";
            _resultText.text = _resultText.text + NUM_COLORS_NAME[d1]+" "+NUM_COLORS_NAME[d2]+" "+NUM_COLORS_NAME[m]+" 金";
            
        }

    }
}