VBspeed / Colors / GrayScale
VBspeed © 2000-10, updated: 15-aug-2004
GrayScale


Function GrayScale
Takes an RGB color value and converts it to an equivalent gray scale value. There are 256 possible gray scale values, as there are 256 possible degrees of brightness of a pixel.
  Function GrayScale(ByVal lColor As Long) As Long
Gray Scaling Images: Reducing images to a gray scale is a common image processing task which is also very simple to implement. The simplest method to do this is simply to take the green value and apply the same value to Red and Blue pixels. This works well because the eye responds to Green light in RGB colors much more strongly than to the Red and Blue values. Obviously completely saturated Reds and Blues will not be represented this way. A more accurate conversion can be performed using weighted formulas of this type: (BrightnessOfRed * Red + BrightnessOfGreen * Green + BrightnessOfBlue * Blue) / (BrightnessOfRed + BrightnessOfGreen + BrightnessOfBlue). Here are the two most commonly seen setups:

  (a) Gray = (77 * Red + 150 * Green + 28 * Blue) / 255
  (b) Gray = (222 * Red + 707 * Green + 71 * Blue) / 1000

Note that the two formulas lead to different results, however none is better or more correct than the other! This is because what these formulas attempt to express is the eye's/the mind's responsiveness to colors. And there's a lot of software involved which hasn't been decompiled so far. In the meantime admire the different shades of Brigitte Bardot.

