Skip to content

Commit

Permalink
Merge pull request #20 from yuki-koyama/travis
Browse files Browse the repository at this point in the history
Travis CI setup and better dependency resolution
  • Loading branch information
yuki-koyama authored Aug 4, 2019
2 parents 489e185 + 761f06e commit 0376f76
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 54 deletions.
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: cpp

matrix:
include:
- compiler: gcc
os: linux
- compiler: clang
os: osx

dist: xenial

addons:
homebrew:
update: true
packages:
- eigen
- qt
apt:
packages:
- libeigen3-dev
- qt5-default

script:
- cmake -DTINYCOLORMAP_BUILD_TOOLS=ON -DTINYCOLORMAP_WITH_EIGEN=ON -DTINYCOLORMAP_WITH_QT5=ON .
- make
- ctest
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ target_include_directories(tinycolormap INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/in
option(TINYCOLORMAP_WITH_EIGEN "Enable Eigen integration" OFF)
if(TINYCOLORMAP_WITH_EIGEN)
find_package(Eigen3 REQUIRED)
if((NOT TARGET Eigen3::Eigen) AND (DEFINED EIGEN3_INCLUDE_DIR))
add_library(AliasEigen3 INTERFACE)
target_include_directories(AliasEigen3 INTERFACE ${EIGEN3_INCLUDE_DIR})
add_library(Eigen3::Eigen ALIAS AliasEigen3)
endif()

target_link_libraries(tinycolormap INTERFACE Eigen3::Eigen)
target_compile_definitions(tinycolormap INTERFACE TINYCOLORMAP_WITH_EIGEN)
endif()
Expand All @@ -26,7 +32,7 @@ if(TINYCOLORMAP_WITH_QT5)
list(APPEND CMAKE_PREFIX_PATH "/usr/local/opt/qt")
endif()

find_package(Qt5 5.6 COMPONENTS Gui REQUIRED)
find_package(Qt5 COMPONENTS Gui REQUIRED)
target_link_libraries(tinycolormap INTERFACE Qt5::Gui)
target_compile_definitions(tinycolormap INTERFACE TINYCOLORMAP_WITH_QT5)
endif()
Expand Down
90 changes: 45 additions & 45 deletions include/tinycolormap.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
MIT License
Copyright (c) 2018 Yuki Koyama
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -46,25 +46,25 @@ namespace tinycolormap
//////////////////////////////////////////////////////////////////////////////////
// Interface
//////////////////////////////////////////////////////////////////////////////////

enum class ColormapType
{
Heat, Jet, Hot, Gray, Magma, Inferno, Plasma, Viridis, Cividis, Github
};

struct Color
{
constexpr Color(double r, double g, double b) : data({{ r, g, b }}) {}

std::array<double, 3> data;

double& r() { return data[0]; }
double& g() { return data[1]; }
double& b() { return data[2]; }
const double& r() const { return data[0]; }
const double& g() const { return data[1]; }
const double& b() const { return data[2]; }

double& operator[](size_t n) { return data[n]; }
const double& operator[](size_t n) const { return data[n]; }
double& operator()(size_t n) { return data[n]; }
Expand All @@ -77,7 +77,7 @@ namespace tinycolormap
Eigen::Vector3d ConvertToEigen() const { return Eigen::Vector3d(data[0], data[1], data[2]); }
#endif
};

inline Color GetColor(double x, ColormapType type = ColormapType::Viridis);
inline Color GetHeatColor(double x);
inline Color GetJetColor(double x);
Expand All @@ -94,21 +94,21 @@ namespace tinycolormap
inline QImage CreateMatrixVisualization(const Eigen::MatrixXd& matrix);
inline void ExportMatrixVisualization(const Eigen::MatrixXd& matrix, const std::string& path);
#endif

//////////////////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////////////////

inline Color operator+(const Color& c0, const Color& c1)
{
return { c0[0] + c1[0], c0[1] + c1[1], c0[2] + c1[2] };
}

inline Color operator*(double s, const Color& c)
{
return { s * c[0], s * c[1], s * c[2] };
}

inline Color GetColor(double x, ColormapType type)
{
switch (type)
Expand Down Expand Up @@ -137,11 +137,11 @@ namespace tinycolormap
break;
}
}

inline Color GetHeatColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.0, 0.0, 1.0 },
Expand All @@ -150,19 +150,19 @@ namespace tinycolormap
{ 1.0, 1.0, 0.0 },
{ 1.0, 0.0, 0.0 }
};

const double a = x * ((sizeof(data) / sizeof(Color)) - 1);
const double t = a - std::floor(a);
const Color c0 = data[static_cast<size_t>(std::floor(a))];
const Color c1 = data[static_cast<size_t>(std::ceil (a))];

return (1.0 - t) * c0 + t * c1;
}

