Simple 8x8 LED Matrix controller which interfaces with a Processing GUI over serial to display sketches
Fork of Multi_WS2811 by
Colors.cpp
- Committer:
- antoniorohit
- Date:
- 2014-11-15
- Revision:
- 2:8adaf48544cc
- Parent:
- 0:a8535703f23b
File content as of revision 2:8adaf48544cc:
#include <math.h>
#include <mbed.h>
#include "Colors.h"
void HSBtoRGB(float hue, float saturation, float brightness, uint8_t *pr, uint8_t *pg, uint8_t *pb)
{
uint8_t r = 0, g = 0, b = 0;
if (saturation == 0) {
r = g = b = (uint8_t) (brightness * 255.0f + 0.5f);
} else {
float h = (hue - (float)floor(hue)) * 6.0f;
float f = h - (float)floor(h);
float p = brightness * (1.0f - saturation);
float q = brightness * (1.0f - saturation * f);
float t = brightness * (1.0f - (saturation * (1.0f - f)));
switch ((int) h) {
case 0:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (t * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 1:
r = (int) (q * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (p * 255.0f + 0.5f);
break;
case 2:
r = (int) (p * 255.0f + 0.5f);
g = (int) (brightness * 255.0f + 0.5f);
b = (int) (t * 255.0f + 0.5f);
break;
case 3:
r = (int) (p * 255.0f + 0.5f);
g = (int) (q * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 4:
r = (int) (t * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (brightness * 255.0f + 0.5f);
break;
case 5:
r = (int) (brightness * 255.0f + 0.5f);
g = (int) (p * 255.0f + 0.5f);
b = (int) (q * 255.0f + 0.5f);
break;
}
}
*pr = r;
*pg = g;
*pb = b;
}
float* RGBtoHSB(uint8_t r, uint8_t g, uint8_t b, float* hsbvals)
{
float hue, saturation, brightness;
if (!hsbvals) {
hsbvals = new float[3];
}
uint8_t cmax = (r > g) ? r : g;
if (b > cmax) cmax = b;
uint8_t cmin = (r < g) ? r : g;
if (b < cmin) cmin = b;
brightness = ((float) cmax) / 255.0f;
if (cmax != 0)
saturation = ((float) (cmax - cmin)) / ((float) cmax);
else
saturation = 0;
if (saturation == 0)
hue = 0;
else {
float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
if (r == cmax)
hue = bluec - greenc;
else if (g == cmax)
hue = 2.0f + redc - bluec;
else
hue = 4.0f + greenc - redc;
hue = hue / 6.0f;
if (hue < 0)
hue = hue + 1.0f;
}
hsbvals[0] = hue;
hsbvals[1] = saturation;
hsbvals[2] = brightness;
return hsbvals;
}
