source: trunk/lib/Colors.cc @ 943

Last change on this file since 943 was 943, checked in by Jari Häkkinen, 12 years ago

Removed premature initialization of variable

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1// $Id: Colors.cc 943 2009-12-03 07:26:24Z jari $
2
3/*
4  Copyright (C) 2009 Jari Häkkinen
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].r=  0; color_map_[ 0].g=146; color_map_[ 0].b= 72;
40    color_map_[ 1].label="blue";
41    color_map_[ 1].r= 27; color_map_[ 1].g=117; color_map_[ 1].b=186;
42    color_map_[ 2].label="red";
43    color_map_[ 2].r=189; color_map_[ 2].g= 32; color_map_[ 2].b= 46;
44    color_map_[ 3].label="grey";
45    color_map_[ 3].r=131; color_map_[ 3].g=131; color_map_[ 3].b=131;
46    color_map_[ 4].label="orange";
47    color_map_[ 4].r=245; color_map_[ 4].g=145; color_map_[ 4].b= 32;
48    color_map_[ 5].label="brown";
49    color_map_[ 5].r=117; color_map_[ 5].g= 77; color_map_[ 5].b= 41;
50    color_map_[ 6].label="cyan";
51    color_map_[ 6].r= 40; color_map_[ 6].g=170; color_map_[ 6].b=225;
52    color_map_[ 7].label="lime";
53    color_map_[ 7].r=213; color_map_[ 7].g=221; color_map_[ 7].b= 38;
54    color_map_[ 8].label="purple";
55    color_map_[ 8].r=143; color_map_[ 8].g= 40; color_map_[ 8].b=140;
56    color_map_[ 9].label="lgrey";
57    color_map_[ 9].r=220; color_map_[ 9].g=220; color_map_[ 9].b=220;
58    color_map_[10].label="lbrown";
59    color_map_[10].r=194; color_map_[10].g=151; color_map_[10].b=106;
60    color_map_[11].label="pink";
61    color_map_[11].r=230; color_map_[11].g=125; color_map_[11].b=230;
62    color_map_[12].label="black";
63    color_map_[12].r=  0; color_map_[12].g=  0; color_map_[12].b=  0;
64
65    typedef std::map<std::string, std::string> ACmap;
66    const ACmap& authcols=Configuration::instance().author_colors();
67    // reserve sufficient size in vector here to avoid reallocation
68    // and iterators being invalidated
69    color_map_.reserve(color_map_.size()+authcols.size());
70    next_color_=color_map_.begin();
71    for (ACmap::const_iterator i=authcols.begin(); i!=authcols.end(); i++) {
72      color c;
73      c.label=i->first;
74      str2rgb(i->second, c.r, c.g, c.b);
75      next_color_=color_map_.insert(next_color_,c);
76      author_map_.insert(std::make_pair(c.label, next_color_++));
77    }
78  }
79
80
81  void Colors::get_color(const std::string& label, unsigned char& r,
82                           unsigned char& g, unsigned char& b)
83  {
84    std::map<std::string,std::vector<color>::iterator>::iterator i;
85    i = author_map_.lower_bound(label);
86    if (i==author_map_.end() || i->first != label) {
87      // no color defined for label, set color for label
88      i = author_map_.insert(i, std::make_pair(label, next_color_++));
89      if (next_color_==color_map_.end())
90        // end of color map reach, start over
91        next_color_=color_map_.begin();
92    }
93    r = i->second->r;
94    g = i->second->g;
95    b = i->second->b;
96  }
97
98
99  Colors& Colors::instance(void)
100  {
101    if (!instance_)
102      instance_ = new Colors;
103    return *instance_;
104  }
105
106
107  int to_decimal(int hex)
108  {
109    hex=toupper(hex);
110    if (hex>47 && hex<58) // 0--9
111      return hex-48;
112    if (hex>64 && hex<71) // A--F
113      return hex-55;
114    std::ostringstream ss;
115    ss << static_cast<char>(hex) << " is not hexadecimal";
116    throw std::runtime_error(ss.str());
117    return -1;
118  }
119
120  void str2rgb(const std::string& str, unsigned char& r, unsigned char& g,
121               unsigned char& b)
122  {
123    if (str.size()==6) {
124      r = 16*to_decimal(str[0]) + to_decimal(str[1]);
125      g = 16*to_decimal(str[2]) + to_decimal(str[3]);
126      b = 16*to_decimal(str[4]) + to_decimal(str[5]);
127    }
128    else if (str.size()==3) {
129      r = 17*to_decimal(str[0]);
130      g = 17*to_decimal(str[1]);
131      b = 17*to_decimal(str[2]);
132    }
133    else 
134      throw std::runtime_error("invalid color format: " + str);
135  }
136
137
138}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.