Ned Konz / Multi_WS2811

Dependents:   Multi_WS2811_test

Committer:
bikeNomad
Date:
Thu Jun 11 15:24:41 2015 +0000
Revision:
1:86a910560879
Parent:
0:a8535703f23b
changed WS2811 to template class to allow for lower memory usage.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 0:a8535703f23b 1 #include <math.h>
bikeNomad 0:a8535703f23b 2 #include "Colors.h"
bikeNomad 0:a8535703f23b 3
bikeNomad 0:a8535703f23b 4 void HSBtoRGB(float hue, float saturation, float brightness, uint8_t *pr, uint8_t *pg, uint8_t *pb)
bikeNomad 0:a8535703f23b 5 {
bikeNomad 0:a8535703f23b 6 uint8_t r = 0, g = 0, b = 0;
bikeNomad 0:a8535703f23b 7 if (saturation == 0) {
bikeNomad 0:a8535703f23b 8 r = g = b = (uint8_t) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 9 } else {
bikeNomad 0:a8535703f23b 10 float h = (hue - (float)floor(hue)) * 6.0f;
bikeNomad 0:a8535703f23b 11 float f = h - (float)floor(h);
bikeNomad 0:a8535703f23b 12 float p = brightness * (1.0f - saturation);
bikeNomad 0:a8535703f23b 13 float q = brightness * (1.0f - saturation * f);
bikeNomad 0:a8535703f23b 14 float t = brightness * (1.0f - (saturation * (1.0f - f)));
bikeNomad 0:a8535703f23b 15 switch ((int) h) {
bikeNomad 0:a8535703f23b 16 case 0:
bikeNomad 0:a8535703f23b 17 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 18 g = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 19 b = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 20 break;
bikeNomad 0:a8535703f23b 21 case 1:
bikeNomad 0:a8535703f23b 22 r = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 23 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 24 b = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 25 break;
bikeNomad 0:a8535703f23b 26 case 2:
bikeNomad 0:a8535703f23b 27 r = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 28 g = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 29 b = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 30 break;
bikeNomad 0:a8535703f23b 31 case 3:
bikeNomad 0:a8535703f23b 32 r = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 33 g = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 34 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 35 break;
bikeNomad 0:a8535703f23b 36 case 4:
bikeNomad 0:a8535703f23b 37 r = (int) (t * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 38 g = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 39 b = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 40 break;
bikeNomad 0:a8535703f23b 41 case 5:
bikeNomad 0:a8535703f23b 42 r = (int) (brightness * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 43 g = (int) (p * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 44 b = (int) (q * 255.0f + 0.5f);
bikeNomad 0:a8535703f23b 45 break;
bikeNomad 0:a8535703f23b 46 }
bikeNomad 0:a8535703f23b 47 }
bikeNomad 0:a8535703f23b 48 *pr = r;
bikeNomad 0:a8535703f23b 49 *pg = g;
bikeNomad 0:a8535703f23b 50 *pb = b;
bikeNomad 0:a8535703f23b 51 }
bikeNomad 0:a8535703f23b 52
bikeNomad 0:a8535703f23b 53 float* RGBtoHSB(uint8_t r, uint8_t g, uint8_t b, float* hsbvals)
bikeNomad 0:a8535703f23b 54 {
bikeNomad 0:a8535703f23b 55 float hue, saturation, brightness;
bikeNomad 0:a8535703f23b 56 if (!hsbvals) {
bikeNomad 0:a8535703f23b 57 hsbvals = new float[3];
bikeNomad 0:a8535703f23b 58 }
bikeNomad 0:a8535703f23b 59 uint8_t cmax = (r > g) ? r : g;
bikeNomad 0:a8535703f23b 60 if (b > cmax) cmax = b;
bikeNomad 0:a8535703f23b 61 uint8_t cmin = (r < g) ? r : g;
bikeNomad 0:a8535703f23b 62 if (b < cmin) cmin = b;
bikeNomad 0:a8535703f23b 63
bikeNomad 0:a8535703f23b 64 brightness = ((float) cmax) / 255.0f;
bikeNomad 0:a8535703f23b 65 if (cmax != 0)
bikeNomad 0:a8535703f23b 66 saturation = ((float) (cmax - cmin)) / ((float) cmax);
bikeNomad 0:a8535703f23b 67 else
bikeNomad 0:a8535703f23b 68 saturation = 0;
bikeNomad 0:a8535703f23b 69 if (saturation == 0)
bikeNomad 0:a8535703f23b 70 hue = 0;
bikeNomad 0:a8535703f23b 71 else {
bikeNomad 0:a8535703f23b 72 float redc = ((float) (cmax - r)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 73 float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 74 float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin));
bikeNomad 0:a8535703f23b 75 if (r == cmax)
bikeNomad 0:a8535703f23b 76 hue = bluec - greenc;
bikeNomad 0:a8535703f23b 77 else if (g == cmax)
bikeNomad 0:a8535703f23b 78 hue = 2.0f + redc - bluec;
bikeNomad 0:a8535703f23b 79 else
bikeNomad 0:a8535703f23b 80 hue = 4.0f + greenc - redc;
bikeNomad 0:a8535703f23b 81 hue = hue / 6.0f;
bikeNomad 0:a8535703f23b 82 if (hue < 0)
bikeNomad 0:a8535703f23b 83 hue = hue + 1.0f;
bikeNomad 0:a8535703f23b 84 }
bikeNomad 0:a8535703f23b 85 hsbvals[0] = hue;
bikeNomad 0:a8535703f23b 86 hsbvals[1] = saturation;
bikeNomad 0:a8535703f23b 87 hsbvals[2] = brightness;
bikeNomad 0:a8535703f23b 88 return hsbvals;
bikeNomad 0:a8535703f23b 89 }