Line data Source code
1 : #include "utils/color_stringify.hpp"
2 :
3 : #include <spdlog/spdlog.h>
4 :
5 : #include <iostream>
6 :
7 : using namespace std;
8 : using namespace utils::stringify;
9 :
10 0 : string stringify<color::rgb<float>>::toString(const color::rgb<float> &t) {
11 0 : stringstream ss;
12 0 : ss
13 0 : << color::get::red(t) << ","
14 0 : << color::get::green(t) << ","
15 0 : << color::get::blue(t);
16 0 : return ss.str();
17 : }
18 :
19 : const unordered_map<string, color::rgb<float>> string2Color = {
20 : {"white", color::rgb<float>({1.0, 1.0, 1.0})},
21 : {"grey", color::rgb<float>({0.5, 0.5, 0.5})},
22 : {"black", color::rgb<float>({0.0, 0.0, 0.0})},
23 : };
24 :
25 0 : color::rgb<float> stringify<color::rgb<float>>::fromString(const string &s) {
26 0 : color::rgb<float> c;
27 :
28 0 : if(string2Color.find(s) != string2Color.end()) return string2Color.at(s);
29 :
30 0 : size_t l, r;
31 0 : l = 0;
32 0 : r = s.find(',', l);
33 0 : color::set::red(c, stringify<float>::fromString(s.substr(l, r - l)) / 255.0f);
34 0 : l = r + 1;
35 0 : r = s.find(',', l);
36 0 : color::set::green(c, stringify<float>::fromString(s.substr(l, r - l)) / 255.0f);
37 0 : l = r + 1;
38 0 : r = s.find(',', l);
39 0 : color::set::blue(c, stringify<float>::fromString(s.substr(l, r - l)) / 255.0f);
40 :
41 0 : return c;
42 : }
43 :
44 0 : string stringify<color::rgba<float>>::toString(const color::rgba<float> &t) {
45 0 : stringstream ss;
46 0 : ss
47 0 : << color::get::red(t) << ","
48 0 : << color::get::green(t) << ","
49 0 : << color::get::blue(t) << ","
50 0 : << color::get::alpha(t);
51 0 : return ss.str();
52 : }
53 :
54 0 : color::rgba<float> stringify<color::rgba<float>>::fromString(const string &s) {
55 0 : color::rgba<float> c;
56 :
57 0 : size_t l, r;
58 0 : l = 0;
59 0 : r = s.find(',', l) - 1;
60 0 : color::set::red(c, stringify<float>::fromString(s.substr(l, r)));
61 0 : l = r;
62 0 : r = s.find(',', l) - 1;
63 0 : color::set::green(c, stringify<float>::fromString(s.substr(l, r)));
64 0 : l = r;
65 0 : r = s.find(',', l) - 1;
66 0 : color::set::blue(c, stringify<float>::fromString(s.substr(l, r)));
67 0 : l = r;
68 0 : r = s.find(',', l) - 1;
69 0 : color::set::alpha(c, stringify<float>::fromString(s.substr(l, r)));
70 :
71 0 : return c;
72 : }
|