#include <Adafruit_NeoPixel.h>
#define PIN 8
#define NUMPIXELS 46
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
uint8_t red_colour;
uint8_t green_colour;
uint8_t blue_colour;
int red_color;
int green_color;
int blue_color;
uint8_t hue = 0;
double hue_double = 0;
double inline threeway_max(double a, double b, double c)
{
return max(a, max(b, c));
}
double inline threeway_min(double a, double b, double c)
{
return min(a, min(b, c));
}
void RgbToHsv(uint8_t red, uint8_t green, uint8_t blue, double& hue, double& saturation, double& value)
{
auto rd = static_cast<double>(red) / 255;
auto gd = static_cast<double>(green) / 255;
auto bd = static_cast<double>(blue) / 255;
auto max = threeway_max(rd, gd, bd), min = threeway_min(rd, gd, bd);
value = max;
auto d = max - min;
saturation = max == 0 ? 0 : d / max;
hue = 0;
if (max != min)
{
if (max == rd)
{
hue = (gd - bd) / d + (gd < bd ? 6 : 0);
}
else if (max == gd)
{
hue = (bd - rd) / d + 2;
}
else if (max == bd)
{
hue = (rd - gd) / d + 4;
}
hue /= 6;
}
}
void HsvToRgb(double hue, double saturation, double value, uint8_t& red, uint8_t& green, uint8_t& blue)
{
double r, g, b;
auto i = static_cast<int>(hue * 6);
auto f = hue * 6 - i;
auto p = value * (1 - saturation);
auto q = value * (1 - f * saturation);
auto t = value * (1 - (1 - f) * saturation);
switch (i % 6)
{
case 0: r = value , g = t , b = p;
break;
case 1: r = q , g = value , b = p;
break;
case 2: r = p , g = value , b = t;
break;
case 3: r = p , g = q , b = value;
break;
case 4: r = t , g = p , b = value;
break;
case 5: r = value , g = p , b = q;
break;
}
red = static_cast<uint8_t>(r * 255);
green = static_cast<uint8_t>(g * 255);
blue = static_cast<uint8_t>(b * 255);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pixels.begin();
}
void loop() {
// put your main code here, to run repeatedly:
// for (int j = 0; j < 255; j++) {
for (int i = 0; i < NUMPIXELS; i++) {
HsvToRgb(hue_double,1,1, red_colour, green_colour, blue_colour);
red_color = red_colour;
green_color = green_colour;
blue_color = blue_colour;
pixels.setPixelColor(i,pixels.Color(red_color,green_color,blue_color));
}
pixels.show();
delay(50);
Serial.println(hue_double);
hue_double += 0.01;
if(hue_double > 1){
hue_double = 0;
}
}