function w = f(z) // squared
	w = z .^ 2;
endfunction;

function w = oneover(z) // unneed with 1 ./ z
	w = ones(w) .* (abs(z) < 0.001) + z .* (abs(z) >= 0.001);
	w = 1 ./ w;
endfunction

function z = grid(x0, x1, y0, y1, n)
	a = ones(n,1)*(x0:(x1-x0)/(n-1):x1);
	b = (y1:(y0-y1)/(n-1):y0)'*ones(1,n);
	z = a + b*%i;
endfunction;

function [r, g, b] = dcolor(z)
	h = hue(Arg(z));
	s = sat(hypot(z));
	v = value(hypot(z));
	[r g b] = hsv2rgb(h,s,v);
	cmap = [r, g, b];
	w = zeros(z);
	[r, c]=size(w);
	w(:) = (1:r*c)';
	Matplot(w);
	f =gcf();
	f.color_map = cmap;
endfunction;

function phi = Arg(z) // -pi < phi <= pi
	phi = atan(imag(z),real(z));
endfunction

function h = hue(phi)
	h = pmodulo(phi/(2*%pi), 1);
endfunction

function h = hypot(z)
	h = abs(z);
endfunction

sat0 = 0.3;
function s = sat2(h)
	if (h >= 1) then
		h = log(h)
	end
	h = 4*(h - int(h));
	k = int(h)
	h = h - k;
	select k
	case 0
		s = 0.99 - (0.99 - sat0)*h;
	case 1 
		s = 0.99;
	case 2
		s = 0.99 - (0.99 - sat0) * (1 - h);
	case 3
		s = 0.99;
	end
endfunction

function v = sat(h)
	h = h .* (h < 1) + log(h) .* (h >= 1);
	h = 4*(h - int(h));
	k = int(h);
	h = h - k;
	v = (0.99-(0.99-sat0)*h) .* (k==0) + 0.99 .* (k==1) + (0.99-(0.99-sat0).*(1-h)) .* (k==2) + 0.99 .* (k==3);
endfunction

val0 = 0.6;
function v = value2(h)
	if (h >= 1) then
		h = log(h)
	end
	h = 4*(h - int(h));
	k = int(h)
	h = h - k;
	select k
	case 0
		v = 0.99;
	case 1 
		v = 0.99 - (0.99 - val0)*h;
	case 2
		v = val0;
	case 3
		v = 0.99 - (0.99 - val0) * (1 - h);
	end
endfunction

function y = pw(x)
	y = 1 .* (x<=1) + (2-x) .*((x>1)&(x<=2));
endfunction

function v = value(h)
	h = h .* (h < 1) + log(h) .* (h >= 1);
	h = 4*(h - int(h));
	k = int(h);
	h = h - k;
	v = 0.99 .* (k==0) + (0.99-(0.99-val0)*h) .* (k==1) + val0 .* (k==2) + (0.99-(0.99-val0).*(1-h)) .* (k==3);
endfunction



// [r g b] = hsv2rgb(h,s,v)
// rgb = hsv2rgb(hsv) mx3 matrices
// h goes from 0 to 1, s goes from 0 to 1, b goes from 0 to 1
// r gores from 0 to 255 as does g and b


// complex to h s v
// hue = (Arg(z)+pi) mod 2pi
// intensity value(?) becomes brighter (more white)
// saturation 0 is gray, too no white component.

// based on |z|
// sat0 is 0.3 val0 = 0.6
// 

//  Const Int0 = 0.6, Sat0 = 0.3
//  D = Sqr(Zr * Zr + Zi * Zi)
//  If (D >= 1) Then
//    D = Log(D)
//  End If
//  D = 4 * (D - Int(D))
//  K = Int(D)
//  D = D - K
//  Select Case K
//    Case 0
//      Inten = 0.99
//      Sat = 0.99 - (0.99 - Sat0) * D
//    Case 1
//      Inten = 0.99 - (0.99 - Int0) * D
//      Sat = Sat0
//    Case 2
//      Inten = Int0
//      Sat = 0.99 - (0.99 - Sat0) * (1 - D)
//    Case 3
//      Inten = 0.99 - (0.99 - Int0) * (1 - D)
//      Sat = 0.99
//  End Select
//  GetColor = IHS(Inten, Phase1(Zr, Zi), Sat)

z = grid(-10,10,-10,10,250);

scf(3);
clf
dcolor(sin(z));
title('$f(z) = sin(z)$','fontsize',4);
a=gca(); tmp=a.x_ticks;
tmp.locations=[0;125;250];
tmp.labels=["-10";"0";"10"];
tmpy = tmp;
tmpy.labels=["-10 i";"0 i";"10 i"];
a=gca();
a.x_ticks = tmp;
a.y_ticks = tmpy;
scf(2);
clf
dcolor(exp(z));
title('$f(z) = e^z$','fontsize',4);
a=gca();
a.x_ticks = tmp;
a.y_ticks = tmpy;
scf(1);
clf
dcolor(1 ./ z);
title('$f(z) = 1/z$','fontsize',4);
a=gca();
a.x_ticks = tmp;
a.y_ticks = tmpy;
