Skip to content

Commit 1cc165d

Browse files
committed
cleanups
1 parent 5e87724 commit 1cc165d

File tree

6 files changed

+99
-40
lines changed

6 files changed

+99
-40
lines changed

.clang-format

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Standard: Cpp11
2+
IndentWidth: 4
3+
AccessModifierOffset: -4
4+
UseTab: Never
5+
BinPackParameters: false
6+
AllowShortIfStatementsOnASingleLine: false
7+
AllowShortLoopsOnASingleLine: false
8+
AllowShortBlocksOnASingleLine: false
9+
AllowShortFunctionsOnASingleLine: false
10+
ConstructorInitializerAllOnOneLineOrOnePerLine: true
11+
AlwaysBreakTemplateDeclarations: true
12+
NamespaceIndentation: None
13+
PointerBindsToType: true
14+
SpacesInParentheses: false
15+
BreakBeforeBraces: Attach
16+
ColumnLimit: 100
17+
Cpp11BracedListStyle: false
18+
SpacesBeforeTrailingComments: 1

Makefile

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
CXXFLAGS = -std=c++11 -Wall -Wextra -Wno-unused-parameter
1+
CXXFLAGS = -std=c++11 -Wall -Wextra -Wpedantic -Weverything -Wno-c++98-compat -Wno-missing-prototypes -Wno-padded -Wno-unused-parameter
22

3-
BIN = color
4-
SRCS = main.cpp
5-
SRCS += csscolorparser.cpp
6-
OBJS = $(patsubst %.cpp,%.o,$(SRCS))
3+
build: test
74

8-
build: $(BIN)
9-
10-
$(BIN): $(OBJS)
5+
test: csscolorparser.o test.o
116
$(CXX) $(CXXFLAGS) -o $@ $^
127

138
%.o: %.cpp
149
$(CXX) $(CXXFLAGS) -c -o $@ $^
1510

1611
clean:
17-
rm -rf *.o $(BIN)
12+
rm -rf *.o test
1813

1914
.PHONY: clean

csscolorparser.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// (c) Dean McNamee <dean@gmail.com>, 2012.
2-
// C++ port by Konstantin Käfer <mail@kkaefer.com>, 2014.
2+
// C++ port by Mapbox, Konstantin Käfer <mail@kkaefer.com>, 2014-2017.
33
//
44
// https://github.com/deanm/css-color-parser-js
55
// https://github.com/kkaefer/css-color-parser-cpp
@@ -27,7 +27,6 @@
2727
#include <cstdint>
2828
#include <vector>
2929
#include <sstream>
30-
#include <cmath>
3130
#include <algorithm>
3231

