-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCGRealHSB.cpp
141 lines (110 loc) · 2.19 KB
/
CGRealHSB.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
// CGRealHSB.cpp
//
// CGRealHSB Class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
CGRealHSB CGRealHSB::FromRGB (const CGRealRGB &rgbColor)
// FromRGB
//
// Generates a real HSB value from 32-bit RGB
// From: Computer Graphics, Foley & van Dam, p.592
{
enum EColors
{
colorRed = 0,
colorGreen = 1,
colorBlue = 2,
};
// Figure out which is the primary (brightest) color
int iPrimary;
Metric rMax;
Metric rMin;
// R G B
// R B G
// B R G
if (rgbColor.GetRed() >= rgbColor.GetGreen())
{
// R G B
// R B G
if (rgbColor.GetRed() >= rgbColor.GetBlue())
{
iPrimary = colorRed;
rMax = rgbColor.GetRed();
if (rgbColor.GetGreen() >= rgbColor.GetBlue())
rMin = rgbColor.GetBlue();
else
rMin = rgbColor.GetGreen();
}
// B R G
else
{
iPrimary = colorBlue;
rMax = rgbColor.GetBlue();
rMin = rgbColor.GetGreen();
}
}
// G R B
// G B R
// B G R
else
{
// G R B
// G B R
if (rgbColor.GetGreen() >= rgbColor.GetBlue())
{
iPrimary = colorGreen;
rMax = rgbColor.GetGreen();
if (rgbColor.GetRed() > rgbColor.GetBlue())
rMin = rgbColor.GetBlue();
else
rMin = rgbColor.GetRed();
}
// B G R
else
{
iPrimary = colorBlue;
rMax = rgbColor.GetBlue();
rMin = rgbColor.GetRed();
}
}
// Brightness
Metric rBrightness = rMax;
// Saturation
Metric rDelta = rMax - rMin;
Metric rSaturation = (rMax != 0.0 ? (rDelta / rMax) : 0.0);
// Hue
Metric rHue;
if (rSaturation == 0.0)
rHue = 0.0; // Undefined, but we just set it to 0
else
{
switch (iPrimary)
{
case colorRed:
rHue = (rgbColor.GetGreen() - rgbColor.GetBlue()) / rDelta;
break;
case colorGreen:
rHue = 2.0 + (rgbColor.GetBlue() - rgbColor.GetRed()) / rDelta;
break;
case colorBlue:
rHue = 4.0 + (rgbColor.GetRed() - rgbColor.GetGreen()) / rDelta;
break;
}
rHue *= 60.0;
if (rHue < 0.0)
rHue += 360.0;
}
return CGRealHSB(
rHue,
rSaturation,
rBrightness,
rgbColor.GetAlpha()
);
}
CGRealHSB CGRealHSB::FromRGB (CG32bitPixel rgbColor)
// FromRGB
//
// Converts from RGB to HSB
{
return CGRealHSB::FromRGB(CGRealRGB(rgbColor));
}