colourConverter.h 5.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
	/**
	 * \enum ColourEncoding
	 * Supported colour spaces
	 */
	enum ColourEncoding
	{
61 62 63 64 65 66
		C_RGB = 0, /*!< R,G,B in [0,1] */
		C_XYZ = 1, /*!< X,Y,Z in [0,1] */
		C_Luv = 2, /*!< L in [0,100], u in [-83,175], v in [-134,108] */
		C_Lab = 3, /*!< L in [0,100], u in [-86,98], v in [-108,95] */
		C_HSV = 4,  /*!< H,S,V in [0,1] */
		C_HSL = 5  /*!< H,S,L in [0,1] */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
67 68 69
	} ;

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

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

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

public: // members
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
125
	enum ColourEncoding originalEnc ;  /*!< Colour space of original (unaltered) data */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
126 127 128 129

private: // private members
	VEC3 *RGB ;
	VEC3 *Luv ;
130
	VEC3 *Lab ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
131
	VEC3 *HSV ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
132
	VEC3 *XYZ ;
133
	VEC3 *HSL ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
134 135 136

	bool convert(enum ColourEncoding from, enum ColourEncoding to) ;
	void convertRGBtoXYZ() ;
137 138
	void convertXYZtoRGB() ;

Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
139 140
	void convertXYZtoLuv() ;
	void convertLuvToXYZ() ;
141 142 143 144

	void convertXYZtoLab() ;
	void convertLabToXYZ() ;

145 146 147 148
	/**
	 * Converts RGB to HSV. All is normalized between 0 and 1.
	 * Conversion formula adapted from http://en.wikipedia.org/wiki/HSL_color_space.
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
149
	void convertRGBtoHSV() ;
150 151 152 153
	/**
	 * Converts HSV to RGB. All is normalized between 0 and 1.
	 * Conversion formula adapted from http://en.wikipedia.org/wiki/HSL_color_space.
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
154 155
	void convertHSVtoRGB() ;

156 157 158 159 160 161 162 163 164 165 166 167 168
	/**
	 * Converts RGB to HSL. All is normalized between 0 and 1.
	 * Conversion formula adapted from http://en.wikipedia.org/wiki/HSL_color_space.
	 */
	void convertRGBtoHSL() ;
	/**
	 * Converts HSL to RGB. All is normalized between 0 and 1.
	 * Conversion formula adapted from http://en.wikipedia.org/wiki/HSL_color_space.
	 */
	void convertHSLtoRGB() ;

	static REAL hue2rgb(const REAL& p, const REAL& q, REAL t) ;

169 170 171 172 173 174 175 176
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
177 178 179 180 181 182 183 184 185 186

} ;

} // namespace Utils

} // namespace CGoGN

#include "colourConverter.hpp"

#endif // __COLOURCONVERTER_H__