We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there!
I was questioning the values of my pixel read out sketch and found that the values looked odd (negative). Therefore, I followed an old discussion from the previous forum but still do not understand some basic principles here, and discussed in this old discussion (https://forum.processing.org/one/topic/pixel-arrays-and-integers.html).
Is there anybody who can help me out? 1) WHY is full white -1 and not just 0 and black being 255, or the other way around? Is this some specific special case for processing or is this general when using RGB alpha 8-bit? 2) If I understoot the above (1), then I could understand why white is represented by 2147483647 = 2^31-1.
I'm sorry, I couldn't find it in the FAQ as suggested in the discussion and feel stupid about it. Thanks in advance!
Regards, Melanie
Answers
https://forum.processing.org/two/discussion/8086/what-is-a-color-in-processing
it's mostly to do with how java represents negative numbers - if the top bit is a 1 then the number is treated as negative. google 'two's complement'
using two's complement and 32 bits then -1 is 11111111 11111111 11111111 11111111
splitting that into 4 parts of 8 bits each the first part is the transparency (255 = solid colour) and the next 3 parts are the red, green and blue channels respectively. 255 = full on for each. which is white.
i would steer away from using -1 for white. use color(255, 255, 255) or color(#ffffff) instead, they are all clearer. why write code that's hard to decipher?
(or color(255) - if the given colour is between 0 and 255 then it is taken as a greyscale value with each of r, g and b being set to the given value. above 255 and it's taken as an integer and it deciphers is as a 32 bit ARGB value, as above)
A negative value is just a representation of the current data type being used as mentioned in previous comments. I would like to add that if Processing were using unsigned data types, you wouldn't have a negative value for white. Unsigned values, by definition, cannot handle negative values. However, we need to remember that there is no need to use unsigned for this. There is nothing wrong with negative values. If you switch into hexadecimal mode (aka. 0xffffffff) then there is no need to think about a sign at all (when thinking about color).
Kf
Thanks both for answering, that really cleared things up. I was not aware of the two's complement system and still not really understand how it speeds up calculations, but I'll drop that issue for now. Good to know what color value's represent and how binary relate to hexadecimals and signed values. Thanks!!
This is precisely the type of place where a little confusion occurs because of the way Processing handles things. If Processing had just stuck to the Java Color class, things would've been easier to understand, especially for beginners. Many beginners are even under the impression that color is a completely different data type. This leads to them not understanding some of the error messages that pop up. This also means that we cannot use the word "color" to name variables.
Besides using the Color class approach:
http://docs.Oracle.com/javase/8/docs/api/java/awt/Color.html
In order to avoid the negative values, Processing API would need to pick
long
instead ofint
:But that'd double the total size of PImage's pixels[] b/c
long
is 64-bit andint
32-bit: :-SSMaybe the Color class approach would be even worse than
long
! :-&Color approach would obviously be worse than
int
and perhaps even worse thanlong
because it's not a primitive.But clearly, there would be no point in using
long
- most beginners would still not clearly understand why the values end up as they are. In fact, most will think that color actually stores the three different values in three different variables.