#define pHue IniParams[255].x
#define pSaturation IniParams[255].y
#define pValue IniParams[255].z
#define pBrightness IniParams[255].w

float3 rgb2hsv(float3 rgb) {

	float3 hsv = (float3)0;

	// RGB 2 HSV
	float maxValue = max(rgb.r, max(rgb.g, rgb.b));
	float minValue = min(rgb.r, min(rgb.g, rgb.b));
	float delta = maxValue - minValue;

	//v
	hsv.z = maxValue;

	//s
	if (maxValue != 0.0){
		hsv.y = delta / maxValue;
	} else {
		hsv.y = 0.0;
	}

	//h
	if (hsv.y > 0.0){
		if (rgb.r == maxValue){
			hsv.x = (rgb.g - rgb.b) / delta;
		} else if (rgb.g == maxValue){
			hsv.x = 2 + (rgb.b - rgb.r) / delta;
		} else {
			hsv.x = 4 + (rgb.r - rgb.g) / delta;
		}
		hsv.x /= 6.0;
		if (hsv.x < 0) hsv.x += 1.0;
	}

	return hsv;

}

float3 hsv2rgb(float3 hsv) {

	float3 rgb;

	// HSV 2 RGB
	if (hsv.y == 0){
		rgb.r = rgb.g = rgb.b = hsv.z; //v
	} else {
		hsv.x *= 6.0;
		float i = floor (hsv.x);
		float f = hsv.x - i;
		float aa = hsv.z * (1 - hsv.y);
		float bb = hsv.z * (1 - (hsv.y * f));
		float cc = hsv.z * (1 - (hsv.y * (1 - f)));
		if (i < 1){
			rgb.r = hsv.z; rgb.g = cc; rgb.b = aa;
		} else if (i < 2){
			rgb.r = bb; rgb.g = hsv.z; rgb.b = aa;
		} else if (i < 3){
			rgb.r = aa; rgb.g = hsv.z; rgb.b = cc;
		} else if (i < 4){
			rgb.r = aa; rgb.g = bb; rgb.b = hsv.z;
		} else if (i < 5){
			rgb.r = cc; rgb.g = aa; rgb.b = hsv.z;
		} else {
			rgb.r = hsv.z; rgb.g = aa; rgb.b = bb;
		}
	}

	return rgb;

}

float4 getChangeColor(float3 hsv, float4 col) {

	float4 ret = (float4)0;

	hsv.x += pHue;
	if (1.0 <= hsv.x) hsv.x -= 1.0;
	hsv.y *= pSaturation;
	if (1.0 <= hsv.y) hsv.y = 1.0;
	hsv.z *= pValue;
	//if (1.0 <= hsv.z) hsv.z = 1.0;

	ret.xyz = hsv2rgb(hsv);
	ret.w = (col.w * pBrightness);

	return ret;

}

float4 getColor(float4 col) {

	return getChangeColor(rgb2hsv(col.rgb), col);

}
