Gabz

Bitmap Replace Color

Jul 29th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.47 KB | None | 0 0
  1. # Bitmap Replace Color
  2. # @author  : Gabriel "Gab!" Teles
  3. # @date    : 2014/07/30
  4. # @website : http://www.mundorpgmaker.com.br/
  5.  
  6. class Bitmap
  7.   MemcpyPL = Win32API.new('msvcrt', 'memcpy', 'pll', 'v')
  8.   MemcpyLP = Win32API.new('msvcrt', 'memcpy', 'lpl', 'v')
  9.  
  10.   def address
  11.     ptr = [0].pack('L')
  12.     MemcpyPL.call(ptr, object_id * 2 + 16, 4)
  13.     MemcpyPL.call(ptr, ptr.unpack('L').first + 8, 4)
  14.     MemcpyPL.call(ptr, ptr.unpack('L').first + 16, 4)
  15.     return ptr.unpack('L').first
  16.   end
  17.  
  18.   def getRawData
  19.     buf = 0.chr * (width * height * 4)
  20.     Bitmap::MemcpyPL.call(buf, address, buf.size)
  21.     return buf
  22.   end
  23.  
  24.   def setRawData(buf)
  25.     raise(RuntimeError, "Tamanho Incorreto") if (buf.size != (width * height * 4))
  26.     Bitmap::MemcpyLP.call(address, buf, buf.size)
  27.   end
  28.  
  29.   def replaceColor(fC, tC)
  30.     data = getRawData
  31.     offset = 0
  32.    
  33.     f = [fC.blue, fC.green, fC.red, fC.alpha].pack('C4')
  34.     t = [tC.blue, tC.green, tC.red, tC.alpha].pack('C4')
  35.    
  36.     (width * height).times{
  37.       data[offset, 4] = t if data[offset, 4] == f
  38.       offset += 4
  39.     }
  40.    
  41.     setRawData(data)
  42.   end
  43. end
  44.  
  45. x = Bitmap.new(100, 100)
  46. x.fill_rect(0, 0, 50, 50, Color.new(255, 0, 0))
  47. x.fill_rect(50, 0, 50, 50, Color.new(0, 255, 0))
  48. x.fill_rect(0, 50, 50, 50, Color.new(0, 0, 255))
  49. x.fill_rect(50, 50, 50, 50, Color.new(255, 0, 255))
  50. x.replaceColor(Color.new(255, 0, 0), Color.new(255, 255, 0))
  51.  
  52. y = Sprite.new
  53. y.bitmap = x
  54.  
  55. loop { Graphics.update }
Advertisement
Add Comment
Please, Sign In to add comment