Quantcast
Viewing all articles
Browse latest Browse all 21350

Make Image Brighter/Make Image Un-brighter

I have this code that I got from this site (one of my previous threads) which is used to make an image brighter. I use it in my games to indicate a piece has been selected by the player. Now, after the player moves the piece I want the image to return to it original colors. Can someone look at this code and figure out if there is a magic number that will un-brighten an image that has been brightened

Code:

  '
  '

Public Sub MakeBrighter(Brightness As Single)
 DrawVBBrightness Brightness / 100
End Sub

Private Sub DrawVBBrightness(ByVal Brightness As Single)
 'Coordinate variables
 Dim X As Long, Y As Long
   
 'Color variables
 Dim r As Long, g As Long, b As Long
   
 'Build a look-up table for all possible brightness values
 Dim bTable(0 To 255) As Long
 Dim TempColor As Long
   
 For X = 0 To 255
  'Calculate the brightness for pixel value x
  TempColor = X * Brightness
       
  'Make sure that the calculated value is between 0 and 255 (so we don't get an error)
  ByteMe TempColor
       
  'Place the corrected value into its array spot
  bTable(X) = TempColor
 Next X
   
 'Temporary width and height variables are faster than accessing the Scale properties over and over again
 Dim TempWidth As Long, TempHeight As Long
 TempWidth = UserControl.ScaleWidth - 1
 TempHeight = UserControl.ScaleHeight - 1
   
 'run a loop through the picture to change every pixel
 For X = 0 To TempWidth
  For Y = 0 To TempHeight
    'Get the color (using Point) and extract the red, green, and blue values
    TempColor = UserControl.Point(X, Y)
       
    r = ExtractR(TempColor)
    g = ExtractG(TempColor)
    b = ExtractB(TempColor)
       
    If r = 255 And g = 255 And b = 255 Then GoTo Ignore
     
    'Use the values in the look-up table to quickly change the brightness values
    'of the selected colors.  The look-up table is much faster than doing the math
    'over and over for each individual pixel.
    r = bTable(r)
    g = bTable(g)
    b = bTable(b)
     
    'Now set that data using the "PSet" command
    UserControl.PSet (X, Y), RGB(r, g, b) 'DstPicture.PSet (x, y), RGB(r, g, b)
Ignore:
  Next Y
 
  'refresh the picture box every 25 lines (a nice progress bar effect if AutoRedraw is set)
  If UserControl.AutoRedraw = True And (X Mod 25) = 0 Then UserControl.Refresh
 Next X
   
 'final picture refresh
 'If DstPicture.AutoRedraw = True Then DstPicture.Refresh
 If UserControl.AutoRedraw = True Then UserControl.Refresh
End Sub

'Standardized routines for color extraction
Private Function ExtractR(ByVal CurrentColor As Long) As Byte
 ExtractR = CurrentColor And 255
End Function

Private Function ExtractG(ByVal CurrentColor As Long) As Byte
 ExtractG = (CurrentColor \ 256) And 255
End Function

Private Function ExtractB(ByVal CurrentColor As Long) As Byte
 ExtractB = (CurrentColor \ 65536) And 255
End Function

'Standardized routine for converting to absolute byte values
Private Sub ByteMe(ByRef TempVar As Long)
 If TempVar > 255 Then TempVar = 255: Exit Sub
 If TempVar < 0 Then TempVar = 0: Exit Sub
End Sub
  '
  '


Viewing all articles
Browse latest Browse all 21350


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>