-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathColorUtil.cpp
More file actions
64 lines (52 loc) · 1.74 KB
/
ColorUtil.cpp
File metadata and controls
64 lines (52 loc) · 1.74 KB
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
#include "Utility/package.hpp"
#include "ColorUtil.hpp"
#include <cstdlib>
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
tVector3f ColorUtil::ColorToHSV(const tColor4f& color)
{
tVector3f c(color.r, color.g, color.b);
float v = tMath::max(c.x, tMath::max(c.y, c.z));
float chroma = v - tMath::min(c.x, tMath::min(c.y, c.z));
if (chroma != 0.0f)
{
float s = chroma / v;
if (c.x >= c.y && c.y >= c.z)
{
float h = (c.y - c.z) / chroma;
if (h < 0)
{
h += 6;
}
return tVector3f(h, s, v);
}
else if (c.y >= c.z && c.y >= c.x)
{
return tVector3f((c.z - c.x) / chroma + 2, s, v);
}
return tVector3f((c.x - c.y) / chroma + 4, s, v);
}
return tVector3f(0, 0, v);
}
tColor4f ColorUtil::HSVToColor(const tVector3f& hsv)
{
return HSVToColor(hsv.x, hsv.y, hsv.z);
}
tColor4f ColorUtil::HSVToColor(float h, float s, float v)
{
if (h == 0 && s == 0)
{
return tColor4f(v, v, v, 1.0f);
}
float c = s * v;
float x = c * (1 - abs(int32_t(h) % 2 - 1));
float m = v - c;
if (h < 1) return tColor4f(c + m, x + m, m, 1.0f);
else if (h < 2) return tColor4f(x + m, c + m, m, 1.0f);
else if (h < 3) return tColor4f(m, c + m, x + m, 1.0f);
else if (h < 4) return tColor4f(m, x + m, c + m, 1.0f);
else if (h < 5) return tColor4f(x + m, m, c + m, 1.0f);
else return tColor4f(c + m, m, x + m, 1.0f);
}