November 24, 2020, 07:14:27 AM

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

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Color replacement - proportions by Matt Merkulov [ 1+ years ago ]
« on: June 29, 2017, 12:28:38 AM »
Title : Color replacement - proportions
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 - proportions by Matt Merkulov
2.
3. Graphics 640,480,32
4.
6. DrawBlock i, 0,0
7.
8. ; Color to be replaced
9. r1# = 255
10. g1# = 64
11. b1# = 64
12. ; Calculating its proportional coefficients
13. s1 = r1# + g1# + b1#
14. kr1# = r1# / s1
15. kg1# = g1# / s1
16. kb1# = b1# / s1
17. ; Color to replace with
18. r2# = 0
19. g2# = 0
20. b2# = 255
21. ; Calculating its proportional coefficients
22. s2=r2# + g2# + b2#
23. r2# = r2# / s2
24. g2# = g2# / s2
25. b2# = b2# / s2
26. ; An admissible difference of factors
27. skmax# =.5
28.
29. For y = 0 To 479
30.  For x = 0 To 639
31.  ; Decompositing color on components
33.  b# = p And 255
34.  g# = (p Shr 8) And 255
35.  r# = (p Shr 16) And 255
36.  ; Calculating proportion coefficients of initial color
37.  s = r# + g# + b#
38.  kr# = r# / s
39.  kg# = g# / s
40.  kb# = b# / s
41.  sk# = Abs (kr1# - kr#) + Abs (kg1# - kg#) + Abs (kb1# - kb#)
42.  ; Check on an admissible difference of coefficients
43.  If sk# <= skmax# Then
44.   ; Calculating coefficients for mixing
45.   sk1# = sk# / skmax#
46.   sk2# = (1 - sk1#)*s
47.   ; Intensivity components' values
48.   rr = Int (sk1#*r+sk2#*r2#)
49.   If rr < 0 Then rr = 0 Else If rr > 255 Then rr = 255
50.   gg = Int (sk1# * g + sk2# * g2#)
51.   If gg < 0 Then gg = 0 Else If gg > 255 Then gg = 255
52.   bb = Int (sk1# * b + sk2# * b2#)
53.   If bb < 0 Then bb = 0 Else If bb > 255 Then bb = 255
54.   ; Drawing pixel
55.   WritePixel x, y, bb + (gg Shl 8) + (rr Shl 16)
56.  End If
57.  Next
58. Next
59.
60. WaitKey