Browse Source

changed Matrix.colorSaturation (base 0), renamed colorBrightness to colorLightness, added Drawable.adjustColor, added AdjustColor sample

ncannasse 8 years ago
parent
commit
5802f47a08
6 changed files with 129 additions and 55 deletions
  1. 17 0
      h2d/Drawable.hx
  2. 5 4
      h3d/Matrix.hx
  3. 48 0
      samples/AdjustColor.hx
  4. 14 14
      samples/Generator.hx
  5. 43 0
      samples/SampleApp.hx
  6. 2 37
      samples/Sao.hx

+ 17 - 0
h2d/Drawable.hx

@@ -57,6 +57,23 @@ class Drawable extends Sprite {
 		return colorKey = v;
 	}
 
+	public function adjustColor( saturation = 0., lightness = 0., hue = 0., contrast = 0. ) {
+		if( saturation == 0 && lightness == 0 && hue == 0 && contrast == 0 )
+			colorMatrix = null;
+		else {
+			var m = colorMatrix;
+			if( m == null ) {
+				m = new h3d.Matrix();
+				colorMatrix = m;
+			}
+			m.identity();
+			if( hue != 0 ) m.colorHue(hue);
+			if( saturation != 0 ) m.colorSaturation(saturation);
+			if( contrast != 0 ) m.colorContrast(contrast);
+			if( lightness != 0 ) m.colorLightness(lightness);
+		}
+	}
+
 	function get_colorMatrix() {
 		var s = getShader(h3d.shader.ColorMatrix);
 		return s == null ? null : s.matrix;

+ 5 - 4
h3d/Matrix.hx

@@ -534,6 +534,7 @@ class Matrix {
 	}
 
 	public function colorSaturation( sat : Float ) {
+		sat += 1;
 		var is = 1 - sat;
 		var r = is * lumR;
 		var g = is * lumG;
@@ -570,10 +571,10 @@ class Matrix {
 		multiply3x4(this, tmp);
 	}
 
-	public function colorBrightness( brightness : Float ) {
-		_41 += brightness;
-		_42 += brightness;
-		_43 += brightness;
+	public function colorLightness( lightness : Float ) {
+		_41 += lightness;
+		_42 += lightness;
+		_43 += lightness;
 	}
 
 	public function colorBits( bits : Int, blend : Float ) {

+ 48 - 0
samples/AdjustColor.hx

@@ -0,0 +1,48 @@
+
+class AdjustColor extends SampleApp {
+
+	var hue = 0.;
+	var sat = 0.;
+	var bright = 0.;
+	var contrast = 0.;
+
+	var bmps : Array<h2d.Bitmap>;
+
+	override function init() {
+		super.init();
+
+		engine.backgroundColor = 0x404040;
+
+		bmps = [];
+		for( i in 0...4 ) {
+			var gradient = new hxd.BitmapData(256, 256);
+			var red = (i + 1) & 1;
+			var green = ((i + 1) >> 1) & 1;
+			var blue = (i + 1) >> 2;
+			for( x in 0...gradient.width )
+				for( y in 0...gradient.height )
+					gradient.setPixel(x,y, 0xFF000000 | ((x << 16) * red) | ((y << 8) * green) | (((x + y) >> 1) * blue));
+
+			var bmp = new h2d.Bitmap(h2d.Tile.fromBitmap(gradient), s2d);
+			bmp.x = 50 + (i&1) * 270;
+			bmp.y = 100 + (i >> 1) * 270;
+			bmps.push(bmp);
+		}
+
+		addSlider("Hue", -180, 180, function() return hue, function(s) hue = s);
+		addSlider("Saturation", -100, 100, function() return sat, function(s) sat = s);
+		addSlider("Brightness", -100, 100, function() return bright, function(s) bright = s);
+		addSlider("Contrast", -100, 100, function() return contrast, function(s) contrast = s);
+	}
+
+	override function update(dt:Float) {
+		for( b in bmps )
+			b.adjustColor(sat / 100, bright / 100, hue * Math.PI / 180, contrast / 100);
+	}
+
+	public static function main() {
+		new AdjustColor();
+	}
+
+
+}

+ 14 - 14
samples/Generator.hx

@@ -1,5 +1,5 @@
 class Generator {
-	
+
 	static function compile() {
 		var errored = [];
 		Sys.setCwd("build");
@@ -30,41 +30,41 @@ class Generator {
 		}
 		Sys.println("DONE");
 	}
-	
+
 	#if hscript
 	static function generateProjects() {
-		
+
 		var templates = [];
 		for( f in sys.FileSystem.readDirectory("templates") )
 			templates.push({ file : f, data : sys.io.File.getContent("templates/" + f) });
-		
+
 		try sys.FileSystem.createDirectory("build") catch( e : Dynamic ) {};
 		sys.io.File.saveContent("build/README.txt","This directory is automatically generated by samples/Script.hx using samples/templates");
-		
+
 		for( f in sys.FileSystem.readDirectory(".") ) {
-		
+
 			if( sys.FileSystem.isDirectory(f) || !StringTools.endsWith(f,".hx") )
 				continue;
-			
+
 			var title = f.split(".").shift();
-			
-			if( title == "Generator" ) continue;
-			
+
+			if( title == "Generator" || title == "SampleApp" ) continue;
+
 			var name = title.charAt(0).toLowerCase() + title.substr(1);
 			var params = [];
 			if( sys.FileSystem.exists(name+"_res") )
 				params.push("-D resourcesPath=../../"+name+"_res");
-			
+
 			var content = sys.io.File.getContent(f);
 			~/\/\/PARAM=(.*)/g.map(content,function(r) { params.push(StringTools.trim(r.matched(1))); return ""; });
-			
+
 			var context = {
 				main : title,
 				name : name,
 				params : params.join("\n"),
 				StringTools : StringTools,
 			};
-			
+
 			try sys.FileSystem.createDirectory("build/"+name) catch( e : Dynamic ) {};
 			#if !silent
 			Sys.println(name);
@@ -83,6 +83,6 @@ class Generator {
 				sys.io.File.saveContent("build/" + name + "/" + file, data);
 			}
 		}
-	}	
+	}
 	#end
 }

+ 43 - 0
samples/SampleApp.hx

@@ -0,0 +1,43 @@
+class SampleApp extends hxd.App {
+
+	var fui : h2d.Flow;
+
+	override function init() {
+		fui = new h2d.Flow(s2d);
+		fui.isVertical = true;
+		fui.verticalSpacing = 5;
+		fui.padding = 10;
+	}
+
+	function addSlider( text, min : Float, max : Float, get : Void -> Float, set : Float -> Void ) {
+		var f = new h2d.Flow(fui);
+
+		f.horizontalSpacing = 5;
+
+		var font = hxd.res.DefaultFont.get();
+		var tf = new h2d.Text(font, f);
+		tf.text = text;
+		tf.maxWidth = 70;
+		tf.textAlign = Right;
+
+		var sli = new h2d.Slider(100, 10, f);
+		sli.minValue = min;
+		sli.maxValue = max;
+		sli.value = get();
+
+		var tf = new h2d.TextInput(font, f);
+		tf.text = "" + hxd.Math.fmt(sli.value);
+		sli.onChange = function() {
+			set(sli.value);
+			tf.text = "" + hxd.Math.fmt(sli.value);
+			f.needReflow = true;
+		};
+		tf.onChange = function() {
+			var v = Std.parseFloat(tf.text);
+			if( Math.isNaN(v) ) return;
+			sli.value = v;
+			set(v);
+		};
+	}
+
+}

+ 2 - 37
samples/Sao.hx

@@ -49,10 +49,9 @@ class CustomRenderer extends h3d.scene.Renderer {
 
 }
 
-class Sao extends hxd.App {
+class Sao extends SampleApp {
 
 	var wscale = 1.;
-	var fui : h2d.Flow;
 	var renderer : CustomRenderer;
 
 	function initMaterial( m : h3d.mat.MeshMaterial ) {
@@ -72,10 +71,7 @@ class Sao extends hxd.App {
 	}
 
 	override function init() {
-
-		fui = new h2d.Flow(s2d);
-		fui.isVertical = true;
-		fui.verticalSpacing = 5;
+		super.init();
 
 		var r = new hxd.Rand(Std.random(0xFFFFFF));
 
@@ -123,37 +119,6 @@ class Sao extends hxd.App {
 		onResize();
 	}
 
-	function addSlider( text, min : Float, max : Float, get : Void -> Float, set : Float -> Void ) {
-		var f = new h2d.Flow(fui);
-
-		f.horizontalSpacing = 5;
-
-		var font = hxd.res.DefaultFont.get();
-		var tf = new h2d.Text(font, f);
-		tf.text = text;
-		tf.maxWidth = 70;
-		tf.textAlign = Right;
-
-		var sli = new h2d.Slider(100, 10, f);
-		sli.minValue = min;
-		sli.maxValue = max;
-		sli.value = get();
-
-		var tf = new h2d.TextInput(font, f);
-		tf.text = "" + hxd.Math.fmt(sli.value);
-		sli.onChange = function() {
-			set(sli.value);
-			tf.text = "" + hxd.Math.fmt(sli.value);
-			f.needReflow = true;
-		};
-		tf.onChange = function() {
-			var v = Std.parseFloat(tf.text);
-			if( Math.isNaN(v) ) return;
-			sli.value = v;
-			set(v);
-		};
-	}
-
 	function reset() {
 		while( s3d.numChildren > 0 )
 			s3d.getChildAt(0).remove();