change max number of LEDs to 200.
Fork of Multi_WS2811 by
Colors.cpp@3:2b5b03a3c0a5, 2014-04-02 (annotated)
- Committer:
- Tomo2k
- Date:
- Wed Apr 02 11:54:27 2014 +0000
- Revision:
- 3:2b5b03a3c0a5
- Parent:
- 0:a8535703f23b
Documentation updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bikeNomad | 0:a8535703f23b | 1 | #include <math.h> |
bikeNomad | 0:a8535703f23b | 2 | #include <mbed.h> |
bikeNomad | 0:a8535703f23b | 3 | #include "Colors.h" |
bikeNomad | 0:a8535703f23b | 4 | |
Tomo2k | 3:2b5b03a3c0a5 | 5 | void Colors::HSBtoRGB(float hue, float saturation, float brightness, uint8_t *pr, uint8_t *pg, uint8_t *pb) |
bikeNomad | 0:a8535703f23b | 6 | { |
bikeNomad | 0:a8535703f23b | 7 | uint8_t r = 0, g = 0, b = 0; |
bikeNomad | 0:a8535703f23b | 8 | if (saturation == 0) { |
bikeNomad | 0:a8535703f23b | 9 | r = g = b = (uint8_t) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 10 | } else { |
bikeNomad | 0:a8535703f23b | 11 | float h = (hue - (float)floor(hue)) * 6.0f; |
bikeNomad | 0:a8535703f23b | 12 | float f = h - (float)floor(h); |
bikeNomad | 0:a8535703f23b | 13 | float p = brightness * (1.0f - saturation); |
bikeNomad | 0:a8535703f23b | 14 | float q = brightness * (1.0f - saturation * f); |
bikeNomad | 0:a8535703f23b | 15 | float t = brightness * (1.0f - (saturation * (1.0f - f))); |
bikeNomad | 0:a8535703f23b | 16 | switch ((int) h) { |
bikeNomad | 0:a8535703f23b | 17 | case 0: |
bikeNomad | 0:a8535703f23b | 18 | r = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 19 | g = (int) (t * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 20 | b = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 21 | break; |
bikeNomad | 0:a8535703f23b | 22 | case 1: |
bikeNomad | 0:a8535703f23b | 23 | r = (int) (q * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 24 | g = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 25 | b = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 26 | break; |
bikeNomad | 0:a8535703f23b | 27 | case 2: |
bikeNomad | 0:a8535703f23b | 28 | r = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 29 | g = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 30 | b = (int) (t * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 31 | break; |
bikeNomad | 0:a8535703f23b | 32 | case 3: |
bikeNomad | 0:a8535703f23b | 33 | r = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 34 | g = (int) (q * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 35 | b = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 36 | break; |
bikeNomad | 0:a8535703f23b | 37 | case 4: |
bikeNomad | 0:a8535703f23b | 38 | r = (int) (t * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 39 | g = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 40 | b = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 41 | break; |
bikeNomad | 0:a8535703f23b | 42 | case 5: |
bikeNomad | 0:a8535703f23b | 43 | r = (int) (brightness * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 44 | g = (int) (p * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 45 | b = (int) (q * 255.0f + 0.5f); |
bikeNomad | 0:a8535703f23b | 46 | break; |
bikeNomad | 0:a8535703f23b | 47 | } |
bikeNomad | 0:a8535703f23b | 48 | } |
bikeNomad | 0:a8535703f23b | 49 | *pr = r; |
bikeNomad | 0:a8535703f23b | 50 | *pg = g; |
bikeNomad | 0:a8535703f23b | 51 | *pb = b; |
bikeNomad | 0:a8535703f23b | 52 | } |
bikeNomad | 0:a8535703f23b | 53 | |
Tomo2k | 3:2b5b03a3c0a5 | 54 | float* Colors::RGBtoHSB(uint8_t r, uint8_t g, uint8_t b, float* hsbvals) |
bikeNomad | 0:a8535703f23b | 55 | { |
bikeNomad | 0:a8535703f23b | 56 | float hue, saturation, brightness; |
bikeNomad | 0:a8535703f23b | 57 | if (!hsbvals) { |
bikeNomad | 0:a8535703f23b | 58 | hsbvals = new float[3]; |
bikeNomad | 0:a8535703f23b | 59 | } |
bikeNomad | 0:a8535703f23b | 60 | uint8_t cmax = (r > g) ? r : g; |
bikeNomad | 0:a8535703f23b | 61 | if (b > cmax) cmax = b; |
bikeNomad | 0:a8535703f23b | 62 | uint8_t cmin = (r < g) ? r : g; |
bikeNomad | 0:a8535703f23b | 63 | if (b < cmin) cmin = b; |
bikeNomad | 0:a8535703f23b | 64 | |
bikeNomad | 0:a8535703f23b | 65 | brightness = ((float) cmax) / 255.0f; |
bikeNomad | 0:a8535703f23b | 66 | if (cmax != 0) |
bikeNomad | 0:a8535703f23b | 67 | saturation = ((float) (cmax - cmin)) / ((float) cmax); |
bikeNomad | 0:a8535703f23b | 68 | else |
bikeNomad | 0:a8535703f23b | 69 | saturation = 0; |
bikeNomad | 0:a8535703f23b | 70 | if (saturation == 0) |
bikeNomad | 0:a8535703f23b | 71 | hue = 0; |
bikeNomad | 0:a8535703f23b | 72 | else { |
bikeNomad | 0:a8535703f23b | 73 | float redc = ((float) (cmax - r)) / ((float) (cmax - cmin)); |
bikeNomad | 0:a8535703f23b | 74 | float greenc = ((float) (cmax - g)) / ((float) (cmax - cmin)); |
bikeNomad | 0:a8535703f23b | 75 | float bluec = ((float) (cmax - b)) / ((float) (cmax - cmin)); |
bikeNomad | 0:a8535703f23b | 76 | if (r == cmax) |
bikeNomad | 0:a8535703f23b | 77 | hue = bluec - greenc; |
bikeNomad | 0:a8535703f23b | 78 | else if (g == cmax) |
bikeNomad | 0:a8535703f23b | 79 | hue = 2.0f + redc - bluec; |
bikeNomad | 0:a8535703f23b | 80 | else |
bikeNomad | 0:a8535703f23b | 81 | hue = 4.0f + greenc - redc; |
bikeNomad | 0:a8535703f23b | 82 | hue = hue / 6.0f; |
bikeNomad | 0:a8535703f23b | 83 | if (hue < 0) |
bikeNomad | 0:a8535703f23b | 84 | hue = hue + 1.0f; |
bikeNomad | 0:a8535703f23b | 85 | } |
bikeNomad | 0:a8535703f23b | 86 | hsbvals[0] = hue; |
bikeNomad | 0:a8535703f23b | 87 | hsbvals[1] = saturation; |
bikeNomad | 0:a8535703f23b | 88 | hsbvals[2] = brightness; |
bikeNomad | 0:a8535703f23b | 89 | return hsbvals; |
bikeNomad | 0:a8535703f23b | 90 | } |