bpp-core3  3.0.0
ColorTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "ColorTools.h"
6 
7 using namespace std;
8 using namespace bpp;
9 
10 const RGBColor ColorTools::RED = RGBColor(255, 0, 0);
11 const RGBColor ColorTools::GREEN = RGBColor(0, 255, 0);
12 const RGBColor ColorTools::BLUE = RGBColor(0, 0, 255);
13 const RGBColor ColorTools::BLACK = RGBColor(0, 0, 0);
14 const RGBColor ColorTools::WHITE = RGBColor(255, 255, 255);
15 const RGBColor ColorTools::YELLOW = RGBColor(255, 255, 0);
16 const RGBColor ColorTools::CYAN = RGBColor(0, 255, 255);
17 const RGBColor ColorTools::MAGENTA = RGBColor(255, 0, 255);
18 const RGBColor ColorTools::ORANGE = RGBColor(255, 127, 0);
19 
20 /******************************************************************************/
21 
22 std::vector<RGBColor> ColorTools::gradient(unsigned int n, const RGBColor& low, const RGBColor& high)
23 {
24  vector<RGBColor> colors(n);
25  for (unsigned int i = 0; i < n - 1; ++i)
26  {
27  colors[i][0] = low[0] + static_cast<unsigned int>(
28  floor(
29  static_cast<double>(i) * static_cast<double>(static_cast<int>(high[0]) - static_cast<int>(low[0]))
30  / static_cast<double>(n - 1)
31  )
32  );
33  colors[i][1] = low[1] + static_cast<unsigned int>(
34  floor(
35  static_cast<double>(i) * static_cast<double>(static_cast<int>(high[1]) - static_cast<int>(low[1]))
36  / static_cast<double>(n - 1)
37  )
38  );
39  colors[i][2] = low[2] + static_cast<unsigned int>(
40  floor(
41  static_cast<double>(i) * static_cast<double>(static_cast<int>(high[2]) - static_cast<int>(low[2]))
42  / static_cast<double>(n - 1)
43  )
44  );
45  }
46  colors[n - 1] = high;
47  return colors;
48 }
49 
50 /******************************************************************************/
51 
52 std::vector<RGBColor> ColorTools::gradient(unsigned int n, const RGBColor& low, const RGBColor& mid, const RGBColor& high)
53 {
54  unsigned int lower = n / 2;
55  unsigned int upper = n - lower;
56  vector<RGBColor> colors1 = gradient(lower, low, mid);
57  vector<RGBColor> colors2 = gradient(upper + 1, mid, high);
58  for (size_t i = 1; i < colors2.size(); i++)
59  {
60  colors1.push_back(colors2[i]);
61  }
62  return colors1;
63 }
64 
65 /******************************************************************************/
STL namespace.
Describe a color according to its red, green and blue componants.
Definition: RgbColor.h:21