-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColorCvt.cpp
156 lines (125 loc) · 3.38 KB
/
ColorCvt.cpp
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//---------------------------------------------------------------------------
#pragma hdrstop
#include <algorithm>
#include "ColorCvt.h"
#include "cmath"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
namespace ColorCvt {
//---------------------------------------------------------------------------
TRGB HSVToRGB( THSV hsv )
{
double r = 0.0;
double g = 0.0;
double b = 0.0;
if ( hsv.GetS() == 0.0 ) {
r = hsv.GetV();
g = hsv.GetV();
b = hsv.GetV();
}
else {
int i;
double f, p, q, t, h;
h = std::fmod( std::abs( hsv.GetH() ), 360.0 ) / 60.0;
i = (int)std::floor( h );
//f = std::fmod( h, 1.0 );
f = h - i;
p = hsv.GetV() * ( 1.0 - hsv.GetS() );
q = hsv.GetV() * ( 1.0 - ( hsv.GetS() * f ) );
t = hsv.GetV() * ( 1.0 - ( hsv.GetS() * ( 1.0 - f ) ) );
switch (i)
{
case 0:
r = hsv.GetV();
g = t;
b = p;
break;
case 1:
r = q;
g = hsv.GetV();
b = p;
break;
case 2:
r = p;
g = hsv.GetV();
b = t;
break;
case 3:
r = p;
g = q;
b = hsv.GetV();
break;
case 4:
r = t;
g = p;
b = hsv.GetV();
break;
default:
r = hsv.GetV();
g = p;
b = q;
break;
}
}
return TRGB(
static_cast<uint8_t>( std::min( r * 255.0, 255.0 ) ),
static_cast<uint8_t>( std::min( g * 255.0, 255.0 ) ),
static_cast<uint8_t>( std::min( b * 255.0, 255.0 ) )
);
}
//---------------------------------------------------------------------------
RGBTRIPLE TRGB::ToRGBTRIPLE() const
{
RGBTRIPLE triple;
triple.rgbtBlue = b_;;
triple.rgbtGreen = g_;
triple.rgbtRed = r_;
return triple;
}
//---------------------------------------------------------------------------
static double HueToRGB( double v1, double v2, double vH )
{
if ( vH < 0 ) {
vH += 1;
}
if ( vH > 1 ) {
vH -= 1;
}
if ( ( 6.0 * vH ) < 1 ) {
return ( v1 + ( v2 - v1 ) * 6.0 * vH );
}
if ( ( 2.0 * vH ) < 1.0 ) {
return v2;
}
if ( ( 3 * vH ) < 2.0 ) {
return ( v1 + ( v2 - v1 ) * ( ( 2.0 / 3.0 ) - vH ) * 6.0 );
}
return v1;
}
TRGB HSLToRGB( THSL hsl )
{
uint8_t r = 0;
uint8_t g = 0;
uint8_t b = 0;
if ( hsl.GetS() == 0 ) {
r = g = b = static_cast<uint8_t>( hsl.GetL() * 255.0 );
}
else {
float v1, v2;
float hue = static_cast<double>( hsl.GetH() ) / 360.0;
v2 =
( hsl.GetL() < 0.5 ) ?
( hsl.GetL() * ( 1 + hsl.GetS() ) )
:
( (hsl.GetL() + hsl.GetS() ) - ( hsl.GetL() * hsl.GetS() ) );
v1 = 2 * hsl.GetL() - v2;
r = static_cast<uint8_t>( 255.0 * HueToRGB( v1, v2, hue + ( 1.0 / 3 ) ) );
g = static_cast<uint8_t>( 255.0 * HueToRGB( v1, v2, hue ) );
b = static_cast<uint8_t>( 255.0 * HueToRGB( v1, v2, hue - ( 1.0 / 3 ) ) );
}
return TRGB( r, g, b );
}
//---------------------------------------------------------------------------
} // End of namespace ColorCvt
//---------------------------------------------------------------------------