forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhsv.cpp
More file actions
93 lines (76 loc) · 1.78 KB
/
hsv.cpp
File metadata and controls
93 lines (76 loc) · 1.78 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Aseprite Gfx Library
// Copyright (C) 2001-2013 David Capello
//
// This file is released under the terms of the MIT license.
// Read LICENSE.txt for more information.
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gfx/hsv.h"
#include "gfx/rgb.h"
#include <cmath>
namespace gfx {
using namespace std;
Hsv::Hsv(double hue, double saturation, double value)
: m_hue(hue)
, m_saturation(saturation)
, m_value(value)
{
while (m_hue < 0.0)
m_hue += 360.0;
m_hue = fmod(hue, 360.0);
assert(hue >= 0.0 && hue <= 360.0);
assert(saturation >= 0.0 && saturation <= 1.0);
assert(value >= 0.0 && value <= 1.0);
}
// Reference: http://en.wikipedia.org/wiki/HSL_and_HSV
Hsv::Hsv(const Rgb& rgb)
{
int M = rgb.maxComponent();
int m = rgb.minComponent();
int c = M - m;
double chroma = double(c) / 255.0;
double hue_prime = 0.0;
double h, s, v;
double r, g, b;
v = double(M) / 255.0;
if (c == 0) {
h = 0.0; // Undefined Hue because max == min
s = 0.0;
}
else {
r = double(rgb.red()) / 255.0;
g = double(rgb.green()) / 255.0;
b = double(rgb.blue()) / 255.0;
s = chroma / v;
if (M == rgb.red()) {
hue_prime = (g - b) / chroma;
while (hue_prime < 0.0)
hue_prime += 6.0;
hue_prime = fmod(hue_prime, 6.0);
}
else if (M == rgb.green()) {
hue_prime = ((b - r) / chroma) + 2.0;
}
else if (M == rgb.blue()) {
hue_prime = ((r - g) / chroma) + 4.0;
}
h = hue_prime * 60.0;
}
m_hue = h;
m_saturation = s;
m_value = v;
}
int Hsv::hueInt() const
{
return int(floor(m_hue + 0.5));
}
int Hsv::saturationInt() const
{
return int(floor(m_saturation*100.0 + 0.5));
}
int Hsv::valueInt() const
{
return int(floor(m_value*100.0 + 0.5));
}
} // namespace gfx