Skip to content

Commit

Permalink
improved precision of hexstr -> color and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Bitdiddle committed Oct 8, 2012
1 parent 022f1d1 commit c6b9a83
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace p {
enum {width = M};
enum {height = N};

explicit mat() {}
mat() = default;
explicit mat(T val) {
std::uninitialized_fill(components, components + M*N, val);
std::fill(components, components + M*N, val);
}

vec<T, M> &row(std::size_t j) {
Expand Down
2 changes: 2 additions & 0 deletions unittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ endif(CMAKE_COMPILER_IS_GNUCXX)


find_package(GTest REQUIRED)
find_package(Threads)

add_executable(unittest EXCLUDE_FROM_ALL
vector_test.cpp
Expand All @@ -44,4 +45,5 @@ include_directories(
target_link_libraries(unittest
${GTEST_MAIN_LIBRARY}
${GTEST_LIBRARY}
pthread
)
25 changes: 18 additions & 7 deletions unittest/vector_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,16 +202,27 @@ TEST(utils_vector, color_parse) {
EXPECT_TRUE(ss >> color_reader(c));
EXPECT_FLOAT_EQ(1.0f, c.r);
EXPECT_FLOAT_EQ(0.66666666f, c.g);
EXPECT_NEAR(0.13333333f, c.b, 0.02f);
EXPECT_NEAR(0.13333333f, c.b, 0.01f);
}

{
std::stringstream ss("0xFF0088");
ubvec3 c;
EXPECT_TRUE(ss >> color_reader(c));
EXPECT_EQ(0xFF, c.r);
EXPECT_EQ(0x00, c.g);
EXPECT_EQ(0x88, c.b);
{
std::stringstream ss("0xFF0088");
ubvec3 c;
EXPECT_TRUE(ss >> color_reader(c));
EXPECT_EQ(0xFF, c.r);
EXPECT_EQ(0x00, c.g);
EXPECT_EQ(0x88, c.b);
}

{
std::stringstream ss("0xFF0080");
vec3 c;
EXPECT_TRUE(ss >> color_reader(c));
EXPECT_FLOAT_EQ(1.0f, c.r);
EXPECT_FLOAT_EQ(0.0f, c.g);
EXPECT_NEAR(0.5f, c.b, 0.01f);
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion vector_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ namespace p {
else if (textual.size() > 2 &&
textual[0] == '0' && textual[1] == 'x') {
// parse it as hex
// convert_from_bin_component to type_component
// convert(0x000000FF, 8, float)
// TODO: this involves some muls/divs; integer conversions
// might turn out incorrect. Maybe there should be a
// fast-path for those.
unsigned long val = std::strtoul(textual.c_str(), NULL, 16);
for (std::size_t i = size; i--; ) {
target[i] = ((val & 0xFF) / 255.0) * color_limits<T>::max();
target[i] = (val & 0xFF) * (color_limits<T>::max() / 255.0);
val >>= 8;
}
}
Expand Down

0 comments on commit c6b9a83

Please sign in to comment.