Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Jan 29, 2024
1 parent 7089250 commit 895b19b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 44 deletions.
3 changes: 1 addition & 2 deletions wled00/FX_fcn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ CRGBPalette16 IRAM_ATTR &Segment::loadPalette(CRGBPalette16 &targetPalette, uint
case 1: {//periodically replace palette with a random one
unsigned long timeSinceLastChange = millis() - _lastPaletteChange;
if (timeSinceLastChange > randomPaletteChangeTime * 1000U) {
//_randomPalette = _newRandomPalette;
_newRandomPalette = generateRandomPalette(&_randomPalette);
_newRandomPalette = generateRandomPalette(_randomPalette);
_lastPaletteChange = millis();
handleRandomPalette(); // do a 1st pass of blend
}
Expand Down
66 changes: 25 additions & 41 deletions wled00/colors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,48 +92,37 @@ void setRandomColor(byte* rgb)
}

/*
*generates a random palette based on color theory
* generates a random palette based on color theory
*/

CRGBPalette16 generateRandomPalette(CRGBPalette16* basepalette)
CRGBPalette16 generateRandomPalette(CRGBPalette16 &basepalette)
{
CHSV palettecolors[4]; //array of colors for the new palette
uint8_t keepcolorposition = random8(4); //color position of current random palette to keep
palettecolors[keepcolorposition] = rgb2hsv_approximate(basepalette->entries[keepcolorposition*5]); //read one of the base colors of the current palette
CHSV palettecolors[4]; // array of colors for the new palette
uint8_t keepcolorposition = random8(4); // color position of current random palette to keep

palettecolors[keepcolorposition] = rgb2hsv_approximate(basepalette.entries[keepcolorposition*5]); // read one of the base colors of the current palette
palettecolors[keepcolorposition].hue += random8(20)-10; // +/- 10 randomness
//generate 4 saturation and brightness value numbers
//only one saturation is allowed to be below 200 creating mostly vibrant colors
//only one brigthness value number is allowed to below 200, creating mostly bright palettes
// generate 4 saturation and brightness value numbers
// only one saturation is allowed to be below 200 creating mostly vibrant colors
// only one brightness value number is allowed to below 200, creating mostly bright palettes

for (int i = 0; i<3; i++) { //generate three high values
for (int i = 0; i < 3; i++) { //generate three high values
palettecolors[i].saturation = random8(180,255);
palettecolors[i].value = random8(180,255);
palettecolors[i].value = random8(180,255);
}
//allow one to be lower
// allow one to be lower
palettecolors[3].saturation = random8(80,255);
palettecolors[3].value = random8(50,255);
palettecolors[3].value = random8(50,255);

//shuffle the arrays using Fisher-Yates algorithm
// shuffle the arrays using Fisher-Yates algorithm
for (int i = 3; i > 0; i--) {
uint8_t j = random8(0, i + 1);
//swap [i] and [j]
uint8_t temp = palettecolors[i].saturation;
palettecolors[i].saturation = palettecolors[j].saturation;
palettecolors[j].saturation = temp;
std::swap(palettecolors[i].saturation, palettecolors[random8(0, i + 1)].saturation);
std::swap(palettecolors[i].value, palettecolors[random8(0, i + 1)].value);
}

for (int i = 3; i > 0; i--) {
uint8_t j = random8(0, i + 1);
//swap [i] and [j]
uint8_t temp = palettecolors[i].value;
palettecolors[i].value = palettecolors[j].value;
palettecolors[j].value = temp;
}

//now generate three new hues based off of the hue of the chosen current color
// now generate three new hues based off of the hue of the chosen current color
uint8_t basehue = palettecolors[keepcolorposition].hue;
uint8_t harmonics[3]; //hues that are harmonic but still a littl random
uint8_t type = random8(5); //choose a harmony type
uint8_t harmonics[3]; // hues that are harmonic but still a little random
uint8_t type = random8(5); // choose a harmony type

switch (type) {
case 0: // analogous
Expand Down Expand Up @@ -167,28 +156,23 @@ CRGBPalette16 generateRandomPalette(CRGBPalette16* basepalette)
break;
}

//shuffle the hues:
// shuffle the hues:
for (int i = 2; i > 0; i--) {
uint8_t j = random8(0, i + 1);
//swap [i] and [j]
uint8_t temp = harmonics[i];
harmonics[i] = harmonics[j];
harmonics[j] = temp;
std::swap(harmonics[i], harmonics[random8(0, i + 1)]);
}

//now set the hues
// now set the hues
int j=0;
for (int i = 0; i<4; i++) {
if(i==keepcolorposition) continue; //skip the base color
for (int i = 0; i < 4; i++) {
if (i == keepcolorposition) continue; // skip the base color
palettecolors[i].hue = harmonics[j];
j++;
}
}

return CRGBPalette16( palettecolors[0],
palettecolors[1],
palettecolors[2],
palettecolors[3]);

}

void colorHStoRGB(uint16_t hue, byte sat, byte* rgb) //hue, sat to rgb
Expand Down
2 changes: 1 addition & 1 deletion wled00/fcn_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class NeoGammaWLEDMethod {
uint32_t color_blend(uint32_t,uint32_t,uint16_t,bool b16=false);
uint32_t color_add(uint32_t,uint32_t, bool fast=false);
uint32_t color_fade(uint32_t c1, uint8_t amount, bool video=false);
CRGBPalette16 generateRandomPalette(CRGBPalette16* basepalette);
CRGBPalette16 generateRandomPalette(CRGBPalette16 &basepalette);
inline uint32_t colorFromRgbw(byte* rgbw) { return uint32_t((byte(rgbw[3]) << 24) | (byte(rgbw[0]) << 16) | (byte(rgbw[1]) << 8) | (byte(rgbw[2]))); }
void colorHStoRGB(uint16_t hue, byte sat, byte* rgb); //hue, sat to rgb
void colorKtoRGB(uint16_t kelvin, byte* rgb);
Expand Down
3 changes: 3 additions & 0 deletions wled00/wled.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ using PSRAMDynamicJsonDocument = BasicJsonDocument<PSRAM_Allocator>;
#define PSRAMDynamicJsonDocument DynamicJsonDocument
#endif

#define FASTLED_INTERNAL //remove annoying pragma messages
#define USE_GET_MILLISECOND_TIMER
#include "FastLED.h"

#include "const.h"
#include "fcn_declare.h"
#include "NodeStruct.h"
Expand Down

0 comments on commit 895b19b

Please sign in to comment.