Skip to content

Commit

Permalink
fix & test mix & gradient
Browse files Browse the repository at this point in the history
  • Loading branch information
profezzorn committed Dec 16, 2023
1 parent ed38ec6 commit 7bd9c87
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 12 deletions.
20 changes: 10 additions & 10 deletions common/typelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,28 @@ template<typename TL> using RestTypeList = typename RestTypeListHelper<TL>::type



// Head
template<size_t n, typename TL> struct HeadTypeListHelper {
typedef typename HeadTypeListHelper<n-1, RestTypeList<TL>>::type type;
// Tail
template<size_t n, typename TL> struct TailTypeListHelper {
typedef typename TailTypeListHelper<n-1, RestTypeList<TL>>::type type;
};
template<typename TL>
struct HeadTypeListHelper<0, TL> {
struct TailTypeListHelper<0, TL> {
typedef TL type;
};

template<size_t n, typename TL> using HeadTypeList = typename::HeadTypeListHelper<n, TL>::type;
template<size_t n, typename TL> using TailTypeList = typename::TailTypeListHelper<n, TL>::type;

// Tail
// Head
template<size_t n, typename TL, typename prefix>
struct TailTypeListHelper {
typedef typename::TailTypeListHelper<n-1, RestTypeList<TL>, ConcatTypeLists<prefix, TypeList<FirstTypeList<TL>>>>::type type;
struct HeadTypeListHelper {
typedef typename::HeadTypeListHelper<n-1, RestTypeList<TL>, ConcatTypeLists<prefix, TypeList<FirstTypeList<TL>>>>::type type;
};
template<typename TL, typename prefix>
struct TailTypeListHelper<0, TL, prefix> {
struct HeadTypeListHelper<0, TL, prefix> {
typedef prefix type;
};

template<size_t n, typename TL> using TailTypeList = typename::TailTypeListHelper<n, TL, TypeList<>>::type;
template<size_t n, typename TL> using HeadTypeList = typename::HeadTypeListHelper<n, TL, TypeList<>>::type;


template<typename TL>
Expand Down
21 changes: 19 additions & 2 deletions styles/gradient.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,25 @@
// Gradient, color A at base, B at tip.
// Any number of colors can be put together into a gradient.

#include "../functions/ramp.h"
#include "mix.h"

template<class... COLORS> using Gradient = Mix<RampF, COLORS...>;
template<class... COLORS>
class Gradient {
public:
void run(BladeBase* blade) {
colors_.run(blade);
mul_ = ((sizeof...(COLORS)-1) << 15) / (blade->num_leds() - 1);
}
private:
PONUA MixHelper<COLORS...> colors_;
int mul_;
public:
auto getColor(int led) -> decltype(colors_.getColor(1,1)) {
int x = led * mul_;
auto a = colors_.getColor(x >> 15, led);
auto b = colors_.getColor((x >> 15) + 1, led);
return MixColors(a, b, x & 0x7fff, 15);
}
};

#endif
29 changes: 29 additions & 0 deletions styles/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,36 @@ void test_argument_parsing() {
CHECK_COLOR(TestRgbArgColors[3], 7, 8, 9, 0);
}

void test_gradient() {
MockBlade mock_blade;
mock_blade.colors.resize(3);
Style<Gradient<Red, Green, Blue>> style;
style.run(&mock_blade);

CHECK_COLOR(mock_blade.colors[0], 65535, 0, 0, 1);
CHECK_COLOR(mock_blade.colors[1], 0, 65535, 0, 1);
CHECK_COLOR(mock_blade.colors[2], 0, 0, 65535, 1);
}

void test_mix() {
MockBlade mock_blade;
mock_blade.colors.resize(1);
Style<Mix<Int<0>, Red, Green, Blue, Yellow, White>> s1;
s1.run(&mock_blade);
CHECK_COLOR(mock_blade.colors[0], 65535, 0, 0, 1);

Style<Mix<Int<4096>, Red, Green, Blue, Yellow, White>> s2;
s2.run(&mock_blade);
CHECK_COLOR(mock_blade.colors[0], 32767, 32767, 0, 1);

Style<Mix<Int<8192>, Red, Green, Blue, Yellow, White>> s3;
s3.run(&mock_blade);
CHECK_COLOR(mock_blade.colors[0], 0, 65535, 0, 1);
}

int main() {
test_mix();
test_gradient();
test_style6();
test_style5();
test_style4();
Expand Down

0 comments on commit 7bd9c87

Please sign in to comment.