Referring to old forum question: how can the value of a pixel be negative?

edited February 2017 in Programming Questions

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

Tagged:

Answers

  • Answer ✓

    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'

  • edited February 2017 Answer ✓

    WHY is full white -1

    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)

  • Answer ✓

    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.

  • ... and still not really understand how it speeds up calculations, ...

    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 of int:

    1. https://Processing.org/reference/long.html
    2. https://Processing.org/reference/int.html

    But that'd double the total size of PImage's pixels[] b/c long is 64-bit and int 32-bit: :-SS

    1. https://Processing.org/reference/PImage.html
    2. https://Processing.org/reference/PImage_pixels.html

    Maybe the Color class approach would be even worse than long! :-&

  • Color approach would obviously be worse than int and perhaps even worse than long 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.

Sign In or Register to comment.