42

Why do we always divide our RGB values by 255? I know that the range is from [0-1]. But why dive only by 255? Can anyone please explain me the concepts of RGB values?

Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
Tabish Sohail
  • 1,040
  • 2
  • 9
  • 22
  • 5
    Because there are (usually) 256-color values (0-255), but the framework you happen to be using uses the unit RGB value scale 0-1. At that point it just becomes a simple algebra problem, for which division yields the correct representation of integer RGB in the unit scale. That said, I'm not entirely sure what this has to do with Objective-C. – CodaFi Dec 10 '13 at 05:20
  • 1
    I think there shouldn't be any difference in model accuracy if you divide the image by 255, but It can be useful when you want to use pre-trained weights. 0-1 range is like a standard – Pavlo Sharhan Jul 04 '21 at 20:53

6 Answers6

64

RGB (Red, Green, Blue) are 8 bit each.
The range for each individual colour is 0-255 (as 2^8 = 256 possibilities).
The combination range is 256*256*256.

By dividing by 255, the 0-255 range can be described with a 0.0-1.0 range where 0.0 means 0 (0x00) and 1.0 means 255 (0xFF).

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
Tarun Chaudhary
  • 1,046
  • 2
  • 12
  • 20
12

This is a bit of a generic question since it can be specific to the platform and even to the method. It really comes down to math and getting a value between 0-1. Since 255 is the maximum value, dividing by 255 expresses a 0-1 representation.

Each channel (Red, Green, and Blue are each channels) is 8 bits, so they are each limited to 256, in this case 255 since 0 is included. As the reference shows, systems typically use values between 0-1 when using floating point values.

http://en.wikipedia.org/wiki/RGB_color_model

See Numeric Representations.

These ranges may be quantified in several different ways: From 0 to 1, with any fractional value in between. This representation is used in theoretical analyses, and in systems that use floating point representations. Each color component value can also be written as a percentage, from 0% to 100%. In computers, the component values are often stored as integer numbers in the range 0 to 255, the range that a single 8-bit byte can offer. These are often represented as either decimal or hexadecimal numbers. High-end digital image equipment are often able to deal with larger integer ranges for each primary color, such as 0..1023 (10 bits), 0..65535 (16 bits) or even larger, by extending the 24-bits (three 8-bit values) to 32-bit, 48-bit, or 64-bit units (more or less independent from the particular computer's word size).

Chris
  • 1,663
  • 1
  • 15
  • 19
4

The RGB value goes up from 0 to 255 because it takes up exactly one byte of data. One byte is equal to 8 bits, and each bit represents either a 0 or a 1. This makes 0 in 8 bit binary: 00000000 and 255 11111111. The last bit says if there is a 1 in the value. The second last says if there is a 2 in the value. The third last says if there is a 4 in the value, and so on doubling every time. If you add up all of the small values that are present, you get the total value. For example,

    =10110101
    =1*128 + 0*64 + 1*32 + 1*16 + 0*8 + 1*4 + 0*2 + 1*1
    =128 + 32 + 16 + 4 + 1
    =181

This means that 10110101 in binary equals 181 in decimal form.

ThePiMan
  • 41
  • 1
4

RGB values are usually stored as integers to save memory. But doing math on colors is usually done in float because it's easier, more powerful, and more precise. The act of converting floats to integers is called "Quantization", and it throws away precision.

Typically, RGB values are encoded as 8-bit integers, which range from 0 to 255. It's an industry standard to think of 0.0f as black and 1.0f as white (max brightness). To convert [0, 255] to [0.0f, 1.0f] all you have to do is divide by 255.0f.

If you care, this is the formula to convert back to integer: (int)floor(x * 255.0f + 0.5f). But first clamp x to [0.0f, 1.0f] if necessary.

JayRizzo
  • 3,234
  • 3
  • 33
  • 49
1

It makes the vector operations simpler... Imagine you have image and you want to change its color to red. With vectors you can just take every pixel and multiply it by (1.0, 0.0, 0.0)

P * (1.0, 0.0, 0.0)

Otherwise it just adds unnecessary steps (in this case dividing it by 255)

P * (255, 0, 0) / 255

And imagine using more complex filters, the unnecessary steps would stack up...

WENDYN
  • 650
  • 7
  • 15
0

Given that each Ocket is nowadays made of 8 bits ( binary digit)

Suppose we have an Ocket filled like this :

1   0   1   0   0   1   0   1

for each bit you get 2 possibilities : 0 or 1

2 x 2 x 2 x 2 x 2 x 2 x 2 x 2    = 2^8 = 256

total : 256

And for hexadecimal colors :
given that you have 3 couples of characters, dash excluded => ex: #00ff00
0, 1, 2, 3, 4 , 5, 6, 7, 8, 9, a, b, c, d, e, f = 16 possibilities 

16 x 16 = 256

R      V     B   = color
256  x 256 x 256 = 16 777 216 colors)
marcdahan
  • 2,654
  • 25
  • 25