OdbDesignLib
OdbDesign ODB++ Parsing Library
RgbColor.cpp
1 #include "RgbColor.h"
2 #include <cstdint>
3 
4 namespace Odb::Lib::FileModel::Design
5 {
6  RgbColor::RgbColor()
7  : red(0)
8  , green(0)
9  , blue(0)
10  , noPreference(false)
11  {
12  }
13 
14  RgbColor::RgbColor(const std::string& str)
15  : RgbColor()
16  {
17  from_string(str);
18  }
19 
20  bool RgbColor::from_string(const std::string& str)
21  {
22  if (str.length() == 6)
23  {
24  auto strRed = str.substr(0, 2);
25  auto strGreen = str.substr(2, 2);
26  auto strBlue = str.substr(4, 2);
27 
28  // valid values are [0,100]
29  red = static_cast<uint8_t>(std::stoi(strRed));
30  blue = static_cast<uint8_t>(std::stoi(strBlue));
31  green = static_cast<uint8_t>(std::stoi(strGreen));
32 
33  noPreference = false;
34  return true;
35  }
36  else if (str.length() == 1)
37  {
38  if (str[0] == '0')
39  {
40  noPreference = true;
41  return true;
42  }
43  }
44 
45  return false;
46  }
47 
48  std::string RgbColor::to_string() const
49  {
50  if (noPreference)
51  {
52  return "0";
53  }
54  else
55  {
56  return std::to_string(red) + std::to_string(green) + std::to_string(blue);
57  }
58  }
59 }