Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Colors.cpp
- Revision:
- 0:a8535703f23b
- Child:
- 1:86a910560879
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Colors.cpp Sat Jan 04 00:40:08 2014 +0000
@@ -0,0 +1,90 @@
+#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;
+}
Generic WS2811/WS2812