 # Thread: Extracting the RGB components from an RGB value?

1. ## Extracting the RGB components from an RGB value?

Hi Folks.

I'm trying to back-engineer the maths behind RGB_Values. Here is what it says in the MMF help file:

"Replace Color
Allows you to replace a color by another one in the current image of the object. Select the color to replace in the color selector, or via the Calculation button, and then the new color. The Calculation button in the Color selector allows you to enter a RGB value. A RGB value can be calculated from its Red, Green, and Blue components by the following formula:
RGB_Value = B*65536 + G*256 + R.
To extract the R, B and B components from a RGB value, use:
R = RGB_Value AND 255
G = (RGB_Value / 256) AND 255
B = (RGB_Value / 65536) AND 255
"

I can create the RGB_Value without issue given the three R, G and B numbers between 0 and 255. But what if I want to back-engineer an RGB_Value to get the individual RGB components? I can't understand the maths listed in the help file to do this above?!? So if I have an RGB_Value of 16777215, which is pure white, how do I back-engineer this number to get 255, 255, 255?  Reply With Quote

2. just use the built-in expressions (they're not hidden)  Reply With Quote

3. It shows you right there how to do it, use the AND operator with 255 to isolate Red, but divide first by 256 to isolate green, or divide by 256² to get blue. This is easier to see if you look at the numbers in hex.

0x000000 is black. Red, green, and blue all have values of 0.
0xFFFFFF is white. Red, green, and blue all have values of 255 (0xFF)

00 in binary is 00000000, where FF in binary is 11111111
The AND operator (usually denoted as &) returns 1 if both the input values are 1

011 & 110 = 010, because only the second bit had 1 for both input values.

When you do RGB AND 255, you can represent this in hex as (value) & FF. Using a random colour as an example
76FC8B & FF is the same as
76FC8B & 0000FF. Knowing that FF is all 1s and 00 is all 0s, the result would have to be
00008B, isolating the blue value.

Hexadecimal is base 16, so each digit has a value from 0 to 9 and then A to F.
10 in base 10 is just 10¹, where in hex it's 16¹
100 in base 10 is 10², where in hex it's 16² (256)
This is why dividing the number by 256 moves it two hexadecimal places to the right, in the same way dividing a number by 100 would do that in decimal.
78FC8B / 100 = 78FC.8B which truncates to 78FC
78FC & 00FF = FC, isolating green.

Hopefully this explains it well enough. That help file says you just & 255 to isolate red, which is either wrong or MMF stores values in BGR format instead of industry standard RGB.  Reply With Quote

4. After a battle with Excel, I managed to work out the maths but it's interesting to see the hexdecimal way (though it melts my brain! LOL!!). I didn't know about the built in MMF functions to do this, thanks for the heads up.  Reply With Quote

5. Yeah, you can use getRed() getGreen() and getBlue(). If you want to make yourself look like an awesome coder to anyone that looks, just use

blue = RGB and 0x0000FF
green = RGB and 0x00FF00
red = RGB and 0xFF0000

remembering 0xRRGGBB for where the Fs go  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•