916 |
30 Nov 09 |
jari |
// $Id$ |
916 |
30 Nov 09 |
jari |
2 |
|
916 |
30 Nov 09 |
jari |
3 |
/* |
978 |
12 Dec 09 |
peter |
Copyright (C) 2009 Jari Häkkinen, Peter Johansson |
1635 |
30 Mar 23 |
peter |
Copyright (C) 2010, 2023 Peter Johansson |
916 |
30 Nov 09 |
jari |
6 |
|
916 |
30 Nov 09 |
jari |
This file is part of svndigest, http://dev.thep.lu.se/svndigest |
916 |
30 Nov 09 |
jari |
8 |
|
916 |
30 Nov 09 |
jari |
svndigest is free software; you can redistribute it and/or modify it |
916 |
30 Nov 09 |
jari |
under the terms of the GNU General Public License as published by |
916 |
30 Nov 09 |
jari |
the Free Software Foundation; either version 3 of the License, or |
916 |
30 Nov 09 |
jari |
(at your option) any later version. |
916 |
30 Nov 09 |
jari |
13 |
|
916 |
30 Nov 09 |
jari |
svndigest is distributed in the hope that it will be useful, but |
916 |
30 Nov 09 |
jari |
WITHOUT ANY WARRANTY; without even the implied warranty of |
916 |
30 Nov 09 |
jari |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
916 |
30 Nov 09 |
jari |
General Public License for more details. |
916 |
30 Nov 09 |
jari |
18 |
|
916 |
30 Nov 09 |
jari |
You should have received a copy of the GNU General Public License |
916 |
30 Nov 09 |
jari |
along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
916 |
30 Nov 09 |
jari |
21 |
*/ |
916 |
30 Nov 09 |
jari |
22 |
|
1619 |
12 Mar 23 |
peter |
23 |
#include <config.h> |
1619 |
12 Mar 23 |
peter |
24 |
|
929 |
02 Dec 09 |
jari |
25 |
#include "Colors.h" |
916 |
30 Nov 09 |
jari |
26 |
|
928 |
02 Dec 09 |
jari |
27 |
#include "Configuration.h" |
928 |
02 Dec 09 |
jari |
28 |
|
916 |
30 Nov 09 |
jari |
29 |
#include <cstdlib> |
928 |
02 Dec 09 |
jari |
30 |
#include <ctype.h> |
1237 |
23 Oct 10 |
peter |
31 |
#include <sstream> |
916 |
30 Nov 09 |
jari |
32 |
|
916 |
30 Nov 09 |
jari |
33 |
namespace theplu { |
916 |
30 Nov 09 |
jari |
34 |
namespace svndigest { |
916 |
30 Nov 09 |
jari |
35 |
|
929 |
02 Dec 09 |
jari |
36 |
Colors* Colors::instance_=NULL; |
916 |
30 Nov 09 |
jari |
37 |
|
916 |
30 Nov 09 |
jari |
38 |
|
929 |
02 Dec 09 |
jari |
39 |
Colors::Colors(void) |
943 |
03 Dec 09 |
jari |
40 |
: color_map_(13) |
916 |
30 Nov 09 |
jari |
41 |
{ |
929 |
02 Dec 09 |
jari |
42 |
color_map_[ 0].label="green"; |
963 |
09 Dec 09 |
peter |
43 |
color_map_[ 0].str="009248"; |
929 |
02 Dec 09 |
jari |
44 |
color_map_[ 1].label="blue"; |
963 |
09 Dec 09 |
peter |
45 |
color_map_[ 1].str="1b75ba"; |
929 |
02 Dec 09 |
jari |
46 |
color_map_[ 2].label="red"; |
963 |
09 Dec 09 |
peter |
47 |
color_map_[ 2].str="bd202e"; |
929 |
02 Dec 09 |
jari |
48 |
color_map_[ 3].label="grey"; |
963 |
09 Dec 09 |
peter |
49 |
color_map_[ 3].str="838383"; |
929 |
02 Dec 09 |
jari |
50 |
color_map_[ 4].label="orange"; |
963 |
09 Dec 09 |
peter |
51 |
color_map_[ 4].str="f59120"; |
929 |
02 Dec 09 |
jari |
52 |
color_map_[ 5].label="brown"; |
963 |
09 Dec 09 |
peter |
53 |
color_map_[ 5].str="754d29"; |
929 |
02 Dec 09 |
jari |
54 |
color_map_[ 6].label="cyan"; |
963 |
09 Dec 09 |
peter |
55 |
color_map_[ 6].str="28aae1"; |
929 |
02 Dec 09 |
jari |
56 |
color_map_[ 7].label="lime"; |
963 |
09 Dec 09 |
peter |
57 |
color_map_[ 7].str="d5dd26"; |
929 |
02 Dec 09 |
jari |
58 |
color_map_[ 8].label="purple"; |
963 |
09 Dec 09 |
peter |
59 |
color_map_[ 8].str="8f288c"; |
929 |
02 Dec 09 |
jari |
60 |
color_map_[ 9].label="lgrey"; |
963 |
09 Dec 09 |
peter |
61 |
color_map_[ 9].str="dcdcdc"; |
929 |
02 Dec 09 |
jari |
62 |
color_map_[10].label="lbrown"; |
963 |
09 Dec 09 |
peter |
63 |
color_map_[10].str="c2976a"; |
929 |
02 Dec 09 |
jari |
64 |
color_map_[11].label="pink"; |
963 |
09 Dec 09 |
peter |
65 |
color_map_[11].str="e67de6"; |
929 |
02 Dec 09 |
jari |
66 |
color_map_[12].label="black"; |
963 |
09 Dec 09 |
peter |
67 |
color_map_[12].str="000000"; |
963 |
09 Dec 09 |
peter |
// calculate corresponding rgb values |
1513 |
23 Sep 12 |
peter |
69 |
for (std::vector<color>::iterator col = color_map_.begin(); |
963 |
09 Dec 09 |
peter |
70 |
col!=color_map_.end(); ++col) { |
963 |
09 Dec 09 |
peter |
71 |
str2rgb(col->str, col->r, col->g, col->b); |
963 |
09 Dec 09 |
peter |
72 |
} |
928 |
02 Dec 09 |
jari |
73 |
|
928 |
02 Dec 09 |
jari |
74 |
typedef std::map<std::string, std::string> ACmap; |
928 |
02 Dec 09 |
jari |
75 |
const ACmap& authcols=Configuration::instance().author_colors(); |
940 |
03 Dec 09 |
peter |
// reserve sufficient size in vector here to avoid reallocation |
940 |
03 Dec 09 |
peter |
// and iterators being invalidated |
940 |
03 Dec 09 |
peter |
78 |
color_map_.reserve(color_map_.size()+authcols.size()); |
940 |
03 Dec 09 |
peter |
79 |
next_color_=color_map_.begin(); |
928 |
02 Dec 09 |
jari |
80 |
for (ACmap::const_iterator i=authcols.begin(); i!=authcols.end(); i++) { |
929 |
02 Dec 09 |
jari |
81 |
color c; |
928 |
02 Dec 09 |
jari |
82 |
c.label=i->first; |
941 |
03 Dec 09 |
peter |
83 |
str2rgb(i->second, c.r, c.g, c.b); |
963 |
09 Dec 09 |
peter |
84 |
c.str = i->second; |
929 |
02 Dec 09 |
jari |
85 |
next_color_=color_map_.insert(next_color_,c); |
929 |
02 Dec 09 |
jari |
86 |
author_map_.insert(std::make_pair(c.label, next_color_++)); |
928 |
02 Dec 09 |
jari |
87 |
} |
916 |
30 Nov 09 |
jari |
88 |
} |
916 |
30 Nov 09 |
jari |
89 |
|
916 |
30 Nov 09 |
jari |
90 |
|
963 |
09 Dec 09 |
peter |
91 |
const std::string& Colors::color_str(const std::string& label) |
916 |
30 Nov 09 |
jari |
92 |
{ |
963 |
09 Dec 09 |
peter |
93 |
return get_color(label).str; |
963 |
09 Dec 09 |
peter |
94 |
} |
963 |
09 Dec 09 |
peter |
95 |
|
963 |
09 Dec 09 |
peter |
96 |
|
963 |
09 Dec 09 |
peter |
97 |
const Colors::color& Colors::get_color(const std::string& label) |
963 |
09 Dec 09 |
peter |
98 |
{ |
929 |
02 Dec 09 |
jari |
99 |
std::map<std::string,std::vector<color>::iterator>::iterator i; |
921 |
01 Dec 09 |
peter |
100 |
i = author_map_.lower_bound(label); |
921 |
01 Dec 09 |
peter |
101 |
if (i==author_map_.end() || i->first != label) { |
929 |
02 Dec 09 |
jari |
// no color defined for label, set color for label |
929 |
02 Dec 09 |
jari |
103 |
i = author_map_.insert(i, std::make_pair(label, next_color_++)); |
929 |
02 Dec 09 |
jari |
104 |
if (next_color_==color_map_.end()) |
929 |
02 Dec 09 |
jari |
// end of color map reach, start over |
929 |
02 Dec 09 |
jari |
106 |
next_color_=color_map_.begin(); |
919 |
30 Nov 09 |
jari |
107 |
} |
963 |
09 Dec 09 |
peter |
108 |
return *(i->second); |
916 |
30 Nov 09 |
jari |
109 |
} |
916 |
30 Nov 09 |
jari |
110 |
|
916 |
30 Nov 09 |
jari |
111 |
|
963 |
09 Dec 09 |
peter |
112 |
void Colors::get_color(const std::string& label, unsigned char& r, |
963 |
09 Dec 09 |
peter |
113 |
unsigned char& g, unsigned char& b) |
963 |
09 Dec 09 |
peter |
114 |
{ |
963 |
09 Dec 09 |
peter |
115 |
const color& col(get_color(label)); |
963 |
09 Dec 09 |
peter |
116 |
r = col.r; |
963 |
09 Dec 09 |
peter |
117 |
g = col.g; |
963 |
09 Dec 09 |
peter |
118 |
b = col.b; |
963 |
09 Dec 09 |
peter |
119 |
} |
963 |
09 Dec 09 |
peter |
120 |
|
963 |
09 Dec 09 |
peter |
121 |
|
929 |
02 Dec 09 |
jari |
122 |
Colors& Colors::instance(void) |
916 |
30 Nov 09 |
jari |
123 |
{ |
916 |
30 Nov 09 |
jari |
124 |
if (!instance_) |
929 |
02 Dec 09 |
jari |
125 |
instance_ = new Colors; |
916 |
30 Nov 09 |
jari |
126 |
return *instance_; |
916 |
30 Nov 09 |
jari |
127 |
} |
916 |
30 Nov 09 |
jari |
128 |
|
928 |
02 Dec 09 |
jari |
129 |
|
941 |
03 Dec 09 |
peter |
130 |
int to_decimal(int hex) |
928 |
02 Dec 09 |
jari |
131 |
{ |
928 |
02 Dec 09 |
jari |
132 |
hex=toupper(hex); |
928 |
02 Dec 09 |
jari |
133 |
if (hex>47 && hex<58) // 0--9 |
928 |
02 Dec 09 |
jari |
134 |
return hex-48; |
928 |
02 Dec 09 |
jari |
135 |
if (hex>64 && hex<71) // A--F |
928 |
02 Dec 09 |
jari |
136 |
return hex-55; |
942 |
03 Dec 09 |
peter |
137 |
std::ostringstream ss; |
942 |
03 Dec 09 |
peter |
138 |
ss << static_cast<char>(hex) << " is not hexadecimal"; |
942 |
03 Dec 09 |
peter |
139 |
throw std::runtime_error(ss.str()); |
928 |
02 Dec 09 |
jari |
140 |
return -1; |
928 |
02 Dec 09 |
jari |
141 |
} |
928 |
02 Dec 09 |
jari |
142 |
|
941 |
03 Dec 09 |
peter |
143 |
void str2rgb(const std::string& str, unsigned char& r, unsigned char& g, |
941 |
03 Dec 09 |
peter |
144 |
unsigned char& b) |
941 |
03 Dec 09 |
peter |
145 |
{ |
941 |
03 Dec 09 |
peter |
146 |
if (str.size()==6) { |
941 |
03 Dec 09 |
peter |
147 |
r = 16*to_decimal(str[0]) + to_decimal(str[1]); |
941 |
03 Dec 09 |
peter |
148 |
g = 16*to_decimal(str[2]) + to_decimal(str[3]); |
941 |
03 Dec 09 |
peter |
149 |
b = 16*to_decimal(str[4]) + to_decimal(str[5]); |
941 |
03 Dec 09 |
peter |
150 |
} |
941 |
03 Dec 09 |
peter |
151 |
else if (str.size()==3) { |
941 |
03 Dec 09 |
peter |
152 |
r = 17*to_decimal(str[0]); |
941 |
03 Dec 09 |
peter |
153 |
g = 17*to_decimal(str[1]); |
941 |
03 Dec 09 |
peter |
154 |
b = 17*to_decimal(str[2]); |
941 |
03 Dec 09 |
peter |
155 |
} |
1513 |
23 Sep 12 |
peter |
156 |
else |
941 |
03 Dec 09 |
peter |
157 |
throw std::runtime_error("invalid color format: " + str); |
941 |
03 Dec 09 |
peter |
158 |
} |
941 |
03 Dec 09 |
peter |
159 |
|
941 |
03 Dec 09 |
peter |
160 |
|
916 |
30 Nov 09 |
jari |
161 |
}} // end of namespace svndigest and namespace theplu |