Code
GrayScale01
Public Function GrayScale01(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20010922
  Dim lGrayValue As Long
  lGrayValue = (77& * (lColor And &HFF&) + _
               150& * (lColor And &HFF00&) \ &H100& + _
                28& * ((lColor And &HFF0000) \ &H10000)) \ 255&
  GrayScale01 = RGB(lGrayValue, lGrayValue, lGrayValue)
End Function
GrayScale02
Public Function GrayScale02(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20010922
  Dim lGrayValue As Long
  lGrayValue = (77& * (lColor And &HFF&) + _
               150& * (lColor And &HFF00&) \ &H100& + _
                28& * ((lColor And &HFF0000) \ &H10000)) \ 255&
  GrayScale02 = lGrayValue * &H10000 + lGrayValue * &H100& + lGrayValue
End Function
GrayScale03
Public Function GrayScale03(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20010922
  Dim lGrayValue As Long
  lGrayValue = (77& * (lColor And &HFF&) + _
               150& * (lColor And &HFF00&) \ &H100& + _
                28& * ((lColor And &HFF0000) \ &H10000)) \ 255&
  GrayScale03 = lGrayValue * &H10101
End Function
GrayScale04
Public Function GrayScale04(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20010922
  GrayScale04 = ((77& * (lColor And &HFF&) + _
                 150& * (lColor And &HFF00&) \ &H100& + _
                  28& * ((lColor And &HFF0000) \ &H10000)) \ 255&) * &H10101
End Function
GrayScale05
Public Function GrayScale05(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20011121
  GrayScale05 = (77& * (lColor And &HFF&) + _
                150& * (lColor And &HFF00&) \ &H100& + _
                 28& * ((lColor And &HFF0000) \ &H10000)) \ 255&
  GrayScale05 = GrayScale05 * &H10101
End Function
GrayScale06
Public Function GrayScale06(ByVal lColor As Long, _
    Optional ByVal R As Long = 77, _
    Optional ByVal G As Long = 150, _
    Optional ByVal B As Long = 28 _
    ) As Long
' by Donald, donald@xbeat.net, 20011121
  GrayScale06 = (R * (lColor And &HFF&) + _
                 G * (lColor And &HFF00&) \ &H100& + _
                 B * ((lColor And &HFF0000) \ &H10000)) \ 255&
  GrayScale06 = GrayScale06 * &H10101
End Function
GrayScale07
Public Function GrayScale07(ByVal lColor As Long) As Long
' by Paul, wpsjr1@syix.com, 20010923
  GrayScale07 = ((77& * (lColor And &HFF&) + _
                 150& * (lColor And &HFF00&) \ &H100& + _
                  28& * ((lColor And &HFF0000) \ &H10000)) \ 256&) * &H10101
End Function
GrayScale08
Public Function GrayScale08(ByVal lColor As Long) As Long
' by Paul, wpsjr1@syix.com, 20010923
  Dim lGrayValue As Long
  lGrayValue = (77& * (lColor And &HFF&) + _
               150& * (lColor And &HFF00&) \ &H100& + _
                28& * ((lColor And &HFF0000) \ &H10000)) \ 256&
  GrayScale08 = (lGrayValue + 1&) * &H10101
End Function
GrayScale09
Public Function GrayScale09(ByVal lColor As Long) As Long
' by Donald, donald@xbeat.net, 20011123
  GrayScale09 = ((77& * (lColor And &HFF&) + _
                 152& * (lColor And &HFF00&) \ &H100& + _
                  28& * ((lColor And &HFF0000) \ &H10000)) \ 256&) * &H10101
End Function
GrayScale10
Public Function GrayScale10(ByVal lColor As Long) As Long
' by Paul, wpsjr1@syix.com, 20040811
  GrayScale10 = ((77& * (lColor And &HFF&) + _
                 152& * (lColor And &HFF00&) \ &H100& + _
                  28& * (lColor \ &H10000)) \ 256&) * &H10101
End Function
Calls
1lColor = &H987654
2lColor = &HFFFFFF
3lColor = &H000000
Charts
 VB5 Charts
CodeAuthorDopingNotes
GrayScale01 Donald  
GrayScale02 Donald  
GrayScale03 Donald  
GrayScale04 Donald  
GrayScale05 Donald  
GrayScale06 Donald  
GrayScale07 Paul  
GrayScale08 Paul  
GrayScale09 Donald  
GrayScale10 Paul  
Call 1
101.960.033Ás
81.280.022Ás
71.280.022Ás
51.280.022Ás
61.280.022Ás
91.420.024Ás
21.070.018Ás
31.100.019Ás
11.000.017Ás
41.170.020Ás
Call 2
101.970.033Ás
71.290.022Ás
51.290.022Ás
61.290.022Ás
81.290.022Ás
91.430.024Ás
21.070.018Ás
31.110.019Ás
11.000.017Ás
41.170.020Ás
Call 3
101.980.033Ás
81.290.022Ás
71.280.022Ás
51.280.022Ás
61.290.022Ás
91.430.024Ás
21.070.018Ás
31.110.019Ás
11.000.017Ás
41.180.020Ás
 VB6 Charts
CodeAuthorDopingNotes
GrayScale01 Donald  
GrayScale02 Donald  
GrayScale03 Donald  
GrayScale04 Donald  
GrayScale05 Donald  
GrayScale06 Donald  
GrayScale07 Paul  
GrayScale08 Paul  
GrayScale09 Donald  
GrayScale10 Paul  
Call 1
102.870.048Ás
51.320.022Ás
91.400.024Ás
71.390.023Ás
61.320.022Ás
81.400.024Ás
41.190.020Ás
31.110.019Ás
11.000.017Ás
21.070.018Ás
Call 2
102.860.048Ás
61.320.022Ás
81.390.023Ás
91.390.024Ás
51.320.022Ás
71.390.023Ás
41.180.020Ás
31.110.019Ás
11.000.017Ás
21.070.018Ás
Call 3
102.860.048Ás
61.320.022Ás
91.390.023Ás
81.390.023Ás
51.320.022Ás
71.390.023Ás
41.180.020Ás
31.110.019Ás
11.000.017Ás
21.070.018Ás
Notes & Conclusions
GrayScale01: 'tis no surprise for speedcorers that VB's native RGB is not exactly a rocket. In the shady world of gray, &H10101 can do the job as well.
GrayScale02 to GrayScale05: counting pico-seconds ...
GrayScale06 gives you some options for a relatively small price.
GrayScale07 divides by 256 instead of 255, that's a shift and hence faster. Good idea. The result gets a little too dark though.
GrayScale08 and GrayScale09 try to cope with that.
GrayScale10 looks slower in the charts (made on Athlon XP 2000+), but Paul reports: "GrayScale10 is about 7% faster on my P3, so it's probably a processor difference. It's actually more instructions, but pairs better on Pentium."

Note that all above functions use fast integer division (... \ 255). Since this operation truncates fractions (insteading of rounding up or down), about half of the pixels come out one degree too dark. I can live with that.
Got comments? How to read all those numbers

top




VBspeed © 2000-10 by Donald Lessau