I'm using the FastLED library to control a digital RGB LED strip.

While this library seems very comprehensive in capability and ease of use, I do not yet see a way to simply set the brightness value of a individual pixel on an absolute scale. In other words, I want to simply set the brightness of a pixel, without passing a hue or saturation, and without it effecting other pixels.

FastLED.setBrightness() scales down the brightness of all pixels.
leds[i].fadeLightBy() scales down an individual pixel, but if it's called again for the same pixel before updating the LEDs, then the pixel will be faded again from it's already-faded value.

Is there truly no built-in method for setting the brightness of individual pixels to a particular value?


There is no dedicated function to do this, because you can just do exactly this by using the HSV color model. Hue (H) controls the color it self, S stands for saturation of the color and V is called lightness or value. That is what you try to control. Just set the LEDs color to a HSV value and change it's V value:

leds[i] = HSV(color, saturation, brightness);

While color and saturation are staying the same (to keep the same color), you can change the brightness directly with the thirds value. Note, that leds[i] is still defined as CRGB, not as HSV. This assigment uses an implicit conversion, that is defined in the library.

You can see the usage of HSV in the Cylon example of the FastLED library. There it is the Hue value, that get's changed, but it is the same principle with the brightness.

Now there is a problem to solve: The FastLED library can convert HSV colors to RGB colors, but not the other way round. So you cannot simply get the hue and saturation of an RGB color with this library. You can go 2 ways here:

  1. You can just define an extra LEDs array with HSV colors and synchronize both arrays to contain the same data (everytime you want to change a color, you would set a HSV color in the second array, convert that array to RGB by using CRGB(hsv_leds[i]) and save that in the first array, which get's used by the FastLED library). This is relatively easy, but you effectively need double the memory for storing the color values. This is not an issue, if you have enough memory or few enough LEDs to control.

  2. You can write your own function to convert RGB values to HSV, then use it to access the hue and saturation values of the targeted RGB color, create a new HSV color with it and the new brightness and use it to set the RGB values for the FastLED library. In this forum thread there is a brief discussion about that, though it leaves you with the implementation yourself. I think there are suitable implementations, that can be found on the web. A quick search gave me this code from github gist. I have not tested it, so I cannot say, if it really works correctly, but you should give it a try. I'm sure there are also other implementations.

  • I realize that HSV let's you pass brightness, however I have applications that call for changing just brightness alone, given a certain trigger. It seems to me now that the solution is to do something like the following pseudo code: leds[i] = HSV(leds[i].color, leds[i].saturation, 127); so that I can manipulate the brightness to 127 without knowing offhand the current color, saturation, or brightness of that pixel at that time. I'll have to look around for the actual syntax of it. I certainly hope it can be done that way. – Bort Oct 27 '19 at 20:03
  • @Bort I added a short explanation for that topic. – chrisl Oct 27 '19 at 21:11
  • I think your answer is misleading when you say "you can just do exactly this" because I think your answer then goes on to explain that I cannot do that and I have to do something else. For your solution #1, why would I need to convert to RGB when the library already takes HSV as input? I'll try #2 in a bit. – Bort Oct 28 '19 at 13:15
  • It's more of a "you can do exactly this, but it is a bit more complicated, than you wanted", since you can control the brightness of a single pixel with this, but it is more complicated than simply using one dedicated inbuild function. About #1: All examples, that I have seen, use CRGB as the array type, that is passed to the FastLED constructor. I tried using the construcktor with the HSV type, but that resulted in a corresponding compilation error. Thus I deducted, that the library works only with RGB, but also gives you HSV, which can then be converted to RGB – chrisl Oct 28 '19 at 13:28
  • That deduction may be wrong. If you are able to directly use HSV for the array, please tell us and I will change my answer. – chrisl Oct 28 '19 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.