3332
namespace CSSColorParser {
@@ -117,12 +116,12 @@ const size_t namedColorCount = sizeof (namedColors) / sizeof (NamedColor);
117116
template <typename T>
118117
uint8_t clamp_css_byte(T i) { // Clamp to integer 0 .. 255.
119118
i = ::round(i); // Seems to be what Chrome does (vs truncation).
120-
return i < 0 ? 0 : i > 255 ? 255 : i;
119+
return i < 0 ? 0 : i > 255 ? 255 : uint8_t(i);
121120
}
122121

123122
template <typename T>
124123
float clamp_css_float(T f) { // Clamp to float 0.0 .. 1.0.
125-
return f < 0 ? 0 : f > 1 ? 1 : f;
124+
return f < 0 ? 0 : f > 1 ? 1 : float(f);
126125
}
127126

128127
float parseFloat(const std::string& str) {
@@ -163,7 +162,7 @@ float css_hue_to_rgb(float m1, float m2, float h) {
163162
return m2;
164163
}
165164
if (h * 3.0f < 2.0f) {
166-
return m1 + (m2 - m1) * (2.0 / 3.0 - h) * 6.0f;
165+
return m1 + (m2 - m1) * (2.0f / 3.0f - h) * 6.0f;
167166
}
168167
return m1;
169168
}

csscolorparser.hpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// (c) Dean McNamee <dean@gmail.com>, 2012.
2-
// C++ port by Konstantin Käfer <mail@kkaefer.com>, 2014.
2+
// C++ port by Mapbox, Konstantin Käfer <mail@kkaefer.com>, 2014-2017.
33
//
44
// https://github.com/deanm/css-color-parser-js
55
// https://github.com/kkaefer/css-color-parser-cpp
@@ -26,19 +26,30 @@
2626
#define CSS_COLOR_PARSER_CPP
2727

2828
#include <string>
29+
#include <cmath>
2930

3031
namespace CSSColorParser {
3132

3233
struct Color {
33-
inline Color() {}
34+
inline Color() {
35+
}
3436
inline Color(unsigned char r_, unsigned char g_, unsigned char b_, float a_)
35-
: r(r_), g(g_), b(b_), a(a_) {}
37+
: r(r_), g(g_), b(b_), a(a_ > 1 ? 1 : a_ < 0 ? 0 : a_) {
38+
}
3639
unsigned char r = 0, g = 0, b = 0;
3740
float a = 1.0f;
3841
};
3942

40-
Color parse(const std::string& css_str);
43+
inline bool operator==(const Color& lhs, const Color& rhs) {
44+
return lhs.r == rhs.r && lhs.g == rhs.g && lhs.b == rhs.b && ::fabs(lhs.a - lhs.b) < 0.0001f;
45+
}
4146

47+
inline bool operator!=(const Color& lhs, const Color& rhs) {
48+
return !(lhs == rhs);
4249
}
4350

51+
Color parse(const std::string& css_str);
52+
53+
} // namespace CSSColorParser
54+
4455
#endif

main.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

test.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include "csscolorparser.hpp"
2+
3+
#include <iostream>
4+
5+
using namespace CSSColorParser;
6+
7+
std::ostream& operator<<(std::ostream& os, const Color& color) {
8+
return os << "rgba(" << int(color.r) << ", " << int(color.g) << ", " << int(color.b) << ", "
9+
<< color.a << ")";
10+
}
11+
12+
static bool errored = false;
13+
14+
void ASSERT_EQUAL(const Color& expected, const std::string& input) {
15+
const auto actual = parse(input);
16+
if (expected != actual) {
17+
errored = true;
18+
std::cerr << "\033[1mERROR!: expected " << expected << " != parsed " << actual
19+
<< " when parsing \"" << input << "\"\033[0m" << std::endl;
20+
} else {
21+
std::cerr << "Passed: " << actual << std::endl;
22+
}
23+
}
24+
25+
void ASSERT_EQUAL(const Color& expected, const Color& actual) {
26+
if (expected != actual) {
27+
errored = true;
28+
std::cerr << "\033[1mERROR!: expected " << expected << " != actual " << actual << "\"\033[0m" << std::endl;
29+
} else {
30+
std::cerr << "Passed: " << actual << std::endl;
31+
}
32+
}
33+
34+
int main() {
35+
try {
36+
ASSERT_EQUAL(Color{ 255, 128, 12, 0.5 }, " rgba (255, 128, 12, 0.5)");
37+
ASSERT_EQUAL(Color{ 255, 255, 255, 1 }, "#fff");
38+
ASSERT_EQUAL(Color{ 255, 0, 17, 1 }, "#ff0011");
39+
ASSERT_EQUAL(Color{ 106, 90, 205, 1 }, "slateblue");
40+
ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "blah");
41+
ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "ffffff");
42+
ASSERT_EQUAL(Color{ 226, 233, 233, 0.5 }, "hsla(900, 15%, 90%, 0.5)");
43+
ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "hsla(900, 15%, 90%)");
44+
ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 15%, 90%)");
45+
ASSERT_EQUAL(Color{ 226, 233, 233, 1 }, "hsl(900, 0.15, 90%)"); // NOTE: not spec compliamt.
46+
47+
// Out of range:
48+
ASSERT_EQUAL(Color{ 0, 0, 0, 1 }, "xxx");
49+
ASSERT_EQUAL(Color{ 255, 128, 12, 1 }, " rgba (255, 128, 12, 2)");
50+
ASSERT_EQUAL(Color{ 255, 128, 12, 1 }, " rgba (400, 128, 12, 2)");
51+
ASSERT_EQUAL(Color{ 255, 128, 12, 1 }, Color{ 255, 128, 12, 3 });
52+
} catch(std::exception& ex) {
53+
std::cerr << "EXCEPTION!: " << ex.what() << std::endl;
54+
return 2;
55+
}
56+
57+
return errored ? 1 : 0;
58+
}

0 commit comments

Comments
 (0)