source: branches/0.8-stable/lib/Colors.cc

Last change on this file was 978, checked in by Peter Johansson, 12 years ago

refs #429. Convert copyright statements to UTF-8

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1// $Id: Colors.cc 978 2009-12-12 20:09:41Z 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
29namespace theplu {
30namespace svndigest {
31
32  Colors* Colors::instance_=NULL;
33
34
35  Colors::Colors(void)
36    : color_map_(13)
37  {
38    color_map_[ 0].label="green";
39    color_map_[ 0].str="009248";
40    color_map_[ 1].label="blue";
41    color_map_[ 1].str="1b75ba";
42    color_map_[ 2].label="red";
43    color_map_[ 2].str="bd202e";
44    color_map_[ 3].label="grey";
45    color_map_[ 3].str="838383";
46    color_map_[ 4].label="orange";
47    color_map_[ 4].str="f59120";
48    color_map_[ 5].label="brown";
49    color_map_[ 5].str="754d29";
50    color_map_[ 6].label="cyan";
51    color_map_[ 6].str="28aae1";
52    color_map_[ 7].label="lime";
53    color_map_[ 7].str="d5dd26";
54    color_map_[ 8].label="purple";
55    color_map_[ 8].str="8f288c";
56    color_map_[ 9].label="lgrey";
57    color_map_[ 9].str="dcdcdc";
58    color_map_[10].label="lbrown";
59    color_map_[10].str="c2976a";
60    color_map_[11].label="pink";
61    color_map_[11].str="e67de6";
62    color_map_[12].label="black";
63    color_map_[12].str="000000";
64    // calculate corresponding rgb values
65    for (std::vector<color>::iterator col = color_map_.begin(); 
66         col!=color_map_.end(); ++col) {
67      str2rgb(col->str, col->r, col->g, col->b);
68    }
69
70    typedef std::map<std::string, std::string> ACmap;
71    const ACmap& authcols=Configuration::instance().author_colors();
72    // reserve sufficient size in vector here to avoid reallocation
73    // and iterators being invalidated
74    color_map_.reserve(color_map_.size()+authcols.size());
75    next_color_=color_map_.begin();
76    for (ACmap::const_iterator i=authcols.begin(); i!=authcols.end(); i++) {
77      color c;
78      c.label=i->first;
79      str2rgb(i->second, c.r, c.g, c.b);
80      c.str = i->second;
81      next_color_=color_map_.insert(next_color_,c);
82      author_map_.insert(std::make_pair(c.label, next_color_++));
83    }
84  }
85
86
87  const std::string& Colors::color_str(const std::string& label)
88  {
89    return get_color(label).str;
90  }
91
92
93  const Colors::color& Colors::get_color(const std::string& label)
94  {
95    std::map<std::string,std::vector<color>::iterator>::iterator i;
96    i = author_map_.lower_bound(label);
97    if (i==author_map_.end() || i->first != label) {
98      // no color defined for label, set color for label
99      i = author_map_.insert(i, std::make_pair(label, next_color_++));
100      if (next_color_==color_map_.end())
101        // end of color map reach, start over
102        next_color_=color_map_.begin();
103    }
104    return *(i->second);
105  }
106
107
108  void Colors::get_color(const std::string& label, unsigned char& r,
109                           unsigned char& g, unsigned char& b)
110  {
111    const color& col(get_color(label));
112    r = col.r;
113    g = col.g;
114    b = col.b;
115  }
116
117
118  Colors& Colors::instance(void)
119  {
120    if (!instance_)
121      instance_ = new Colors;
122    return *instance_;
123  }
124
125
126  int to_decimal(int hex)
127  {
128    hex=toupper(hex);
129    if (hex>47 && hex<58) // 0--9
130      return hex-48;
131    if (hex>64 && hex<71) // A--F
132      return hex-55;
133    std::ostringstream ss;
134    ss << static_cast<char>(hex) << " is not hexadecimal";
135    throw std::runtime_error(ss.str());
136    return -1;
137  }
138
139  void str2rgb(const std::string& str, unsigned char& r, unsigned char& g,
140               unsigned char& b)
141  {
142    if (str.size()==6) {
143      r = 16*to_decimal(str[0]) + to_decimal(str[1]);
144      g = 16*to_decimal(str[2]) + to_decimal(str[3]);
145      b = 16*to_decimal(str[4]) + to_decimal(str[5]);
146    }
147    else if (str.size()==3) {
148      r = 17*to_decimal(str[0]);
149      g = 17*to_decimal(str[1]);
150      b = 17*to_decimal(str[2]);
151    }
152    else 
153      throw std::runtime_error("invalid color format: " + str);
154  }
155
156
157}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.