1/// @ref gtx_color_space
2/// @file glm/gtx/color_space.inl
3
4namespace glm
5{
6	template <typename T, precision P>
7	GLM_FUNC_QUALIFIER tvec3<T, P> rgbColor(const tvec3<T, P>& hsvColor)
8	{
9		tvec3<T, P> hsv = hsvColor;
10		tvec3<T, P> rgbColor;
11
12		if(hsv.y == static_cast<T>(0))
13			// achromatic (grey)
14			rgbColor = tvec3<T, P>(hsv.z);
15		else
16		{
17			T sector = floor(hsv.x / T(60));
18			T frac = (hsv.x / T(60)) - sector;
19			// factorial part of h
20			T o = hsv.z * (T(1) - hsv.y);
21			T p = hsv.z * (T(1) - hsv.y * frac);
22			T q = hsv.z * (T(1) - hsv.y * (T(1) - frac));
23
24			switch(int(sector))
25			{
26			default:
27			case 0:
28				rgbColor.r = hsv.z;
29				rgbColor.g = q;
30				rgbColor.b = o;
31				break;
32			case 1:
33				rgbColor.r = p;
34				rgbColor.g = hsv.z;
35				rgbColor.b = o;
36				break;
37			case 2:
38				rgbColor.r = o;
39				rgbColor.g = hsv.z;
40				rgbColor.b = q;
41				break;
42			case 3:
43				rgbColor.r = o;
44				rgbColor.g = p;
45				rgbColor.b = hsv.z;
46				break;
47			case 4:
48				rgbColor.r = q;
49				rgbColor.g = o;
50				rgbColor.b = hsv.z;
51				break;
52			case 5:
53				rgbColor.r = hsv.z;
54				rgbColor.g = o;
55				rgbColor.b = p;
56				break;
57			}
58		}
59
60		return rgbColor;
61	}
62
63	template <typename T, precision P>
64	GLM_FUNC_QUALIFIER tvec3<T, P> hsvColor(const tvec3<T, P>& rgbColor)
65	{
66		tvec3<T, P> hsv = rgbColor;
67		float Min   = min(min(rgbColor.r, rgbColor.g), rgbColor.b);
68		float Max   = max(max(rgbColor.r, rgbColor.g), rgbColor.b);
69		float Delta = Max - Min;
70
71		hsv.z = Max;
72
73		if(Max != static_cast<T>(0))
74		{
75			hsv.y = Delta / hsv.z;
76			T h = static_cast<T>(0);
77
78			if(rgbColor.r == Max)
79				// between yellow & magenta
80				h = static_cast<T>(0) + T(60) * (rgbColor.g - rgbColor.b) / Delta;
81			else if(rgbColor.g == Max)
82				// between cyan & yellow
83				h = static_cast<T>(120) + T(60) * (rgbColor.b - rgbColor.r) / Delta;
84			else
85				// between magenta & cyan
86				h = static_cast<T>(240) + T(60) * (rgbColor.r - rgbColor.g) / Delta;
87
88			if(h < T(0))
89				hsv.x = h + T(360);
90			else
91				hsv.x = h;
92		}
93		else
94		{
95			// If r = g = b = 0 then s = 0, h is undefined
96			hsv.y = static_cast<T>(0);
97			hsv.x = static_cast<T>(0);
98		}
99
100		return hsv;
101	}
102
103	template <typename T>
104	GLM_FUNC_QUALIFIER tmat4x4<T, defaultp> saturation(T const s)
105	{
106		tvec3<T, defaultp> rgbw = tvec3<T, defaultp>(T(0.2126), T(0.7152), T(0.0722));
107
108		tvec3<T, defaultp> const col((T(1) - s) * rgbw);
109
110		tmat4x4<T, defaultp> result(T(1));
111		result[0][0] = col.x + s;
112		result[0][1] = col.x;
113		result[0][2] = col.x;
114		result[1][0] = col.y;
115		result[1][1] = col.y + s;
116		result[1][2] = col.y;
117		result[2][0] = col.z;
118		result[2][1] = col.z;
119		result[2][2] = col.z + s;
120		return result;
121	}
122
123	template <typename T, precision P>
124	GLM_FUNC_QUALIFIER tvec3<T, P> saturation(const T s, const tvec3<T, P>& color)
125	{
126		return tvec3<T, P>(saturation(s) * tvec4<T, P>(color, T(0)));
127	}
128
129	template <typename T, precision P>
130	GLM_FUNC_QUALIFIER tvec4<T, P> saturation(const T s, const tvec4<T, P>& color)
131	{
132		return saturation(s) * color;
133	}
134
135	template <typename T, precision P>
136	GLM_FUNC_QUALIFIER T luminosity(const tvec3<T, P>& color)
137	{
138		const tvec3<T, P> tmp = tvec3<T, P>(0.33, 0.59, 0.11);
139		return dot(color, tmp);
140	}
141}//namespace glm
142