fork download
  1. //
  2. // Some C code I used to generate the values->colors map. I didn't run this code on the Arduino. I used ideone.com
  3. // and then pasted the output into my arduino code.
  4.  
  5. #include <iostream>
  6. #include <stdint.h>
  7. #include <math.h>
  8. using namespace std;
  9.  
  10.  
  11.  
  12. const int numValues = 64; // number of colors in our output array. This should correspond
  13. // to the max value you want to display.
  14. double breakPoint = log10(numValues)/2.0;
  15.  
  16. //
  17. // Blend two colors together based on the ratio. ratio of 0.0 will be 100% color a and
  18. // ratio of 1.0 will be 100% color b.
  19. uint32_t blend (uint32_t ina, uint32_t inb, double ratio)
  20. {
  21. int r = (((ina >> 16) & 0xff) * (1.0-ratio)) + (((inb >> 16) & 0xff) * ratio);
  22. int g = (((ina >> 8) & 0xff) * (1.0-ratio)) + (((inb >> 8) & 0xff) * ratio);
  23. int b = (((ina >> 0) & 0xff) * (1.0-ratio)) + (((inb >> 0) & 0xff) * ratio);
  24. return ((r << 16) | (g << 8) | b);
  25. }
  26.  
  27. //
  28. // Scale the intensity of the passed in color. I am using max brightness colors and
  29. // 0.0 - 1.0 as the scale value.
  30. uint32_t scale (uint32_t ina, double scale_value)
  31. {
  32. int r = ((ina >> 16) & 0xff) * scale_value;
  33. int g = ((ina >> 8) & 0xff) * scale_value;
  34. int b = ((ina >> 0) & 0xff) * scale_value;
  35. return ((r << 16) | (g << 8) | b);
  36. }
  37.  
  38. //
  39. // Fade blue -> green -> red. I've built in a logarithmic response to make it more of a
  40. // dB meter.
  41. int main() {
  42. for (int i = 1; i <= numValues; i++)
  43. {
  44. double logValue = log10(i);
  45. double scaleValue = log10(i+2) / log10(numValues+2);
  46. double ratio = (logValue < breakPoint) ? logValue / breakPoint : (logValue - breakPoint) / breakPoint;
  47. uint32_t color = 0;
  48.  
  49. if (logValue < breakPoint)
  50. color = blend (0x0000ff, 0x00ff00, ratio);
  51. else
  52. color = blend (0x00ff00, 0xff0000, ratio);
  53. color = scale (color, scaleValue);
  54. cout << "0x" << hex << color << ", ";
  55. }
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
0x42, 0x1c37, 0x332e, 0x4823, 0x5b1a, 0x6c11, 0x7c08, 0x8c00, 0x88900, 0x108600, 0x178300, 0x1e8100, 0x267e00, 0x2d7b00, 0x347700, 0x3a7400, 0x407100, 0x466e00, 0x4d6b00, 0x526800, 0x586500, 0x5e6200, 0x636000, 0x685d00, 0x6d5a00, 0x725700, 0x775400, 0x7c5100, 0x804f00, 0x864c00, 0x8a4900, 0x8f4600, 0x924400, 0x974100, 0x9b3f00, 0x9f3c00, 0xa33a00, 0xa83700, 0xab3500, 0xaf3200, 0xb33000, 0xb72e00, 0xbb2b00, 0xbe2900, 0xc12700, 0xc52400, 0xc92200, 0xcc2000, 0xd01e00, 0xd31c00, 0xd71900, 0xda1700, 0xdc1500, 0xe01300, 0xe31100, 0xe60f00, 0xe90d00, 0xec0b00, 0xf00800, 0xf30600, 0xf60400, 0xf90200, 0xfc0000, 0xff0000,