| 1 |
// Copyright 2007 Christian Henning. |
| 2 |
// Distributed under the Boost Software License, Version 1.0. (See |
| 3 |
// accompanying file LICENSE_1_0.txt or copy at |
| 4 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 5 |
|
| 6 |
/*************************************************************************************************/ |
| 7 |
|
| 8 |
#ifndef GIL_HSL_H |
| 9 |
#define GIL_HSL_H |
| 10 |
|
| 11 |
//////////////////////////////////////////////////////////////////////////////////////// |
| 12 |
/// \file |
| 13 |
/// \brief Support for HSL color space |
| 14 |
/// \author Christian Henning \n |
| 15 |
//////////////////////////////////////////////////////////////////////////////////////// |
| 16 |
|
| 17 |
#include <mapnik/warning.hpp> |
| 18 |
MAPNIK_DISABLE_WARNING_PUSH |
| 19 |
#include <mapnik/warning_ignore.hpp> |
| 20 |
#if BOOST_VERSION >= 106900 |
| 21 |
#include <boost/gil.hpp> |
| 22 |
#else |
| 23 |
#include <boost/gil/gil_all.hpp> |
| 24 |
#endif |
| 25 |
|
| 26 |
MAPNIK_DISABLE_WARNING_POP |
| 27 |
|
| 28 |
namespace boost { namespace gil { |
| 29 |
|
| 30 |
/// \addtogroup ColorNameModel |
| 31 |
/// \{ |
| 32 |
namespace hsl_color_space |
| 33 |
{ |
| 34 |
/// \brief Hue |
| 35 |
struct hue_t {}; |
| 36 |
/// \brief Saturation |
| 37 |
struct saturation_t {}; |
| 38 |
/// \brief Lightness |
| 39 |
struct lightness_t {}; |
| 40 |
} |
| 41 |
/// \} |
| 42 |
|
| 43 |
/// \ingroup ColorSpaceModel |
| 44 |
typedef mpl::vector3< hsl_color_space::hue_t |
| 45 |
, hsl_color_space::saturation_t |
| 46 |
, hsl_color_space::lightness_t |
| 47 |
> hsl_t; |
| 48 |
|
| 49 |
/// \ingroup LayoutModel |
| 50 |
typedef layout<hsl_t> hsl_layout_t; |
| 51 |
|
| 52 |
#if BOOST_VERSION >= 106800 |
| 53 |
using bits32f = boost::gil::float32_t; |
| 54 |
GIL_DEFINE_ALL_TYPEDEFS( 32f, float32_t, hsl ); |
| 55 |
#else |
| 56 |
GIL_DEFINE_ALL_TYPEDEFS( 32f, hsl ); |
| 57 |
#endif |
| 58 |
|
| 59 |
/// \ingroup ColorConvert |
| 60 |
/// \brief RGB to HSL |
| 61 |
template <> |
| 62 |
struct default_color_converter_impl< rgb_t, hsl_t > |
| 63 |
{ |
| 64 |
template <typename P1, typename P2> |
| 65 |
void operator()( const P1& src, P2& dst ) const |
| 66 |
{ |
| 67 |
using namespace hsl_color_space; |
| 68 |
|
| 69 |
// only bits32f for hsl is supported |
| 70 |
bits32f temp_red = channel_convert<bits32f>( get_color( src, red_t() )); |
| 71 |
bits32f temp_green = channel_convert<bits32f>( get_color( src, green_t() )); |
| 72 |
bits32f temp_blue = channel_convert<bits32f>( get_color( src, blue_t() )); |
| 73 |
|
| 74 |
bits32f hue, saturation, lightness; |
| 75 |
bits32f min_color, max_color; |
| 76 |
|
| 77 |
if( temp_red < temp_green ) |
| 78 |
{ |
| 79 |
min_color = std::min( temp_blue, temp_red ); |
| 80 |
max_color = std::max( temp_blue, temp_green ); |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
min_color = std::min( temp_blue, temp_green ); |
| 85 |
max_color = std::max( temp_blue, temp_red ); |
| 86 |
} |
| 87 |
|
| 88 |
if ( max_color - min_color < 0.001 ) |
| 89 |
{ |
| 90 |
// rgb color is gray |
| 91 |
|
| 92 |
hue = 0.f; |
| 93 |
saturation = 0.f; |
| 94 |
|
| 95 |
// doesn't matter which rgb channel we use. |
| 96 |
lightness = temp_red; |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
|
| 101 |
bits32f diff = max_color - min_color; |
| 102 |
|
| 103 |
// lightness calculation |
| 104 |
|
| 105 |
lightness = ( min_color + max_color ) / 2.f; |
| 106 |
|
| 107 |
// saturation calculation |
| 108 |
|
| 109 |
if( lightness < 0.5f ) |
| 110 |
{ |
| 111 |
saturation = diff |
| 112 |
/ ( min_color + max_color ); |
| 113 |
} |
| 114 |
else |
| 115 |
{ |
| 116 |
saturation = ( max_color - min_color ) |
| 117 |
/ ( 2.f - diff ); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
// hue calculation |
| 122 |
if( std::abs( max_color - temp_red ) < 0.0001f ) |
| 123 |
{ |
| 124 |
// max_color is red |
| 125 |
hue = ( temp_green - temp_blue ) |
| 126 |
/ diff; |
| 127 |
|
| 128 |
} |
| 129 |
else if( std::abs( max_color - temp_green) < 0.0001f ) |
| 130 |
{ |
| 131 |
// max_color is green |
| 132 |
// 2.0 + (b - r) / (maxColor - minColor) |
| 133 |
hue = 2.f |
| 134 |
+ ( temp_blue - temp_red ) |
| 135 |
/ diff; |
| 136 |
|
| 137 |
} |
| 138 |
else |
| 139 |
{ |
| 140 |
// max_color is blue |
| 141 |
// 4.0 + (r - g) / (maxColor - minColor) |
| 142 |
hue = 4.f |
| 143 |
+ ( temp_red - temp_green ) |
| 144 |
/ diff; |
| 145 |
} |
| 146 |
|
| 147 |
hue /= 6.f; |
| 148 |
|
| 149 |
if( hue < 0.f ) |
| 150 |
{ |
| 151 |
hue += 1.f; |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
get_color( dst,hue_t() ) = hue; |
| 156 |
get_color( dst,saturation_t() ) = saturation; |
| 157 |
get_color( dst,lightness_t() ) = lightness; |
| 158 |
} |
| 159 |
}; |
| 160 |
|
| 161 |
/// \ingroup ColorConvert |
| 162 |
/// \brief HSL to RGB |
| 163 |
template <> |
| 164 |
struct default_color_converter_impl<hsl_t,rgb_t> |
| 165 |
{ |
| 166 |
template <typename P1, typename P2> |
| 167 |
void operator()( const P1& src, P2& dst) const |
| 168 |
{ |
| 169 |
using namespace hsl_color_space; |
| 170 |
|
| 171 |
bits32f red, green, blue; |
| 172 |
|
| 173 |
if( std::abs( get_color( src, saturation_t() )) < 0.0001 ) |
| 174 |
{ |
| 175 |
// If saturation is 0, the color is a shade of gray |
| 176 |
red = get_color( src, lightness_t() ); |
| 177 |
green = get_color( src, lightness_t() ); |
| 178 |
blue = get_color( src, lightness_t() ); |
| 179 |
} |
| 180 |
else |
| 181 |
{ |
| 182 |
float temp1, temp2; |
| 183 |
float tempr, tempg, tempb; |
| 184 |
|
| 185 |
//Set the temporary values |
| 186 |
if( get_color( src, lightness_t() ) < 0.5 ) |
| 187 |
{ |
| 188 |
temp2 = get_color( src, lightness_t() ) |
| 189 |
* ( 1.f + get_color( src, saturation_t() ) ); |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
temp2 = ( get_color( src, lightness_t() ) + get_color( src, saturation_t() )) |
| 194 |
- ( get_color( src, lightness_t() ) * get_color( src, saturation_t() )); |
| 195 |
} |
| 196 |
|
| 197 |
temp1 = 2.f |
| 198 |
* get_color( src, lightness_t() ) |
| 199 |
- temp2; |
| 200 |
|
| 201 |
tempr = get_color( src, hue_t() ) + 1.f / 3.f; |
| 202 |
|
| 203 |
if( tempr > 1.f ) |
| 204 |
{ |
| 205 |
tempr--; |
| 206 |
} |
| 207 |
|
| 208 |
tempg = get_color( src, hue_t() ); |
| 209 |
tempb = get_color( src, hue_t() ) - 1.f / 3.f; |
| 210 |
|
| 211 |
if( tempb < 0.f ) |
| 212 |
{ |
| 213 |
tempb++; |
| 214 |
} |
| 215 |
|
| 216 |
//Red |
| 217 |
if( tempr < 1.f / 6.f ) |
| 218 |
{ |
| 219 |
red = temp1 + ( temp2 - temp1 ) * 6.f * tempr; |
| 220 |
} |
| 221 |
else if( tempr < 0.5f ) |
| 222 |
{ |
| 223 |
red = temp2; |
| 224 |
} |
| 225 |
else if( tempr < 2.f / 3.f ) |
| 226 |
{ |
| 227 |
red = temp1 + (temp2 - temp1) |
| 228 |
* (( 2.f / 3.f ) - tempr) * 6.f; |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
red = temp1; |
| 233 |
} |
| 234 |
|
| 235 |
//Green |
| 236 |
if( tempg < 1.f / 6.f ) |
| 237 |
{ |
| 238 |
green = temp1 + ( temp2 - temp1 ) * 6.f * tempg; |
| 239 |
} |
| 240 |
else if( tempg < 0.5f ) |
| 241 |
{ |
| 242 |
green = temp2; |
| 243 |
} |
| 244 |
else if( tempg < 2.f / 3.f ) |
| 245 |
{ |
| 246 |
green = temp1 + ( temp2 - temp1 ) |
| 247 |
* (( 2.f / 3.f ) - tempg) * 6.f; |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
green = temp1; |
| 252 |
} |
| 253 |
|
| 254 |
//Blue |
| 255 |
if( tempb < 1.f / 6.f ) |
| 256 |
{ |
| 257 |
blue = temp1 + (temp2 - temp1) * 6.f * tempb; |
| 258 |
} |
| 259 |
else if( tempb < 0.5f ) |
| 260 |
{ |
| 261 |
blue = temp2; |
| 262 |
} |
| 263 |
else if( tempb < 2.f / 3.f ) |
| 264 |
{ |
| 265 |
blue = temp1 + (temp2 - temp1) |
| 266 |
* (( 2.f / 3.f ) - tempb) * 6.f; |
| 267 |
} |
| 268 |
else |
| 269 |
{ |
| 270 |
blue = temp1; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
get_color(dst,red_t()) = |
| 275 |
channel_convert<typename color_element_type< P2, red_t >::type>( red ); |
| 276 |
get_color(dst,green_t())= |
| 277 |
channel_convert<typename color_element_type< P2, green_t >::type>( green ); |
| 278 |
get_color(dst,blue_t()) = |
| 279 |
channel_convert<typename color_element_type< P2, blue_t >::type>( blue ); |
| 280 |
} |
| 281 |
}; |
| 282 |
|
| 283 |
} } // namespace boost::gil |
| 284 |
|
| 285 |
#endif // GIL_HSL_H |