From 895b19b7454c0b5136831cc7eff4c359efe493e2 Mon Sep 17 00:00:00 2001 From: Blaz Kristan Date: Mon, 29 Jan 2024 20:06:07 +0100 Subject: [PATCH] Minor tweaks --- wled00/FX_fcn.cpp | 3 +- wled00/colors.cpp | 66 +++++++++++++++++--------------------------- wled00/fcn_declare.h | 2 +- wled00/wled.h | 3 ++ 4 files changed, 30 insertions(+), 44 deletions(-) diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 3ecbf37b6c..c78653813f 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -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 } diff --git a/wled00/colors.cpp b/wled00/colors.cpp index 8dcc735c3c..37a05953d2 100644 --- a/wled00/colors.cpp +++ b/wled00/colors.cpp @@ -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 @@ -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 diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index ac68a6f8c6..ff37184617 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -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); diff --git a/wled00/wled.h b/wled00/wled.h index 6f8a029579..3e74787d0d 100755 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -177,7 +177,10 @@ using PSRAMDynamicJsonDocument = BasicJsonDocument; #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"