1

I'm using a potentiometer that outputs a value 0-255. What I want it to do is change the color of an RGB LED in a way that contains 256 steps which will show all colors on the LED with as much precision you can get.

Question: How would I convert that single value (0-255) to an rgb code that I can apply to the LED?

The most obvious solution is to create a dictionary with all 256 possible values and manually assing RGB codes to those values. I don't want to do this and I'm trying to find a more mathematical solution.

Ciprum
  • 734
  • 1
  • 11
  • 18
  • What sort of precision do you want? What sort of color map are you looking for? You may want to take a look at http://matplotlib.org/users/colormaps.html first, as determining how you want the three colors to map to your set of 256 values affects what mathematical equations to use. – JAB Sep 06 '16 at 20:53
  • What are the ranges for each LED - also 0 to 255? – stdunbar Sep 06 '16 at 20:54
  • rgb values are a triplet of values from 0 to 255. So your single 0-255 range would have to be mapped somehow – joel goldstick Sep 06 '16 at 20:55
  • Please explain what format does the LED take values? Is it analog or digital, if analog means what range? if digital means how many bits? With out these info question is not complete. – Sreekanth Karumanaghat Sep 06 '16 at 21:07
  • 1
    @SreekanthKarumanaghat My LED accepts digital input, but I use PWM to change it's intensity. For the sake of the question lets's say my LED accepts a value 0-255 for each of red, green and blue diodes. – Ciprum Sep 06 '16 at 21:19

1 Answers1

1

Use one of the matplotlib colormaps. I would recommend the jet colormap since most people are familiar with the interpretation that blue means small values and red means large values.

from matplotlib import cm

pot_values = [0, 51, 102, 153, 204, 255]
rgb = []
for x in pot_values:
    val = cm.jet(float(x)/255)[:3]  # The 4th element is gamma
    rgb.append([round(x*255) for x in val])

print(rgb)

# Output:
# [[0.0, 0.0, 128.0],
#  [0.0, 76.0, 255.0],
#  [41.0, 255.0, 206.0],
#  [206.0, 255.0, 41.0],
#  [255.0, 104.0, 0.0],
#  [128.0, 0.0, 0.0]]
Chris Mueller
  • 6,490
  • 5
  • 29
  • 35
  • That's exactly what I was looking for. I can say that matplotlib is probably the library I least expected to have this feature. – Ciprum Sep 06 '16 at 21:27
  • 1
    @Janekmuric The nice thing about using `matplotlib` for this is that it is relatively easy to try out different colormaps by switching from `cm.jet` to `cm.cool`, `cm.nipy_spectral`, `cm.rainbow`, etc. – Chris Mueller Sep 06 '16 at 21:32