Blamite Game Engine - Blam (Core)
colors.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <d2d1.h>
4 #include <string>
5 #include <vector>
6 
8 
9 #ifndef BLAM
10 #define BLAM
11 #endif
12 
18 class BlamColor
19 {
20 public:
21  short r = 255;
22  short g = 255;
23  short b = 255;
24  short a = 255;
25 
27  {
28  r = 255;
29  g = 255;
30  b = 255;
31  a = 255;
32  }
33 
34  BlamColor(short _r, short _g, short _b)
35  {
36  r = _r;
37  g = _g;
38  b = _b;
39  a = 255;
40  }
41 
42  BlamColor(short _r, short _g, short _b, short _a)
43  {
44  r = _r;
45  g = _g;
46  b = _b;
47  a = _a;
48  }
49 
55  std::string ToString()
56  {
57  return std::string(std::to_string(r) + "," + std::to_string(g) + "," + std::to_string(b) + "," + std::to_string(a));
58  }
59 
76  std::string ToString(std::string format)
77  {
78  std::string new_string = format;
79 
80  new_string = Blam::Utils::String::Replace(new_string, "{r}", std::to_string(r));
81  new_string = Blam::Utils::String::Replace(new_string, "{g}", std::to_string(g));
82  new_string = Blam::Utils::String::Replace(new_string, "{b}", std::to_string(b));
83  new_string = Blam::Utils::String::Replace(new_string, "{a}", std::to_string(a));
84 
85  double r_float = (double)r / 255.0f;
86  double g_float = (double)g / 255.0f;
87  double b_float = (double)b / 255.0f;
88  double a_float = (double)a / 255.0f;
89 
90  new_string = Blam::Utils::String::Replace(new_string, "{r>f}", std::to_string(r_float));
91  new_string = Blam::Utils::String::Replace(new_string, "{g>f}", std::to_string(g_float));
92  new_string = Blam::Utils::String::Replace(new_string, "{b>f}", std::to_string(b_float));
93  new_string = Blam::Utils::String::Replace(new_string, "{a>f}", std::to_string(a_float));
94 
95  return new_string;
96  }
97 
103  std::string ToStringForCSS()
104  {
105  return ToString("rgba({r},{g},{b},{a>f})");
106  }
107 
117  bool FromString(std::string string)
118  {
119  std::vector<std::string> color_values = Blam::Utils::String::Split(string, ",");
120 
121  if (color_values.size() == 3)
122  {
123  r = atoi(color_values.at(0).c_str());
124  g = atoi(color_values.at(1).c_str());
125  b = atoi(color_values.at(2).c_str());
126  a = 255;
127 
128  return true;
129  }
130  else if (color_values.size() == 4)
131  {
132  r = atoi(color_values.at(0).c_str());
133  g = atoi(color_values.at(1).c_str());
134  b = atoi(color_values.at(2).c_str());
135  a = atoi(color_values.at(3).c_str());
136 
137  return true;
138  }
139  else
140  {
141  return false;
142  }
143  }
144 };
145 
149 namespace Blam::Colors
150 {
159  BLAM BlamColor TranslateTextColorCode(char character);
160 
168  BLAM D2D1_COLOR_F D2DColorFromBlamColor(BlamColor color);
169 }
Blam::Utils::String::Replace
BLAM std::string Replace(std::string orig, std::string to_replace, std::string replace_with)
Replaces part of a string with another string.
Definition: string.cpp:67
BlamColor::BlamColor
BlamColor()
Definition: colors.h:26
Blam::Utils::String::Split
BLAM std::vector< std::string > Split(std::string string, std::string splitter)
Splits a string around any instance of a substring.
Definition: string.cpp:87
Blam::Colors::TranslateTextColorCode
BLAM BlamColor TranslateTextColorCode(char character)
Converts a character to its equivalent color code for use in multi-colored text.
Definition: colors.cpp:3
BlamColor::g
short g
The Green value of the color.
Definition: colors.h:22
string.h
Blam::Colors
Namespace for things relating to global engine color data.
Definition: colors.h:149
Blam::Colors::D2DColorFromBlamColor
BLAM D2D1_COLOR_F D2DColorFromBlamColor(BlamColor color)
Converts a BlamColor object into its equivalent D2D1_COLOR_F object.
Definition: colors.cpp:44
BlamColor::a
short a
The Alpha value of the color.
Definition: colors.h:24
BlamColor::r
short r
The Red value of the color.
Definition: colors.h:21
BlamColor::BlamColor
BlamColor(short _r, short _g, short _b)
Definition: colors.h:34
BlamColor::b
short b
The Blue value of the color.
Definition: colors.h:23
BlamColor::ToString
std::string ToString()
Converts the color value to a string.
Definition: colors.h:55
BlamColor::ToStringForCSS
std::string ToStringForCSS()
Converts the color to a string that can be used with CSS.
Definition: colors.h:103
BlamColor::FromString
bool FromString(std::string string)
Attempts to set color information from a string.
Definition: colors.h:117
BlamColor::ToString
std::string ToString(std::string format)
Converts the color value to a string in the specified format.
Definition: colors.h:76
BlamColor::BlamColor
BlamColor(short _r, short _g, short _b, short _a)
Definition: colors.h:42
BLAM
#define BLAM
Definition: colors.h:10
BlamColor
Structure representing a color.
Definition: colors.h:18