colourConverter.h 5.66 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
/*!
 * \class ColourConverter
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
41
 * \brief Class for switching between different tri-channel color-spaces (see \link #ColourEncoding ColourEncoding enumeration\endlink  for available types)
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
42
 *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
43 44 45
 * Usage:\n
 *  VEC3 colRGB ; 								// current colour in RGB for example\n
 *  ColourConverter<REAL> cc(colRGB,C_RGB) ;	// Tell constructor you provided RGB\n
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
46 47
 *  VEC3 colLuv = cc.getLuv() ; 				// ask whatever supported colour type you require
 *
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
48
 * Some conversion formulae were provided by "Notes about color", january 5th, 2011 by B. Sauvage
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
49 50 51 52 53
 */
template <typename REAL>
class ColourConverter
{
public: // types
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
54 55 56 57 58 59
	/**
	 * \enum ColourEncoding
	 * Supported colour spaces
	 */
	enum ColourEncoding
	{
60 61 62
		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] */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
63
		C_Lab = 3, /*!< L in [0,100], a in [-86,98], b in [-108,95] */
64 65
		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
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 116
	/**
	 * getR
	 * @return HSL value of provided colour
	 */
	VEC3 getHSL() ;
117 118 119 120
	/**
	 * getR
	 * @return XYZ value of provided colour
	 */
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
121 122 123
	VEC3 getXYZ() ;

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

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

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

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

	void convertXYZtoLab() ;
	void convertLabToXYZ() ;

144 145 146 147
	/**
	 * 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
148
	void convertRGBtoHSV() ;
149 150 151 152
	/**
	 * 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
153 154
	void convertHSVtoRGB() ;

155 156 157 158 159 160 161 162 163 164 165 166 167
	/**
	 * 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) ;

168 169
private: // private constants
	// D65 reference white
Sylvain Thery's avatar
Sylvain Thery committed
170 171 172
	//static constexpr REAL Xn = 0.950456 ;
	//static constexpr REAL Yn = 1.0 ;
	//static constexpr REAL Zn = 1.088754 ;
173

Sylvain Thery's avatar
Sylvain Thery committed
174 175
	//static constexpr REAL un = 0.197832 ;
	//static constexpr REAL vn = 0.468340 ;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
176

Sylvain Thery's avatar
Sylvain Thery committed
177 178 179 180 181 182 183 184

	// constexpr not supported in VS2013
	static const REAL Xn;
	static const REAL Yn;
	static const REAL Zn;

	static const REAL un;
	static const REAL vn;
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
185 186 187 188 189 190
} ;

} // namespace Utils

} // namespace CGoGN

191
#include "Utils/colourConverter.hpp"
Kenneth Vanhoey's avatar
Kenneth Vanhoey committed
192 193

#endif // __COLOURCONVERTER_H__