1 /*
2 palette.cpp MindForger thinking notebook
3
4 Copyright (C) 2016-2020 Martin Dvorak <martin.dvorak@mindforger.com>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 2
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "palette.h"
20
21 namespace m8r {
22
23 using namespace std;
24
25 Palette::Palette() = default;
26
27 Palette::~Palette() = default;
28
addBuiltInColors()29 void Palette::addBuiltInColors()
30 {
31 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::RED().asLong(), Color::RED()));
32 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::GREEN().asLong(), Color::GREEN()));
33 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::BLUE().asLong(), Color::BLUE()));
34 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::WHITE().asLong(), Color::WHITE()));
35 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::BLACK().asLong(), Color::BLACK()));
36 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::LIGHT_GRAY().asLong(), Color::LIGHT_GRAY()));
37 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::DARK_GRAY().asLong(), Color::DARK_GRAY()));
38
39 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_RED().asLong(), Color::MF_RED()));
40 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_GREEN().asLong(), Color::MF_GREEN()));
41 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_BLUE().asLong(), Color::MF_BLUE()));
42 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_YELLOW().asLong(), Color::MF_YELLOW()));
43 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_PURPLE().asLong(), Color::MF_PURPLE()));
44 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_BLACK().asLong(), Color::MF_BLACK()));
45 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_GRAY().asLong(), Color::MF_GRAY()));
46 colorsMap.insert(std::pair<unsigned long,const Color&>(Color::MF_TURQUOISE().asLong(), Color::MF_TURQUOISE()));
47 }
48
addBuildInColor(const Color * c)49 void Palette::addBuildInColor(const Color* c)
50 {
51 colorsMap.insert(std::pair<unsigned long,const Color&>(c->asLong(), *c));
52 colors.push_back(c);
53 }
54
findOrCreate(unsigned char r,unsigned char g,unsigned char b)55 const Color& Palette::findOrCreate(unsigned char r, unsigned char g, unsigned char b)
56 {
57 unsigned long k = (r<<16) + (g<<8) + b;
58 auto result = colorsMap.find(k);
59 if(result!=colorsMap.end()) {
60 return result->second;
61 }
62
63 Color* newColor = new Color{r,g,b};
64 colors.push_back(newColor);
65 colorsMap.insert(std::pair<unsigned long,const Color&>(newColor->asLong(), *newColor));
66 customColors.push_back(std::unique_ptr<Color>(newColor));
67 return *newColor;
68 }
69
colorForName(const string & name) const70 const Color& Palette::colorForName(const string& name) const
71 {
72 unsigned int result=5381;
73 for(unsigned int i=0; i<name.size(); i++) {
74 result=result*33+name[i];
75 }
76 result = result^(result>>16);
77
78 return *colors[result%colors.size()];
79 }
80
81 } // m8r namespace
82