Float vs. double.

This commit is contained in:
Blaz Kristan 2023-03-19 11:24:59 +01:00
parent 747c920420
commit 11b687cdc2
2 changed files with 50 additions and 69 deletions

View File

@ -57,39 +57,42 @@ void setRandomColor(byte* rgb)
void colorHStoRGB(uint16_t hue, byte sat, byte* rgb) //hue, sat to rgb void colorHStoRGB(uint16_t hue, byte sat, byte* rgb) //hue, sat to rgb
{ {
float h = ((float)hue)/65535.0; float h = ((float)hue)/65535.0f;
float s = ((float)sat)/255.0; float s = ((float)sat)/255.0f;
byte i = floor(h*6); int i = floorf(h*6);
float f = h * 6-i; float f = h * 6.0f - i;
float p = 255 * (1-s); int p = int(255.0f * (1.0f-s));
float q = 255 * (1-f*s); int q = int(255.0f * (1.0f-f*s));
float t = 255 * (1-(1-f)*s); int t = int(255.0f * (1.0f-(1.0f-f)*s));
p = constrain(p, 0, 255);
q = constrain(q, 0, 255);
t = constrain(t, 0, 255);
switch (i%6) { switch (i%6) {
case 0: rgb[0]=255,rgb[1]=t, rgb[2]=p; break; case 0: rgb[0]=255,rgb[1]=t, rgb[2]=p; break;
case 1: rgb[0]=q, rgb[1]=255,rgb[2]=p; break; case 1: rgb[0]=q, rgb[1]=255,rgb[2]=p; break;
case 2: rgb[0]=p, rgb[1]=255,rgb[2]=t; break; case 2: rgb[0]=p, rgb[1]=255,rgb[2]=t; break;
case 3: rgb[0]=p, rgb[1]=q, rgb[2]=255;break; case 3: rgb[0]=p, rgb[1]=q, rgb[2]=255;break;
case 4: rgb[0]=t, rgb[1]=p, rgb[2]=255;break; case 4: rgb[0]=t, rgb[1]=p, rgb[2]=255;break;
case 5: rgb[0]=255,rgb[1]=p,rgb[2]=q; case 5: rgb[0]=255,rgb[1]=p, rgb[2]=q; break;
} }
} }
//get RGB values from color temperature in K (https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html) //get RGB values from color temperature in K (https://tannerhelland.com/2012/09/18/convert-temperature-rgb-algorithm-code.html)
void colorKtoRGB(uint16_t kelvin, byte* rgb) //white spectrum to rgb, calc void colorKtoRGB(uint16_t kelvin, byte* rgb) //white spectrum to rgb, calc
{ {
float r = 0, g = 0, b = 0; int r = 0, g = 0, b = 0;
float temp = kelvin / 100; float temp = kelvin / 100.0f;
if (temp <= 66) { if (temp <= 66.0f) {
r = 255; r = 255;
g = round(99.4708025861 * log(temp) - 161.1195681661); g = roundf(99.4708025861f * logf(temp) - 161.1195681661f);
if (temp <= 19) { if (temp <= 19.0f) {
b = 0; b = 0;
} else { } else {
b = round(138.5177312231 * log((temp - 10)) - 305.0447927307); b = roundf(138.5177312231f * logf((temp - 10.0f)) - 305.0447927307f);
} }
} else { } else {
r = round(329.698727446 * pow((temp - 60), -0.1332047592)); r = roundf(329.698727446f * powf((temp - 60.0f), -0.1332047592f));
g = round(288.1221695283 * pow((temp - 60), -0.0755148492)); g = roundf(288.1221695283f * powf((temp - 60.0f), -0.0755148492f));
b = 255; b = 255;
} }
//g += 12; //mod by Aircoookie, a bit less accurate but visibly less pinkish //g += 12; //mod by Aircoookie, a bit less accurate but visibly less pinkish
@ -147,9 +150,9 @@ void colorXYtoRGB(float x, float y, byte* rgb) //coordinates to rgb (https://www
b = 1.0f; b = 1.0f;
} }
// Apply gamma correction // Apply gamma correction
r = r <= 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * pow(r, (1.0f / 2.4f)) - 0.055f; r = r <= 0.0031308f ? 12.92f * r : (1.0f + 0.055f) * powf(r, (1.0f / 2.4f)) - 0.055f;
g = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * pow(g, (1.0f / 2.4f)) - 0.055f; g = g <= 0.0031308f ? 12.92f * g : (1.0f + 0.055f) * powf(g, (1.0f / 2.4f)) - 0.055f;
b = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * pow(b, (1.0f / 2.4f)) - 0.055f; b = b <= 0.0031308f ? 12.92f * b : (1.0f + 0.055f) * powf(b, (1.0f / 2.4f)) - 0.055f;
if (r > b && r > g) { if (r > b && r > g) {
// red is biggest // red is biggest
@ -173,9 +176,9 @@ void colorXYtoRGB(float x, float y, byte* rgb) //coordinates to rgb (https://www
b = 1.0f; b = 1.0f;
} }
} }
rgb[0] = 255.0*r; rgb[0] = byte(255.0f*r);
rgb[1] = 255.0*g; rgb[1] = byte(255.0f*g);
rgb[2] = 255.0*b; rgb[2] = byte(255.0f*b);
} }
void colorRGBtoXY(byte* rgb, float* xy) //rgb to coordinates (https://www.developers.meethue.com/documentation/color-conversions-rgb-xy) void colorRGBtoXY(byte* rgb, float* xy) //rgb to coordinates (https://www.developers.meethue.com/documentation/color-conversions-rgb-xy)
@ -242,35 +245,13 @@ float maxf (float v, float w)
return v; return v;
} }
/*
uint32_t colorRGBtoRGBW(uint32_t c)
{
byte rgb[4];
rgb[0] = R(c);
rgb[1] = G(c);
rgb[2] = B(c);
rgb[3] = W(c);
colorRGBtoRGBW(rgb);
return RGBW32(rgb[0], rgb[1], rgb[2], rgb[3]);
}
void colorRGBtoRGBW(byte* rgb) //rgb to rgbw (http://codewelt.com/rgbw). (RGBW_MODE_LEGACY)
{
float low = minf(rgb[0],minf(rgb[1],rgb[2]));
float high = maxf(rgb[0],maxf(rgb[1],rgb[2]));
if (high < 0.1f) return;
float sat = 100.0f * ((high - low) / high); // maximum saturation is 100 (corrected from 255)
rgb[3] = (byte)((255.0f - sat) / 255.0f * (rgb[0] + rgb[1] + rgb[2]) / 3);
}
*/
byte correctionRGB[4] = {0,0,0,0};
uint16_t lastKelvin = 0;
// adjust RGB values based on color temperature in K (range [2800-10200]) (https://en.wikipedia.org/wiki/Color_balance) // adjust RGB values based on color temperature in K (range [2800-10200]) (https://en.wikipedia.org/wiki/Color_balance)
// called from bus manager when color correction is enabled!
uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb) uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb)
{ {
//remember so that slow colorKtoRGB() doesn't have to run for every setPixelColor() //remember so that slow colorKtoRGB() doesn't have to run for every setPixelColor()
static byte correctionRGB[4] = {0,0,0,0};
static uint16_t lastKelvin = 0;
if (lastKelvin != kelvin) colorKtoRGB(kelvin, correctionRGB); // convert Kelvin to RGB if (lastKelvin != kelvin) colorKtoRGB(kelvin, correctionRGB); // convert Kelvin to RGB
lastKelvin = kelvin; lastKelvin = kelvin;
byte rgbw[4]; byte rgbw[4];

View File

@ -11,15 +11,15 @@ void _overlayAnalogClock()
{ {
_overlayAnalogCountdown(); return; _overlayAnalogCountdown(); return;
} }
double hourP = ((double)(hour(localTime)%12))/12; float hourP = ((float)(hour(localTime)%12))/12.0f;
double minuteP = ((double)minute(localTime))/60; float minuteP = ((float)minute(localTime))/60.0f;
hourP = hourP + minuteP/12; hourP = hourP + minuteP/12.0f;
double secondP = ((double)second(localTime))/60; float secondP = ((float)second(localTime))/60.0f;
int hourPixel = floor(analogClock12pixel + overlaySize*hourP); int hourPixel = floorf(analogClock12pixel + overlaySize*hourP);
if (hourPixel > overlayMax) hourPixel = overlayMin -1 + hourPixel - overlayMax; if (hourPixel > overlayMax) hourPixel = overlayMin -1 + hourPixel - overlayMax;
int minutePixel = floor(analogClock12pixel + overlaySize*minuteP); int minutePixel = floorf(analogClock12pixel + overlaySize*minuteP);
if (minutePixel > overlayMax) minutePixel = overlayMin -1 + minutePixel - overlayMax; if (minutePixel > overlayMax) minutePixel = overlayMin -1 + minutePixel - overlayMax;
int secondPixel = floor(analogClock12pixel + overlaySize*secondP); int secondPixel = floorf(analogClock12pixel + overlaySize*secondP);
if (secondPixel > overlayMax) secondPixel = overlayMin -1 + secondPixel - overlayMax; if (secondPixel > overlayMax) secondPixel = overlayMin -1 + secondPixel - overlayMax;
if (analogClockSecondsTrail) if (analogClockSecondsTrail)
{ {
@ -36,7 +36,7 @@ void _overlayAnalogClock()
{ {
for (byte i = 0; i <= 12; i++) for (byte i = 0; i <= 12; i++)
{ {
int pix = analogClock12pixel + round((overlaySize / 12.0) *i); int pix = analogClock12pixel + roundf((overlaySize / 12.0f) *i);
if (pix > overlayMax) pix -= overlaySize; if (pix > overlayMax) pix -= overlaySize;
strip.setPixelColor(pix, 0x00FFAA); strip.setPixelColor(pix, 0x00FFAA);
} }
@ -52,29 +52,29 @@ void _overlayAnalogCountdown()
if ((unsigned long)toki.second() < countdownTime) if ((unsigned long)toki.second() < countdownTime)
{ {
long diff = countdownTime - toki.second(); long diff = countdownTime - toki.second();
double pval = 60; float pval = 60.0f;
if (diff > 31557600L) //display in years if more than 365 days if (diff > 31557600L) //display in years if more than 365 days
{ {
pval = 315576000L; //10 years pval = 315576000.0f; //10 years
} else if (diff > 2592000L) //display in months if more than a month } else if (diff > 2592000L) //display in months if more than a month
{ {
pval = 31557600L; //1 year pval = 31557600.0f; //1 year
} else if (diff > 604800) //display in weeks if more than a week } else if (diff > 604800) //display in weeks if more than a week
{ {
pval = 2592000L; //1 month pval = 2592000.0f; //1 month
} else if (diff > 86400) //display in days if more than 24 hours } else if (diff > 86400) //display in days if more than 24 hours
{ {
pval = 604800; //1 week pval = 604800.0f; //1 week
} else if (diff > 3600) //display in hours if more than 60 minutes } else if (diff > 3600) //display in hours if more than 60 minutes
{ {
pval = 86400; //1 day pval = 86400.0f; //1 day
} else if (diff > 60) //display in minutes if more than 60 seconds } else if (diff > 60) //display in minutes if more than 60 seconds
{ {
pval = 3600; //1 hour pval = 3600.0f; //1 hour
} }
int overlaySize = overlayMax - overlayMin +1; int overlaySize = overlayMax - overlayMin +1;
double perc = (pval-(double)diff)/pval; float perc = (pval-(float)diff)/pval;
if (perc > 1.0) perc = 1.0; if (perc > 1.0f) perc = 1.0f;
byte pixelCnt = perc*overlaySize; byte pixelCnt = perc*overlaySize;
if (analogClock12pixel + pixelCnt > overlayMax) if (analogClock12pixel + pixelCnt > overlayMax)
{ {