0

i was studying some projects and i started The ColorPicker Project. i could not understand the LongToRgb Function >>> which as follow :

**

Private Function LongToRGB(lColor As Long) As String
    Dim iRed As Long, iGreen As Long, iBlue As Long
    iRed = lColor Mod 256
    iGreen = ((lColor And &HFF00) / 256&) Mod 256&
    iBlue = (lColor And &HFF0000) / 65536
    LongToRGB = Format$(iRed, "000") & ", " & Format$(iGreen, "000") & ", " & Format$(iBlue, "000")
End Function

**

i want someone to explain to me in plain English ...

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

2 Answers2

3

Mod is the modulo operation, which is ofter referred as % too.

It takes the remainder of the integer division between the two values. In your situation it's useful to get the meaningful part of component color (red, green, blue) from a long that contains all of them packed.

Eg:

1234 Mod 100 = 34
Jack
  • 131,802
  • 30
  • 241
  • 343
  • MOD is the modulo (or modulus) operation. In the U.S. it's often called "Clock Arithmetic" in school. It is just the remainder left over when you divide one integer by another. So, **(43 MOD 10) = 3** because 43 divided by 10 is 4 (the quotient) with 3 left over (the remainder). The Division operator ("/") returns the quotient, and the Modulo operator ("MOD") returns the remainder. – RBarryYoung Apr 10 '12 at 18:46
  • @RBarryYoung, I think his topic is off... he seems to not really be asking what the modulo operator is, but rather why it is being used in the mathematics of reverse-engineering the RGB colors from a long. At least, that's my guess. – myermian Apr 10 '12 at 18:58
  • Errp! Well, that was a lot of wasted text on my part ... :) – RBarryYoung Apr 10 '12 at 19:03
  • @RBarryYoung, Well between my answer and Jack's answer about the modulo operator I think this question is fully answered. – myermian Apr 10 '12 at 19:03
  • because every component in an rgb value is 0-255, so by using modulo 256 you just take the interesting part. It's basilar math, check this out http://stackoverflow.com/questions/2609315/recognizing-when-to-use-the-mod-operator – Jack Apr 10 '12 at 19:19
  • Could you give me an example for converting a specified color ?! –  Apr 10 '12 at 19:26
3

Mod Operator (% in C#)

Basically, it returns the remainder of a division operation. For example, 13 Mod 4 = 1, because 13 / 4 = 3 w/ a remainder of 1. It's important to understand how the long value is created to understand why the function does what it does.


All the colors (Red, Green, Blue) are represented in the amounts of 0-255. For example, imagine the following value: R(8), G(3), B(1).

To understand why the function does what it does, let's look at a scenario where number values range from 0-9 (10 values) instead of 0-255 (256 values). How would you represent a single value that you can reverse engineer the values from? You can not simply add the values together (8 + 3 + 1 = 12) because it would be impossible to reverse engineer the original values. Instead, you must multiply values by the base. The base depends on the value range... in the example it is 10 because there are 10 values. The position is a zero-based index. Red's position is 0, Green's position is 1, Blue's position is 2.

  • Value * (Base^Position))
  • Red(8) = (8 * 10^0) = 8 * 1 = 8
  • Green(3) = (3 * 10^1) = 3 * 10 = 30
  • Blue(1) = (1 * 10^2) = 1 * 100 = 100

8 + 30 + 100 = 138. And 138 can easily be reverse engineered (in fact, just by looking at it!). Mathematically reverse engineering it is done as so:

  • (CombinedValue / (Base^Position)) % Base = OriginalValue.
  • (138 / (10^0)) % 10 = (138 / 1) % 10 = 138 % 10 = 8 (Red)
  • (138 / (10^1)) % 10 = (138 / 10) % 10 = 13 (decimal is truncated) % 10 = 3 (Green)
  • (138 / (10^2)) % 10 = (138 / 100) %10 = 1 (decimal is truncated) % 10 = 1 (Blue)

The function does a few things:

  • It uselessly does a bitwise operator (lColor And &HFF00) and (lColor And &HFF0000) for some reason.
  • It simplifies the mathematics. There is no point in dividing by 1 for red (256^0 = 1), and there is no point to using the modulo operator to retrieve Green because X % 256 = X for all X where X < 256. Also, 256^2 is equal to 65536.
  • It uses the actual range that color values can be represented (0-255, 256 values).

You can actually use a simplified version of the function instead:

Private Function LongToRGB(lColor As Long) As String
    Dim iRed As Long, iGreen As Long, iBlue As Long

    iRed = lColor Mod 256
    iGreen = (lColor / 256) Mod 256
    iBlue = lColor / 65536

    LongToRGB = Format$(iRed, "000") & ", " & Format$(iGreen, "000") & ", " & Format$(iBlue, "000")
End Function

Note that the last method is simply a string formatting function and has nothing to do with the mathematics.

myermian
  • 31,823
  • 24
  • 123
  • 215
  • Thanks a lot. you helped me big time. could you please tell me a book name for Reverse engineering in programming field Visual Basic For free ?! –  Apr 10 '12 at 23:36
  • I'm not a VB.Net expert, so I have no idea... so I removed it as it seems unnecessary. – myermian Apr 11 '12 at 17:00