-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHsv.cpp
More file actions
31 lines (26 loc) · 787 Bytes
/
Hsv.cpp
File metadata and controls
31 lines (26 loc) · 787 Bytes
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
#include <SFML/Graphics.hpp>
sf::Color Hsv(int hue, float sat, float val, float d)
{
hue %= 360;
while (hue < 0) hue += 360;
if (sat < 0.f) sat = 0.f;
if (sat > 1.f) sat = 1.f;
if (val < 0.f) val = 0.f;
if (val > 1.f) val = 1.f;
int h = hue / 60;
float f = float(hue) / 60 - h;
float p = val * (1.f - sat);
float q = val * (1.f - sat * f);
float t = val * (1.f - sat * (1 - f));
switch (h)
{
default:
case 0:
case 6: return sf::Color(val * 255, t * 255, p * 255, d);
case 1: return sf::Color(q * 255, val * 255, p * 255, d);
case 2: return sf::Color(p * 255, val * 255, t * 255, d);
case 3: return sf::Color(p * 255, q * 255, val * 255, d);
case 4: return sf::Color(t * 255, p * 255, val * 255, d);
case 5: return sf::Color(val * 255, p * 255, q * 255, d);
}
}