Mana
Loading...
Searching...
No Matches
palette.cpp
Go to the documentation of this file.
1/*
2 * Configurable text colors
3 * Copyright (C) 2008 Douglas Boffey <dougaboffey@netscape.net>
4 * Copyright (C) 2009 The Mana World Development Team
5 * Copyright (C) 2009-2012 The Mana Developers
6 *
7 * This file is part of The Mana Client.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23#include "palette.h"
24
25#include <cmath>
26
27static constexpr double PI = 3.14159265;
30
31const gcn::Color Palette::RAINBOW_COLORS[7] = {
32 gcn::Color(255, 0, 0),
33 gcn::Color(255, 153, 0),
34 gcn::Color(255, 255, 0),
35 gcn::Color(0, 153, 0),
36 gcn::Color(0, 204, 204),
37 gcn::Color(51, 0, 153),
38 gcn::Color(153, 0, 153)
39};
42
44 mColors(size)
45{
46 mInstances.insert(this);
47}
48
50 : mColors(std::move(pal.mColors))
51 , mGradVector(std::move(pal.mGradVector))
52{
53 mInstances.insert(this);
54}
55
57{
58 mInstances.erase(this);
59}
60
62{
63 if (this != &pal)
64 {
65 mColors = std::move(pal.mColors);
66 mGradVector = std::move(pal.mGradVector);
67 }
68 return *this;
69}
70
71void Palette::setColor(int type,
72 const gcn::Color &color,
73 const std::optional<gcn::Color> &outlineColor,
74 GradientType grad,
75 int delay)
76{
77 auto &elem = mColors[type];
78 elem.set(type, color, grad, delay);
79 elem.outlineColor = outlineColor;
80}
81
83{
84 const int advance = mRainbowTimer.elapsed() / 5;
85 if (advance <= 0)
86 return;
87
88 mRainbowTimer.extend(advance * 5);
89
90 for (auto palette : mInstances)
91 palette->advanceGradient(advance);
92}
93
94void Palette::advanceGradient(int advance)
95{
96 for (auto elem : mGradVector)
97 {
98 int delay = elem->delay;
99
100 if (elem->grad == PULSE)
101 delay = delay / 20;
102
103 const int numOfColors = (elem->grad == SPECTRUM ? 6 :
104 elem->grad == PULSE ? 127 :
106
107 elem->gradientIndex = (elem->gradientIndex + advance) %
108 (delay * numOfColors);
109
110 const int pos = elem->gradientIndex % delay;
111 const int colIndex = elem->gradientIndex / delay;
112
113 switch (elem->grad) {
114 case STATIC:
115 break;
116 case PULSE: {
117 const int colVal = (int) (255.0 * sin(PI * colIndex / numOfColors));
118 const gcn::Color &col = elem->testColor;
119
120 elem->color.r = ((colVal * col.r) / 255) % (col.r + 1);
121 elem->color.g = ((colVal * col.g) / 255) % (col.g + 1);
122 elem->color.b = ((colVal * col.b) / 255) % (col.b + 1);
123 break;
124 }
125 case SPECTRUM: {
126 int colVal;
127
128 if (colIndex % 2)
129 { // falling curve
130 colVal = (int)(255.0 * (cos(PI * pos / delay) + 1) / 2);
131 }
132 else
133 { // ascending curve
134 colVal = (int)(255.0 * (cos(PI * (delay - pos) / delay) +
135 1) / 2);
136 }
137
138 elem->color.r =
139 (colIndex == 0 || colIndex == 5) ? 255 :
140 (colIndex == 1 || colIndex == 4) ? colVal : 0;
141 elem->color.g =
142 (colIndex == 1 || colIndex == 2) ? 255 :
143 (colIndex == 0 || colIndex == 3) ? colVal : 0;
144 elem->color.b =
145 (colIndex == 3 || colIndex == 4) ? 255 :
146 (colIndex == 2 || colIndex == 5) ? colVal : 0;
147 break;
148 }
149 case RAINBOW: {
150 const gcn::Color &startCol = RAINBOW_COLORS[colIndex];
151 const gcn::Color &destCol =
152 RAINBOW_COLORS[(colIndex + 1) % numOfColors];
153
154 const double startColVal = (cos(PI * pos / delay) + 1) / 2;
155 const double destColVal = 1 - startColVal;
156
157 elem->color.r =(int)(startColVal * startCol.r +
158 destColVal * destCol.r);
159
160 elem->color.g =(int)(startColVal * startCol.g +
161 destColVal * destCol.g);
162
163 elem->color.b =(int)(startColVal * startCol.b +
164 destColVal * destCol.b);
165 break;
166 }
167 }
168 }
169}
Class controlling the game's color palette.
Definition palette.h:42
static const gcn::Color RAINBOW_COLORS[]
Colors used for the rainbow gradient.
Definition palette.h:31
void advanceGradient(int advance)
Definition palette.cpp:94
~Palette()
Definition palette.cpp:56
Palette(int size)
Definition palette.cpp:43
static const int RAINBOW_COLOR_COUNT
Number of Elemets of RAINBOW_COLORS.
Definition palette.h:116
std::set< Palette * > Palettes
Definition palette.h:121
static Palettes mInstances
Definition palette.h:122
static void advanceGradients()
Updates all colors, that are non-static.
Definition palette.cpp:82
void setColor(int type, const gcn::Color &color, const std::optional< gcn::Color > &outlineColor, GradientType grad, int delay)
Definition palette.cpp:71
Palette & operator=(const Palette &)=delete
std::vector< ColorElem * > mGradVector
Definition palette.h:158
std::vector< ColorElem > mColors
Vector containing the colors.
Definition palette.h:157
GradientType
Colors can be static or can alter over time.
Definition palette.h:53
@ SPECTRUM
Definition palette.h:56
@ STATIC
Definition palette.h:54
@ PULSE
Definition palette.h:55
@ RAINBOW
Definition palette.h:57
static Timer mRainbowTimer
Timer used when updating gradient-type colors.
Definition palette.h:119
Simple timer that can be used to check if a certain amount of time has passed.
Definition time.h:62
int32_t elapsed() const
Returns the number of milliseconds elapsed since the set time, or a negative value if the timer hasn'...
Definition time.cpp:65
void extend(uint32_t ms)
Extend the timer by the given number of milliseconds.
Definition time.h:94
#define PI