inline Color GetJetColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.0, 0.0, 0.5 },
Expand All @@ -175,23 +175,23 @@ namespace tinycolormap
{ 1.0, 0.0, 0.0 },
{ 0.5, 0.0, 0.0 }
};

const double a = x * ((sizeof(data) / sizeof(Color)) - 1);
const double t = a - std::floor(a);
const Color c0 = data[static_cast<size_t>(std::floor(a))];
const Color c1 = data[static_cast<size_t>(std::ceil (a))];

return (1.0 - t) * c0 + t * c1;
}

inline Color GetHotColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color r{ 1.0, 0.0, 0.0 };
constexpr Color g{ 0.0, 1.0, 0.0 };
constexpr Color b{ 0.0, 0.0, 1.0 };

if (x < 0.4)
{
const double t = x / 0.4;
Expand All @@ -208,18 +208,18 @@ namespace tinycolormap
return r + g + t * b;
}
}

inline Color GetGrayColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

return (1.0 - x) * Color{ 1.0, 1.0, 1.0 };
}

inline Color GetMagmaColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.001462, 0.000466, 0.013866 },
Expand Down Expand Up @@ -479,14 +479,14 @@ namespace tinycolormap
{ 0.987387, 0.984288, 0.742002 },
{ 0.987053, 0.991438, 0.749504 }
};

return data[static_cast<size_t>(std::round(x * 255.0))];
}

inline Color GetInfernoColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.001462, 0.000466, 0.013866 },
Expand Down Expand Up @@ -746,14 +746,14 @@ namespace tinycolormap
{ 0.982257, 0.994109, 0.631017 },
{ 0.988362, 0.998364, 0.644924 }
};

return data[static_cast<size_t>(std::round(x * 255.0))];
}

inline Color GetPlasmaColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.050383, 0.029803, 0.527975 },
Expand Down Expand Up @@ -1013,14 +1013,14 @@ namespace tinycolormap
{ 0.941896, 0.968590, 0.140956 },
{ 0.940015, 0.975158, 0.131326 }
};

return data[static_cast<size_t>(std::round(x * 255.0))];
}

inline Color GetViridisColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.267004, 0.004874, 0.329415 },
Expand Down Expand Up @@ -1280,10 +1280,10 @@ namespace tinycolormap
{ 0.983868, 0.904867, 0.136897 },
{ 0.993248, 0.906157, 0.143936 }
};

return data[static_cast<size_t>(std::round(x * 255.0))];
}

inline Color GetCividisColor(double x)
{
x = std::max(0.0, std::min(1.0, x));
Expand Down Expand Up @@ -1554,7 +1554,7 @@ namespace tinycolormap
inline Color GetGithubColor(double x)
{
x = std::max(0.0, std::min(1.0, x));

constexpr Color data[] =
{
{ 0.933333, 0.933333, 0.933333 },
Expand All @@ -1563,12 +1563,12 @@ namespace tinycolormap
{ 0.137254, 0.603921, 0.231372 },
{ 0.098039, 0.380392, 0.152941 }
};

const double a = x * ((sizeof(data) / sizeof(Color)) - 1);
const double t = a - std::floor(a);
const Color c0 = data[static_cast<size_t>(std::floor(a))];
const Color c1 = data[static_cast<size_t>(std::ceil (a))];

return (1.0 - t) * c0 + t * c1;
}

Expand All @@ -1586,8 +1586,8 @@ namespace tinycolormap
{
for (int y = 0; y < h; ++ y)
{
const auto color = tinycolormap::GetColor(normalized(y, x));
image.setPixelColor(x, y, color.ConvertToQColor());
const QColor color = tinycolormap::GetColor(normalized(y, x)).ConvertToQColor();
image.setPixel(x, y, color.rgb());
}
}

Expand Down
16 changes: 8 additions & 8 deletions tools/png-exporter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ int main(int argc, char* argv[])

constexpr int width = 300;
constexpr int height = 30;

const std::vector<std::pair<tinycolormap::ColormapType, std::string>> colormap_types
{
{ tinycolormap::ColormapType::Heat, "Heat" },
Expand All @@ -32,20 +32,20 @@ int main(int argc, char* argv[])
for (const auto& colormap_type : colormap_types)
{
QImage image(width, height, QImage::Format_ARGB32);

for (int x = 0; x < width; ++ x)
{
const double value = static_cast<double>(x) / static_cast<double>(width - 1);
const tinycolormap::Color color = tinycolormap::GetColor(value, colormap_type.first);
const double value = static_cast<double>(x) / static_cast<double>(width - 1);
const QColor color = tinycolormap::GetColor(value, colormap_type.first).ConvertToQColor();

for (int y = 0; y < height; ++ y)
{
image.setPixelColor(x, y, color.ConvertToQColor());
image.setPixel(x, y, color.rgb());
}
}

image.save(QString::fromStdString(save_directory_path + "/" + colormap_type.second + ".png"));
}

return 0;
}

0 comments on commit 0376f76

Please sign in to comment.