source: trunk/lib/Colors.cc @ 1267

Last change on this file since 1267 was 1267, checked in by Peter Johansson, 11 years ago

updating copyright statements

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