-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCGRealRGB.cpp
62 lines (48 loc) · 1.64 KB
/
CGRealRGB.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
// CGRealRGB.cpp
//
// CGRealRGB Class
// Copyright (c) 2015 by Kronosaur Productions, LLC. All Rights Reserved.
#include "PreComp.h"
CGRealRGB::CGRealRGB (CG32bitPixel rgbColor) :
m_rR(rgbColor.GetRed() / 255.0),
m_rG(rgbColor.GetGreen() / 255.0),
m_rB(rgbColor.GetBlue() / 255.0),
m_rA(rgbColor.GetAlpha() / 255.0)
// CGRealRGB constructor
{
}
CGRealRGB CGRealRGB::FromHSB (const CGRealHSB &hsbColor)
// FromHSB
//
// Converts from HSB to RGB
// From: Computer Graphics, Foley & van Dam, p.593
{
if (hsbColor.GetSaturation() == 0.0)
return CGRealRGB(hsbColor.GetBrightness(), hsbColor.GetBrightness(), hsbColor.GetBrightness(), hsbColor.GetAlpha());
else
{
Metric rH = (hsbColor.GetHue() == 360.0 ? 0.0 : hsbColor.GetHue()) / 60.0;
Metric rI = floor(rH);
Metric rF = rH - rI;
Metric rP = hsbColor.GetBrightness() * (1.0 - hsbColor.GetSaturation());
Metric rQ = hsbColor.GetBrightness() * (1.0 - hsbColor.GetSaturation() * rF);
Metric rT = hsbColor.GetBrightness() * (1.0 - hsbColor.GetSaturation() * (1.0 - rF));
switch ((int)rI)
{
case 0:
return CGRealRGB(hsbColor.GetBrightness(), rT, rP, hsbColor.GetAlpha());
case 1:
return CGRealRGB(rQ, hsbColor.GetBrightness(), rP, hsbColor.GetAlpha());
case 2:
return CGRealRGB(rP, hsbColor.GetBrightness(), rT, hsbColor.GetAlpha());
case 3:
return CGRealRGB(rP, rQ, hsbColor.GetBrightness(), hsbColor.GetAlpha());
case 4:
return CGRealRGB(rT, rP, hsbColor.GetBrightness(), hsbColor.GetAlpha());
case 5:
return CGRealRGB(hsbColor.GetBrightness(), rP, rQ, hsbColor.GetAlpha());
default:
return CGRealRGB(0.0, 0.0, 0.0, 0.0);
}
}
}