Code archives/Graphics/ReplaceImage
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
This code contains a function that allows you to replace a certain color in an image with another color. Detailed information is commented in the file. | |||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;; ReplaceImage() function by Kai Rosecrans ;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;To use this function, simply type in the following parameters: ; r - the red value to replace in the image ; g - the green value to replace in the image ; b - the blue value to replace in the image ; r_rep - the red value to replace r with in the image ; g_rep - the green value to replace g with in the image ; b_rep - the blue value to replace b with in the image ; image - the image to do all this stuff to ;It could be very useful for the user to be able to create customized characters while you save space ;and time; you only need create one image with unique colors for each body region. You could also use it ;in a game where there are teams and each user can choose his/her own color for his units. Function ReplaceImage(r,g,b,r_rep,g_rep,b_rep,image) Local ret_r = ColorRed() Local ret_g = ColorGreen() Local ret_b = ColorBlue() Local buffer = GraphicsBuffer() SetBuffer ImageBuffer(image) DrawImage image,0,0 For x = 0 To ImageWidth(image) For y = 0 To ImageHeight(image) GetColor x,y If ColorRed() = r And ColorGreen() = g And ColorBlue() = b Then Color r_rep,g_rep,b_rep Plot x,y EndIf Next Next Color ret_r,ret_g,ret_b SetBuffer buffer Return image End Function |
Comments
| ||
I have no use for this but it is a nice idea. I may find a need for it some day. :) |
| ||
This should be a lot fasterAppTitle "Fast image color replacement" Global sw%, sh% sw% = 800: sh% = 600 Graphics sw%, sh%, 32, 2 SetBuffer BackBuffer() SeedRnd MilliSecs() ClsColor 255,255,255 Cls Color 0,0,0 For i = 0 To 900 Oval Rand(0,sw%), Rand(0,sh%), Rand(10,30), Rand(10,30), True Next orgImage = CreateImage(sw%, sh%) GrabImage orgImage, 0, 0 Color 0, 0, 128 Rect 30, 10, 320, 25 Color 255, 255, 255 Text 35, 15,"Left-Click to replace black with red" Flip WaitMouse() FlushMouse() st = MilliSecs() newImage% = newColor%(orgImage%, 0, 0, 0, 255, 0, 0) et = MilliSecs()-st DrawBlock newImage%, 0, 0 Color 0,0,128 Rect 30, 10, 150, 25, True Color 255, 255, 255 Text 35, 15, "Elapsed Time: "+et Flip WaitMouse() ;Left-Click to Exit If orgImage% <> 0 Then FreeImage orgImage% If newImage% <> 0 Then FreeImage newImage% End Function newColor(orgImage%, oldR%, oldG%, oldB%, newR%, newG%, newB%) MaskImage(orgImage%, oldR%, oldG%, oldB%) w% = ImageWidth(orgImage%) h% = ImageHeight(orgImage%) newImage% = CreateImage(w%, h%) SetBuffer ImageBuffer(newImage%) Color newR%, newG%, newB% Rect 0, 0, w%, h%, True DrawImage orgImage%, 0, 0 SetBuffer BackBuffer() Return newImage% End Function |
Code Archives Forum