1 /* BColors.cpp
2 * Copyright (C) 2018 Sven Jähnichen
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18 #include "BColors.hpp"
19
20 #ifndef LIMIT
21 #define LIMIT(val, min, max) (val < min ? min : (val < max ? val : max))
22 #endif
23
24 namespace BColors
25 {
26
27 /*****************************************************************************
28 * Class BColors::Color
29 *****************************************************************************/
30
Color()31 Color::Color () : Color (0.0, 0.0, 0.0, 0.0) {};
32
Color(const double red,const double green,const double blue,const double alpha)33 Color::Color (const double red, const double green, const double blue, const double alpha) :
34 red_ (LIMIT (red, 0.0, 1.0)), green_ (LIMIT (green, 0.0, 1.0)), blue_ (LIMIT (blue, 0.0, 1.0)), alpha_ (LIMIT (alpha, 0.0, 1.0)) {}
35
Color(const uint32_t red32,const uint32_t green32,const uint32_t blue32,const uint32_t alpha32)36 Color::Color (const uint32_t red32, const uint32_t green32, const uint32_t blue32, const uint32_t alpha32) :
37 red_ (red32 / 0xFFFF), green_ (green32 / 0xFFFF), blue_ (blue32 / 0xFFFF), alpha_ (alpha32 / 0xFFFF) {}
38
operator ==(const Color & that) const39 bool Color::operator== (const Color& that) const {return (0 == compare (that));}
40
operator !=(const Color & that) const41 bool Color::operator!= (const Color& that) const {return !(0 == compare (that));}
42
setRGBA(const double red,const double green,const double blue,const double alpha)43 void Color::setRGBA (const double red, const double green, const double blue, const double alpha)
44 {
45
46 red_ = LIMIT (red, 0.0, 1.0);
47 green_ = LIMIT (green, 0.0, 1.0);
48 blue_ = LIMIT (blue, 0.0, 1.0);
49 alpha_ = LIMIT (alpha, 0.0, 1.0);
50 }
51
setRGB(const double red,const double green,const double blue)52 void Color::setRGB (const double red, const double green, const double blue)
53 {
54 red_ = LIMIT (red, 0.0, 1.0);
55 green_ = LIMIT (green, 0.0, 1.0);
56 blue_ = LIMIT (blue, 0.0, 1.0);
57 }
58
setAlpha(const double alpha)59 void Color::setAlpha (const double alpha)
60 {
61 alpha_ = LIMIT (alpha, 0.0, 1.0);
62 }
63
getRed() const64 double Color::getRed() const {return red_;}
getGreen() const65 double Color::getGreen() const {return green_;}
getBlue() const66 double Color::getBlue() const {return blue_;}
getAlpha() const67 double Color::getAlpha() const {return alpha_;}
68
compare(const Color & that) const69 int Color::compare (const Color& that) const
70 {
71 return ((red_ == that.red_) && (green_ == that.green_) && (blue_ == that.blue_) && (alpha_ == that.alpha_) ? 0 : 1);
72 }
73
applyBrightness(const double brightness)74 void Color::applyBrightness (const double brightness)
75 {
76 double b = LIMIT (brightness, -1.0, 1.0);
77 if (b < 0)
78 {
79 red_ = red_ * (b + 1.0);
80 green_ = green_ * (b + 1.0);
81 blue_ = blue_ * (b + 1.0);
82 }
83 else if (b > 0)
84 {
85 red_ = red_ + (1.0 - red_) * b;
86 green_ = green_ + (1.0 - green_) * b;
87 blue_ = blue_ + (1.0 - blue_) * b;
88 }
89 }
90
91 /*
92 * End of class BColors::Color
93 *****************************************************************************/
94
95 /*****************************************************************************
96 * Class BColors::ColorSet
97 *****************************************************************************/
98
ColorSet()99 ColorSet::ColorSet () : ColorSet ({grey, lightgrey, darkgrey}) {};
ColorSet(const std::vector<Color> vectorOfColors)100 ColorSet::ColorSet (const std::vector<Color> vectorOfColors) : colors (vectorOfColors) {};
101
operator ==(const ColorSet & that) const102 bool ColorSet::operator== (const ColorSet& that) const {return (colors == that.colors);}
103
operator !=(const ColorSet & that) const104 bool ColorSet::operator!= (const ColorSet& that) const {return (colors != that.colors);}
105
compare(const ColorSet & that) const106 int ColorSet::compare (const ColorSet& that) const {return (colors == that.colors ? 0 : 1);}
107
addColor(const State state,const Color & color)108 void ColorSet::addColor (const State state, const Color& color)
109 {
110 // Filling undefined vector elements with Color invisible
111 int size = colors.size ();
112 for (int i = size; i <= (int) state; ++i) colors.push_back (invisible);
113
114 colors[state] = color;
115 }
116
removeColor(const State state)117 void ColorSet::removeColor (const State state)
118 {
119 if (state < colors.size ())
120 {
121 colors[state] = invisible;
122 }
123
124 // TODO shrink vector colors if last element is removed
125 }
126
getColor(const State state)127 Color* ColorSet::getColor (const State state)
128 {
129 if (state < colors.size ())
130 {
131 return &colors[state];
132 }
133 else
134 {
135 return &noColor;
136 }
137 }
138
139 /*
140 * End of class BColors::ColorSet
141 *****************************************************************************/
142
143 }
144