summaryrefslogtreecommitdiffstats
path: root/DTColorStack.c
blob: 608f0903020633dcc28a7f618b23f5fd8078ea04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#strict 2

global func GetClrModulationStack(object target)
{
	target || FatalError("GetClrModulationStack: no target object given");
	return GetEffect("ClrModulationStack", target) || AddEffect("ClrModulationStack", target, 200);
}

global func AddClrModulation(int color, int baseColor, int factor, int precision, object target)
{
	baseColor = baseColor || RGB(127, 127, 127);
	target = target || this || FatalError("AddClrModulation: no target object given");
	factor = factor || 1;
	precision = precision || 1;

	return EffectCall(target, GetClrModulationStack(target), "AddModulation", color, baseColor, factor, precision);
}

global func RemoveClrModulation(int id, object target)
{
	target = target || this || FatalError("RemoveClrModulation: no target object given");

	return EffectCall(target, GetClrModulationStack(target), "RemoveModulation", id);
}

global func FxClrModulationStackStart(object target, int effectNumber, int temp)
{
	if(!temp)
	{
		EffectVar(1, target, effectNumber) = [];
	}
}

global func FxClrModulationStackAddModulation(object target, int effectNumber, int color, int baseColor, int factor, int precision)
{
	EffectVar(1, target, effectNumber)[] = [++EffectVar(0, target, effectNumber), color, baseColor, factor, precision];
	EffectCall(target, effectNumber, "ApplyModulation");
	return EffectVar(0, target, effectNumber);
}

global func FxClrModulationStackRemoveModulation(object target, int effectNumber, int id)
{
	for(var i = 0; i < GetLength(EffectVar(1, target, effectNumber)); ++i)
	{
		if(EffectVar(1, target, effectNumber)[i][0] == id)
		{
			ArrayErase(EffectVar(1, target, effectNumber), i);
			EffectCall(target, effectNumber, "ApplyModulation");
			return true;
		}
	}
	return 0;
}

global func FxClrModulationStackApplyModulation(object target, int effectNumber)
{
	if(GetLength(EffectVar(1, target, effectNumber)) == 0)
	{
		SetClrModulation(0, target);
		return 0;
	}
	var r, g, b, a;
	SplitRGBaValue(EffectVar(1, target, effectNumber)[0][1], r, g, b, a);
	a = 255 - a;
	for(var i = 1; i < GetLength(EffectVar(1, target, effectNumber)); ++i)
	{
		var clrPart = EffectVar(1, target, effectNumber)[i];
		var mr, mg, mb, ma, br, bg, bb, ba;
		SplitRGBaValue(clrPart[1], mr, mg, mb, ma);
		SplitRGBaValue(clrPart[2], br, bg, bb, ba);
		ma = 255 - ma;
		a *= ma;
		a /= 0xFF;
		r += mr - br;
		r *= clrPart[3];
		r /= clrPart[4];
		g += mg - bg;
		g *= clrPart[3];
		g /= clrPart[4];
		b += mb - bb;
		b *= clrPart[3];
		b /= clrPart[4];
	}
	SetClrModulation(RGBa(BoundBy(r, 0, 255), BoundBy(g, 0, 255), BoundBy(b, 0, 255), 255 - a), target);
	return true;
}