49

An RGB color is composed of three components: Red (0-255), Green (0-255) and Blue (0-255).

What exactly is BGR color space? How is it different from RGB color space?

4 Answers 4

52

Its about endianness.

RGB is a byte-order. But a deliberate implementation choice of most vanilla Graphics libraries is that they treat colours as unsigned 32-bit integers internally, with the three (or four, as alpha is typically included) components packed into the integer.

On a little-endian machine (such as x86) the integer 0x01020304 will actually be stored in memory as 0x04030201. And thus 0x00BBGGRR will be stored as 0xRRGGBB00!

So the term BGR (and BGRA etc) is a leaky abstraction where the graphics library is explaining how the integer is logically ordered, so as to make your code that is directly accessing the colour components individually more readable.

Remember that bitmaps are usually accessed by more parts of the hardware than your processor, and the endian that is specified by, say, conventional display adapters, is not necessarily the same as the endian of your CPU. At the level of manipulating the channels in the pixel its no problem for a CPU to extract the fields whatever their order; its purely a programmer understanding the labelling thing.

5
  • 2
    But in Windows, the necessary byte order is 0xBB, 0xGG, 0xRR - how do you explain that? Dec 15, 2008 at 22:49
  • 5
    I don't believe it has anything to do with endianness. The Gamegear uses BGR color, as well as the Wonderswan Color, but the Gameboy Color and Advance use RGB color. All are little-endian systems (Z80, 80286-based, Z80-based, and ARM, respectively).
    – strager
    Dec 16, 2008 at 20:24
  • 4
    dev.chromium.org/developers/design-documents/graphics-and-skia search for "The epic battle of RGBA and BGRA"
    – Will
    Jul 17, 2009 at 5:43
  • 7
    My theory: They wanted them in the order of increasing wavelength.
    – letmaik
    May 9, 2014 at 17:32
  • For anyone who wanders in now, according to this answer, the original use of BGR on the PC platform traces back to a convenience trick for interacting with the RAMDAC used with the original IBM VGA implementation. Windows using BGR would be something similar.
    – ssokolow
    Feb 28, 2019 at 6:11
33

RGB stands for Red Green Blue. Most often, an RGB color is stored in a structure or unsigned integer with Blue occupying the least significant "area" (a byte in 32-bit and 24-bit formats), Green the second least, and Red the third least.

BGR is the same, except the order of areas is reversed. Red occupies the least significant area, Green the second (still), and Blue the third.

On some platforms (e.g. the Gamegear), a BGR model is used. However, often, RGB might be used (though BGR is supported by many graphics API's for compatibility). I'm not sure why exactly it's used; probably historical.

Example: #FF0000 is pure red when reading as an RGB hex color (#rrggbb), because the third area (numbers are read right to left!) is FF (maximum value, full color) and the other two areas are 00 (minimum value, no color). If #FF0000 were read as a BGR hex color, it'd be pure blue.

1
  • 14
    This answer is incorrect: See docs.opencv.org/doc/tutorials/core/how_to_scan_images/…. The convention for OpenCV is BGR, and B is stored at a lower byte address than R. Whether B or R is least significant depends or the byte order of the machine, and also the 24 bit BGR pixels are not aligned to the 32 bit boundaries of a unsigned integer (so it does not make much sense to use that for an explanation).
    – Bull
    Sep 9, 2014 at 0:57
5

It is about the way the color components are laid out in memory. For BGR the order is BGRBGRBGRBGR..., and for RGB the order is RGBRGBRGB... For BGR, the default order for OpenCV, it is like this:

enter image description here (See the OpenCV documentation for How the image matrix is stored in the memory?)

Note the other answers have referred to certain colors being least or most significance, but that actually depends on the endianness of your machine. And the packing order of components in an unsigned int, e.g, actually depends on your software, or the library you are using. However, regardless of your library, or the endiannes of your machine, for BGR pixel the byte address of the B color component will be one less than for G and and two less than for R (and for RGB it is just the other way around).

2

It's just RGB in a more tightly controlled order, independent of endianness. Converting between them is simple once you know the endianness of your environment.

The BGR is a 24-bit representation where the lower-addressed 8 bits are blue, the next-addressed 8 are green and higher-addressed 8 are red.

RGB values tend to be written as RGB(r,g,b) where the r/g/b values are between 0 and 255 inclusive or as #rrggbb, where rr/gg/bb are 8-bit hex values. All the BGR values I've seen tend to be simple integers between 0 and 16777215 (0xffffff).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.