Skip to content

Commit

Permalink
Moves ColorCycle out of Axis class and adds an equivalent cycling…
Browse files Browse the repository at this point in the history
… function for `MarkStyle`.
  • Loading branch information
pazmivaniye committed Jun 18, 2024
1 parent 9c15b8c commit 26fafd5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 31 deletions.
6 changes: 2 additions & 4 deletions axis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,8 @@ std::string pgfplotter::Axis::plot_src(const std::string& path, int subplot) con
}
else if(markers[i].mark < 0)
{
const std::size_t idx = i%DefaultMarks.size();
src += "mark = " + convert_marker(DefaultMarks[idx].mark) + ", mark"
" size = " + ToString(3.*DefaultMarks[idx].size*markers[i].
size);
src += "mark = " + convert_marker(MarkCycle(i).mark) + ", mark size"
" = " + ToString(3.*MarkCycle(i).size*markers[i].size);
if(markers[i].spacing)
{
src += ", mark repeat = " + std::to_string(markers[i].spacing);
Expand Down
57 changes: 31 additions & 26 deletions pgfplotter
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace pgfplotter
namespace Color
{
// These have uniformly increasing perceived lightness.
static constexpr std::array<std::array<int, 3>, 5> Defaults =
constexpr std::array<std::array<int, 3>, 5> Defaults =
{{
{ 0, 57, 181},
{202, 46, 0},
Expand All @@ -33,26 +33,31 @@ namespace pgfplotter
}};
// Blue to white to red, with equal perceived lightness and saturation
// at both ends.
static constexpr std::array<std::array<int, 3>, 3> Bidir =
constexpr std::array<std::array<int, 3>, 3> Bidir =
{{
{ 10, 10, 143},
{255, 255, 255},
{160, 10, 10}
}};
static constexpr std::array<int, 3> Black = {};
static constexpr std::array<int, 3> DarkGray = {64, 64, 64};
static constexpr std::array<int, 3> Gray = {128, 128, 128};
static constexpr std::array<int, 3> LightGray = {191, 191, 191};
static constexpr std::array<int, 3> White = {255, 255, 255};
static constexpr std::array<int, 3> Red = {255, 0, 0};
static constexpr std::array<int, 3> Green = {0, 255, 0};
static constexpr std::array<int, 3> Blue = {0, 0, 255};
constexpr std::array<int, 3> Black = {};
constexpr std::array<int, 3> DarkGray = {64, 64, 64};
constexpr std::array<int, 3> Gray = {128, 128, 128};
constexpr std::array<int, 3> LightGray = {191, 191, 191};
constexpr std::array<int, 3> White = {255, 255, 255};
constexpr std::array<int, 3> Red = {255, 0, 0};
constexpr std::array<int, 3> Green = {0, 255, 0};
constexpr std::array<int, 3> Blue = {0, 0, 255};
// Selects color from `Defaults`.
static constexpr std::array<int, 3> Auto = {-1};
static constexpr std::array<int, 3> FromX = {-2};
static constexpr std::array<int, 3> FromY = {-3};
static constexpr std::array<int, 3> FromZ = {-4};
static constexpr std::array<int, 3> FromW = {-5};
constexpr std::array<int, 3> Auto = {-1};
constexpr std::array<int, 3> FromX = {-2};
constexpr std::array<int, 3> FromY = {-3};
constexpr std::array<int, 3> FromZ = {-4};
constexpr std::array<int, 3> FromW = {-5};
}

constexpr std::array<int, 3> ColorCycle(std::size_t i)
{
return Color::Defaults[i%Color::Defaults.size()];
}

enum class LineStyle
Expand All @@ -79,7 +84,7 @@ namespace pgfplotter
}
};

static constexpr std::array<MarkStyle, 5> DefaultMarks =
constexpr std::array<MarkStyle, 5> DefaultMarks =
{{
{'S', 2./3., 0},
{'*', 2./3., 0},
Expand All @@ -88,6 +93,11 @@ namespace pgfplotter
MarkStyle::None()
}};

constexpr MarkStyle MarkCycle(std::size_t i)
{
return DefaultMarks[i%DefaultMarks.size()];
}

struct DrawStyle
{
std::array<int, 3> color;
Expand All @@ -96,12 +106,12 @@ namespace pgfplotter
double lineWidth;
double opacity;
};
static constexpr DrawStyle BasicLine = {Color::Auto, MarkStyle::None(),
LineStyle::Solid, 1., 1.};
static constexpr DrawStyle BasicScatter = {Color::Auto, MarkStyle::Point(),
constexpr DrawStyle BasicLine = {Color::Auto, MarkStyle::None(), LineStyle::
Solid, 1., 1.};
constexpr DrawStyle BasicScatter = {Color::Auto, MarkStyle::Point(),
LineStyle::None, 1., 1.};
static constexpr DrawStyle Default = {Color::Auto, MarkStyle::Auto(),
LineStyle::Solid, 1., 1.};
constexpr DrawStyle Default = {Color::Auto, MarkStyle::Auto(), LineStyle::
Solid, 1., 1.};

class Axis
{
Expand Down Expand Up @@ -187,11 +197,6 @@ namespace pgfplotter
std::string plot_src(const std::string& dir, int subplot) const;

public:
static constexpr std::array<int, 3> ColorCycle(std::size_t i)
{
return Color::Defaults[i%Color::Defaults.size()];
}

static std::string ToString(double x, unsigned int precision = 10);

static constexpr unsigned int Fixed = 1;
Expand Down
22 changes: 21 additions & 1 deletion test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace pgf = pgfplotter;

static const std::string PlotName = "plot";
static constexpr double TwoPi = 6.28318530717958647692528676656;

static std::string get_dir(const std::string& path)
{
Expand Down Expand Up @@ -114,7 +115,26 @@ int main(int, char** argv)

try
{
std::vector<pgf::Axis> v(2, p);
std::vector<pgf::Axis> v(2);
v[0] = p;
v[0].resize(0.6, 0.6);
pgf::DrawStyle style = pgf::Default;
style.markStyle.spacing = 20;
std::vector<double> x(200), y(x.size()), z(x.size());
for(std::size_t i = 0; i < x.size(); ++i)
{
x[i] = i*TwoPi/(x.size() - 1);
y[i] = std::cos(x[i]);
z[i] = 1. + std::sin(x[i]);
}
v[1].draw(style, x, y, {}, {}, "$\\cos\\args{x}$");
v[1].draw(style, x, z, {}, {}, "$1+\\sin\\args{x}$");
v[1].squeezeX();
v[1].resize(1., 0.6);
v[1].setTitle("Other Plot");
v[1].setXLabel("$x$");
v[1].setYLabel("$y$");
v[1].legend(pgf::Axis::Southeast);
pgf::plot(outputDir + "/" + PlotName + "-2", v);
}
CATCH
Expand Down

0 comments on commit 26fafd5

Please sign in to comment.