forked from aseprite/aseprite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrgb.cpp
More file actions
82 lines (68 loc) · 1.39 KB
/
rgb.cpp
File metadata and controls
82 lines (68 loc) · 1.39 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
// 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.
#include "gfx/rgb.h"
#include "gfx/hsv.h"
#include <cmath>
namespace gfx {
using namespace std;
// Reference: http://en.wikipedia.org/wiki/HSL_and_HSV
Rgb::Rgb(const Hsv& hsv)
{
double chroma = hsv.value() * hsv.saturation();
double hue_prime = hsv.hue() / 60.0;
double x = chroma * (1.0 - fabs(fmod(hue_prime, 2.0) - 1.0));
double r, g, b;
r = g = b = 0.0;
switch (int(hue_prime)) {
case 6:
case 0:
r = chroma;
g = x;
break;
case 1:
r = x;
g = chroma;
break;
case 2:
g = chroma;
b = x;
break;
case 3:
g = x;
b = chroma;
break;
case 4:
b = chroma;
r = x;
break;
case 5:
b = x;
r = chroma;
break;
}
double m = hsv.value() - chroma;
r += m;
g += m;
b += m;
m_red = int(r*255.0+0.5);
m_green = int(g*255.0+0.5);
m_blue = int(b*255.0+0.5);
}
int Rgb::maxComponent() const
{
if (m_red > m_green)
return (m_red > m_blue) ? m_red: m_blue;
else
return (m_green > m_blue) ? m_green: m_blue;
}
int Rgb::minComponent() const
{
if (m_red < m_green)
return (m_red < m_blue) ? m_red: m_blue;
else
return (m_green < m_blue) ? m_green: m_blue;
}
} // namespace gfx