Cleaned up repository
- only moving files around
This commit is contained in:
35
esp-firmware/lib/ledtl/helpers/BellCurve.h
Normal file
35
esp-firmware/lib/ledtl/helpers/BellCurve.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
static inline float bellCurveApproximation(float x, float inverseWidth)
|
||||
{
|
||||
if (x < 0)
|
||||
x = -x;
|
||||
|
||||
const auto nx = x * inverseWidth * 4;
|
||||
if (nx > 2)
|
||||
return 0.0f;
|
||||
|
||||
const auto x2 = nx * nx;
|
||||
const auto x3 = x2 * nx;
|
||||
const auto res = 1.0f + 0.27606958941084f * x3 - 0.80213917882168f * x2;
|
||||
|
||||
return res < 0.0f ? 0.0f : res;
|
||||
}
|
||||
|
||||
// Function start at 0 (x=0) and goes smoothly up to 1 and arrives 1 at x=width
|
||||
// inverse width has to be 1 / width (should be cached outside)
|
||||
static inline float stepFunction(float x, float width, float inverseWidth)
|
||||
{
|
||||
if (x < 0.0f)
|
||||
return 0.0f;
|
||||
if (x >= width)
|
||||
return 1.0f;
|
||||
|
||||
auto bellInvWidth = inverseWidth * 0.5f;
|
||||
auto nx = (-x + width) * bellInvWidth * 4;
|
||||
auto x2 = nx * nx;
|
||||
auto x3 = x2 * nx;
|
||||
return 1 + 0.2760695894 * x3 - 0.8021391 * x2;
|
||||
}
|
||||
116
esp-firmware/lib/ledtl/helpers/ColorConversions.h
Normal file
116
esp-firmware/lib/ledtl/helpers/ColorConversions.h
Normal file
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
|
||||
#include "helpers/ColorHSV.h"
|
||||
#include "helpers/ColorRGBW.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
|
||||
// https://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both
|
||||
inline ColorHSV rgb2hsv(const ColorRGBW &in)
|
||||
{
|
||||
ColorHSV out;
|
||||
|
||||
const float r = (float)(in.r) / 255.0f;
|
||||
const float g = (float)(in.g) / 255.0f;
|
||||
const float b = (float)(in.b) / 255.0f;
|
||||
|
||||
const float min = std::min(r, std::min(g, b));
|
||||
const float max = std::max(r, std::max(g, b));
|
||||
|
||||
out.v = max; // v
|
||||
const float delta = max - min;
|
||||
if (delta < 0.00001f)
|
||||
{
|
||||
out.s = 0;
|
||||
out.h = 0; // undefined, maybe nan?
|
||||
return out;
|
||||
}
|
||||
if (max > 0.0f)
|
||||
{ // NOTE: if Max is == 0, this divide would cause a crash
|
||||
out.s = (delta / max); // s
|
||||
}
|
||||
else
|
||||
{
|
||||
// if max is 0, then r = g = b = 0
|
||||
// s = 0, h is undefined
|
||||
out.s = 0.0f;
|
||||
out.h = 0.0f; //NAN; // its now undefined
|
||||
return out;
|
||||
}
|
||||
if (r >= max) // > is bogus, just keeps compiler happy
|
||||
out.h = (g - b) / delta; // between yellow & magenta
|
||||
else if (g >= max)
|
||||
out.h = 2.0f + (b - r) / delta; // between cyan & yellow
|
||||
else
|
||||
out.h = 4.0f + (r - g) / delta; // between magenta & cyan
|
||||
|
||||
out.h *= 60.0f; // degrees
|
||||
|
||||
if (out.h < 0.0f)
|
||||
out.h += 360.0f;
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
ColorRGBW hsv2rgb(const ColorHSV &in)
|
||||
{
|
||||
int i;
|
||||
ColorRGBW out;
|
||||
out.w = 0;
|
||||
|
||||
if (in.s <= 0.0f)
|
||||
{ // < is bogus, just shuts up warnings
|
||||
out.r = (uint8_t)(in.v * 255.0f);
|
||||
out.g = (uint8_t)(in.v * 255.0f);
|
||||
out.b = (uint8_t)(in.v * 255.0f);
|
||||
return out;
|
||||
}
|
||||
float hh = in.h;
|
||||
if (hh >= 360.0f)
|
||||
hh = 0.0f;
|
||||
hh /= 60.0f;
|
||||
i = (long)hh;
|
||||
auto ff = hh - i;
|
||||
float p = in.v * (1.0f - in.s);
|
||||
float q = in.v * (1.0f - (in.s * ff));
|
||||
float t = in.v * (1.0f - (in.s * (1.0f - ff)));
|
||||
|
||||
switch (i)
|
||||
{
|
||||
case 0:
|
||||
out.r = (uint8_t)(in.v * 255.0f);
|
||||
out.g = (uint8_t)(t * 255.0f);
|
||||
out.b = (uint8_t)(p * 255.0f);
|
||||
break;
|
||||
case 1:
|
||||
out.r = (uint8_t)(q * 255.0f);
|
||||
out.g = (uint8_t)(in.v * 255.0f);
|
||||
out.b = (uint8_t)(p * 255.0f);
|
||||
break;
|
||||
case 2:
|
||||
out.r = (uint8_t)(p * 255.0f);
|
||||
out.g = (uint8_t)(in.v * 255.0f);
|
||||
out.b = (uint8_t)(t * 255.0f);
|
||||
break;
|
||||
|
||||
case 3:
|
||||
out.r = (uint8_t)(p * 255.0f);
|
||||
out.g = (uint8_t)(q * 255.0f);
|
||||
out.b = (uint8_t)(in.v * 255.0f);
|
||||
break;
|
||||
case 4:
|
||||
out.r = (uint8_t)(t * 255.0f);
|
||||
out.g = (uint8_t)(p * 255.0f);
|
||||
out.b = (uint8_t)(in.v * 255.0f);
|
||||
break;
|
||||
case 5:
|
||||
default:
|
||||
out.r = (uint8_t)(in.v * 255.0f);
|
||||
out.g = (uint8_t)(p * 255.0f);
|
||||
out.b = (uint8_t)(q * 255.0f);
|
||||
break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
46
esp-firmware/lib/ledtl/helpers/ColorHSV.h
Normal file
46
esp-firmware/lib/ledtl/helpers/ColorHSV.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct ColorHSV
|
||||
{
|
||||
float h, s, v;
|
||||
};
|
||||
|
||||
inline ColorHSV operator*(const ColorHSV &c, float scalar)
|
||||
{
|
||||
|
||||
return {scalar * c.h,
|
||||
scalar * c.s,
|
||||
scalar * c.v};
|
||||
}
|
||||
|
||||
inline ColorHSV operator*(float scalar, const ColorHSV &c)
|
||||
{
|
||||
return {scalar * c.h,
|
||||
scalar * c.s,
|
||||
scalar * c.v};
|
||||
}
|
||||
|
||||
inline ColorHSV operator+(const ColorHSV &c1, const ColorHSV &c2)
|
||||
{
|
||||
return {
|
||||
c1.h + c2.h,
|
||||
c1.s + c2.s,
|
||||
c1.v + c2.v};
|
||||
}
|
||||
|
||||
inline ColorHSV interpolate(const ColorHSV &c1, const ColorHSV &c2, float f)
|
||||
{
|
||||
return ColorHSV{
|
||||
(1.0f - f) * c1.h + f * c2.h,
|
||||
(1.0f - f) * c1.s + f * c2.s,
|
||||
(1.0f - f) * c1.v + f * c2.v};
|
||||
}
|
||||
|
||||
#ifndef PLATFORM_NATIVE
|
||||
inline void print(const char *prefix, const ColorHSV &c)
|
||||
{
|
||||
Serial.printf("%s HSV(%f, %f, %f)\n", prefix, c.h, c.s, c.v);
|
||||
}
|
||||
#endif
|
||||
26
esp-firmware/lib/ledtl/helpers/ColorRGBW.h
Normal file
26
esp-firmware/lib/ledtl/helpers/ColorRGBW.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
struct ColorRGBW
|
||||
{
|
||||
uint8_t r, g, b, w;
|
||||
|
||||
ColorRGBW operator*(float s) const
|
||||
{
|
||||
return {uint8_t(s * r),
|
||||
uint8_t(s * g),
|
||||
uint8_t(s * b),
|
||||
uint8_t(s * w)};
|
||||
}
|
||||
|
||||
static inline ColorRGBW interpolate(const ColorRGBW &c1, const ColorRGBW &c2, float f)
|
||||
{
|
||||
return ColorRGBW{
|
||||
static_cast<uint8_t>((1.0f - f) * static_cast<float>(c1.r) + f * static_cast<float>(c2.r)),
|
||||
static_cast<uint8_t>((1.0f - f) * static_cast<float>(c1.g) + f * static_cast<float>(c2.g)),
|
||||
static_cast<uint8_t>((1.0f - f) * static_cast<float>(c1.b) + f * static_cast<float>(c2.b)),
|
||||
static_cast<uint8_t>((1.0f - f) * static_cast<float>(c1.w) + f * static_cast<float>(c2.w)),
|
||||
};
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user