MCPcopy Create free account
hub / github.com/BZFlag-Dev/bzflag / parseHexFormat

Function parseHexFormat

src/common/ParseColor.cpp:53–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

51
52
53static bool parseHexFormat(const char* str, fvec4& color) {
54 int bytes[8];
55 int index;
56 for (index = 0; index < 8; index++) {
57 const char c = str[index];
58 if ((c == 0) || isspace(c)) {
59 break;
60 }
61 const int byte = parseHexChar(c);
62 if (byte < 0) {
63 return false; // not a hex character
64 }
65 bytes[index] = byte;
66 }
67
68 // check for a valid termination
69 if (index == 8) {
70 const char c = str[8];
71 if ((c != 0) && !isspace(c)) {
72 return false;
73 }
74 }
75
76 switch (index) {
77 case 3: { // rgb
78 color.r = (float)bytes[0] / 15.0f;
79 color.g = (float)bytes[1] / 15.0f;
80 color.b = (float)bytes[2] / 15.0f;
81 return true;
82 }
83 case 4: { // rgba
84 color.r = (float)bytes[0] / 15.0f;
85 color.g = (float)bytes[1] / 15.0f;
86 color.b = (float)bytes[2] / 15.0f;
87 color.a = (float)bytes[3] / 15.0f;
88 return true;
89 }
90 case 6: { // rrggbb
91 color.r = (float)((bytes[0] << 4) + bytes[1]) / 255.0f;
92 color.g = (float)((bytes[2] << 4) + bytes[3]) / 255.0f;
93 color.b = (float)((bytes[4] << 4) + bytes[5]) / 255.0f;
94 return true;
95 }
96 case 8: { // rrggbbaa
97 color.r = (float)((bytes[0] << 4) + bytes[1]) / 255.0f;
98 color.g = (float)((bytes[2] << 4) + bytes[3]) / 255.0f;
99 color.b = (float)((bytes[4] << 4) + bytes[5]) / 255.0f;
100 color.a = (float)((bytes[6] << 4) + bytes[7]) / 255.0f;
101 return true;
102 }
103 }
104
105 return false;
106}
107
108
109//============================================================================//

Callers 1

parseColorCStringFunction · 0.85

Calls 1

parseHexCharFunction · 0.85

Tested by

no test coverage detected