Difference between revisions of "RYBG"

From Noisebridge
Jump to: navigation, search
(Tables)
(Tables)
 
Line 3: Line 3:
 
== Tables ==
 
== Tables ==
 
<pre>
 
<pre>
4^0           0
+
pow  red    yellow        blue      green
4^1           4
+
-------------------------------------------
4^2           16
+
4^0   0           1          2          3
4^3           64
+
4^1   0          4           8          12
4^4         256
+
4^2   0          16         32          48
4^5       1,024
+
4^3   0          64         128        384
4^6       4,096
+
4^4   0        256         512        768
4^7       16,384
+
4^5   0      1,024       2,048      3,072
4^8       65,536
+
4^6   0      4,096       8,192      12,288
4^9     262,144
+
4^7   0      16,384     32,768      49,152
4^10  1,048,576
+
4^8   0      65,536     131,072    196,608
4^11  4,194,304
+
4^9   0    262,144     524,288    786,432
4^12  16,777,216
+
4^10 0   1,048,576   2,097,152  3,145,728
 +
4^11 0   4,194,304   8,388,608  12,582,912
 +
4^12 0 16,777,216 33,554,432  50,331,648
 
</pre>
 
</pre>
  

Latest revision as of 20:03, 3 September 2014

Sample RYBG Calculator

Tables[edit]

pow  red    yellow        blue      green
-------------------------------------------
4^0   0           1           2           3
4^1   0           4           8          12
4^2   0          16          32          48
4^3   0          64         128         384
4^4   0         256         512         768
4^5   0       1,024       2,048       3,072
4^6   0       4,096       8,192      12,288
4^7   0      16,384      32,768      49,152
4^8   0      65,536     131,072     196,608
4^9   0     262,144     524,288     786,432
4^10  0   1,048,576   2,097,152   3,145,728
4^11  0   4,194,304   8,388,608  12,582,912
4^12  0  16,777,216  33,554,432  50,331,648

Code[edit]

Sample code in AS3

Main.as[edit]

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[edit]

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();
		}
		
	}

}