Is there a way to manipulate CRGB() as a whole as opposed to manipulating

Is there a way to manipulate CRGB() as a whole as opposed to manipulating each R, G, and B value separately then feeding them back into CRGB()?

For example, if I set a pixel to CRGB(200, 200, 200) then I want to half those values down to CRGB(100, 100, 100), then later again to CRGB(50, 50, 50) … can I do that to CRGB() as a whole, or do I keep a separate variable that I feed back into CRGB(), ala x = 200; CRGB(x, x, x); Then x >>= 1 and feed it back into CRGB() again …

https://code.google.com/p/fastspi/wiki/CRGBreference#Color_Math

If you want to halve everything, it’s as easy as saying leds[x] /= 2; If you want to scale everything to, say, 80% of its current value you can do leds[x] %= 204; etc… etc…

Great question. The answer is yes, there are a whole bunch of methods defined on the CRGB class that let you manipulate it as ‘a color’, instead of a collection of three channel values (R, G, B). This page https://code.google.com/p/fastspi/wiki/CRGBreference has reasonably up-to-date documentation.

As for your particular question, “How can I cut the brightness of a CRGB by half, in a single step?”, check out https://code.google.com/p/fastspi/wiki/CRGBreference#Dimming_and_Brightening_Colors

Here are some ways you could do it; I’m going to assume that “leds[i]” is the color in question

// Use the “/=” operator to divide by a constant
leds[i] /= 2; // divide each channel by two

// Use fadeToBlackBy(X) which takes a fade value
// from 0 (no fading) to 255 (total fading)
leds[i].fadeToBlackBy( 128); // fade by 50%
// Note repeatedly using this will fade to black
// Same as leds[i].nscale8( 255 - X );

// Use fadeLightBy(X) which takes a fade value
// from 0 (no fading) to 255 (maximum fading)
leds[i].fadeLightBy( 128); // fade by 50%
// Note repeatedly using this will NOT fade to full black!
// Same as leds[i].nscale8_video( 255 - X);

// Use operator %=, which scales light down
// to a ‘percentage’ of the old value, except that
// the ‘percentage’ is from 0-255, not 0-99:
leds[i] %= 128; // fade to 50% of previous value
// Same as leds[i].nscale8_video( X );

There are a bunch of other “color math” functions that operate on a whole CRGB at a time; most of them are smaller and faster than writing the code out ‘longhand’. If you find a case where that’s not true, please let me know. And if you find a ‘missing function’, something that you’d expect given what we already provide, please speak up about that too.

Finally, please let me know whether or not this helps you get where you wanted to go, and if the methods provided make your code faster to write. That’s one of the things we’re shooting for.

That’s exactly the stuff I needed! Woo, thanks!

To both of you!

Mark and Daniel, thanks for these details - I’ve been getting to grips with V2, and must admit I hadn’t read the CRGB Wiki in detail, so I had missed a lot of tricks. I’ve been wondering why my 0-360 degree rainbow scroll code was flickering on repeat, now changed to 0-255 and its looking great!

Also, setting the brightness, genius :slight_smile: I was about to embark on a load of coding to do that, as I like to run LEDs at a lower brightness to save power and make them more gentle for night time lighting, but again the functions in the library have saved me a lot of work.

I’d like to answer your question about speed of coding; so far, after the initial learning curve, I am finding it a lot easier to knock together code quickly using the inbuilt functions. fill_rainbow is currently my favourite! Although I’m an experienced software developer, I am still getting to grips with the Arduino and its limited memory, so having all the LED functions ready-made is helping me concentrate on learning the techniques of developing for the Arduino.

Right, I’m going to carry on coding now. We are suffering 80mph+ winds here in the UK tonight, but I’ll keep going until the power cuts out :slight_smile:

Good luck with the wind!
And thanks for the feedback; it helps a lot!

Cheers!

I understand how to dim the leds down how do you brighten them back up?
Thanks

