Difference between revisions of "RYBG"

From Noisebridge
Jump to: navigation, search
m
(Tables)
Line 2: Line 2:
  
 
== Tables ==
 
== Tables ==
 
+
<pre>
 +
4^0            0
 +
4^1            4
 +
4^2          16
 +
4^3          64
 +
4^4          256
 +
4^5        1,024
 +
4^6        4,096
 +
4^7      16,384
 +
4^8      65,536
 +
4^9      262,144
 +
4^10  1,048,576
 +
4^11  4,194,304
 +
4^12  16,777,216
 +
</pre>
  
 
== Code ==
 
== Code ==

Revision as of 19:55, 3 September 2014

Sample RYBG Calculator

Tables

4^0            0
4^1            4
4^2           16
4^3           64
4^4          256
4^5        1,024
4^6        4,096
4^7       16,384
4^8       65,536
4^9      262,144
4^10   1,048,576
4^11   4,194,304
4^12  16,777,216

Code

Sample code in AS3

Main.as

package 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author thex
	 */
	public class Main extends Sprite 
	{
		public var total_txt:TextField;
		
		private var pixels:Array;
		
		public function Main():void 
		{
			var p:Pixel;
			
			pixels = new Array();
			
			for (var i:int = 0; i < 4; i++)
			{
				p = new Pixel(Math.pow(4, i));
				p.name = "p" + i.toString();
				p.x = (4 - i) * 160 + 120;
				p.y = 256;
				p.addEventListener(MouseEvent.MOUSE_DOWN, tapHandler);
				addChild(p);
				
				pixels.push(p);
			}
			
			var fmt:TextFormat = new TextFormat("_sans", 64, 0x000000, true);
			
			total_txt = new TextField();
			total_txt.width = 128;
			total_txt.height = 128;
			total_txt.x = 32;
			total_txt.y = 160;
			total_txt.defaultTextFormat = fmt;
			total_txt.selectable = false;
			total_txt.text = "0";
			addChild(total_txt);
			
		}
		
		private function update():void
		{
			var p:Pixel;
			var count:int = 0;
			for (var i:int = 0; i < pixels.length; i++)
			{
				p = pixels[i];
				count += (p.count * p.multi);
			}
			total_txt.text = count.toString();
			trace(count);
		}
		
		public function tapHandler(e:MouseEvent):void
		{
			trace("tap: " + e.target.name);
			(e.target as Pixel).bump();
			update();
		}
		
	}
	
}

Pixel.as

package  
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author thex
	 */
	public class Pixel extends Sprite 
	{
		public var txt:TextField;
		
		public var count:int = 0;
		public var multi:int = 1;
		
		public function Pixel(p_multi:int)
		{
			multi = p_multi;			
			
			var fmt:TextFormat = new TextFormat("_sans", 64, 0x000000, true, null, null, null, null, "center");
			
			txt = new TextField();
			txt.defaultTextFormat = fmt;
			txt.width = 128;
			txt.height = 64;
			txt.x = -128;
			txt.y = -96;
			txt.selectable = false;
			txt.mouseEnabled = false;
			addChild(txt);
			
			update();
		}
		
		public function update():void
		{
			var c:int;
			
			if (count == 0)
			{
				// red
				c = 0xff0000;
			}
			else if (count == 1)
			{
				// yellow
				c = 0xffff00;
			}
			else if (count == 2)
			{
				// green
				c = 0x0000ff
			}
			else if (count == 3)
			{
				// blue
				c = 0x00ff00;
			}
			
			this.graphics.clear();
			this.graphics.beginFill(c, 1);
			this.graphics.drawCircle( -64, -64, 64);
			
			txt.text = String(count * multi);
		}
		
		public function bump():void
		{
			if (++count > 3)
				count = 0;
			update();
		}
		
	}

}