Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions src/veg/gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ gradient::~gradient()
linear_gradient::linear_gradient(const r4::vector2<real>& p0, const r4::vector2<real>& p1)
#if VEG_BACKEND == VEG_BACKEND_AGG
:
gradient(this->linear_pad, this->linear_reflect, this->linear_repeat)
spread_wrappers({&this->linear_pad, &this->linear_reflect, &this->linear_repeat})
#endif
{
#if VEG_BACKEND == VEG_BACKEND_CAIRO
Expand All @@ -72,6 +72,9 @@ linear_gradient::linear_gradient(const r4::vector2<real>& p0, const r4::vector2<
throw std::runtime_error("cairo_pattern_create_linear() failed");
}
#elif VEG_BACKEND == VEG_BACKEND_AGG
// set the initial spread method
this->set_spread_method(gradient_spread_method::pad);

auto v = p1 - p0;
auto len = v.norm();

Expand All @@ -93,7 +96,7 @@ linear_gradient::linear_gradient(const r4::vector2<real>& p0, const r4::vector2<
radial_gradient::radial_gradient(const r4::vector2<real>& f, const r4::vector2<real>& c, real r)
#if VEG_BACKEND == VEG_BACKEND_AGG
:
gradient(this->radial_pad, this->radial_reflect, this->radial_repeat)
spread_wrappers({&this->radial_pad, &this->radial_reflect, &this->radial_repeat})
#endif
{
#if VEG_BACKEND == VEG_BACKEND_CAIRO
Expand All @@ -109,6 +112,9 @@ radial_gradient::radial_gradient(const r4::vector2<real>& f, const r4::vector2<r
throw std::runtime_error("cairo_pattern_create_radial() failed");
}
#elif VEG_BACKEND == VEG_BACKEND_AGG
// set the initial spread method
this->set_spread_method(gradient_spread_method::pad);

// gradient parameters are stretched so that the gradient radius becomes
// color_lut_size, this is for optimal color picking from the color_lut
real lut_r = decltype(gradient::lut)::color_lut_size;
Expand Down Expand Up @@ -156,19 +162,7 @@ void gradient::set_spread_method(gradient_spread_method spread_method)
SL
);
#elif VEG_BACKEND == VEG_BACKEND_AGG
switch (spread_method) {
default:
utki::assert(false, SL);
case gradient_spread_method::pad:
this->cur_grad = &this->pad;
break;
case gradient_spread_method::reflect:
this->cur_grad = &this->reflect;
break;
case gradient_spread_method::repeat:
this->cur_grad = &this->repeat;
break;
}
this->cur_grad = &this->get_spread_method_wrapper(spread_method);
#endif
}

Expand Down
41 changes: 26 additions & 15 deletions src/veg/gradient.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ SOFTWARE.
#elif VEG_BACKEND == VEG_BACKEND_AGG
# include <agg/agg_gradient_lut.h>
# include <agg/agg_span_gradient.h>

# include <utki/enum_array.hpp>
#endif

namespace veg {
Expand All @@ -59,7 +61,9 @@ enum class gradient_spread_method {
/**
* @brief Repeat gradients stops in direct order.
*/
repeat
repeat,

enum_size
};

/**
Expand Down Expand Up @@ -111,11 +115,14 @@ class gradient
}
};

const gradient_wrapper_base& pad;
const gradient_wrapper_base& reflect;
const gradient_wrapper_base& repeat;
// The concrete gradient wrappers are members of the derived classes, so
// derived classes must set this in their constructor bodies and provide
// the wrappers via get_spread_method_wrapper().
gradient_wrapper_base* cur_grad = nullptr;

virtual gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) = 0;

const gradient_wrapper_base* cur_grad;
gradient() = default;

const gradient_wrapper_base& get_agg_gradient() const
{
Expand All @@ -128,16 +135,6 @@ class gradient

r4::matrix2<real> local_matrix;

gradient(
const gradient_wrapper_base& pad,
const gradient_wrapper_base& reflect,
const gradient_wrapper_base& repeat
) :
pad(pad),
reflect(reflect),
repeat(repeat),
cur_grad(&this->pad)
{}
#endif

public:
Expand Down Expand Up @@ -180,6 +177,13 @@ class linear_gradient : public gradient
gradient_wrapper<agg::gradient_x> linear_pad;
spread_gradient_wrapper<agg::gradient_x, agg::gradient_reflect_adaptor> linear_reflect;
spread_gradient_wrapper<agg::gradient_x, agg::gradient_repeat_adaptor> linear_repeat;

utki::enum_array<gradient_wrapper_base*, gradient_spread_method> spread_wrappers;

gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) override
{
return *this->spread_wrappers[spread_method];
}
#endif

public:
Expand All @@ -203,6 +207,13 @@ class radial_gradient : public gradient
gradient_wrapper<agg::gradient_radial_focus> radial_pad;
spread_gradient_wrapper<agg::gradient_radial_focus, agg::gradient_reflect_adaptor> radial_reflect;
spread_gradient_wrapper<agg::gradient_radial_focus, agg::gradient_repeat_adaptor> radial_repeat;

utki::enum_array<gradient_wrapper_base*, gradient_spread_method> spread_wrappers;

gradient_wrapper_base& get_spread_method_wrapper(gradient_spread_method spread_method) override
{
return *this->spread_wrappers[spread_method];
}
#endif

public:
Expand Down
Loading