November 28, 2020, 10:59:06 AM

### Author Topic: [bb] Color replacement - distance by Matt Merkulov [ 1+ years ago ]  (Read 556 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Color replacement - distance by Matt Merkulov [ 1+ years ago ]
« on: June 29, 2017, 12:28:41 AM »
Title : Color replacement - distance
Author : Matt Merkulov
Posted : 1+ years ago

Description : From <a href="http://blitzetcetera.org/" target="_blank">Blitz et cetera[/url] article
Image used:

Code :
Code: BlitzBasic
1. ;Color replacement - distance by Matt Merkulov
2.
3. Graphics 640,480,32
4.
5. i = LoadImage ("image1.jpg")
6. DrawBlock i, 0,0
7.
8. ; What color is to be replaced
9. r1=224
10. g1=224
11. b1=0
12. ; What color is to be replaced with
13. r2=64
14. g2=64
15. b2=255
17. rad# = 128
18.
19. For y = 0 To 479
20.  For x = 0 To 639
21.   ; Decompositing color on components
22.   p = ReadPixel (x, y)
23.   b = p And 255
24.   g = (p Shr 8) And 255
25.   r = (p Shr 16) And 255
26.   ; Distance between initial color and replaced one
27.   d# = Sqr((r - r1) * (r - r1) + (g - g1) * (g - g1) + (b - b1) * (b - b1))
28.   ; Checking is the current color is inside of sphere
29.   If d# <= rad# Then
30.    ; Calculating factors
31.    d1# = d#/rad#
32.    d2# = 1-d1#
33.    ; Components' intensivity values
34.    r = Int (d1# * r + d2# * r2)
35.    g = Int (d1# * g + d2# * g2)
36.    b = Int (d1# * b + d2# * b2)
37.   End If
38.   ; Writing pixel
39.   WritePixel x, y, b + (g Shl 8) + (r Shl 16)
40.  Next
41. Next
42.
43. WaitKey

`;Color replacement - distance by Matt Merkulov Graphics 640,480,32SetBuffer BackBuffer()i=LoadImage("flower.jpg")DrawBlock i, 0,0 FlipWaitKey()i=ReplaceColours(i,224,224,0,64,64,255,128)DrawBlock i, 0,0 FlipFunction ReplaceColours%(i,r1,g1,b1,r2,g2,b2,rad#) buffer=ImageBuffer(i) LockBuffer buffer For y = 0 To 479 For x = 0 To 639  ; Decompositing color on components  p = ReadPixelFast(x, y,buffer)  b = p And 255  g = (p Shr 8) And 255  r = (p Shr 16) And 255  ; Distance between initial color and replaced one  d# = Sqr((r - r1) * (r - r1) + (g - g1) * (g - g1) + (b - b1) * (b - b1))  ; Checking is the current color is inside of sphere  If d# <= rad# Then   ; Calculating factors   d1# = d#/rad#   d2# = 1-d1#   ; Components' intensivity values   r = Int (d1# * r + d2# * r2)   g = Int (d1# * g + d2# * g2)   b = Int (d1# * b + d2# * b2)  End If  ; Writing pixel  WritePixelFast x, y, b + (g Shl 8) + (r Shl 16),buffer NextNextUnlockBuffer bufferBufferDirty bufferReturn iEnd Function`