Skip to content

Commit

Permalink
add back color functions
Browse files Browse the repository at this point in the history
  • Loading branch information
BaptisteHudyma committed May 27, 2024
1 parent 1d8b0f5 commit bc39c61
Show file tree
Hide file tree
Showing 20 changed files with 3,784 additions and 6 deletions.
781 changes: 781 additions & 0 deletions src/system/colors/animations.cpp

Large diffs are not rendered by default.

131 changes: 131 additions & 0 deletions src/system/colors/animations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#ifndef ANIMATIONS_ANIMATIONS_H
#define ANIMATIONS_ANIMATIONS_H

#include "../utils/colorspace.h"
#include "../utils/strip.h"
#include "colors.h"

namespace animations {

/**
* \brief Fill the display with a color, with an optional cutoff value
* \param[in] color class that returns a color to display
* \param[in, out] strip The led strip to control
* \param[in] cutOff between 0 and 1, how much this gradient will fill the
* display before suddently cutting of
*/
void fill(const Color& color, LedStrip& strip, const float cutOff = 1);

/**
* \brief Do a wipe down followed by a wipe up animation
* \param[in] color class that returns a color to display
* \param[in] duration The duration of the animation, in milliseconds
* \param[in] fadeOut The animation fade speed (0: no fade)
* \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to control
* \param[in] cutOff between 0 and 1, how much this gradient will fill the
* display before suddently cutting of \return True if the animation is finished
*/
bool dot_ping_pong(const Color& color, const uint32_t duration,
const uint8_t fadeOut, const bool restart, LedStrip& strip,
const float cutOff = 1);

/**
* \brief Do color pulse
* \param[in] color class that returns a color to display
* \param[in] durationPulseUp The duration of the color fill up animation, in
* milliseconds \param[in] durationPulseDown The duration of the pong animation,
* in milliseconds \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to control
* \param[in] cutOff between 0 and 1, how much this color will fill the display
* before suddently cutting of \return True if the animation is finished
*/
bool color_pulse(const Color& color, const uint32_t durationPulseUp,
const uint32_t durationPulseDown, const bool restart,
LedStrip& strip, const float cutOff = 1);

/**
* \brief Fill the display from both side simultaneously
* \param[in] color class that returns a color to display
* \param[in] duration The duration of the animation, in milliseconds
* \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to control
* \return True if the animation is finished
*/
bool double_side_fill(const Color& color, const uint32_t duration,
const bool restart, LedStrip& strip);

/**
* \brief Do police light animation
* \param[in] duration The duration of the animation, in milliseconds
* \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to control
* \return True if the animation is finished
*/
bool police(const uint32_t duration, const bool restart, LedStrip& strip);

/**
* \brief Do a fade out of the colors currently displayed
* \param[in] duration The duration of the animation, in milliseconds
* \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to control
* \return True if the animation is finished
*/
bool fade_out(const uint32_t duration, const bool restart, LedStrip& strip);

/**
* \brief Do a fade in of a color
* \param[in] color class that returns a color to display
* \param[in] duration The duration of the animation, in milliseconds
* \param[in] restart If true, the animation will restart
* \param[in, out] strip The led strip to contro
* \param[in] firstCutOff between 0 and 1, how much this color will fill the
* display before suddently cutting of \param[in] secondCutOff between 0 and 1,
* how much this color will fill the display before suddently cutting of \return
* True if the animation is finished
*/
bool fade_in(const Color& color, const uint32_t duration, const bool restart,
LedStrip& strip, const float firstCutOff = 0.0,
const float secondCutOff = 1.0);

/**
* Fire animation
* https://editor.soulmatelights.com/gallery/234-fire
*/
void fire(const uint8_t scalex, const uint8_t scaley, const uint8_t speed,
const palette_t& palette, LedStrip& strip);

void random_noise(const palette_t& palette, LedStrip& strip, const bool restart,
const bool isColorLoop, const uint16_t scale);

void candle(const palette_t& palette, LedStrip& strip);

/**
* \brief Display some sinewave of colors, going back and forth
* \param[in] moder: add some random noise
*/
void phases(const bool moder, const uint8_t speed, const palette_t& palette,
LedStrip& strip);
void mode_2DPolarLights(const uint8_t scale, const uint8_t speed,
const palette_t& palette, const bool reset,
LedStrip& strip);
void mode_2DDrift(const uint8_t intensity, const uint8_t speed,
const palette_t& palette, LedStrip& strip);
void hiphotic(const uint8_t speed, LedStrip& strip);

void mode_2Ddistortionwaves(const uint8_t scale, const uint8_t speed,
LedStrip& strip);

void mode_lake(const uint8_t speed, const palette_t& palette, LedStrip& strip);

// Adjustable sinewave. By Andrew Tuline
void mode_sinewave(const uint8_t speed, const uint8_t intensity,
const palette_t& palette, LedStrip& strip);

void running_base(bool saw, bool dual, const uint8_t speed,
const uint8_t intensity, const palette_t& palette,
LedStrip& strip);

}; // namespace animations

#endif
80 changes: 80 additions & 0 deletions src/system/colors/colors.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "colors.h"

#include <cmath>
#include <cstdint>

#include "../utils/colorspace.h"
#include "../utils/constants.h"
#include "../utils/strip.h"
#include "../utils/utils.h"
#include "palettes.h"

uint32_t GenerateSolidColor::get_color_internal(const uint16_t index,
const uint16_t maxIndex) const {
return _color;
}

uint32_t GenerateRainbowColor::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
const uint16_t hue = map(constrain(index, 0, maxIndex), 0, maxIndex, 0, 360);
return utils::hue_to_rgb_sinus(hue);
}

uint32_t GenerateGradientColor::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
return utils::get_gradient(_colorStart, _colorEnd, index / (float)maxIndex);
}

uint32_t GenerateRoundColor::get_color_internal(const uint16_t index,
const uint16_t maxIndex) const {
const double segmentsPerTurns = 3.1;
const double modulo = std::fmod(index, segmentsPerTurns) / segmentsPerTurns;
return utils::hue_to_rgb_sinus(modulo * 360.0);
}

uint32_t GenerateRainbowSwirl::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
const uint16_t pixelHue = _firstPixelHue + (index * UINT16_MAX / maxIndex);
return utils::hue_to_rgb_sinus(map(pixelHue, 0, UINT16_MAX, 0, 360));
}

uint32_t GeneratePaletteStep::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
return get_color_from_palette(_index, *_paletteRef);
}

uint32_t GeneratePaletteIndexed::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
return get_color_from_palette(_index, *_paletteRef);
}

uint32_t GenerateRainbowPulse::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
return LedStrip::ColorHSV(_currentPixelHue);
}

uint32_t GenerateRainbowIndex::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
return LedStrip::ColorHSV(_currentPixelHue);
}

uint32_t GeneratePastelPulse::get_color_internal(
const uint16_t index, const uint16_t maxIndex) const {
const double hue = double(_currentPixelHue) / double(UINT16_MAX) * 360.0;

// using OKLCH will create softer colors
auto res = utils::ColorSpace::OKLCH(0.752, 0.126, hue);
return res.get_rgb().color;
}

GenerateRandomColor::GenerateRandomColor()
: _color(utils::get_random_color()) {}

void GenerateRandomColor::internal_update(const uint32_t deltaTimeMilli) {
_color = utils::get_random_color();
}

void GenerateComplementaryColor::internal_update(
const uint32_t deltaTimeMilli) {
_color = utils::get_random_complementary_color(_color, _randomVariation);
};
Loading

0 comments on commit bc39c61

Please sign in to comment.