Sorry, I needed to make a smaller version of my project to show the problem in the forum, because otherwise the code gets to lengthy.
Below I did.
My .h file:
#include "WProgram.h"
#ifndef ColorLight_h
#define ColorLight_h
class ColourBase
{
public:
ColourBase();
float interpolation;
int baseHue; // hue value on the circle
int wheelHSBColor[3];
int tintHSBColor[3];
int shadeHSBColor[3];
int interpolatedHSBColor[3]; // interpolated color between shade/tint
unsigned long wheelRGBColor;
unsigned long shadeRGBColor;
unsigned long tintRGBColor;
unsigned long interpolatedRGBColor;
};
class ColourPalette
{
public:
ColourPalette();
protected:
int mainHue;
int complementOffset;
int hueAngle;
int complementAngle;
int minSaturation;
int maxSaturation;
int minBrightness;
int maxBrightness;
ColourBase cArray[6];
void setMinSaturation(int);
void setMaxSaturation(int);
void setMinBrightness(int);
void setMaxBrightness(int);
void updatePalette();
void setShadeValues(float, float, float , float);
void calculateShadeTint(ColourBase);
void updateInterpolation(ColourBase);
void ColourAtRYBWheel(int, int*);
int lerpInt(int, int, int, int);
int lerpf(int, int, float);
};
#endif
My .cpp file
#include "WProgram.h"
#include "ColorLight.h"
const int rybHueWheel[25] =
{ 0 , 12 , 24 , 30 , 36 , 42 , 48 , 54 , 60 , 72 , 84 , 108, 120, 154, 180, 206, 225, 240, 260, 265, 280, 300, 315, 333, 360 };
ColourBase::ColourBase()
{ interpolation = 0.5;
baseHue = 0;
Serial.println("constructor colourbase");
}
ColourPalette::ColourPalette()
{
//for (int i=0;i<6; i++)
//{ //cArray[i] = ColourBase();
//}
// defaults
mainHue = 5;
complementOffset = 0;
hueAngle = 30;
complementAngle = 30;
minSaturation = 0;
maxSaturation = 255;
minBrightness = 0;
maxBrightness = 255;
updatePalette();
}
// =FUNCTIONS====================================================
void ColourPalette::updatePalette()
{
Serial.println("- function update palette");
cArray[0].baseHue = mainHue;
cArray[1].baseHue = mainHue - hueAngle;
cArray[2].baseHue = mainHue + hueAngle;
cArray[3].baseHue = mainHue + 180 + complementOffset;
cArray[4].baseHue = cArray[3].baseHue - complementAngle;
cArray[5].baseHue = cArray[3].baseHue + complementAngle;
for (int i=0; i<6;i++)
{ // convert to new hue numbers with the new ColorWheel (HueToRYB)
ColourAtRYBWheel(cArray[i].baseHue, cArray[i].wheelHSBColor);
calculateShadeTint(cArray[i]);
updateInterpolation(cArray[i]);
}
}
void ColourPalette::ColourAtRYBWheel(int hue, int c[3])
{
if (hue<0) hue = 360+hue;
hue = hue%360; // stick to 0 - 359
int rybWheelIndex = hue/15; // colourWheel has 24 base colours
int rybWheelAmount = hue%15;
c[0] = lerpInt(rybHueWheel[rybWheelIndex], rybHueWheel[rybWheelIndex+1], rybWheelAmount,15);
c[1] = 255; // saturation
c[2] = 255; // brightness
Serial.println();
Serial.print("- function ColourAtRYBWheel result: ");
Serial.print(c[0],DEC);
Serial.print(",");
Serial.print(c[1],DEC);
Serial.print(",");
Serial.println(c[2],DEC);
}
void ColourPalette::calculateShadeTint(ColourBase c)
{ Serial.println("- function calculateShadeTint");
c.tintHSBColor[0] = c.shadeHSBColor[0] = c.wheelHSBColor[0];
c.shadeHSBColor[1] = minSaturation;
c.tintHSBColor[1] = maxSaturation;
c.shadeHSBColor[2] = minBrightness;
c.tintHSBColor[2] = maxBrightness;
Serial.print(" c.tintHSBColor[0]: ");
Serial.print(c.tintHSBColor[0]);
Serial.print(" c.shadeHSBColor[0]: ");
Serial.println(c.shadeHSBColor[0]);
Serial.print(" c.tintHSBColor[1]: ");
Serial.print(c.tintHSBColor[1]);
Serial.print(" c.shadeHSBColor[1]: ");
Serial.println(c.shadeHSBColor[1]);
Serial.print(" c.tintHSBColor[2]: ");
Serial.print(c.tintHSBColor[2]);
Serial.print(" c.shadeHSBColor[2]: ");
Serial.println(c.shadeHSBColor[2]);
}
void ColourPalette::updateInterpolation(ColourBase c)
{
Serial.println("- function updateInterpolation");
Serial.print(" c.tintHSBColor[0]: ");
Serial.print(c.tintHSBColor[0]);
Serial.print(" c.shadeHSBColor[0]: ");
Serial.println(c.shadeHSBColor[0]);
Serial.print(" c.tintHSBColor[1]: ");
Serial.print(c.tintHSBColor[1]);
Serial.print(" c.shadeHSBColor[1]: ");
Serial.println(c.shadeHSBColor[1]);
Serial.print(" c.tintHSBColor[2]: ");
Serial.print(c.tintHSBColor[2]);
Serial.print(" c.shadeHSBColor[2]: ");
Serial.println(c.shadeHSBColor[2]);
}
int ColourPalette::lerpInt(int a, int b, int f, int max)
{ // f is a number between 0 - max
return a + ( (b-a) * f/max );
}
In the function calculateShadeTint I expect to read the same values as in updateInterpolation.
Below you'll see that that doesn't happen.
What is printed:
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
constructor colourbase
- function update palette
- function ColourAtRYBWheel result: 4,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 4 c.shadeHSBColor[0]: 4
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: -19659 c.shadeHSBColor[0]: -257
c.tintHSBColor[1]: -4910 c.shadeHSBColor[1]: -1265
c.tintHSBColor[2]: 16309 c.shadeHSBColor[2]: -722
- function ColourAtRYBWheel result: 321,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 321 c.shadeHSBColor[0]: 321
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: -19659 c.shadeHSBColor[0]: -257
c.tintHSBColor[1]: -4910 c.shadeHSBColor[1]: -1265
c.tintHSBColor[2]: 16309 c.shadeHSBColor[2]: -722
- function ColourAtRYBWheel result: 26,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 26 c.shadeHSBColor[0]: 26
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: 1536 c.shadeHSBColor[0]: 9734
c.tintHSBColor[1]: 38 c.shadeHSBColor[1]: -18426
c.tintHSBColor[2]: 0 c.shadeHSBColor[2]: 19974
- function ColourAtRYBWheel result: 131,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 131 c.shadeHSBColor[0]: 131
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: 1791 c.shadeHSBColor[0]: 1025
c.tintHSBColor[1]: 1236 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 1061 c.shadeHSBColor[2]: -512
- function ColourAtRYBWheel result: 92,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 92 c.shadeHSBColor[0]: 92
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: 6913 c.shadeHSBColor[0]: 1280
c.tintHSBColor[1]: 614 c.shadeHSBColor[1]: 19459
c.tintHSBColor[2]: -6656 c.shadeHSBColor[2]: 11776
- function ColourAtRYBWheel result: 188,255,255
- function calculateShadeTint
c.tintHSBColor[0]: 188 c.shadeHSBColor[0]: 188
c.tintHSBColor[1]: 255 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 255 c.shadeHSBColor[2]: 0
- function updateInterpolation
c.tintHSBColor[0]: 0 c.shadeHSBColor[0]: 0
c.tintHSBColor[1]: 0 c.shadeHSBColor[1]: 0
c.tintHSBColor[2]: 0 c.shadeHSBColor[2]: 0