Skip to content

Commit 0648ce0

Browse files
committed
use constepxr for NamedColors[]
1 parent 77a5430 commit 0648ce0

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

csscolorparser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
using namespace CSSColorParser;
3838

3939
// http://www.w3.org/TR/css3-color/
40-
struct NamedColor { const char *const name; const Color color; };
41-
const NamedColor namedColors[] = {
40+
struct NamedColor { const char *const name; const ConstColor color; };
41+
42+
constexpr NamedColor namedColors[] = {
4243
{ "aliceblue", { 240, 248, 255, 1 } },
4344
{ "antiquewhite", { 250, 235, 215, 1 } },
4445
{ "aqua", { 0, 255, 255, 1 } },

csscolorparser.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,18 @@
2828
#include <string>
2929

3030
namespace CSSColorParser {
31+
struct ConstColor {
32+
constexpr inline ConstColor(unsigned char r, unsigned char g, unsigned char b, float a)
33+
: r(r), g(g), b(b), a(a) {}
34+
const unsigned char r = 0, g = 0, b = 0;
35+
const float a = 1.0f;
36+
};
3137

3238
struct Color {
3339
inline Color() {}
40+
inline Color(const ConstColor& c)
41+
: r(c.r), g(c.g), b(c.b), a(c.a) {}
42+
3443
inline Color(unsigned char r, unsigned char g, unsigned char b, float a)
3544
: r(r), g(g), b(b), a(a) {}
3645
unsigned char r = 0, g = 0, b = 0;

0 commit comments

Comments
 (0)