//**********************************************
// Filename:	Tuning.OGS
// Creation:	11/06/01 by SDB
// Copyright (c) OriginLab Corporation 1992-2000
// Purpose:	Contrast/Brightness dialog scripts
//**********************************************
// Modification Log:

[OnInit]
	//Initialize dialog
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	Tuning!Slider1.max=100;
	Tuning!Slider2.max=100;
	Tuning!Slider1.ticfreq=25;
	Tuning!Slider2.ticfreq=25;
	Tuning!Slider1.value=50;
	Tuning!Slider2.value=50;
	if(z1==0/0)
	{
		Tuning!Slider1.enable=0;
		Tuning!Slider2.enable=0;
		Tuning!Z1.enable=0;
		Tuning!Z2.enable=0;
		return 1;
	}
	Tuning!Z1.v1=zmin;
	Tuning!Z2.v1=zmax;
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	return 0;

[Slider1.OnSlide]
	//User slides the brightness slider
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	sl1 = 1 - Tuning!Slider1.value/100;
	sl2 = 1 - Tuning!Slider2.value/100;
	z1=(sl1*((zmax+(2*zmax-zmin))-(zmin+(2*zmin-zmax)))+(zmin+(2*zmin-zmax))-sl2*((zmax-zmin)*2))/2;
	z2=sl2*((zmax-zmin)*2)+z1+1;
	Tuning!Z1.v1 = z1;
	Tuning!Z2.v2 = z2;
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	delete -v sl1;
	delete -v sl2;
	return 0;
	
[Slider2.OnSlide]
	//User slides the contrast slider
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	sl1 = 1 - Tuning!Slider1.value/100;
	sl2 = 1 - Tuning!Slider2.value/100;
	z1=(sl1* ((zmax+(2*zmax-zmin))-(zmin+(2*zmin-zmax)))+(zmin+(2*zmin-zmax))-sl2*((zmax-zmin)*2))/2;
	z2=sl2*((zmax-zmin)*2)+z1+1;
	Tuning!Z1.v1 = z1;
	Tuning!Z2.v2 = z2;
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	delete -v sl1;
	delete -v sl2;
	return 0;

[Reset.OnClick]
	//User clicks the Reset button
	matrix -id;
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	z1 = zmin;
	z2 = zmax;
	if(z1==0/0 || z1 == z2)
	{
		Tuning!Slider1.enable=0;
		Tuning!Slider2.enable=0;
		Tuning!Z1.enable=0;
		Tuning!Z2.enable=0;
		return 1;
	}
	else
	{
		Tuning!Slider1.enable=1;
		Tuning!Slider2.enable=1;
		Tuning!Z1.enable=1;
		Tuning!Z2.enable=1;
	}
	Tuning!Slider1.value=50;
	Tuning!Slider2.value=50;
	Tuning!Z1.v1=zmin;
	Tuning!Z2.v2=zmax;
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	return 0;

[OnWinActivate]
	//Another window is activated, controls will update according to new image
	Tuning!Z1.v1=z1;
	Tuning!Z2.v1=z2;
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	if(z1==0/0 || z1 == z2)
	{
		Tuning!Slider1.enable=0;
		Tuning!Slider2.enable=0;
		Tuning!Z1.enable=0;
		Tuning!Z2.enable=0;
		return 1;
	}
	else
	{
		Tuning!Slider1.enable=1;
		Tuning!Slider2.enable=1;
		Tuning!Z1.enable=1;
		Tuning!Z2.enable=1;
	}
	sl1 =((z1+z2)-(zmin+(2*zmin-zmax)))/((zmax+(2*zmax-zmin))-(zmin+(2*zmin-zmax)));
	Tuning!Slider1.value = 100 * (1 - sl1);
	sl2 = (z2-z1)/((zmax-zmin)*2);
	Tuning!Slider2.value = 100 * (1 - sl2);
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	delete -v sl1;
	delete -v sl2;
	return 0;

[Z1.OnKillFocus]
	//User makes change to Z1 value and focus is changed
	if(Tuning!Z1.v1>Tuning!Z2.v1-1)
	{
		Tuning!Z1.v1=Tuning!Z2.v1-1;
	}
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	if(Tuning!Z1.v1>zmax)
	{
		Tuning!Z1.v1=zmax;
	}
	z1=Tuning!Z1.v1;
	sl1 =((z1+z2)-(zmin+(2*zmin-zmax)))/((zmax+(2*zmax-zmin))-(zmin+(2*zmin-zmax)));
	Tuning!Slider1.value = 100 * (1 - sl1);
	sl2 = (z2-z1)/((zmax-zmin)*2);
	Tuning!Slider2.value = 100 * (1 - sl2);
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	delete -v sl1;
	delete -v sl2;
	return 0;	

[Z2.OnKillFocus]
	//User makes change to Z2 value and focus is changed
	if(Tuning!Z2.v1<Tuning!Z1.v1+1)
	{
		Tuning!Z2.v1=Tuning!Z1.v1+1;
	}
	get %c -f zmin;
	get %c -i zmax;
	Tuning!Zmin.v1=zmin;
	Tuning!Zmax.v1=zmax;
	if(Tuning!Z2.v1<zmin)
	{
		Tuning!Z2.v1=zmin;
	}
	z2=Tuning!Z2.v1;
	sl1 =((z1+z2)-(zmin+(2*zmin-zmax)))/((zmax+(2*zmax-zmin))-(zmin+(2*zmin-zmax)));
	Tuning!Slider1.value = 100 * sl1;
	sl2 = (z2-z1)/((zmax-zmin)*2);
	Tuning!Slider2.value = 100 * sl2;
	//delete global variables
	delete -v zmin;
	delete -v zmax;
	delete -v sl1;
	delete -v sl2;
	return 0;
