colourConverter.h 4.45 KB
Newer Older
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
21 22 23 24 25 26 27
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __COLOURCONVERTER_H__
#define __COLOURCONVERTER_H__

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
28 29
#include <iostream>

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
30 31
#include "Geometry/vector_gen.h"
#include "Geometry/matrix.h"
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
32 33

// #define DISPLAY
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
34 35 36 37 38

namespace CGoGN {

namespace Utils {

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
39 40 41 42
/*!
 * \class ColourConverter
 * \brief Class for switching between different tri-channel color-spaces
 *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
43 44 45 46 47 48 49 50 51 52 53 54
 * Class for colour conversions between the enumerated colour spaces.
 * Usage :
 *  VEC3 colRGB ; 								// current colour in RGB for example
 *  ColourConverter<REAL> cc(colRGB,C_RGB) ;	// Tell constructor you provided RGB
 *  VEC3 colLuv = cc.getLuv() ; 				// ask whatever supported colour type you require
 *
 * All conversion formulae were provided by "Notes about color", january 5th, 2011 by B. Sauvage
 */
template <typename REAL>
class ColourConverter
{
public: // types
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
55 56 57 58 59 60 61 62 63
	/**
	 * \enum ColourEncoding
	 * Supported colour spaces
	 */
	enum ColourEncoding
	{
		C_RGB = 0,
		C_XYZ = 1,
		C_Luv = 2,
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
64 65
		C_Lab = 3,
		C_HSV = 4
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
66 67 68
	} ;

	typedef Geom::Vector<3,REAL> VEC3 ; /*!< Triplet for color encoding */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
69 70

public: // methods
71
	/**
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
72
	 * \brief Constructor
73 74 75
	 * @param col a VEC3 colour
	 * @param enc the colour space of provided colour
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
76
	ColourConverter(const VEC3& col, const enum ColourEncoding& enc) ;
77
	/**
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
78
	 * \brief Destructor
79
	 */
80
	~ColourConverter() ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
81

82
	/**
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
83
	 * \brief getR
84 85 86 87
	 * @return original value (in its original space)
	 */
	VEC3 getOriginal() ;
	/**
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
88 89
	 * \brief getR
	 * @return value of provided colour
90 91
	 */
	VEC3 getColour(enum ColourEncoding enc) ;
92
	/**
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
93
	 * \brief getR
94 95
	 * @return RGB value of provided colour
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
96
	VEC3 getRGB() ;
97 98 99 100
	/**
	 * getR
	 * @return Luv value of provided colour
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
101
	VEC3 getLuv() ;
102 103 104 105 106
	/**
	 * getR
	 * @return Lab value of provided colour
	 */
	VEC3 getLab() ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
107 108 109 110 111
	/**
	 * getR
	 * @return Lab value of provided colour
	 */
	VEC3 getHSV() ;
112 113 114 115
	/**
	 * getR
	 * @return XYZ value of provided colour
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
116 117 118
	VEC3 getXYZ() ;

public: // members
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
119
	enum ColourEncoding originalEnc ;  /*!< Colour space of original (unaltered) data */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
120 121 122 123

private: // private members
	VEC3 *RGB ;
	VEC3 *Luv ;
124
	VEC3 *Lab ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
125
	VEC3 *HSV ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
126 127 128 129
	VEC3 *XYZ ;

	bool convert(enum ColourEncoding from, enum ColourEncoding to) ;
	void convertRGBtoXYZ() ;
130 131
	void convertXYZtoRGB() ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
132 133
	void convertXYZtoLuv() ;
	void convertLuvToXYZ() ;
134 135 136 137

	void convertXYZtoLab() ;
	void convertLabToXYZ() ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
138 139 140
	void convertRGBtoHSV() ;
	void convertHSVtoRGB() ;

141 142 143 144 145 146 147 148
private: // private constants
	// D65 reference white
	static const REAL Xn = 0.950456 ;
	static const REAL Yn = 1.0 ;
	static const REAL Zn = 1.088754 ;

	static const REAL un = 0.197832 ;
	static const REAL vn = 0.468340 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
149 150 151 152 153 154 155 156 157 158

} ;

} // namespace Utils

} // namespace CGoGN

#include "colourConverter.hpp"

#endif // __COLOURCONVERTER_H__