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