If you’re trying to dim the whole string and re-brighten it, look at FastLED.setBrightness( n ). It adjusts the brightness of the whole strip up and down.

The problem with dimming and re-brightening individual pixels is that some data is lost. Consider: dimming a value of “200” down to 10% gives “20”. But dimming 201, 202, 203… and 209 down to 10% also gives a value of “20”. And one you’ve got “20” and you want to brighten it back up again, you can’t tell if this “20” used to be 200 or 203 or 209.

There is a method called maximizeBrightness(), with will try to make the pixel in question be as bright as possible while keeping the same hue (more or less).
leds[i].maximizeBrightness();
It’s probably not what you want.

You can always multiply the R, G, and B channels each separately by some “scale up” factor, like 1.5. To keep that fast and to prevent wrap-around, you could do this, which could be read as “make the red channel be the sum of what it is now and half that much again, clamping the result to no more than 255.”

leds[i].r = qadd8( leds[i].r, leds[i].r / 2);

You’d want to do the same thing for the green and blue channels for each pixel, too.

Any of this help?

The issue I have is that have I splitting the string into segments which can be different colors and brightness. So would like to control each of the segments independently up and down. Is there a better way to do this other than controlling the individuals pixels?

Ahhh, OK, I think I get it. So what I’d do, if I understand what you’re doing correctly, is keep a separate array of what the brightness and color should be for each segment; don’t try to use the ‘leds’ array itself as the main place to store that information. Then write a routine that sets each pixel in each segment to the right color and brightness.

Do you have a ‘fixed’ number of segments, or does it change dynamically?

Basically, I’d make one CRGB (or CHSV) object for each segment, and then every time you want to update the frame, use a loop to copy that value into each pixel in the corresponding segment.

(I’m recovering from a cold, and my head is full of decongestants etc., otherwise I’d write more explicit code for you tonight.)

What about calculating the color value you need, then using fill_solid() to fill that segment of the string accordingly? For example:

fill_solid(&(leds[SEGMENT_START]), SEGMENT_LENGTH, CRGB(r, g, b));
( or CHSV(h, s, v) )

Yes the segments are fixed. I like the idea of calculating the color value but I don’t know how to keep the color linear from bright to dark

Thanks, Ashley. That’s what I was trying to say.

I’d also do this:

typedef struct {
int mStart;
int mLength;
CRGB mColor;
} TSegment;

And then declare an array of them:

TSegment segments[7]; // or however many

Then you could loop over the segments, and do the fill_solid as described above.

Again, forgive my muddled explanations tonight; I blame Nyquil.

As for color, try using a Hue-Saturation-Value color instead of an RGB color for the ‘color’ of each segment. Then you can just adjust the Value (brightness) up and down, while keeping the Hue the same.
See https://code.google.com/p/fastspi/wiki/CRGBreference#Setting_HSV_Colors

I have looked at that as well is there a way to convert a color (RGB) to hue? I know they use a system that is limited to 255 not the standard of 360?

There’s no built-in RGB-to-HSV conversion function right now, no. It’s on the list to add in a subsequent release of the library.

Are you trying to convert specific colors so you can set them up? (e.g., what’s the hue for Orange?) Or are you trying to do RGB-to-HSV in code? Where are your color values coming from originally?

I’m trying to setup a specific colors

Well, this page https://code.google.com/p/fastspi/wiki/CRGBreference#Setting_HSV_Colors
has a few of the ‘cardinal points’ of the color wheel listed:
Red (0…)
Orange (32…)
Yellow (64…)
Green (96…)
Aqua (128…)
Blue (160…)
Purple (192…)
Pink(224…)
You can interpolate between them, or just experiment.

I really should make a color wheel picture or something. Or provide the RGB-to-HSV function. Those are both already on the to-do list for after the “2.0” release; sorry we don’t have them available yet!

thanks for all your help!!!