VCG Library
colorspace.cpp
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004-2016 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 
24 // Standard headers
25 #include <iostream>
26 
27 // VCG headers
28 #include <vcg/space/color4.h>
29 #include <vcg/space/colorspace.h>
30 
31 using namespace std;
32 
33 typedef vcg::ColorSpace<double> ColorSpace;
34 
35 int main(int argc,char ** argv)
36 {
37  // Some Color Space processing examples
38 
39  cout << endl;
40 
41  if (argc != 4)
42  {
43  cout << " Usage: colorspace <r> <g> <b>" << endl << endl;
44  cout << " <r> <g> <b> : RGB triplet (0-255)" << endl << endl;
45  cout << " Note: The RGB triplet is assumed in the sRGB space (D65 illuminant).";
46  cout << endl << endl;
47  exit(-2);
48  }
49 
50  double r = static_cast<double>(atof(argv[1]));
51  double g = static_cast<double>(atof(argv[2]));
52  double b = static_cast<double>(atof(argv[3]));
53 
54  // RGB components have to be in the range [0.0, 1.0]
55  vcg::Color4<double> color(r/255.0, g/255.0, b/255.0, 0.0);
56 
57 
58  // RGB --> RGB (RGB space changing)
60 
61  cout << " * RGB --> RGB conversion" << endl << endl;
62 
63  vcg::Color4<double> rgb = ColorSpace::RGBtoRGB(color, ColorSpace::SRGB, ColorSpace::PAL_RGB);
64  rgb *= 255.0;
65  cout << " RGB (PAL/SECAM): " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl;
66 
67  rgb = ColorSpace::RGBtoRGB(color, ColorSpace::SRGB, ColorSpace::WIDE_GAMUT);
68  rgb *= 255.0;
69  cout << " RGB (Wide Gamut): " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl << endl;
70 
71 
72  // RGB <--> HSL (Hue, Saturation, Lightness)
74 
75  cout << " * RGB <--> HSL conversion" << endl << endl;
76 
77  vcg::Color4<double> hsl = ColorSpace::RGBtoHSL(color);
78  cout << " RGB --> HSL: " << hsl[0]*360.0 << " " << hsl[1]*100.0 << "% " << hsl[2]*100.0 << "% " << endl;
79 
80  rgb = ColorSpace::HSLtoRGB(hsl);
81  rgb *= 255.0;
82  cout << " HSL --> RGB: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl << endl;
83 
84 
85  // RGB <--> HSV (Hue, Saturation, Value)
87 
88  cout << " * RGB <--> HSV conversion" << endl << endl;
89 
90  vcg::Color4<double> hsv = ColorSpace::RGBtoHSV(color);
91  cout << " RGB --> HSV: " << hsv[0]*360.0 << " " << hsv[1]*100.0 << "% " << hsv[2]*100.0 << "% " << endl;
92 
93  rgb = ColorSpace::HSVtoRGB(hsv);
94  rgb *= 255.0;
95  cout << " HSV --> RGB: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl << endl;
96 
97 
98  // RGB --> CIELab
100 
101  cout << " * RGB <--> Lab conversion" << endl << endl;
102 
103  vcg::Color4<double> xyzD65 = ColorSpace::RGBtoXYZ(color, ColorSpace::SRGB, ColorSpace::VCG_ILLUMINANT_D65);
104  vcg::Color4<double> lab = ColorSpace::XYZtoCIELab(xyzD65, ColorSpace::refIlluminant(ColorSpace::SRGB));
105  cout << " RGB --> CIELab: " << lab[0] << " " << lab[1] << " " << lab[2] << endl;
106 
107  vcg::Color4<double> xyz = ColorSpace::CIELabtoXYZ(lab, ColorSpace::refIlluminant(ColorSpace::SRGB));
108  rgb = ColorSpace::XYZtoRGB(xyz, ColorSpace::refIlluminant(ColorSpace::SRGB), ColorSpace::SRGB);
109  rgb *= 255.0;
110  cout << " CIELab --> RGB: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl << endl;
111 
112 
113 
114  // RGB <--> XYZ
116 
117  cout << " * RGB <--> XYZ conversion" << endl << endl;
118 
119  // RGB --> XYZ (D65)
120  cout << " RGB --> XYZ (D65): " << xyzD65[0] << " " << xyzD65[1] << " " << xyzD65[2] << endl;
121 
122  // XYZ (D65) --> RGB
123  rgb = ColorSpace::XYZtoRGB(xyzD65, ColorSpace::VCG_ILLUMINANT_D65, ColorSpace::SRGB);
124  rgb *= 255.0;
125  cout << " XYZ (D65) --> RGB: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl;
126 
127  // RGB --> XYZ (D50)
128  vcg::Color4<double> xyzD50 = ColorSpace::RGBtoXYZ(color, ColorSpace::SRGB, ColorSpace::VCG_ILLUMINANT_D50);
129  cout << " RGB --> XYZ (D50): " << xyzD50[0] << " " << xyzD50[1] << " " << xyzD50[2] << endl;
130 
131  // XYZ (D50) --> RGB
132  rgb = ColorSpace::XYZtoRGB(xyzD50, ColorSpace::VCG_ILLUMINANT_D50, ColorSpace::SRGB);
133  rgb *= 255.0;
134  cout << " XYZ (D50) --> RGB: " << rgb[0] << " " << rgb[1] << " " << rgb[2] << endl;
135 
136  // Direct way
137  xyz = ColorSpace::chromaticAdaptation(xyzD65, ColorSpace::VCG_ILLUMINANT_D65, ColorSpace::VCG_ILLUMINANT_D50);
138  cout << " XYZ (D65 --> D50): " << xyz[0] << " " << xyz[1] << " " << xyz[2] << endl << endl;
139 
140  return 0;
141 }
142