NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
linear_gradient.hpp
浏览该文件的文档.
1#ifndef NEFORCE_TUI_DOM_LINEAR_GRADIENT_HPP__
2#define NEFORCE_TUI_DOM_LINEAR_GRADIENT_HPP__
3
10
14NEFORCE_BEGIN_NAMESPACE__
15NEFORCE_BEGIN_TUI__
16
21
29private:
33 struct color_stop {
34 _NEFORCE color color;
35 optional<float> position;
36
42 explicit color_stop(_NEFORCE color c, optional<float> pos = none) :
43 color(move(c)),
44 position(move(pos)) {}
45 };
46
47 float angle_ = 0.0F;
48 vector<color_stop> stops_;
49
50public:
55 stops_.push_back(color_stop(_NEFORCE color::transparent(), 0.0F));
56 stops_.push_back(color_stop(_NEFORCE color::black(), 1.0F));
57 }
58
64 linear_gradient(_NEFORCE color begin, struct color end) {
65 stops_.push_back(color_stop(begin, 0.0F));
66 stops_.push_back(color_stop(end, 1.0F));
67 }
68
74 linear_gradient& angle(const float a) {
75 angle_ = a;
76 return *this;
77 }
78
86 stops_.push_back(color_stop(c, pos));
87 return *this;
88 }
89
95 NEFORCE_NODISCARD _NEFORCE color sample(float t) const {
96 if (stops_.empty()) {
97 return _NEFORCE color{};
98 }
99 if (stops_.size() == 1) {
100 return stops_[0].color;
101 }
102
103 size_t idx = 0;
104 for (size_t i = 0; i < stops_.size() - 1; ++i) {
105 const float p1 = stops_[i].position.value_or(0.0F);
106 const float p2 = stops_[i + 1].position.value_or(1.0F);
107 if (t >= p1 && t <= p2) {
108 idx = i;
109 break;
110 }
111 }
112
113 const color_stop& s1 = stops_[idx];
114 const color_stop& s2 = stops_[idx + 1];
115 const float p1 = s1.position.value_or(0.0F);
116 const float p2 = s2.position.value_or(1.0F);
117 const float range = p2 - p1;
118 const float factor = (range > 0.0F) ? (t - p1) / range : 0.0F;
119
120 return color::lerp(s1.color, s2.color, factor);
121 }
122};
123 // TUI
125
126NEFORCE_END_TUI__
127NEFORCE_END_NAMESPACE__
128#endif // NEFORCE_TUI_DOM_LINEAR_GRADIENT_HPP__
static constexpr color lerp(const color &from, const color &to, double t) noexcept
线性插值两个颜色
static constexpr color black() noexcept
黑色
static constexpr color transparent() noexcept
完全透明
constexpr value_type value_or(value_type value) const &noexcept(is_nothrow_copy_constructible_v< value_type >)
取出存储的值的拷贝值
动态大小数组容器
颜色类
constexpr none_t none
默认空表示
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
decorator color(color c)
前景色装饰器
constexpr decltype(auto) end(Container &cont) noexcept(noexcept(cont.end()))
获取容器的结束迭代器
constexpr decltype(auto) begin(Container &cont) noexcept(noexcept(cont.begin()))
获取容器的起始迭代器
可选值类型
linear_gradient & add_stop(const struct color &c, optional< float > pos=none)
添加色标
linear_gradient(_NEFORCE color begin, struct color end)
从起止颜色构造
linear_gradient & angle(const float a)
设置渐变角度
_NEFORCE color sample(float t) const
在指定位置采样颜色
动态大小数组容器