0

getPixelColor does not work if you've setBrightness
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 6:02 am

I've been trying to retrieve the color of a pixel using the getPixelMethod and it kept failing. It appeared to always return a black value. I had previously used the setBrightness method to turn down the intensity of the pixels. When I eliminated that setBrightness call the getPixelColor call works correctly.

Is it true, as my testing seems to indicate, that if you call setBrightness then any subsequent call to getPixelColor will in fact NOT return the color, but will instead always return a "black" value?

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Re: getPixelColor does not work if you've setBrightness

by adafruit_support_bill on Fri Mar 06, 2015 7:28 am

GetColor returns the current color of the pixel. If you reduce the intensity to zero, the current color of the pixel is black.

adafruit_support_bill
 
Posts: 82158
Joined: Sat Feb 07, 2009 10:11 am

Re: getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 5:33 pm

My call to setBrightness is setting the value to 128 (half brightness), NOT setting it to zero. I would expect that the value returned by getPixelColor would return a color value that's about half of what it was set to originally. OR, perhaps it might return the full actual color value, with the brightness influenceonly happening out at the actual pixel and NOT in the internally saved array of color values since, after all, you can modify setBrightness numerous times and it (should) always operate on theoriginal full color value.

BUT, no matter what value I setBrightness to, when I subsequently call getPixelColor it returns ZERO instead of the pixel's actual color value.

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Re: getPixelColor does not work if you've setBrightness

by adafruit_support_bill on Fri Mar 06, 2015 6:19 pm

since, after all, you can modify setBrightness numerous times and it (should) always operate on theoriginal full color value.

This is not the case. setbrightness alters the pixel color directly. GetPixelColor attempts to re-constitute the original, but it is a lossy function. It cannot restore a color that has been set to brightness zero.

BUT, no matter what value I setBrightness to, when I subsequently call getPixelColor it returns ZERO instead of the pixel's actual color value.

Please post your code. I am not able to reproduce this problem.

adafruit_support_bill
 
Posts: 82158
Joined: Sat Feb 07, 2009 10:11 am

Re: getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 6:29 pm

So, if I setBrightness to zero at one point and then later setBrightness to 255 (the brightest brightness value) then the subsequent setBrightness will actually do nothing at all since all the pixel colors will have been set to zero by the prior call? That doesn't sound right.

As for my code, here's basically what I'm doing:

#define COLOR_RED pPixels->Color(255,0,0)

pPixels->setBrightness(128); // Half brightness
pPixels->setPixelColor(0, COLOR_RED);
pPixels->show();
uint32_t Color;
Color = pPixels->getPixelColor(0);
if (Color != COLOR_RED)
{
// Indicate an error.
}

If I remove the call to setBrightness then there is no error indicated.

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Re: getPixelColor does not work if you've setBrightness

by adafruit_support_bill on Fri Mar 06, 2015 6:43 pm

So, if I setBrightness to zero at one point and then later setBrightness to 255 (the brightest brightness value) then the subsequent setBrightness will actually do nothing at all since all the pixel colors will have been set to zero by the prior call?

That is correct. As documented here: https://learn.adafruit.com/adafruit-neo ... no-library
You can’t move from a lower brightness to a higher setting without some loss in fidelity. Certain animation effects are better served by leaving the brightness at max and calling setPixel() repeatedly to fill the strip.


Code: Select all | TOGGLE FULL SIZE
if (Color != COLOR_RED)
{
// Indicate an error.
}


You have defined COLOR_RED as 255,0,0 which is full-intensity red. Then you have decreased the intensity to half that. So (Color != COLOR_RED) evaluates to true because the color of the pixel is no longer full intensity red.

adafruit_support_bill
 
Posts: 82158
Joined: Sat Feb 07, 2009 10:11 am

Re: getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 6:43 pm

Here's a better example:

pPixels->setBrightness(128); // Half brightness
pPixels->setPixelColor(0, COLOR_RED);
pPixels->show();
uint32_t RetrievedColor;
RetrievedColor = pPixels->getPixelColor(0);
pPixels->setPixelColor(1, RetrievedColor);
pPixels->show(); // The second pixel comes out BLACK instead of RED.

In this one, which is closer to the original code where I initially encountered the problem, I set a color, retrieve that color, then set ANOTHER pixel to that color. I expect the second pixel to have the same color as the first (regardless of whether the brightness has been set), but the second pixel always comes out black, with no color at all.

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Re: getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 6:54 pm

Looking at the actual assembly code for setPixelColor and getPixelColor it appears that setPixelColor does indeed modify the color value that's stored in the internal array so that it's reduced per the brightness value. And then when getPixelColor is called it retrieves the stored (brightness-diminished) color value and un-does the brightness modification so that you get the original value back (supposedly).

But my experience is that you do NOT actually get the restored, original color value back. Instead, you get black (all zeroes).

I'm still looking at that assembly code to see if I can figure out what's actually happening.

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Re: getPixelColor does not work if you've setBrightness

by RogerInHawaii on Fri Mar 06, 2015 7:11 pm

I may have figured out the problem, and it has to do with what setBrightness actually means. I had assumed that setBrightness uses an 8-bit value in which 0 means to set the brightness of the color to 0 (i.e. not showing at all), that 255 means to essentially NOT modify the color value at all, leaving it at precisely whatever color it was initially set at (i.e. the full brightness of the original color) and that any value between 0 and 255 would modify the color value DOWN to the specified amount. Thus, setBrightness REDUCES the brightness of the colors.

But that apparently is NOT what setBrightness does. It appears (now that I've looked through the actual assembly code) that setBrightness attempts to INCREASE the brightness of the colors. From the code for setPixelColor:

r = (r * brightness) >> 8;
g = (g * brightness) >> 8;
b = (b * brightness) >> 8;

i.e. it's MULTIPLYING the specified color values by the brightness value. And in getPixelColor it does:

c_ptr[0] = (c_ptr[0] << 8)/brightness;
c_ptr[1] = (c_ptr[1] << 8)/brightness;
c_ptr[2] = (c_ptr[2] << 8)/brightness;

which DIVIDES the (previously multiplied) color value in an attempt to restore it back to whatever color value was initially set. But that multiply and divide can result in the returned value being significantly different than the original value, dues to loss during the multiply and divide operations.

So, maybe some clarification is due as to what setBrightness is intended to do.

RogerInHawaii
 
Posts: 63
Joined: Wed Oct 22, 2014 1:27 am

Please be positive and constructive with your questions and comments.