$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
hex_to_color.hh
1 // Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef SCRIBO_UTIL_HEX_TO_COLOR_HH
27 # define SCRIBO_UTIL_HEX_TO_COLOR_HH
28 
32 
33 #include <iostream>
34 #include <string>
35 #include <vector>
36 #include <mln/value/rgb8.hh>
37 #include <mln/value/int_u8.hh>
38 
39 namespace scribo
40 {
41 
42  namespace util
43  {
44  using namespace mln;
45 
46  // \brief Convert hexadecimal encoded colors to value::rgb8.
47  value::rgb8 hex_to_color(const std::string& hex);
48 
49 
50 # ifndef MLN_INCLUDE_ONLY
51 
52  namespace internal
53  {
54 
55  value::int_u8 convert_from_hex(const std::string& hex)
56  {
57  value::int_u8 value = 0;
58  int a = 0;
59  int b = hex.length() - 1;
60 
61  for (; b >= 0; a++, b--)
62  {
63  if (hex[b] >= '0' && hex[b] <= '9')
64  {
65  value += (hex[b] - '0') * (1 << (a * 4));
66  }
67  else
68  {
69  switch (hex[b])
70  {
71  case 'A':
72  case 'a':
73  value += 10 * (1 << (a * 4));
74  break;
75  case 'B':
76  case 'b':
77  value += 11 * (1 << (a * 4));
78  break;
79  case 'C':
80  case 'c':
81  value += 12 * (1 << (a * 4));
82  break;
83  case 'D':
84  case 'd':
85  value += 13 * (1 << (a * 4));
86  break;
87  case 'E':
88  case 'e':
89  value += 14 * (1 << (a * 4));
90  break;
91  case 'F':
92  case 'f':
93  value += 15 * (1 << (a * 4));
94  break;
95  default:
96  std::cerr << "Error, invalid character '"
97  << hex[a] << "' in hex number" << std::endl;
98  break;
99  }
100  }
101  }
102 
103  return value;
104  }
105 
106  } // end of namespace scribo::util::internal
107 
108 
109 
110  value::rgb8 hex_to_color(const std::string& hex)
111  {
112  mln_trace("scribo::util::hex_to_color");
113 
114  mln_precondition(!hex.empty());
115 
116  std::string red, green, blue;
117 
118  if (hex[0] == '#')
119  {
120  red = hex.substr(1, 2);
121  green = hex.substr(3, 2);
122  blue = hex.substr(5, 2);
123  }
124  else
125  {
126  red = hex.substr(0, 2);
127  green = hex.substr(2, 2);
128  blue = hex.substr(4, 2);
129  }
130 
131  value::rgb8 v(internal::convert_from_hex(red),
132  internal::convert_from_hex(green),
133  internal::convert_from_hex(blue));
134 
135  return v;
136  }
137 
138 # endif // ! MLN_INCLUDE_ONLY
139 
140  } // end of namespace scribo::util
141 
142 } // end of namespace scribo
143 
144 #endif // ! SCRIBO_UTIL_HEX_TO_COLOR_HH